@nhankyjangchan/koajs-cors

Cross-Origin Resource Sharing(CORS) for KoaJS written in TypeScript
Handles CORS requests by setting appropriate headers for both simple requests and preflight (OPTIONS) requests. Validates the Origin header against the configured whitelist and throws a 403 error for unauthorized origins.
Installation
$ npm i @nhankyjangchan/koajs-cors@^1.1.0
or
$ bun add @nhankyjangchan/koajs-cors@^1.1.0
Options
export interface Options {
origin?: string | string[] | ((ctx: Context) => string | Promise<string>);
allowMethods?: string | string[];
exposeHeaders?: string | string[];
allowHeaders?: string | string[];
maxAge?: string | number | undefined;
credentials?: boolean;
privateNetworkAccess?: boolean;
originOpenerPolicy?: boolean;
originEmbedderPolicy?: boolean;
keepHeadersOnError?: boolean;
shouldSkip?: undefined | false | ((ctx: Context) => boolean | Promise<boolean>);
}
Default options
const defaultOptions: Options = {
origin: '*',
allowMethods: ['HEAD', 'POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
maxAge: '3600',
credentials: false,
privateNetworkAccess: false,
originOpenerPolicy: false,
originEmbedderPolicy: false,
keepHeadersOnError: true,
shouldSkip: false
};
Usage
import Koa from 'koa';
import cors from '@nhankyjangchan/koajs-cors';
const Koa = require('koa');
const cors = require('@nhankyjangchan/koajs-cors').default;
const app = new Koa();
app.use(
cors({
origin: ['https://example.com', 'https://another.com'],
credentials: true,
allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
maxAge: '3600'
})
);
app.listen(3000);
Tests
$ bun test
Releases
CHANGELOG
License
MIT