From 9fab331bea8319dd9c88fc7b4d2f2a40f3a8771f Mon Sep 17 00:00:00 2001
From: DavidCai <376462191@qq.com>
Date: Sat, 23 Jan 2016 15:52:49 +0800
Subject: [PATCH 1/2] Make the type of koa's ctx accessible
---
koa/koa.d.ts | 197 ++++++++++++++++++++++++++-------------------------
1 file changed, 101 insertions(+), 96 deletions(-)
diff --git a/koa/koa.d.ts b/koa/koa.d.ts
index 450134278..ebbffafc9 100644
--- a/koa/koa.d.ts
+++ b/koa/koa.d.ts
@@ -8,6 +8,10 @@
import * as Koa from "koa"
const app = new Koa()
+ async function (ctx: Koa.IContext, next: Function) {
+ // ...
+ }
+
=============================================== */
///
@@ -16,99 +20,101 @@ declare module "koa" {
import * as http from "http";
import * as net from "net";
- interface IContext extends IRequest, IResponse {
- body?: any;
- request?: IRequest;
- response?: IResponse;
- originalUrl?: string;
- state?: any;
- name?: string;
- cookies?: any;
- writable?: Boolean;
- respond?: Boolean;
- app?: Koa;
- req?: http.IncomingMessage;
- res?: http.ServerResponse;
- onerror(err: any): void;
- toJSON(): any;
- inspect(): any;
- throw(): void;
- assert(): void;
- }
+ module Koa {
+ export interface IContext extends IRequest, IResponse {
+ body?: any;
+ request?: IRequest;
+ response?: IResponse;
+ originalUrl?: string;
+ state?: any;
+ name?: string;
+ cookies?: any;
+ writable?: Boolean;
+ respond?: Boolean;
+ app?: Koa;
+ req?: http.IncomingMessage;
+ res?: http.ServerResponse;
+ onerror(err: any): void;
+ toJSON(): any;
+ inspect(): any;
+ throw(code?: any, message?: any): void;
+ assert(): void;
+ }
- interface IRequest {
- _querycache?: string;
- app?: Koa;
- req?: http.IncomingMessage;
- res?: http.ServerResponse;
- response?: IResponse;
- ctx?: IContext;
- headers?: any;
- header?: any;
- method?: string;
- length?: any;
- url?: string;
- origin?: string;
- originalUrl?: string;
- href?: string;
- path?: string;
- querystring?: string;
- query?: any;
- search?: string;
- idempotent?: Boolean;
- socket?: net.Socket;
- protocol?: string;
- host?: string;
- hostname?: string;
- fresh?: Boolean;
- stale?: Boolean;
- charset?: string;
- secure?: Boolean;
- ips?: Array;
- ip?: string;
- subdomains?: Array;
- accept?: any;
- type?: string;
- accepts?: () => any;
- acceptsEncodings?: () => any;
- acceptsCharsets?: () => any;
- acceptsLanguages?: () => any;
- is?: (types: any) => any;
- toJSON?: () => any;
- inspect?: () => any;
- get?: (field: string) => string;
- }
+ export interface IRequest {
+ _querycache?: string;
+ app?: Koa;
+ req?: http.IncomingMessage;
+ res?: http.ServerResponse;
+ response?: IResponse;
+ ctx?: IContext;
+ headers?: any;
+ header?: any;
+ method?: string;
+ length?: any;
+ url?: string;
+ origin?: string;
+ originalUrl?: string;
+ href?: string;
+ path?: string;
+ querystring?: string;
+ query?: any;
+ search?: string;
+ idempotent?: Boolean;
+ socket?: net.Socket;
+ protocol?: string;
+ host?: string;
+ hostname?: string;
+ fresh?: Boolean;
+ stale?: Boolean;
+ charset?: string;
+ secure?: Boolean;
+ ips?: Array;
+ ip?: string;
+ subdomains?: Array;
+ accept?: any;
+ type?: string;
+ accepts?: () => any;
+ acceptsEncodings?: () => any;
+ acceptsCharsets?: () => any;
+ acceptsLanguages?: () => any;
+ is?: (types: any) => any;
+ toJSON?: () => any;
+ inspect?: () => any;
+ get?: (field: string) => string;
+ }
- interface IResponse {
- _body?: any;
- _explicitStatus?: Boolean;
- app?: Koa;
- res?: http.ServerResponse;
- req?: http.IncomingMessage;
- ctx?: IContext;
- request?: IRequest;
- socket?: net.Socket;
- header?: any;
- headers?: any;
- status?: number;
- message?: string;
- type?: string;
- body?: any;
- length?: any;
- headerSent?: Boolean;
- lastModified?: Date;
- etag?: string;
- writable?: Boolean;
- is?: (types: any) => any;
- redirect?: (url: string, alt: string) => void;
- attachment?: (filename?: string) => void;
- vary?: (field: string) => void;
- get?: (field: string) => string;
- set?: (field: any, val: any) => void;
- remove?: (field: string) => void;
- append?: (field: string, val: any) => void;
- toJSON?: () => any;
- inspect?: () => any;
+ export interface IResponse {
+ _body?: any;
+ _explicitStatus?: Boolean;
+ app?: Koa;
+ res?: http.ServerResponse;
+ req?: http.IncomingMessage;
+ ctx?: IContext;
+ request?: IRequest;
+ socket?: net.Socket;
+ header?: any;
+ headers?: any;
+ status?: number;
+ message?: string;
+ type?: string;
+ body?: any;
+ length?: any;
+ headerSent?: Boolean;
+ lastModified?: Date;
+ etag?: string;
+ writable?: Boolean;
+ is?: (types: any) => any;
+ redirect?: (url: string, alt: string) => void;
+ attachment?: (filename?: string) => void;
+ vary?: (field: string) => void;
+ get?: (field: string) => string;
+ set?: (field: any, val: any) => void;
+ remove?: (field: string) => void;
+ append?: (field: string, val: any) => void;
+ toJSON?: () => any;
+ inspect?: () => any;
+ }
}
class Koa extends EventEmitter {
@@ -117,12 +123,12 @@ declare module "koa" {
proxy: Boolean;
server: http.Server;
env: string;
- context: IContext;
- request: IRequest;
- response: IResponse;
+ context: Koa.IContext;
+ request: Koa.IRequest;
+ response: Koa.IResponse;
silent: Boolean;
constructor();
- use(middleware: (ctx: IContext, next: Function) => any): Koa;
+ use(middleware: (ctx: Koa.IContext, next: Function) => any): Koa;
callback(): (req: http.IncomingMessage, res: http.ServerResponse) => void;
listen(port: number, callback?: Function): http.Server;
toJSON(): any;
@@ -131,6 +137,5 @@ declare module "koa" {
}
namespace Koa {}
-
export = Koa;
}
From 3d7909ee394891b4f6ca5a0b2a556c60924b6bf1 Mon Sep 17 00:00:00 2001
From: DavidCai <376462191@qq.com>
Date: Mon, 25 Jan 2016 12:54:24 +0800
Subject: [PATCH 2/2] Remove prefix I
---
koa/koa.d.ts | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/koa/koa.d.ts b/koa/koa.d.ts
index ebbffafc9..6d9fcd279 100644
--- a/koa/koa.d.ts
+++ b/koa/koa.d.ts
@@ -8,7 +8,7 @@
import * as Koa from "koa"
const app = new Koa()
- async function (ctx: Koa.IContext, next: Function) {
+ async function (ctx: Koa.Context, next: Function) {
// ...
}
@@ -21,10 +21,10 @@ declare module "koa" {
import * as net from "net";
module Koa {
- export interface IContext extends IRequest, IResponse {
+ export interface Context extends Request, Response {
body?: any;
- request?: IRequest;
- response?: IResponse;
+ request?: Request;
+ response?: Response;
originalUrl?: string;
state?: any;
name?: string;
@@ -41,13 +41,13 @@ declare module "koa" {
assert(): void;
}
- export interface IRequest {
+ export interface Request {
_querycache?: string;
app?: Koa;
req?: http.IncomingMessage;
res?: http.ServerResponse;
- response?: IResponse;
- ctx?: IContext;
+ response?: Response;
+ ctx?: Context;
headers?: any;
header?: any;
method?: string;
@@ -84,14 +84,14 @@ declare module "koa" {
get?: (field: string) => string;
}
- export interface IResponse {
+ export interface Response {
_body?: any;
_explicitStatus?: Boolean;
app?: Koa;
res?: http.ServerResponse;
req?: http.IncomingMessage;
- ctx?: IContext;
- request?: IRequest;
+ ctx?: Context;
+ request?: Request;
socket?: net.Socket;
header?: any;
headers?: any;
@@ -123,12 +123,12 @@ declare module "koa" {
proxy: Boolean;
server: http.Server;
env: string;
- context: Koa.IContext;
- request: Koa.IRequest;
- response: Koa.IResponse;
+ context: Koa.Context;
+ request: Koa.Request;
+ response: Koa.Response;
silent: Boolean;
constructor();
- use(middleware: (ctx: Koa.IContext, next: Function) => any): Koa;
+ use(middleware: (ctx: Koa.Context, next: Function) => any): Koa;
callback(): (req: http.IncomingMessage, res: http.ServerResponse) => void;
listen(port: number, callback?: Function): http.Server;
toJSON(): any;