mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-22 20:39:17 +08:00
[express-mung] Fix function return types and add write function (#29352)
* [express-mung] Fix function return types and add write function * [express-mung] Indicate types are for version 0.5.1
This commit is contained in:
@@ -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 });
|
||||
|
||||
47
types/express-mung/index.d.ts
vendored
47
types/express-mung/index.d.ts
vendored
@@ -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 <https://github.com/cyrilschumacher>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
@@ -8,37 +8,50 @@
|
||||
/// <reference types="node"/>
|
||||
|
||||
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<any>;
|
||||
type TransformHeader = (request: Request, response: Response) => any;
|
||||
type TransformHeaderAsync = (request: Request, response: Response) => PromiseLike<any>;
|
||||
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<any>;
|
||||
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<any>;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user