From 8aa214b8eeb631052eb6aa1ff62c575028dbf8d8 Mon Sep 17 00:00:00 2001 From: Pringgo Radianto Date: Mon, 12 Aug 2024 15:41:18 +0700 Subject: [PATCH] feat(path-rewrite): add path rewrite feature --- src/core.ts | 1 + src/runtime/server/plugin.ts | 33 +++++++++++++++++++++------------ src/utils/path-rewrite.ts | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/utils/path-rewrite.ts diff --git a/src/core.ts b/src/core.ts index 1cbfedd..f7484da 100644 --- a/src/core.ts +++ b/src/core.ts @@ -7,6 +7,7 @@ export interface ProxyParty { baseUrl: string target: string handler?: DefineProxyPartyHandler + pathRewrite?: { [s: string]: string } | ((path: string) => string) } export const defineProxyParty = (proxies: ProxyParty[]) => proxies diff --git a/src/runtime/server/plugin.ts b/src/runtime/server/plugin.ts index c5e4d3b..727b251 100644 --- a/src/runtime/server/plugin.ts +++ b/src/runtime/server/plugin.ts @@ -1,27 +1,36 @@ import { consola } from 'consola' import { defineEventHandler, getQuery, proxyRequest } from 'h3' import { withQuery, joinURL } from 'ufo' +import type { ProxyParty } from '../../core' +import { rewritePath } from '../../utils/path-rewrite' import { defineNitroPlugin } from '#imports' // @ts-expect-error virtual file import configs from '#nuxt-proxy-party-options' -export default defineNitroPlugin(async ({ router }) => { - try { - if (Array.isArray(configs)) { - configs.forEach((config) => { - const handler = defineEventHandler((event) => { - const path = event.context.params?._ ?? '/' +const proxyHandler = (config: ProxyParty) => { + return defineEventHandler((event) => { + const path = event.context.params?._ ?? '/' - const url = withQuery(joinURL(config.target, path), getQuery(event)) + let url = withQuery(joinURL(config.target, path), getQuery(event)) - if (config.handler && typeof config.handler === 'function') { - config.handler(event) - } + if (config.pathRewrite) { + url = rewritePath(config.pathRewrite, url) + } - return proxyRequest(event, url) - }) + if (config.handler && typeof config.handler === 'function') { + config.handler(event) + } + return proxyRequest(event, url) + }) +} + +export default defineNitroPlugin(async ({ router }) => { + try { + if (Array.isArray(configs)) { + configs.forEach((config: ProxyParty) => { + const handler = proxyHandler(config) router.use(config.baseUrl, handler) router.use(`${config.baseUrl}/**`, handler) }) diff --git a/src/utils/path-rewrite.ts b/src/utils/path-rewrite.ts new file mode 100644 index 0000000..0fdaffc --- /dev/null +++ b/src/utils/path-rewrite.ts @@ -0,0 +1,17 @@ +import type { ProxyParty } from '../core' + +export const rewritePath = (pathRewrite: ProxyParty['pathRewrite'], url: string) => { + if (!pathRewrite) return url + + if (typeof pathRewrite === 'function') { + return pathRewrite(url) + } + + for (const [pattern, replacement] of Object.entries(pathRewrite)) { + const regex = new RegExp(pattern) + if (regex.test(url)) { + return url.replace(regex, replacement) + } + } + return url +}