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

fix(proxy): append request query params for single proxy route rules #1163

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 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,11 @@ export function createRouteRulesHandler() {
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else {
const query = getQuery(event.path);
if (query) {
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
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": 123,
},
});
expect(data.headers["x-test"]).toBe("123");
expect(data.url).toBe("/api/echo?foo=bar");
});

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