Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions src/executor/request-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ function buildUrl(
path: string,
query: Record<string, string>,
): string {
const url = new URL(path, baseUrl);
const base = new URL(baseUrl);
base.pathname = joinUrlPaths(base.pathname, path);
for (const [key, value] of Object.entries(query)) {
url.searchParams.set(key, value);
base.searchParams.set(key, value);
}
return url.toString();
return base.toString();
}

function joinUrlPaths(basePath: string, operationPath: string): string {
const trimmedBase = basePath.replace(/\/+$/, "");
const trimmedOperation = operationPath.replace(/^\/+/, "");

if (!trimmedOperation) {
return trimmedBase || "/";
}

return `${trimmedBase}/${trimmedOperation}`;
}
12 changes: 12 additions & 0 deletions test/executor/request-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ describe("buildRequest", () => {
expect(req.url).toContain("include=email");
});

it("should preserve server path prefixes for absolute operation paths", () => {
const endpoint: EndpointRef = {
...baseEndpoint,
baseUrl: "https://api.example.com/v1",
path: "/users/{userId}",
};

const req = buildRequest({ userId: "42" }, endpoint);

expect(req.url).toBe("https://api.example.com/v1/users/42");
});

it("should skip undefined args", () => {
const req = buildRequest({ userId: "42" }, baseEndpoint);
expect(req.url).not.toContain("include");
Expand Down