mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 20:40:20 +00:00
koa__cors: Allow origin to be a function returning a promise. (#41288)
This commit is contained in:
Vendored
+2
-2
@@ -1,4 +1,4 @@
|
||||
// Type definitions for @koa/cors 2.2
|
||||
// Type definitions for @koa/cors 3.0
|
||||
// Project: https://github.com/koajs/cors
|
||||
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>
|
||||
// Izayoi Ko <https://github.com/izayoiko>
|
||||
@@ -15,7 +15,7 @@ declare function cors(options?: cors.Options): Koa.Middleware;
|
||||
|
||||
declare namespace cors {
|
||||
interface Options {
|
||||
origin?: ((ctx: Koa.Context) => string) | string;
|
||||
origin?: ((ctx: Koa.Context) => string) | ((ctx: Koa.Context) => PromiseLike<string>) | string;
|
||||
allowMethods?: string[] | string;
|
||||
exposeHeaders?: string[] | string;
|
||||
allowHeaders?: string[] | string;
|
||||
|
||||
@@ -4,10 +4,18 @@ import cors = require('@koa/cors');
|
||||
const app = new Koa();
|
||||
app.use(cors());
|
||||
|
||||
// Trying using cors() passing in a function ..
|
||||
// Trying using cors() passing in a function...
|
||||
function testCorsFunction(ctx: Koa.Context) {
|
||||
const requestOrigin = ctx.request.origin;
|
||||
return requestOrigin;
|
||||
}
|
||||
|
||||
app.use(cors({ origin: testCorsFunction }));
|
||||
|
||||
// Trying using cors() passing in a function that returns a promise...
|
||||
function testCorsPromiseFunction(ctx: Koa.Context) {
|
||||
const requestOrigin = ctx.request.origin;
|
||||
return Promise.resolve(requestOrigin);
|
||||
}
|
||||
|
||||
app.use(cors({ origin: testCorsPromiseFunction }));
|
||||
|
||||
Reference in New Issue
Block a user