Skip to content

fix(rewritePath): change 'rewritePath' type for apply pattern priority #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2021
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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ export default (req: NextApiRequest, res: NextApiResponse) => (
? httpProxyMiddleware(req, res, {
// You can use the `http-proxy` option
target: 'https://www.example.com',
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy`
pathRewrite: {
'^/api/new': '/v2',
'^/api': '',
},
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`
pathRewrite: [{
patternStr: '^/api/new',
replaceStr: '/v2'
}, {
patternStr: '^/api',
replaceStr: ''
}],
})
: res.status(404).send(null)
);
Expand Down
21 changes: 19 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { rewritePath } from "./index";

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

test("Remove root URI context", () => {
test("[deprecated] Remove root URI context", () => {
const originUrl = "/api/a/b";
expect("/auth/a/b").toEqual(rewritePath(originUrl, { "/api": "/auth" }));
});

test("Replace root URI context", () => {
const originUrl = "/api/a/b";
expect("/auth/a/b").toEqual(rewritePath(originUrl, [{
patternStr: "/api",
replaceStr: "/auth"
},
]));
});

test("Remove root URI context", () => {
const originUrl = "/api/a/b";
expect("/auth/a/b").toEqual(rewritePath(originUrl, [{
patternStr: "/api",
replaceStr: "/auth"
}]));
});
});
36 changes: 26 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiResponse, NextApiRequest } from "next";
import httpProxy, { ServerOptions } from "http-proxy";
export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
pathRewrite?: { [key: string]: string };
pathRewrite?: { [key: string]: string }
| { patternStr: string, replaceStr: string }[];
}

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

/**
* If a key pattern is found in `pathRewrite` that matches the url value,
* replace matched string of url with the `pathRewrite` value.
* @param req
* If pattern information matching the input url information is found in the `pathRewrite` array,
* the url value is partially replaced with the `pathRewrite.replaceStr` value.
* @param url
* @param pathRewrite
*/
export const rewritePath = (
url: string,
pathRewrite: { [key: string]: string }
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
) => {
for (let patternStr in pathRewrite) {
const pattern = RegExp(patternStr);
const path = pathRewrite[patternStr];
if (pattern.test(url as string)) {
return url.replace(pattern, path);
if(Array.isArray(pathRewrite)){
for (let item of pathRewrite) {
const {
patternStr,
replaceStr
} = item;
const pattern = RegExp(patternStr);
if (pattern.test(url as string)) {
return url.replace(pattern, replaceStr);
}
}
} 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) {
const pattern = RegExp(patternStr);
const path = pathRewrite[patternStr];
if (pattern.test(url as string)) {
return url.replace(pattern, path);
}
}
}
return url;
Expand Down