Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(express): remove body-parser types and ship these types our own #11325

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions packages/platform-express/adapters/express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
import { RouterMethodFactory } from '@nestjs/core/helpers/router-method-factory';
import {
json as bodyParserJson,
OptionsJson,
OptionsUrlencoded,
urlencoded as bodyParserUrlencoded,
} from 'body-parser';
import * as bodyparser from 'body-parser';
Expand All @@ -37,6 +35,7 @@ import * as http from 'http';
import * as https from 'https';
import { Duplex, pipeline } from 'stream';
import { NestExpressBodyParserOptions } from '../interfaces/nest-express-body-parser-options.interface';
import { NestExpressBodyParserType } from '../interfaces/nest-express-body-parser.interface';
import { ServeStaticOptions } from '../interfaces/serve-static-options.interface';
import { getBodyParserOptions } from './utils/get-body-parser-options.util';

Expand Down Expand Up @@ -236,11 +235,10 @@ export class ExpressAdapter extends AbstractHttpAdapter {
}

public registerParserMiddleware(prefix?: string, rawBody?: boolean) {
const bodyParserJsonOptions = getBodyParserOptions<OptionsJson>(rawBody);
const bodyParserUrlencodedOptions = getBodyParserOptions<OptionsUrlencoded>(
rawBody,
{ extended: true },
);
const bodyParserJsonOptions = getBodyParserOptions(rawBody);
const bodyParserUrlencodedOptions = getBodyParserOptions(rawBody, {
extended: true,
});

const parserMiddleware = {
jsonParser: bodyParserJson(bodyParserJsonOptions),
Expand All @@ -251,12 +249,12 @@ export class ExpressAdapter extends AbstractHttpAdapter {
.forEach(parserKey => this.use(parserMiddleware[parserKey]));
}

public useBodyParser<Options extends bodyparser.Options = bodyparser.Options>(
type: keyof bodyparser.BodyParser,
public useBodyParser<Options = NestExpressBodyParserOptions>(
type: NestExpressBodyParserType,
rawBody: boolean,
options?: NestExpressBodyParserOptions<Options>,
options?: Omit<Options, 'verify'>,
): this {
const parserOptions = getBodyParserOptions(rawBody, options || {});
const parserOptions = getBodyParserOptions<Options>(rawBody, options);
const parser = bodyparser[type](parserOptions);

this.use(parser);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RawBodyRequest } from '@nestjs/common';
import type { Options } from 'body-parser';
import type { IncomingMessage, ServerResponse } from 'http';
import type { NestExpressBodyParserOptions } from '../../interfaces';

const rawBodyParser = (
req: RawBodyRequest<IncomingMessage>,
Expand All @@ -13,11 +13,11 @@ const rawBodyParser = (
return true;
};

export function getBodyParserOptions<ParserOptions extends Options>(
export function getBodyParserOptions<Options = NestExpressBodyParserOptions>(
rawBody: boolean,
options?: ParserOptions | undefined,
): ParserOptions {
let parserOptions: ParserOptions = options ?? ({} as ParserOptions);
options?: Omit<Options, 'verify'> | undefined,
): Options {
let parserOptions: Options = (options || {}) as Options;

if (rawBody === true) {
parserOptions = {
Expand Down
3 changes: 2 additions & 1 deletion packages/platform-express/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './nest-express-application.interface';
export * from './nest-express-body-parser-options.interface';
export { NestExpressBodyParserOptions } from './nest-express-body-parser-options.interface';
export * from './nest-express-body-parser.interface';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Server } from 'http';
import { INestApplication } from '@nestjs/common';
import * as bodyparser from 'body-parser';
import { NestExpressBodyParserOptions } from './nest-express-body-parser-options.interface';
import { ServeStaticOptions } from './serve-static-options.interface';
import { NestExpressBodyParserType } from './nest-express-body-parser.interface';

/**
* Interface describing methods on NestExpressApplication.
Expand Down Expand Up @@ -88,9 +88,9 @@ export interface NestExpressApplication extends INestApplication {
*
* @returns {this}
*/
useBodyParser<Options extends bodyparser.Options = bodyparser.Options>(
parser: keyof bodyparser.BodyParser,
options?: NestExpressBodyParserOptions<Options>,
useBodyParser<Options = NestExpressBodyParserOptions>(
parser: NestExpressBodyParserType,
options?: Omit<Options, 'verify'>,
): this;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import type { Options } from 'body-parser';
import type { IncomingMessage } from 'http';

export type NestExpressBodyParserOptions<T extends Options = Options> = Omit<
T,
'verify'
>;
/**
* Type alias to keep compatibility with @types/body-parser
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/dcd1673c4fa18a15ea8cd8ff8af7d563bb6dc8e6/types/body-parser/index.d.ts#L48-L66#L48-L66
* @publicApi
*/
export interface NestExpressBodyParserOptions {
/** 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: IncomingMessage) => any) | undefined;

// Catch-all for body-parser type specific options
[key: string]: unknown;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Interface defining possible body parser types, to be used with `NestExpressApplication.useBodyParser()`.
*/
export type NestExpressBodyParserType = 'json' | 'urlencoded' | 'text' | 'raw';