koa: Introduce Koa.Middleware type and add 'kcors' definition (#13447)

This commit is contained in:
Andy
2016-12-19 14:33:25 -08:00
committed by GitHub
parent 677f96b63a
commit cce31af92d
15 changed files with 75 additions and 28 deletions

21
kcors/index.d.ts vendored Normal file
View File

@@ -0,0 +1,21 @@
// Type definitions for kcors 2.2
// Project: https://github.com/koajs/cors
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>
// 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;
}
}

5
kcors/kcors-tests.ts Normal file
View File

@@ -0,0 +1,5 @@
import koa = require('koa');
import cors = require('kcors');
const app = new koa();
app.use(cors());

20
kcors/tsconfig.json Normal file
View File

@@ -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"
]
}

1
kcors/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "../tslint.json" }

View File

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

View File

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

View File

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

View File

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

6
koa-json/index.d.ts vendored
View File

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

View File

@@ -7,5 +7,6 @@
import * as Koa from 'koa';
declare var KoaLogger: () => (ctx: Koa.Context, next: () => Promise<any>) => any;
declare function KoaLogger(): Koa.Middleware;
declare namespace KoaLogger {}
export = KoaLogger;

10
koa-mount/index.d.ts vendored
View File

@@ -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 { }

View File

@@ -32,23 +32,22 @@ declare module "koa" {
import * as passport from "passport";
interface Middleware { (ctx: Koa.Context, next: () => Promise<any>): 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;

View File

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

View File

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

4
koa/index.d.ts vendored
View File

@@ -145,6 +145,8 @@ declare namespace Koa {
toJSON(): any;
inspect(): any;
}
export type Middleware = (ctx: Koa.Context, next: () => Promise<any>) => 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>) => any): Koa;
use(middleware: Koa.Middleware): Koa;
toJSON(): any;
inspect(): any;