From 4cd3d1995545cb7377b0ee9005b0592681e34ecd Mon Sep 17 00:00:00 2001 From: Aankhen Date: Fri, 3 Jan 2020 05:08:50 +0530 Subject: [PATCH] koa__cors: Allow `origin` to be a function returning a promise. (#41288) --- types/koa__cors/index.d.ts | 4 ++-- types/koa__cors/koa__cors-tests.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/types/koa__cors/index.d.ts b/types/koa__cors/index.d.ts index da58136652..8b44fb8a43 100644 --- a/types/koa__cors/index.d.ts +++ b/types/koa__cors/index.d.ts @@ -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 // Izayoi Ko @@ -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; allowMethods?: string[] | string; exposeHeaders?: string[] | string; allowHeaders?: string[] | string; diff --git a/types/koa__cors/koa__cors-tests.ts b/types/koa__cors/koa__cors-tests.ts index 25f068eb13..0889f7c70d 100644 --- a/types/koa__cors/koa__cors-tests.ts +++ b/types/koa__cors/koa__cors-tests.ts @@ -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 }));