Skip to content

Commit 6f79a44

Browse files
committed
feat: extract fixRequestBody to external handler
1 parent 13815ff commit 6f79a44

5 files changed

Lines changed: 31 additions & 27 deletions

File tree

src/handlers/fix-request-body.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ClientRequest } from 'http';
2+
import type { Request } from '../types';
3+
import * as querystring from 'querystring';
4+
5+
/**
6+
* Fix proxied body if bodyParser is involved.
7+
*/
8+
export function fixRequestBody(proxyReq: ClientRequest, req: Request) {
9+
if (!req.body || !Object.keys(req.body).length) {
10+
return;
11+
}
12+
13+
const contentType = proxyReq.getHeader('Content-Type') as string;
14+
const writeBody = (bodyData: string) => {
15+
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
16+
proxyReq.write(bodyData);
17+
};
18+
19+
if (contentType.includes('application/json')) {
20+
writeBody(JSON.stringify(req.body));
21+
}
22+
23+
if (contentType === 'application/x-www-form-urlencoded') {
24+
writeBody(querystring.stringify(req.body));
25+
}
26+
}

src/handlers/public.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { responseInterceptor } from './response-interceptor';
2+
export { fixRequestBody } from './fix-request-body';

src/http-proxy-middleware.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { ClientRequest } from 'http';
21
import type * as https from 'https';
32
import type * as express from 'express';
43
import type { Filter, Request, RequestHandler, Response, Options } from './types';
54
import * as httpProxy from 'http-proxy';
65
import { createConfig, Config } from './config-factory';
7-
import * as querystring from 'querystring';
86
import * as contextMatcher from './context-matcher';
97
import * as handlers from './_handlers';
108
import { getArrow, getInstance } from './logger';
@@ -35,9 +33,6 @@ export class HttpProxyMiddleware {
3533
// log errors for debug purpose
3634
this.proxy.on('error', this.logError);
3735

38-
// fix proxied body if bodyParser is involved
39-
this.proxy.on('proxyReq', this.fixBody);
40-
4136
// https://github.com/chimurai/http-proxy-middleware/issues/19
4237
// expose function to upgrade externally
4338
(this.middleware as any).upgrade = (req, socket, head) => {
@@ -198,24 +193,4 @@ export class HttpProxyMiddleware {
198193

199194
this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference);
200195
};
201-
202-
private fixBody = (proxyReq: ClientRequest, req: Request) => {
203-
if (!req.body || !Object.keys(req.body).length) {
204-
return;
205-
}
206-
207-
const contentType = proxyReq.getHeader('Content-Type') as string;
208-
const writeBody = (bodyData: string) => {
209-
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
210-
proxyReq.write(bodyData);
211-
};
212-
213-
if (contentType.includes('application/json')) {
214-
writeBody(JSON.stringify(req.body));
215-
}
216-
217-
if (contentType === 'application/x-www-form-urlencoded') {
218-
writeBody(querystring.stringify(req.body));
219-
}
220-
};
221196
}

test/e2e/_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as express from 'express';
22
import { Express, RequestHandler } from 'express';
33

4-
export { createProxyMiddleware, responseInterceptor } from '../../dist/index';
4+
export { createProxyMiddleware, responseInterceptor, fixRequestBody } from '../../dist/index';
55

66
export function createApp(...middleware: RequestHandler[]): Express {
77
const app = express();

test/e2e/http-proxy-middleware.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createProxyMiddleware, createApp, createAppWithPath } from './_utils';
1+
import { createProxyMiddleware, createApp, createAppWithPath, fixRequestBody } from './_utils';
22
import * as request from 'supertest';
33
import { Mockttp, getLocal, CompletedRequest } from 'mockttp';
44
import { Request, Response } from '../../src/types';
@@ -86,6 +86,7 @@ describe('E2E http-proxy-middleware', () => {
8686
bodyParser.urlencoded({ extended: false }),
8787
createProxyMiddleware('/api', {
8888
target: `http://localhost:${mockTargetServer.port}`,
89+
onProxyReq: fixRequestBody,
8990
})
9091
)
9192
);
@@ -102,6 +103,7 @@ describe('E2E http-proxy-middleware', () => {
102103
bodyParser.json(),
103104
createProxyMiddleware('/api', {
104105
target: `http://localhost:${mockTargetServer.port}`,
106+
onProxyReq: fixRequestBody,
105107
})
106108
)
107109
);

0 commit comments

Comments
 (0)