UNPKG

4.47 kBTypeScriptView Raw
1// Type definitions for body-parser 1.19
2// Project: https://github.com/expressjs/body-parser
3// Definitions by: Santi Albo <https://github.com/santialbo>
4// Vilic Vane <https://github.com/vilic>
5// Jonathan Häberle <https://github.com/dreampulse>
6// Gevik Babakhani <https://github.com/blendsdk>
7// Tomasz Łaziuk <https://github.com/tlaziuk>
8// Jason Walton <https://github.com/jwalton>
9// Piotr Błażejewicz <https://github.com/peterblazejewicz>
10// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
12/// <reference types="node" />
13
14import { NextHandleFunction } from 'connect';
15import * as http from 'http';
16
17// for docs go to https://github.com/expressjs/body-parser/tree/1.19.0#body-parser
18
19declare namespace bodyParser {
20 interface BodyParser {
21 /**
22 * @deprecated use individual json/urlencoded middlewares
23 */
24 (options?: OptionsJson & OptionsText & OptionsUrlencoded): NextHandleFunction;
25 /**
26 * Returns middleware that only parses json and only looks at requests
27 * where the Content-Type header matches the type option.
28 */
29 json(options?: OptionsJson): NextHandleFunction;
30 /**
31 * Returns middleware that parses all bodies as a Buffer and only looks at requests
32 * where the Content-Type header matches the type option.
33 */
34 raw(options?: Options): NextHandleFunction;
35
36 /**
37 * Returns middleware that parses all bodies as a string and only looks at requests
38 * where the Content-Type header matches the type option.
39 */
40 text(options?: OptionsText): NextHandleFunction;
41 /**
42 * Returns middleware that only parses urlencoded bodies and only looks at requests
43 * where the Content-Type header matches the type option
44 */
45 urlencoded(options?: OptionsUrlencoded): NextHandleFunction;
46 }
47
48 interface Options {
49 /** When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true. */
50 inflate?: boolean | undefined;
51 /**
52 * Controls the maximum request body size. If this is a number,
53 * then the value specifies the number of bytes; if it is a string,
54 * the value is passed to the bytes library for parsing. Defaults to '100kb'.
55 */
56 limit?: number | string | undefined;
57 /**
58 * The type option is used to determine what media type the middleware will parse
59 */
60 type?: string | string[] | ((req: http.IncomingMessage) => any) | undefined;
61 /**
62 * The verify option, if supplied, is called as verify(req, res, buf, encoding),
63 * where buf is a Buffer of the raw request body and encoding is the encoding of the request.
64 */
65 verify?(req: http.IncomingMessage, res: http.ServerResponse, buf: Buffer, encoding: string): void;
66 }
67
68 interface OptionsJson extends Options {
69 /**
70 *
71 * The reviver option is passed directly to JSON.parse as the second argument.
72 */
73 reviver?(key: string, value: any): any;
74 /**
75 * When set to `true`, will only accept arrays and objects;
76 * when `false` will accept anything JSON.parse accepts. Defaults to `true`.
77 */
78 strict?: boolean | undefined;
79 }
80
81 interface OptionsText extends Options {
82 /**
83 * Specify the default character set for the text content if the charset
84 * is not specified in the Content-Type header of the request.
85 * Defaults to `utf-8`.
86 */
87 defaultCharset?: string | undefined;
88 }
89
90 interface OptionsUrlencoded extends Options {
91 /**
92 * The extended option allows to choose between parsing the URL-encoded data
93 * with the querystring library (when `false`) or the qs library (when `true`).
94 */
95 extended?: boolean | undefined;
96 /**
97 * The parameterLimit option controls the maximum number of parameters
98 * that are allowed in the URL-encoded data. If a request contains more parameters than this value,
99 * a 413 will be returned to the client. Defaults to 1000.
100 */
101 parameterLimit?: number | undefined;
102 }
103}
104
105declare const bodyParser: bodyParser.BodyParser;
106
107export = bodyParser;
108
\No newline at end of file