From bbf962905dfad2b704cbd4baf8cbaae4c34054b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Mon, 10 Feb 2020 22:15:53 +0100 Subject: [PATCH] feat(body-parser): bump to 1.19 (#42043) - version update - docs amended - tests updated - default DT configuration Thanks! --- types/body-parser/body-parser-tests.ts | 42 +++++++++-------- types/body-parser/index.d.ts | 65 +++++++++++++++++++++++--- types/body-parser/tsconfig.json | 4 +- types/body-parser/tslint.json | 7 +-- 4 files changed, 83 insertions(+), 35 deletions(-) diff --git a/types/body-parser/body-parser-tests.ts b/types/body-parser/body-parser-tests.ts index 89d905ead0..e040dbb0dd 100644 --- a/types/body-parser/body-parser-tests.ts +++ b/types/body-parser/body-parser-tests.ts @@ -1,11 +1,6 @@ import * as http from 'http'; import express = require('express'); -import { - json, - raw, - text, - urlencoded, -} from 'body-parser'; +import { json, raw, text, urlencoded } from 'body-parser'; const app = express(); @@ -14,22 +9,29 @@ 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; - } -})); +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.post('/api/users', jsonParser, (req, res) => {}); app.use(jsonParser); -// send any data, it should be parsed and printed +const urlencodedParser = urlencoded({ extended: false }); +app.post('/login', urlencodedParser, (req, res) => { + res.send('welcome, ' + req.body.username); +}); +app.use(urlencodedParser); + +app.use(json({ type: 'application/*+json' })); +app.use(raw({ type: 'application/vnd.custom-type' })); +app.use(text({ type: 'text/html' })); + app.all('/', (req, res, next) => { console.log(req.body); res.json(req.body); diff --git a/types/body-parser/index.d.ts b/types/body-parser/index.d.ts index 62f8f5bd82..c3196bb911 100644 --- a/types/body-parser/index.d.ts +++ b/types/body-parser/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for body-parser 1.17 +// Type definitions for body-parser 1.19 // Project: https://github.com/expressjs/body-parser // Definitions by: Santi Albo // Vilic Vane @@ -6,47 +6,98 @@ // Gevik Babakhani // Tomasz Łaziuk // Jason Walton +// Piotr Błażejewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 /// 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 +// for docs go to https://github.com/expressjs/body-parser/tree/1.19.0#body-parser -// @deprecated -declare function bodyParser(options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded): NextHandleFunction; +/** @deprecated */ +declare function bodyParser( + options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded, +): NextHandleFunction; declare namespace bodyParser { interface Options { + /** When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true. */ inflate?: boolean; + /** + * Controls the maximum request body size. If this is a number, + * then the value specifies the number of bytes; if it is a string, + * the value is passed to the bytes library for parsing. Defaults to '100kb'. + */ limit?: number | string; + /** + * The type option is used to determine what media type the middleware will parse + */ type?: string | string[] | ((req: http.IncomingMessage) => any); + /** + * The verify option, if supplied, is called as verify(req, res, buf, encoding), + * where buf is a Buffer of the raw request body and encoding is the encoding of the request. + */ verify?(req: http.IncomingMessage, res: http.ServerResponse, buf: Buffer, encoding: string): void; } interface OptionsJson extends Options { + /** + * + * The reviver option is passed directly to JSON.parse as the second argument. + */ reviver?(key: string, value: any): any; + /** + * When set to `true`, will only accept arrays and objects; + * when `false` will accept anything JSON.parse accepts. Defaults to `true`. + */ strict?: boolean; } interface OptionsText extends Options { + /** + * Specify the default character set for the text content if the charset + * is not specified in the Content-Type header of the request. + * Defaults to `utf-8`. + */ defaultCharset?: string; } interface OptionsUrlencoded extends Options { + /** + * The extended option allows to choose between parsing the URL-encoded data + * with the querystring library (when `false`) or the qs library (when `true`). + */ extended?: boolean; + /** + * The parameterLimit option controls the maximum number of parameters + * that are allowed in the URL-encoded data. If a request contains more parameters than this value, + * a 413 will be returned to the client. Defaults to 1000. + */ parameterLimit?: number; } + /** + * Returns middleware that only parses json and only looks at requests + * where the Content-Type header matches the type option. + */ function json(options?: OptionsJson): NextHandleFunction; - + /** + * Returns middleware that parses all bodies as a Buffer and only looks at requests + * where the Content-Type header matches the type option. + */ function raw(options?: Options): NextHandleFunction; + /** + * Returns middleware that parses all bodies as a string and only looks at requests + * where the Content-Type header matches the type option. + */ function text(options?: OptionsText): NextHandleFunction; - + /** + * Returns middleware that only parses urlencoded bodies and only looks at requests + * where the Content-Type header matches the type option + */ function urlencoded(options?: OptionsUrlencoded): NextHandleFunction; } diff --git a/types/body-parser/tsconfig.json b/types/body-parser/tsconfig.json index 9c0314a557..00bc9ee8dd 100644 --- a/types/body-parser/tsconfig.json +++ b/types/body-parser/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -21,4 +21,4 @@ "index.d.ts", "body-parser-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/body-parser/tslint.json b/types/body-parser/tslint.json index d0f71205de..3db14f85ea 100644 --- a/types/body-parser/tslint.json +++ b/types/body-parser/tslint.json @@ -1,6 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "max-line-length": false - } -} +{ "extends": "dtslint/dt.json" }