From 211a5eb474e2ceadc1eadb1fc92ab4d13344a181 Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Tue, 24 Apr 2018 20:18:28 -0400 Subject: [PATCH] [@types/body-parser] Fix so body-parser works with connect/vanilla node.js apps. (#25115) --- types/body-parser/body-parser-tests.ts | 16 ++++++++++++++++ types/body-parser/index.d.ts | 26 ++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/types/body-parser/body-parser-tests.ts b/types/body-parser/body-parser-tests.ts index 2f6a33ea72..89d905ead0 100644 --- a/types/body-parser/body-parser-tests.ts +++ b/types/body-parser/body-parser-tests.ts @@ -1,3 +1,4 @@ +import * as http from 'http'; import express = require('express'); import { json, @@ -13,6 +14,21 @@ app.use(raw()); app.use(text()); app.use(urlencoded()); +const jsonParser = app.use(json({ + inflate: true, + limit: '100kb', + type: 'application/*', + verify: ( + req: http.IncomingMessage, + res: http.ServerResponse, + buf: Buffer, + encoding: string + ) => { + return true; + } +})); +app.use(jsonParser); + // send any data, it should be parsed and printed app.all('/', (req, res, next) => { console.log(req.body); diff --git a/types/body-parser/index.d.ts b/types/body-parser/index.d.ts index b1485770a1..67774e2149 100644 --- a/types/body-parser/index.d.ts +++ b/types/body-parser/index.d.ts @@ -1,24 +1,30 @@ -// Type definitions for body-parser 1.16 +// Type definitions for body-parser 1.17 // Project: https://github.com/expressjs/body-parser -// Definitions by: Santi Albo , Vilic Vane , Jonathan Häberle , Gevik Babakhani , Tomasz Łaziuk +// Definitions by: Santi Albo +// Vilic Vane +// Jonathan Häberle +// Gevik Babakhani +// Tomasz Łaziuk +// Jason Walton // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 /// -import { Request, RequestHandler, Response } from 'express'; +import { NextHandleFunction } from 'connect'; +import * as http from 'http'; // for docs go to https://github.com/expressjs/body-parser/tree/1.16.0#body-parser // @deprecated -declare function bodyParser(options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded): RequestHandler; +declare function bodyParser(options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded): NextHandleFunction; declare namespace bodyParser { interface Options { inflate?: boolean; limit?: number | string; - type?: string | string[] | ((req: Request) => any); - verify?(req: Request, res: Response, buf: Buffer, encoding: string): void; + type?: string | string[] | ((req: http.IncomingMessage) => any); + verify?(req: http.IncomingMessage, res: http.ServerResponse, buf: Buffer, encoding: string): void; } interface OptionsJson extends Options { @@ -35,13 +41,13 @@ declare namespace bodyParser { parameterLimit?: number; } - function json(options?: OptionsJson): RequestHandler; + function json(options?: OptionsJson): NextHandleFunction; - function raw(options?: Options): RequestHandler; + function raw(options?: Options): NextHandleFunction; - function text(options?: OptionsText): RequestHandler; + function text(options?: OptionsText): NextHandleFunction; - function urlencoded(options?: OptionsUrlencoded): RequestHandler; + function urlencoded(options?: OptionsUrlencoded): NextHandleFunction; } export = bodyParser;