Skip to content

Commit 2eaee87

Browse files
Merge branch 'express-raw-body' of https://github.com/tolgap/nest into tolgap-express-raw-body
2 parents 7ca14a2 + 26fd5a7 commit 2eaee87

15 files changed

Lines changed: 220 additions & 6 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { NestExpressApplication } from '@nestjs/platform-express';
2+
import { Test } from '@nestjs/testing';
3+
import { expect } from 'chai';
4+
import * as request from 'supertest';
5+
import { ExpressModule } from '../src/express.module';
6+
7+
describe('Raw body (Express Application)', () => {
8+
let app: NestExpressApplication;
9+
const body = '{ "amount":0.0 }';
10+
11+
beforeEach(async () => {
12+
const moduleFixture = await Test.createTestingModule({
13+
imports: [ExpressModule],
14+
}).compile();
15+
16+
app = moduleFixture.createNestApplication<NestExpressApplication>(
17+
undefined,
18+
{ rawBody: true },
19+
);
20+
});
21+
22+
it('should return exact post body', async () => {
23+
await app.init();
24+
const response = await request(app.getHttpServer())
25+
.post('/')
26+
.set('Content-Type', 'application/json')
27+
.send(body)
28+
.expect(201);
29+
30+
expect(response.body).to.eql({
31+
parsed: {
32+
amount: 0,
33+
},
34+
raw: '{ "amount":0.0 }',
35+
});
36+
});
37+
38+
it('should work if post body is empty', async () => {
39+
await app.init();
40+
await request(app.getHttpServer())
41+
.post('/')
42+
.set('Content-Type', 'application/json')
43+
.expect(201);
44+
});
45+
46+
afterEach(async () => {
47+
await app.close();
48+
});
49+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { NestFastifyApplication } from '@nestjs/platform-fastify';
2+
import { Test } from '@nestjs/testing';
3+
import { expect } from 'chai';
4+
import * as request from 'supertest';
5+
import { FastifyModule } from '../src/fastify.module';
6+
7+
describe('Raw body (Fastify Application)', () => {
8+
let app: NestFastifyApplication;
9+
const body = '{ "amount":0.0 }';
10+
11+
beforeEach(async () => {
12+
const moduleFixture = await Test.createTestingModule({
13+
imports: [FastifyModule],
14+
}).compile();
15+
16+
app = moduleFixture.createNestApplication<NestFastifyApplication>(null, {
17+
rawBody: true,
18+
});
19+
});
20+
21+
it('should return exact post body', async () => {
22+
await app.init();
23+
const response = await request(app.getHttpServer())
24+
.post('/')
25+
.set('Content-Type', 'application/json')
26+
.set('Accept', 'application/json')
27+
.send(body)
28+
.expect(201);
29+
30+
expect(response.body).to.eql({
31+
parsed: {
32+
amount: 0,
33+
},
34+
raw: '{ "amount":0.0 }',
35+
});
36+
});
37+
38+
it('should work if post body is empty', async () => {
39+
await app.init();
40+
await request(app.getHttpServer())
41+
.post('/')
42+
.set('Content-Type', 'application/json')
43+
.expect(201);
44+
});
45+
46+
afterEach(async () => {
47+
await app.close();
48+
});
49+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Controller, Post, Req, RawBodyRequest } from '@nestjs/common';
2+
import { Request } from 'express';
3+
4+
@Controller()
5+
export class ExpressController {
6+
@Post()
7+
getRawBody(@Req() req: RawBodyRequest<Request>) {
8+
return {
9+
parsed: req.body,
10+
raw: req.rawBody.toString(),
11+
};
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { ExpressController } from './express.controller';
3+
4+
@Module({
5+
controllers: [ExpressController],
6+
})
7+
export class ExpressModule {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Controller, Post, Req, RawBodyRequest } from '@nestjs/common';
2+
import type { FastifyRequest } from 'fastify';
3+
4+
@Controller()
5+
export class FastifyController {
6+
@Post()
7+
getRawBody(@Req() req: RawBodyRequest<FastifyRequest>) {
8+
return {
9+
parsed: req.body,
10+
raw: req.rawBody.toString(),
11+
};
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { FastifyController } from './fastify.controller';
3+
4+
@Module({
5+
controllers: [FastifyController],
6+
})
7+
export class FastifyModule {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": false,
5+
"noImplicitAny": false,
6+
"removeComments": true,
7+
"noLib": false,
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"target": "es6",
11+
"sourceMap": true,
12+
"allowJs": true,
13+
"outDir": "./dist"
14+
},
15+
"include": [
16+
"src/**/*",
17+
"e2e/**/*"
18+
],
19+
"exclude": [
20+
"node_modules"
21+
]
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './http-module.interface';
2+
export * from './raw-body-request.interface';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type RawBodyRequest<T> = T & { rawBody?: Buffer };

packages/common/interfaces/http/http-server.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface HttpServer<TRequest = any, TResponse = any> {
6767
getRequestMethod?(request: TRequest): string;
6868
getRequestUrl?(request: TRequest): string;
6969
getInstance(): any;
70-
registerParserMiddleware(): any;
70+
registerParserMiddleware(...args: any[]): any;
7171
enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>): any;
7272
getHttpServer(): any;
7373
initHttpServer(options: NestApplicationOptions): void;

0 commit comments

Comments
 (0)