From d4b5c5d41199e87f882ab4207948dffbd17662f0 Mon Sep 17 00:00:00 2001 From: S McDowall Date: Mon, 30 Jul 2018 14:59:05 -0400 Subject: [PATCH] Fix problem in definition when passing a function in the origin: option field. (#27576) * Fixed type for function passing to cors() origin If you look at the actual code within @koa/cors and where it invokes any passed function within the origin: option, it invokes it with ctx, the standard Koa context, NOT the Request. Here is the code fragment.. https://github.com/koajs/cors/blob/b5a937fc24666761c61b1ac633f49f54612d1315/index.js#L58 Thus changed the signature of the origin option to reflect this. * Revert back to single quotes -- stupid prettier Fixed type for function passing to cors() origin If you look at the actual code within @koa/cors and where it invokes any passed function within the origin: option, it invokes it with ctx, the standard Koa context, NOT the Request. Here is the code fragment.. https://github.com/koajs/cors/blob/b5a937fc24666761c61b1ac633f49f54612d1315/index.js#L58 Thus changed the signature of the origin option to reflect this. --- types/koa__cors/index.d.ts | 9 ++++++--- types/koa__cors/koa__cors-tests.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/types/koa__cors/index.d.ts b/types/koa__cors/index.d.ts index 25d337716e..da58136652 100644 --- a/types/koa__cors/index.d.ts +++ b/types/koa__cors/index.d.ts @@ -1,10 +1,13 @@ // Type definitions for @koa/cors 2.2 // Project: https://github.com/koajs/cors -// Definitions by: Xavier Stouder , Izayoi Ko , Steve Hipwell +// Definitions by: Xavier Stouder +// Izayoi Ko +// Steve Hipwell +// Steven McDowall // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -import * as Koa from "koa"; +import * as Koa from 'koa'; export = cors; @@ -12,7 +15,7 @@ declare function cors(options?: cors.Options): Koa.Middleware; declare namespace cors { interface Options { - origin?: ((req: Koa.Request) => string) | string; + origin?: ((ctx: Koa.Context) => string) | 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 135845f205..25f068eb13 100644 --- a/types/koa__cors/koa__cors-tests.ts +++ b/types/koa__cors/koa__cors-tests.ts @@ -3,3 +3,11 @@ import cors = require('@koa/cors'); const app = new Koa(); app.use(cors()); + +// Trying using cors() passing in a function .. +function testCorsFunction(ctx: Koa.Context) { + const requestOrigin = ctx.request.origin; + return requestOrigin; +} + +app.use(cors({ origin: testCorsFunction }));