Skip to content

Commit

Permalink
fix(proxy): append request query params for single proxy route rules (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 17, 2023
1 parent 710f9e5 commit a64dcc5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/runtime/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "h3";
import defu from "defu";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import { joinURL, withoutBase } from "ufo";
import { joinURL, withQuery, getQuery, withoutBase } from "ufo";
import { useRuntimeConfig } from "./config";
import type { NitroRouteRules } from "nitropack";

Expand Down Expand Up @@ -42,6 +42,9 @@ export function createRouteRulesHandler() {
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else if (event.path.includes("?")) {
const query = getQuery(event.path);
target = withQuery(target, query);
}
return proxyRequest(event, target, {
fetch: $fetch.raw as any,
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/api/echo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default eventHandler((event) => {
return {
url: event.node.req.url,
method: event.node.req.method,
headers: event.node.req.headers,
};
});
1 change: 1 addition & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default defineNitroConfig({
"/rules/_/noncached/**": { swr: false, cache: false, isr: false },
"/rules/_/cached/noncached": { cache: false, swr: false, isr: false },
"/rules/_/cached/**": { swr: true },
"/api/proxy/**": { proxy: "/api/echo" },
},
prerender: {
crawlLinks: true,
Expand Down
8 changes: 6 additions & 2 deletions test/presets/nitro-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ describe("nitro:preset:nitro-dev", async () => {
testNitro(
ctx,
() => {
return async ({ url }) => {
const res = await ctx.fetch(url);
return async ({ url, headers, method, body }) => {
const res = await ctx.fetch(url, {
headers,
method,
body,
});
return res;
};
},
Expand Down
15 changes: 13 additions & 2 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from "pathe";
import { listen, Listener } from "listhen";
import destr from "destr";
import { fetch, FetchOptions } from "ofetch";
import { expect, it, afterAll } from "vitest";
import { expect, it, afterAll, beforeAll } from "vitest";
import { fileURLToPath } from "mlly";
import { joinURL } from "ufo";
import * as _nitro from "../src";
Expand Down Expand Up @@ -121,7 +121,7 @@ export function testNitro(
};
}

it("setup handler", async () => {
beforeAll(async () => {
_handler = await getHandler();
});

Expand Down Expand Up @@ -297,6 +297,17 @@ export function testNitro(
}
}

it("runtime proxy", async () => {
const { data } = await callHandler({
url: "/api/proxy?foo=bar",
headers: {
"x-test": "foobar",
},
});
expect(data.headers["x-test"]).toBe("foobar");
expect(data.url).toBe("/api/echo?foo=bar");
});

it("app config", async () => {
const { data } = await callHandler({
url: "/app-config",
Expand Down

0 comments on commit a64dcc5

Please sign in to comment.