From 40c131a283627305986a25d0f09937e6e1f53ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brikou=20Carr=C3=A9?= Date: Tue, 21 Nov 2017 01:03:17 +0100 Subject: [PATCH] Add missing url.URL and generic map to context --- types/koa/index.d.ts | 72 +++++++++++++++++++++++++++++++----------- types/koa/koa-tests.ts | 24 ++++++++------ 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/types/koa/index.d.ts b/types/koa/index.d.ts index d46920bf07..ea59b75b29 100644 --- a/types/koa/index.d.ts +++ b/types/koa/index.d.ts @@ -1,6 +1,8 @@ // Type definitions for Koa 2.x // Project: http://koajs.com -// Definitions by: DavidCai1993 , jKey Lu +// Definitions by: DavidCai1993 +// jKey Lu +// Brice Bernard // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -15,20 +17,22 @@ =============================================== */ /// -import { EventEmitter } from 'events'; -import { IncomingMessage, ServerResponse, Server } from 'http'; -import { Socket, ListenOptions } from 'net'; -import * as compose from 'koa-compose'; -import * as Keygrip from 'keygrip'; -import * as httpAssert from 'http-assert'; -import * as Cookies from 'cookies'; -import * as accepts from 'accepts'; +import * as accepts from "accepts"; +import * as Cookies from "cookies"; +import { EventEmitter } from "events"; +import { IncomingMessage, ServerResponse, Server } from "http"; +import * as httpAssert from "http-assert"; +import * as Keygrip from "keygrip"; +import * as compose from "koa-compose"; +import { Socket, ListenOptions } from "net"; +import * as url from "url"; declare interface ContextDelegatedRequest { /** * Return request header. */ header: any; + /** * Return request header, alias as request.header */ @@ -80,7 +84,6 @@ declare interface ContextDelegatedRequest { */ search: string; - /** * Parse the "Host" header field host * and support X-Forwarded-Host when a @@ -95,6 +98,11 @@ declare interface ContextDelegatedRequest { */ hostname: string; + /** + * Get WHATWG parsed URL object. + */ + URL: url.URL; + /** * Check if the request is fresh, aka * Last-Modified and/or the ETag @@ -383,7 +391,7 @@ declare interface ContextDelegatedResponse { * this.set('Accept', 'application/json'); * this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' }); */ - set(field: { [key: string]: string; }): void; + set(field: { [key: string]: string }): void; set(field: string, val: string | string[]): void; /** @@ -435,15 +443,36 @@ declare class Application extends EventEmitter { * * http.createServer(app.callback()).listen(...) */ - listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Server; - listen(port: number, hostname?: string, listeningListener?: () => void): Server; + listen( + port?: number, + hostname?: string, + backlog?: number, + listeningListener?: () => void, + ): Server; + listen( + port: number, + hostname?: string, + listeningListener?: () => void, + ): Server; /* tslint:disable:unified-signatures */ - listen(port: number, backlog?: number, listeningListener?: () => void): Server; + listen( + port: number, + backlog?: number, + listeningListener?: () => void, + ): Server; listen(port: number, listeningListener?: () => void): Server; - listen(path: string, backlog?: number, listeningListener?: () => void): Server; + listen( + path: string, + backlog?: number, + listeningListener?: () => void, + ): Server; listen(path: string, listeningListener?: () => void): Server; listen(options: ListenOptions, listeningListener?: () => void): Server; - listen(handle: any, backlog?: number, listeningListener?: () => void): Server; + listen( + handle: any, + backlog?: number, + listeningListener?: () => void, + ): Server; listen(handle: any, listeningListener?: () => void): Server; /* tslint:enable:unified-signatures*/ @@ -477,7 +506,10 @@ declare class Application extends EventEmitter { * * @api private */ - createContext(req: IncomingMessage, res: ServerResponse): Application.Context; + createContext( + req: IncomingMessage, + res: ServerResponse, + ): Application.Context; /** * Default error handler. @@ -573,7 +605,9 @@ declare namespace Application { toJSON(): any; } - interface BaseContext extends ContextDelegatedRequest, ContextDelegatedResponse { + interface BaseContext + extends ContextDelegatedRequest, + ContextDelegatedResponse { /** * util.inspect() implementation, which * just returns the JSON output. @@ -621,6 +655,8 @@ declare namespace Application { * Default error handling. */ onerror(err: Error): void; + + [key: string]: any; } interface Request extends BaseRequest { diff --git a/types/koa/koa-tests.ts b/types/koa/koa-tests.ts index 1b261fdf44..6dbc073bea 100644 --- a/types/koa/koa-tests.ts +++ b/types/koa/koa-tests.ts @@ -1,21 +1,27 @@ - import * as Koa from "koa"; const app = new Koa(); +app.context.db = () => {}; + +app.use(async ctx => { + console.log(ctx.db); +}); + app.use((ctx, next) => { - const start: any = new Date(); - return next().then(() => { - const end: any = new Date(); - const ms = end - start; - console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); - ctx.assert(true, 404, 'Yep!'); - }); + const start: any = new Date(); + return next().then(() => { + const end: any = new Date(); + const ms = end - start; + console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); + ctx.assert(true, 404, "Yep!"); + }); }); // response app.use(ctx => { - ctx.body = "Hello World"; + ctx.body = "Hello World"; + ctx.body = ctx.URL.toString(); }); app.listen(3000);