Skip to content

Commit

Permalink
feat: extract fixRequestBody to external handler
Browse files Browse the repository at this point in the history
  • Loading branch information
midgleyc committed Apr 18, 2021
1 parent 13815ff commit 6f79a44
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
26 changes: 26 additions & 0 deletions src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ClientRequest } from 'http';
import type { Request } from '../types';
import * as querystring from 'querystring';

/**
* Fix proxied body if bodyParser is involved.
*/
export function fixRequestBody(proxyReq: ClientRequest, req: Request) {
if (!req.body || !Object.keys(req.body).length) {
return;
}

const contentType = proxyReq.getHeader('Content-Type') as string;
const writeBody = (bodyData: string) => {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
};

if (contentType.includes('application/json')) {
writeBody(JSON.stringify(req.body));
}

if (contentType === 'application/x-www-form-urlencoded') {
writeBody(querystring.stringify(req.body));
}
}
1 change: 1 addition & 0 deletions src/handlers/public.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { responseInterceptor } from './response-interceptor';
export { fixRequestBody } from './fix-request-body';
25 changes: 0 additions & 25 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ClientRequest } from 'http';
import type * as https from 'https';
import type * as express from 'express';
import type { Filter, Request, RequestHandler, Response, Options } from './types';
import * as httpProxy from 'http-proxy';
import { createConfig, Config } from './config-factory';
import * as querystring from 'querystring';
import * as contextMatcher from './context-matcher';
import * as handlers from './_handlers';
import { getArrow, getInstance } from './logger';
Expand Down Expand Up @@ -35,9 +33,6 @@ export class HttpProxyMiddleware {
// log errors for debug purpose
this.proxy.on('error', this.logError);

// fix proxied body if bodyParser is involved
this.proxy.on('proxyReq', this.fixBody);

// https://github.com/chimurai/http-proxy-middleware/issues/19
// expose function to upgrade externally
(this.middleware as any).upgrade = (req, socket, head) => {
Expand Down Expand Up @@ -198,24 +193,4 @@ export class HttpProxyMiddleware {

this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference);
};

private fixBody = (proxyReq: ClientRequest, req: Request) => {
if (!req.body || !Object.keys(req.body).length) {
return;
}

const contentType = proxyReq.getHeader('Content-Type') as string;
const writeBody = (bodyData: string) => {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
};

if (contentType.includes('application/json')) {
writeBody(JSON.stringify(req.body));
}

if (contentType === 'application/x-www-form-urlencoded') {
writeBody(querystring.stringify(req.body));
}
};
}
2 changes: 1 addition & 1 deletion test/e2e/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as express from 'express';
import { Express, RequestHandler } from 'express';

export { createProxyMiddleware, responseInterceptor } from '../../dist/index';
export { createProxyMiddleware, responseInterceptor, fixRequestBody } from '../../dist/index';

export function createApp(...middleware: RequestHandler[]): Express {
const app = express();
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createProxyMiddleware, createApp, createAppWithPath } from './_utils';
import { createProxyMiddleware, createApp, createAppWithPath, fixRequestBody } from './_utils';
import * as request from 'supertest';
import { Mockttp, getLocal, CompletedRequest } from 'mockttp';
import { Request, Response } from '../../src/types';
Expand Down Expand Up @@ -86,6 +86,7 @@ describe('E2E http-proxy-middleware', () => {
bodyParser.urlencoded({ extended: false }),
createProxyMiddleware('/api', {
target: `http://localhost:${mockTargetServer.port}`,
onProxyReq: fixRequestBody,
})
)
);
Expand All @@ -102,6 +103,7 @@ describe('E2E http-proxy-middleware', () => {
bodyParser.json(),
createProxyMiddleware('/api', {
target: `http://localhost:${mockTargetServer.port}`,
onProxyReq: fixRequestBody,
})
)
);
Expand Down

0 comments on commit 6f79a44

Please sign in to comment.