Merge pull request #21665 from brikou/koa_missing_context_properties

Add missing url.URL and generic map to context
This commit is contained in:
Armando Aguirre
2017-12-01 14:24:25 -08:00
committed by GitHub
2 changed files with 69 additions and 27 deletions

72
types/koa/index.d.ts vendored
View File

@@ -1,6 +1,8 @@
// Type definitions for Koa 2.x
// Project: http://koajs.com
// Definitions by: DavidCai1993 <https://github.com/DavidCai1993>, jKey Lu <https://github.com/jkeylu>
// Definitions by: DavidCai1993 <https://github.com/DavidCai1993>
// jKey Lu <https://github.com/jkeylu>
// Brice Bernard <https://github.com/brikou>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -15,20 +17,22 @@
=============================================== */
/// <reference types="node" />
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
@@ -388,7 +396,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;
/**
@@ -440,15 +448,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*/
@@ -482,7 +511,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.
@@ -578,7 +610,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.
@@ -626,6 +660,8 @@ declare namespace Application {
* Default error handling.
*/
onerror(err: Error): void;
[key: string]: any;
}
interface Request extends BaseRequest {

View File

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