Exobase
  1. Hooks
  2. useCors

An Exobase hook that automatically responds to OPTIONS requests with standard CORS headers. Also allows you to extends and override the headers with your own values as needed.

Install

yarn add @exobase/use-cors
# or
yarn add @exobase/hooks

Import

import { useCors } from '@exobase/use-cors'
// or
import { useCors } from '@exobase/hooks'

Usage

Add the useCors hook anywhere before your endpoint. When an OPTIONS request is handled the useCors hook will resopnd with the configured (or default) CORS headers and will not call your endpoint function.

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useExpress } from '@exobaes/use-express'
import { useCors } from '@exobase/use-cors'

// Not called when request.method = 'OPTIONS'
const endpoint = (props: Props) => {
  return {
    message: 'success'
  }
}

export default compose(useExpress(), useCors(), endpoint)