diff --git a/body-parser/body-parser-tests.ts b/body-parser/body-parser-tests.ts new file mode 100644 index 0000000000..fa18cad13b --- /dev/null +++ b/body-parser/body-parser-tests.ts @@ -0,0 +1,22 @@ +import * as express from 'express'; +import { + json, + raw, + text, + urlencoded, +} from 'body-parser'; + +const app = express(); + +app.use(json()); +app.use(raw()); +app.use(text()); +app.use(urlencoded()); + +// send any data, it should be parsed and printed +app.all('/', (req, res, next) => { + console.log(req.body); + res.json(req.body); +}); + +app.listen(8080); diff --git a/body-parser/index.d.ts b/body-parser/index.d.ts index 4627c2c5b3..c0ce775bb0 100644 --- a/body-parser/index.d.ts +++ b/body-parser/index.d.ts @@ -1,202 +1,48 @@ -// Type definitions for body-parser -// Project: http://expressjs.com -// Definitions by: Santi Albo , VILIC VANE , Jonathan Häberle , Gevik Babakhani +// Type definitions for body-parser 1.16 +// Project: https://github.com/expressjs/body-parser +// Definitions by: Santi Albo , Vilic Vane , Jonathan Häberle , Gevik Babakhani , Tomasz Łaziuk // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// +import { Request, RequestHandler, Response } from 'express'; +// for docs go to https://github.com/expressjs/body-parser/tree/1.16.0#body-parser -import * as express from "express"; - -/** - * bodyParser: use individual json/urlencoded middlewares - * @deprecated - */ - -declare function bodyParser(options?: { - /** - * if deflated bodies will be inflated. (default: true) - */ - inflate?: boolean; - /** - * maximum request body size. (default: '100kb') - */ - limit?: any; - /** - * function to verify body content, the parsing can be aborted by throwing an error. - */ - verify?: (req: express.Request, res: express.Response, buf: Buffer, encoding: string) => void; - /** - * only parse objects and arrays. (default: true) - */ - strict?: boolean; - /** - * passed to JSON.parse(). - */ - reviver?: (key: string, value: any) => any; - /** - * parse extended syntax with the qs module. (default: true) - */ - extended?: boolean; -}): express.RequestHandler; +// @deprecated +declare function bodyParser(options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded): RequestHandler; declare namespace bodyParser { - /** - * Interface for defining the options for the json() middleware - * - * @export - * @interface JsonOptions - */ - export interface JsonOptions { - /** - * if deflated bodies will be inflated. (default: true) - */ + export interface Options { inflate?: boolean; - /** - * maximum request body size. (default: '100kb') - */ - limit?: any; - /** - * request content-type to parse, passed directly to the type-is library. (default: 'json') - */ - type?: any; - /** - * function to verify body content, the parsing can be aborted by throwing an error. - */ - verify?: (req: express.Request, res: express.Response, buf: Buffer, encoding: string) => void; - /** - * only parse objects and arrays. (default: true) - */ + limit?: number | string; + type?: string | ((req: Request) => any); + verify?: (req: Request, res: Response, buf: Buffer, encoding: string) => void; + } + + export interface OptionsJson extends Options { + reviever?: (key: string, value: any) => any; strict?: boolean; - /** - * passed to JSON.parse(). - */ - reviver?: (key: string, value: any) => any; } - /** - * Interface for defining the options the raw() middleware - * - * @export - * @interface RawOptions - */ - export interface RawOptions { - /** - * if deflated bodies will be inflated. (default: true) - */ - inflate?: boolean; - /** - * maximum request body size. (default: '100kb') - */ - limit?: any; - /** - * request content-type to parse, passed directly to the type-is library. (default: 'application/octet-stream') - */ - type?: any; - /** - * function to verify body content, the parsing can be aborted by throwing an error. - */ - verify?: (req: express.Request, res: express.Response, buf: Buffer, encoding: string) => void; - } - - /** - * Interface for defining the options for the text() middleware - * - * @export - * @interface TextOptions - */ - export interface TextOptions { - /** - * if deflated bodies will be inflated. (default: true) - */ - inflate?: boolean; - /** - * maximum request body size. (default: '100kb') - */ - limit?: any; - /** - * request content-type to parse, passed directly to the type-is library. (default: 'text/plain') - */ - type?: any; - /** - * function to verify body content, the parsing can be aborted by throwing an error. - */ - verify?: (req: express.Request, res: express.Response, buf: Buffer, encoding: string) => void; - /** - * the default charset to parse as, if not specified in content-type. (default: 'utf-8') - */ + export interface OptionsText extends Options { defaultCharset?: string; } - /** - * Interface for defining the options for the urlencoded() middleware - * - * @export - * @interface UrlEncodedOptions - */ - export interface UrlEncodedOptions { - /** - * if deflated bodies will be inflated. (default: true) - */ - inflate?: boolean; - /** - * maximum request body size. (default: '100kb') - */ - limit?: any; - /** - * request content-type to parse, passed directly to the type-is library. (default: 'urlencoded') - */ - type?: any; - /** - * function to verify body content, the parsing can be aborted by throwing an error. - */ - verify?: (req: express.Request, res: express.Response, buf: Buffer, encoding: string) => void; - /** - * parse extended syntax with the qs module. - */ - extended: boolean; + export interface OptionsUrlencoded extends Options { + extended?: boolean; + parameterLimit?: number; } - /** - * Returns middleware that only parses json. This parser accepts any Unicode encoding - * of the body and supports automatic inflation of gzip and deflate encodings. - * - * @export - * @param {JsonOptions} [options] - * @returns {express.RequestHandler} - */ - export function json(options?: JsonOptions): express.RequestHandler; + export function json(options?: OptionsJson): RequestHandler; - /** - * Returns middleware that parses all bodies as a Buffer. This parser supports automatic - * inflation of gzip and deflate encodings. - * - * @export - * @param {RawOptions} [options] - * @returns {express.RequestHandler} - */ - export function raw(options?: RawOptions): express.RequestHandler; + export function raw(options?: Options): RequestHandler; - /** - * Returns middleware that parses all bodies as a string. This parser supports - * automatic inflation of gzip and deflate encodings. - * - * @export - * @param {TextOptions} [options] - * @returns {express.RequestHandler} - */ - export function text(options?: TextOptions): express.RequestHandler; + export function text(options?: OptionsText): RequestHandler; + + export function urlencoded(options?: OptionsUrlencoded): RequestHandler; - /** - * Returns middleware that only parses urlencoded bodies. This parser accepts only - * UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings. - * - * @export - * @param {UrlEncodedOptions} [options] - * @returns {express.RequestHandler} - */ - export function urlencoded(options?: UrlEncodedOptions): express.RequestHandler; } export = bodyParser; diff --git a/body-parser/tsconfig.json b/body-parser/tsconfig.json index 27e01ecd7b..e798cd97d3 100644 --- a/body-parser/tsconfig.json +++ b/body-parser/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es6", + "dom" ], "noImplicitAny": true, "noImplicitThis": true, @@ -16,6 +17,7 @@ "forceConsistentCasingInFileNames": true }, "files": [ - "index.d.ts" + "index.d.ts", + "body-parser-tests.ts" ] -} \ No newline at end of file +} diff --git a/body-parser/tslint.json b/body-parser/tslint.json new file mode 100644 index 0000000000..9311f348d9 --- /dev/null +++ b/body-parser/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "../tslint.json", + "rules": { + "max-line-length": false + } +}