diff --git a/README.md b/README.md index a775b0b..3fc4260 100644 --- a/README.md +++ b/README.md @@ -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) ); diff --git a/src/index.test.ts b/src/index.test.ts index 6306f8f..3f9cd1c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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" + }])); + }); }); diff --git a/src/index.ts b/src/index.ts index e0d751a..db0ab66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }[]; } /** @@ -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;