Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Next.js HTTP Proxy Middleware
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

HTTP Proxy middleware available in API Middleware provided by Next.js.
Expand Down
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
}

/**
* @see https://www.npmjs.com/package/http-proxy
* Please refer to the following links for the specification document for HTTP.
* @see https://tools.ietf.org/html/rfc7231
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
*/
const proxy: httpProxy = httpProxy.createProxy();
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];

/**
* If pattern information matching the input url information is found in the `pathRewrite` array,
Expand All @@ -22,7 +24,7 @@ export const rewritePath = (
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
) => {
if(Array.isArray(pathRewrite)){
for (let item of pathRewrite) {
for (const item of pathRewrite) {
const {
patternStr,
replaceStr
Expand All @@ -35,7 +37,7 @@ export const rewritePath = (
} else {
console.warn('[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
+ '(related issue: https://github.com/stegano/next-http-proxy-middleware/issues/39)');
for (let patternStr in pathRewrite) {
for (const patternStr in pathRewrite) {
const pattern = RegExp(patternStr);
const path = pathRewrite[patternStr];
if (pattern.test(url as string)) {
Expand All @@ -61,20 +63,19 @@ const httpProxyMiddleware = async (
new Promise((resolve, reject) => {
const { pathRewrite, onProxyInit, ...serverOptions } = httpProxyOptions;

/**
* @see https://www.npmjs.com/package/http-proxy
*/
const proxy: httpProxy = httpProxy.createProxy();

if(typeof onProxyInit === 'function') {
onProxyInit(proxy);
}

if (pathRewrite) {
req.url = rewritePath(req.url as string, pathRewrite);
}

/**
* Please refer to the following links for the specification document for HTTP.
* @see https://tools.ietf.org/html/rfc7231
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
*/
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];

if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "object") {
req.body = JSON.stringify(req.body);
}
Expand Down