Skip to content

Commit

Permalink
style(body-parser): don't shadow namespace
Browse files Browse the repository at this point in the history
Shadowing causes issues with some parsers like `eslint-plugin-import`:
import-js/eslint-plugin-import#2861

Even if those plugins are at fault, this is an improvement in style:

Actually, the namespace is not necessary at all anymore after
DefinitelyTyped#57168

Hover doc looks slightly nicer:
`const bodyParser: BodyParser` instead of `const bodyParser: bodyparser.BodyParser`

`unnecessary-bind` linter message on `npm test body-parser` is from `master`.
  • Loading branch information
llllvvuu committed Aug 19, 2023
1 parent a692f9f commit 3cc7d00
Showing 1 changed file with 78 additions and 80 deletions.
158 changes: 78 additions & 80 deletions types/body-parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,90 @@ import * as http from 'http';

// for docs go to https://github.com/expressjs/body-parser/tree/1.19.0#body-parser

declare namespace bodyParser {
interface BodyParser {
/**
* @deprecated use individual json/urlencoded middlewares
*/
(options?: OptionsJson & OptionsText & OptionsUrlencoded): NextHandleFunction;
/**
* Returns middleware that only parses json and only looks at requests
* where the Content-Type header matches the type option.
*/
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.
*/
raw(options?: Options): NextHandleFunction;
interface BodyParser {
/**
* @deprecated use individual json/urlencoded middlewares
*/
(options?: OptionsJson & OptionsText & OptionsUrlencoded): NextHandleFunction;
/**
* Returns middleware that only parses json and only looks at requests
* where the Content-Type header matches the type option.
*/
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.
*/
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.
*/
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
*/
urlencoded(options?: OptionsUrlencoded): NextHandleFunction;
}
/**
* Returns middleware that parses all bodies as a string and only looks at requests
* where the Content-Type header matches the type option.
*/
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
*/
urlencoded(options?: OptionsUrlencoded): NextHandleFunction;
}

interface Options {
/** When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true. */
inflate?: boolean | undefined;
/**
* 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 | undefined;
/**
* The type option is used to determine what media type the middleware will parse
*/
type?: string | string[] | ((req: http.IncomingMessage) => any) | undefined;
/**
* 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 Options {
/** When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true. */
inflate?: boolean | undefined;
/**
* 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 | undefined;
/**
* The type option is used to determine what media type the middleware will parse
*/
type?: string | string[] | ((req: http.IncomingMessage) => any) | undefined;
/**
* 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 | undefined;
}
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 | undefined;
}

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 | undefined;
}
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 | undefined;
}

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 | undefined;
/**
* 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 | undefined;
}
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 | undefined;
/**
* 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 | undefined;
}

declare const bodyParser: bodyParser.BodyParser;
declare const bodyParser: BodyParser;

export = bodyParser;

0 comments on commit 3cc7d00

Please sign in to comment.