From cce31af92dc06fbd620d48ff55a9904be20ddc61 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 19 Dec 2016 14:33:25 -0800 Subject: [PATCH] koa: Introduce `Koa.Middleware` type and add 'kcors' definition (#13447) --- kcors/index.d.ts | 21 +++++++++++++++++++++ kcors/kcors-tests.ts | 5 +++++ kcors/tsconfig.json | 20 ++++++++++++++++++++ kcors/tslint.json | 1 + koa-bodyparser/index.d.ts | 2 +- koa-compress/index.d.ts | 2 +- koa-favicon/index.d.ts | 2 +- koa-json-error/index.d.ts | 2 +- koa-json/index.d.ts | 6 +++--- koa-logger/index.d.ts | 3 ++- koa-mount/index.d.ts | 10 ++++------ koa-passport/index.d.ts | 21 ++++++++++----------- koa-session-minimal/index.d.ts | 2 +- koa-static/index.d.ts | 2 +- koa/index.d.ts | 4 +++- 15 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 kcors/index.d.ts create mode 100644 kcors/kcors-tests.ts create mode 100644 kcors/tsconfig.json create mode 100644 kcors/tslint.json diff --git a/kcors/index.d.ts b/kcors/index.d.ts new file mode 100644 index 0000000000..e2618f63cb --- /dev/null +++ b/kcors/index.d.ts @@ -0,0 +1,21 @@ +// Type definitions for kcors 2.2 +// Project: https://github.com/koajs/cors +// Definitions by: Xavier Stouder +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as Koa from "koa"; + +declare function cors(options?: cors.Options): Koa.Middleware; +export = cors; + +declare namespace cors { + interface Options { + origin?: (req: Koa.Request) => string | string; + allowMethods?: string[] | string; + exposeHeaders?: string[] | string; + allowHeaders: string[] | string; + maxAge?: number | string; + credentials?: boolean; + keepHeadersOnError?: boolean; + } +} diff --git a/kcors/kcors-tests.ts b/kcors/kcors-tests.ts new file mode 100644 index 0000000000..722fa0b5a4 --- /dev/null +++ b/kcors/kcors-tests.ts @@ -0,0 +1,5 @@ +import koa = require('koa'); +import cors = require('kcors'); + +const app = new koa(); +app.use(cors()); diff --git a/kcors/tsconfig.json b/kcors/tsconfig.json new file mode 100644 index 0000000000..972c458d5a --- /dev/null +++ b/kcors/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "kcors-tests.ts" + ] +} diff --git a/kcors/tslint.json b/kcors/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/kcors/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } diff --git a/koa-bodyparser/index.d.ts b/koa-bodyparser/index.d.ts index 64e745daf2..8783538f2c 100644 --- a/koa-bodyparser/index.d.ts +++ b/koa-bodyparser/index.d.ts @@ -61,7 +61,7 @@ declare function bodyParser(opts?: { * support custom error handle */ onerror?: (err: Error, ctx: Koa.Context) => void; -}): { (ctx: Koa.Context, next?: () => any): any }; +}): Koa.Middleware; declare namespace bodyParser { } export = bodyParser; diff --git a/koa-compress/index.d.ts b/koa-compress/index.d.ts index d66cd14df4..e22e923762 100644 --- a/koa-compress/index.d.ts +++ b/koa-compress/index.d.ts @@ -35,7 +35,7 @@ declare module "koa-compress" { /** * Compress middleware for Koa */ - function compress(options?: CompressOptions): { (ctx: Koa.Context, next?: () => any): any }; + function compress(options?: CompressOptions): Koa.Middleware; namespace compress {} export = compress; diff --git a/koa-favicon/index.d.ts b/koa-favicon/index.d.ts index 5004810a22..ea1105053a 100644 --- a/koa-favicon/index.d.ts +++ b/koa-favicon/index.d.ts @@ -26,7 +26,7 @@ declare function favicon(path: string, options?: { */ maxage?: number; -}): { (ctx: Koa.Context, next?: () => any): any }; +}): Koa.Middleware; declare namespace favicon {} export = favicon; diff --git a/koa-json-error/index.d.ts b/koa-json-error/index.d.ts index cf45c1cc08..9f02cce994 100644 --- a/koa-json-error/index.d.ts +++ b/koa-json-error/index.d.ts @@ -25,7 +25,7 @@ interface JSONErrorOptions { /** * Error handler for pure Koa 2.0.0+ JSON apps */ -declare function jsonError(options?: JSONErrorOptions) : { (ctx: Koa.Context, next?: () => any): any }; +declare function jsonError(options?: JSONErrorOptions) : Koa.Middleware; declare namespace jsonError {} export = jsonError; diff --git a/koa-json/index.d.ts b/koa-json/index.d.ts index 16269804df..df97c5c263 100644 --- a/koa-json/index.d.ts +++ b/koa-json/index.d.ts @@ -7,7 +7,7 @@ import * as Koa from 'koa'; import * as json from 'koa-json'; - + const app = new Koa(); app.use(json()); @@ -17,7 +17,7 @@ import * as Koa from "koa"; declare function json(opts?: { - /** + /** * default to pretty response [true] */ pretty?: boolean, @@ -31,6 +31,6 @@ declare function json(opts?: { * JSON spaces [2] */ spaces?: number -}): { (ctx: Koa.Context, next?: () => any): any }; +}): Koa.Middleware; declare namespace json { } export = json; diff --git a/koa-logger/index.d.ts b/koa-logger/index.d.ts index 586355f4e4..bd0d4174b0 100644 --- a/koa-logger/index.d.ts +++ b/koa-logger/index.d.ts @@ -7,5 +7,6 @@ import * as Koa from 'koa'; -declare var KoaLogger: () => (ctx: Koa.Context, next: () => Promise) => any; +declare function KoaLogger(): Koa.Middleware; +declare namespace KoaLogger {} export = KoaLogger; diff --git a/koa-mount/index.d.ts b/koa-mount/index.d.ts index eac8a488f3..f0cec6f938 100644 --- a/koa-mount/index.d.ts +++ b/koa-mount/index.d.ts @@ -6,15 +6,13 @@ import * as Koa from "koa"; -interface Function { (ctx: Koa.Context, next?: () => any): any } +declare function mount(app: Koa.Middleware): Koa.Middleware; -declare function mount(app: Function): Function; +declare function mount(app: Koa): Koa.Middleware; -declare function mount(app: Koa): Function; +declare function mount(prefix: string, app: Koa.Middleware): Koa.Middleware; -declare function mount(prefix: string, app: Function): Function; - -declare function mount(prefix: string, app: Koa): Function; +declare function mount(prefix: string, app: Koa): Koa.Middleware; declare namespace mount { } diff --git a/koa-passport/index.d.ts b/koa-passport/index.d.ts index f17bf87f39..2bdf4bcfac 100644 --- a/koa-passport/index.d.ts +++ b/koa-passport/index.d.ts @@ -32,23 +32,22 @@ declare module "koa" { import * as passport from "passport"; -interface Middleware { (ctx: Koa.Context, next: () => Promise): any; } interface KoaPassport { use(strategy: passport.Strategy): this; use(name: string, strategy: passport.Strategy): this; unuse(name: string): this; framework(fw: passport.Framework): this; - initialize(options?: { userProperty: string; }): Middleware; - session(options?: { pauseStream: boolean; }): Middleware; + initialize(options?: { userProperty: string; }): Koa.Middleware; + session(options?: { pauseStream: boolean; }): Koa.Middleware; - authenticate(strategy: string, callback?: Function): Middleware; - authenticate(strategy: string, options: Object, callback?: Function): Middleware; - authenticate(strategies: string[], callback?: Function): Middleware; - authenticate(strategies: string[], options: Object, callback?: Function): Middleware; - authorize(strategy: string, callback?: Function): Middleware; - authorize(strategy: string, options: Object, callback?: Function): Middleware; - authorize(strategies: string[], callback?: Function): Middleware; - authorize(strategies: string[], options: Object, callback?: Function): Middleware; + authenticate(strategy: string, callback?: Function): Koa.Middleware; + authenticate(strategy: string, options: Object, callback?: Function): Koa.Middleware; + authenticate(strategies: string[], callback?: Function): Koa.Middleware; + authenticate(strategies: string[], options: Object, callback?: Function): Koa.Middleware; + authorize(strategy: string, callback?: Function): Koa.Middleware; + authorize(strategy: string, options: Object, callback?: Function): Koa.Middleware; + authorize(strategies: string[], callback?: Function): Koa.Middleware; + authorize(strategies: string[], options: Object, callback?: Function): Koa.Middleware; serializeUser(fn: (user: any, done: (err: any, id: any) => void) => void): void; deserializeUser(fn: (id: any, done: (err: any, user: any) => void) => void): void; transformAuthInfo(fn: (info: any, done: (err: any, info: any) => void) => void): void; diff --git a/koa-session-minimal/index.d.ts b/koa-session-minimal/index.d.ts index 1c55660dc9..3fe9da3a52 100644 --- a/koa-session-minimal/index.d.ts +++ b/koa-session-minimal/index.d.ts @@ -38,7 +38,7 @@ declare function session(opts?: { * session store */ store?: any; -}): { (ctx: Koa.Context, next?: () => any): any }; +}): Koa.Middleware; declare namespace session {} export = session; diff --git a/koa-static/index.d.ts b/koa-static/index.d.ts index 9324695748..fb58aa0aea 100644 --- a/koa-static/index.d.ts +++ b/koa-static/index.d.ts @@ -42,6 +42,6 @@ declare function serve(root: string, opts?: { * Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true. */ gzip?: boolean; -}): { (ctx: Koa.Context, next?: () => any): any }; +}): Koa.Middleware; declare namespace serve{} export = serve; diff --git a/koa/index.d.ts b/koa/index.d.ts index 3d787b15b3..4bf82152d2 100644 --- a/koa/index.d.ts +++ b/koa/index.d.ts @@ -145,6 +145,8 @@ declare namespace Koa { toJSON(): any; inspect(): any; } + + export type Middleware = (ctx: Koa.Context, next: () => Promise) => any; } declare class Koa extends EventEmitter { @@ -174,7 +176,7 @@ declare class Koa extends EventEmitter { callback(): (req: http.IncomingMessage, res: http.ServerResponse) => void; onerror(err: any): void; - use(middleware: (ctx: Koa.Context, next: () => Promise) => any): Koa; + use(middleware: Koa.Middleware): Koa; toJSON(): any; inspect(): any;