Skip to content

Commit d9cbf29

Browse files
committed
fix(rewritePath): change 'rewritePath' type for apply pattern priority
related: #39
1 parent eb8c0c8 commit d9cbf29

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ export default (req: NextApiRequest, res: NextApiResponse) => (
4343
? httpProxyMiddleware(req, res, {
4444
// You can use the `http-proxy` option
4545
target: 'https://www.example.com',
46-
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy`
47-
pathRewrite: {
48-
'^/api/new': '/v2',
49-
'^/api': '',
50-
},
46+
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`
47+
pathRewrite: [{
48+
patternStr: '^/api/new',
49+
replaceStr: '/v2'
50+
}, {
51+
patternStr: '^/api',
52+
replaceStr: ''
53+
}],
5154
})
5255
: res.status(404).send(null)
5356
);

src/index.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import { rewritePath } from "./index";
22

33
describe("Test `rewritePath` functionn ", () => {
4-
test("Replace root URI context", () => {
4+
test("[deprecated] Replace root URI context", () => {
55
const originUrl = "/api/a/b";
66
expect("/auth/a/b").toEqual(rewritePath(originUrl, { "/api": "/auth" }));
77
});
88

9-
test("Remove root URI context", () => {
9+
test("[deprecated] Remove root URI context", () => {
1010
const originUrl = "/api/a/b";
1111
expect("/auth/a/b").toEqual(rewritePath(originUrl, { "/api": "/auth" }));
1212
});
13+
14+
test("Replace root URI context", () => {
15+
const originUrl = "/api/a/b";
16+
expect("/auth/a/b").toEqual(rewritePath(originUrl, [{
17+
patternStr: "/api",
18+
replaceStr: "/auth"
19+
},
20+
]));
21+
});
22+
23+
test("Remove root URI context", () => {
24+
const originUrl = "/api/a/b";
25+
expect("/auth/a/b").toEqual(rewritePath(originUrl, [{
26+
patternStr: "/api",
27+
replaceStr: "/auth"
28+
}]));
29+
});
1330
});

src/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { NextApiResponse, NextApiRequest } from "next";
22
import httpProxy, { ServerOptions } from "http-proxy";
33
export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
4-
pathRewrite?: { [key: string]: string };
4+
pathRewrite?: { [key: string]: string }
5+
| { patternStr: string, replaceStr: string }[];
56
}
67

78
/**
@@ -10,20 +11,35 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
1011
const proxy: httpProxy = httpProxy.createProxy();
1112

1213
/**
13-
* If a key pattern is found in `pathRewrite` that matches the url value,
14-
* replace matched string of url with the `pathRewrite` value.
15-
* @param req
14+
* If pattern information matching the input url information is found in the `pathRewrite` array,
15+
* the url value is partially replaced with the `pathRewrite.replaceStr` value.
16+
* @param url
1617
* @param pathRewrite
1718
*/
1819
export const rewritePath = (
1920
url: string,
20-
pathRewrite: { [key: string]: string }
21+
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
2122
) => {
22-
for (let patternStr in pathRewrite) {
23-
const pattern = RegExp(patternStr);
24-
const path = pathRewrite[patternStr];
25-
if (pattern.test(url as string)) {
26-
return url.replace(pattern, path);
23+
if(Array.isArray(pathRewrite)){
24+
for (let item of pathRewrite) {
25+
const {
26+
patternStr,
27+
replaceStr
28+
} = item;
29+
const pattern = RegExp(patternStr);
30+
if (pattern.test(url as string)) {
31+
return url.replace(pattern, replaceStr);
32+
}
33+
}
34+
} else {
35+
console.warn('[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
36+
+ '(related issue: https://github.com/stegano/next-http-proxy-middleware/issues/39)');
37+
for (let patternStr in pathRewrite) {
38+
const pattern = RegExp(patternStr);
39+
const path = pathRewrite[patternStr];
40+
if (pattern.test(url as string)) {
41+
return url.replace(pattern, path);
42+
}
2743
}
2844
}
2945
return url;

0 commit comments

Comments
 (0)