Skip to content
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

feat: allow setting rewrite option for devProxy #1893

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ You can use this option to override development server routes and proxy-pass req
devProxy: {
'/proxy/test': 'http://localhost:3001',
'/proxy/example': { target: 'https://example.com', changeOrigin: true }
'/api': { target: 'https://example.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, '') }
}
}
```
Expand Down
6 changes: 6 additions & 0 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
App,
createApp,
eventHandler,
getRequestPath,
fromNodeMiddleware,
H3Error,
H3Event,
Expand Down Expand Up @@ -168,6 +169,11 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
app.use(
route,
eventHandler(async (event) => {
const { rewrite } = opts;
if (rewrite) {
const path = getRequestPath(event);
event.node.req.url = rewrite(path);
gliheng marked this conversation as resolved.
Show resolved Hide resolved
}
await proxy.handle(event);
})
);
Expand Down
4 changes: 3 additions & 1 deletion src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ export interface NitroOptions extends PresetOptions {
dev: boolean;
devServer: DevServerOptions;
watchOptions: WatchOptions;
devProxy: Record<string, string | ProxyServerOptions>;
devProxy: Record<string, string | ProxyServerOptions & {
rewrite?: (string) => string;
}>;

// Logging
logging: {
Expand Down