diff --git a/types/express-mung/express-mung-tests.ts b/types/express-mung/express-mung-tests.ts index 86106e2c59..77aebc6c05 100644 --- a/types/express-mung/express-mung-tests.ts +++ b/types/express-mung/express-mung-tests.ts @@ -1,7 +1,35 @@ import { Request, Response } from "express"; import * as mung from "express-mung"; -function redact(body: Object, req: Request, res: Response) { -    return body; +function redact(body: {}, req: Request, res: Response) { + return body; } + mung.json(redact); +mung.json(redact, { mungError: true }); + +function redactAsync(body: {}, req: Request, res: Response) { + return Promise.resolve(body); +} + +mung.jsonAsync(redactAsync); +mung.jsonAsync(redactAsync, { mungError: true }); + +function transformHeaders(req: Request, res: Response) { + return; +} + +mung.headers(transformHeaders); + +function transformHeadersAsync(req: Request, res: Response) { + return Promise.resolve(); +} + +mung.headersAsync(transformHeadersAsync); + +function transformChunk(chunk: string | Buffer, encoding: string | null, req: Request, res: Response) { + return 'chunk'; +} + +mung.write(transformChunk); +mung.write(transformChunk, { mungError: true }); diff --git a/types/express-mung/index.d.ts b/types/express-mung/index.d.ts index e6de726cd8..1236dc86dc 100644 --- a/types/express-mung/index.d.ts +++ b/types/express-mung/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for express-mung 0.4.2 +// Type definitions for express-mung 0.5.1 // Project: https://github.com/richardschneider/express-mung // Definitions by: Cyril Schumacher // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -8,37 +8,50 @@ /// declare module "express-mung" { - import { Request, Response } from "express"; - import * as http from "http"; + import { Request, Response, RequestHandler } from "express"; type Transform = (body: {}, request: Request, response: Response) => any; - type TransformHeader = (body: http.IncomingMessage, request: Request, response: Response) => any; + type TransformAsync = (body: {}, request: Request, response: Response) => PromiseLike; + type TransformHeader = (request: Request, response: Response) => any; + type TransformHeaderAsync = (request: Request, response: Response) => PromiseLike; + type TransformChunk = (chunk: string | Buffer, encoding: string | null, request: Request, response: Response) => string | Buffer; + type Options = { mungError: boolean }; /** * Transform the JSON body of the response. * @param {Transform} fn A transformation function. - * @return {any} The body. + * @param {Options} [options] json options. + * @return {RequestHandler} Middleware to transform the body */ - export function json(fn: Transform): any; + export function json(fn: Transform, options?: Options): RequestHandler; /** - * Transform the JSON body of the response. - * @param {Transform} fn A transformation function. - * @return {any} The body. + * Asynchronously transform the JSON body of the response. + * @param {TransformAsync} fn A transformation function. + * @param {Options} [options] jsonAsync options. + * @return {RequestHandler} Middleware to transform the body */ - export function jsonAsync(fn: Transform): PromiseLike; + export function jsonAsync(fn: TransformAsync, options?: Options): RequestHandler; /** * Transform the HTTP headers of the response. - * @param {Transform} fn A transformation function. - * @return {any} The body. + * @param {TransformHeader} fn A transformation function. + * @return {RequestHandler} Middleware to transform the headers */ - export function headers(fn: TransformHeader): any; + export function headers(fn: TransformHeader): RequestHandler; /** - * Transform the HTTP headers of the response. - * @param {Transform} fn A transformation function. - * @return {any} The body. + * Asynchronously transform the HTTP headers of the response. + * @param {TransformHeaderAsync} fn A transformation function. + * @return {RequestHandler} Middleware to transform the headers */ - export function headersAsync(fn: TransformHeader): PromiseLike; + export function headersAsync(fn: TransformHeaderAsync): RequestHandler; + + /** + * Transform chunks as they are written to the response + * @param {TransformChunk} fn A transformation function. + * @param {Options} [options] Write options. + * @return {RequestHandler} Middleware to transform chunks. + */ + export function write(fn: TransformChunk, options?: Options): RequestHandler; } diff --git a/types/express-mung/tsconfig.json b/types/express-mung/tsconfig.json index 9dd04b84e6..1588655c88 100644 --- a/types/express-mung/tsconfig.json +++ b/types/express-mung/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "express-mung-tests.ts" ] -} \ No newline at end of file +}