@elysiajs/cors
Version:
Plugin for Elysia that for Cross Origin Requests (CORs)
143 lines (142 loc) • 4.65 kB
TypeScript
import { Elysia } from 'elysia';
type Origin = string | RegExp | ((request: Request) => boolean | void);
export type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE';
type MaybeArray<T> = T | T[];
interface CORSConfig {
/**
* Disable AOT (Ahead of Time) compilation for plugin instance
*
* @default true
*/
aot?: boolean;
/**
* @default `true`
*
* Assign the **Access-Control-Allow-Origin** header.
*
* Value can be one of the following:
* - `string` - String of origin which will be directly assign to `Access-Control-Allow-Origin`
*
* - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)
*
* - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.
*
* - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.
* - Function will accepts `Context` just like `Handler`
*
* ```typescript
* // ? Example usage
* app.use(cors, {
* origin: ({ request, headers }) => true
* })
*
* // Type Definition
* type CORSOriginFn = (context: Context) => boolean | void
* ```
*
* - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept request if one is `true`.
*/
origin?: Origin | boolean | Origin[];
/**
* @default `*`
*
* Assign **Access-Control-Allow-Methods** header.
*
* Value can be one of the following:
* Accept:
* - `undefined | null | ''` - Ignore all methods.
*
* - `*` - Accept all methods.
*
* - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.
* - Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')
*
* - `HTTPMethod[]` - Allow multiple HTTP methods.
* - eg: ['GET', 'PUT', 'POST']
*/
methods?: boolean | undefined | null | '' | '*' | MaybeArray<HTTPMethod | (string & {})>;
/**
* @default `*`
*
* Assign **Access-Control-Allow-Headers** header.
*
* Allow incoming request with the specified headers.
*
* Value can be one of the following:
* - `string`
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
*
* - `string[]` - Allow multiple HTTP methods.
* - eg: ['Content-Type', 'Authorization']
*/
allowedHeaders?: true | string | string[];
/**
* @default `*`
*
* Assign **Access-Control-Expose-Headers** header.
*
* Return the specified headers to request in CORS mode.
*
* Value can be one of the following:
* - `string`
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
*
* - `string[]` - Allow multiple HTTP methods.
* - eg: ['Content-Type', 'X-Powered-By']
*/
exposeHeaders?: true | string | string[];
/**
* @default `true`
*
* Assign **Access-Control-Allow-Credentials** header.
*
* Allow incoming requests to send `credentials` header.
*
* - `boolean` - Available if set to `true`.
*/
credentials?: boolean;
/**
* @default `5`
*
* Assign **Access-Control-Max-Age** header.
*
* Allow incoming requests to send `credentials` header.
*
* - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.
*/
maxAge?: number;
/**
* @default `true`
*
* Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.
*
* - `boolean` - Available if set to `true`.
*/
preflight?: boolean;
}
export declare const cors: (config?: CORSConfig) => Elysia<"", {
decorator: {};
store: {};
derive: {};
resolve: {};
}, {
typebox: {};
error: {};
}, {
schema: {};
standaloneSchema: {};
macro: {};
macroFn: {};
parser: {};
}, {}, {
derive: {};
resolve: {};
schema: {};
standaloneSchema: {};
}, {
derive: {};
resolve: {};
schema: {};
standaloneSchema: {};
}>;
export default cors;