@@ -7,9 +7,11 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
7
7
}
8
8
9
9
/**
10
- * @see https://www.npmjs.com/package/http-proxy
10
+ * Please refer to the following links for the specification document for HTTP.
11
+ * @see https://tools.ietf.org/html/rfc7231
12
+ * @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
11
13
*/
12
- const proxy : httpProxy = httpProxy . createProxy ( ) ;
14
+ const hasRequestBodyMethods : string [ ] = [ "HEAD" , "POST" , "PUT" , "DELETE" , "CONNECT" , "OPTIONS" , "PATCH" ] ;
13
15
14
16
/**
15
17
* If pattern information matching the input url information is found in the `pathRewrite` array,
@@ -22,7 +24,7 @@ export const rewritePath = (
22
24
pathRewrite : NextHttpProxyMiddlewareOptions [ 'pathRewrite' ]
23
25
) => {
24
26
if ( Array . isArray ( pathRewrite ) ) {
25
- for ( let item of pathRewrite ) {
27
+ for ( const item of pathRewrite ) {
26
28
const {
27
29
patternStr,
28
30
replaceStr
@@ -35,7 +37,7 @@ export const rewritePath = (
35
37
} else {
36
38
console . warn ( '[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
37
39
+ '(related issue: https://github.com./stegano/next-http-proxy-middleware/issues/39)' ) ;
38
- for ( let patternStr in pathRewrite ) {
40
+ for ( const patternStr in pathRewrite ) {
39
41
const pattern = RegExp ( patternStr ) ;
40
42
const path = pathRewrite [ patternStr ] ;
41
43
if ( pattern . test ( url as string ) ) {
@@ -61,20 +63,19 @@ const httpProxyMiddleware = async (
61
63
new Promise ( ( resolve , reject ) => {
62
64
const { pathRewrite, onProxyInit, ...serverOptions } = httpProxyOptions ;
63
65
66
+ /**
67
+ * @see https://www.npmjs.com/package/http-proxy
68
+ */
69
+ const proxy : httpProxy = httpProxy . createProxy ( ) ;
70
+
64
71
if ( typeof onProxyInit === 'function' ) {
65
72
onProxyInit ( proxy ) ;
66
73
}
67
74
68
75
if ( pathRewrite ) {
69
76
req . url = rewritePath ( req . url as string , pathRewrite ) ;
70
77
}
71
-
72
- /**
73
- * Please refer to the following links for the specification document for HTTP.
74
- * @see https://tools.ietf.org/html/rfc7231
75
- * @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
76
- */
77
- const hasRequestBodyMethods : string [ ] = [ "HEAD" , "POST" , "PUT" , "DELETE" , "CONNECT" , "OPTIONS" , "PATCH" ] ;
78
+
78
79
if ( hasRequestBodyMethods . indexOf ( req . method as string ) >= 0 && typeof req . body === "object" ) {
79
80
req . body = JSON . stringify ( req . body ) ;
80
81
}
0 commit comments