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..

b5a937fc24/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..

b5a937fc24/index.js (L58)

Thus changed the signature of the origin option to reflect this.
This commit is contained in:
S McDowall
2018-07-30 14:59:05 -04:00
committed by Sheetal Nandi
parent 039caf4113
commit d4b5c5d411
2 changed files with 14 additions and 3 deletions

View File

@@ -1,10 +1,13 @@
// Type definitions for @koa/cors 2.2
// Project: https://github.com/koajs/cors
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>, Izayoi Ko <https://github.com/izayoiko>, Steve Hipwell <https://github.com/stevehipwell>
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>
// Izayoi Ko <https://github.com/izayoiko>
// Steve Hipwell <https://github.com/stevehipwell>
// Steven McDowall <https://github.com/sjmcdowall>
// 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;

View File

@@ -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 }));