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(openapi-fetch): baseUrl per request #1817

Merged
merged 6 commits into from
Aug 18, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/happy-singers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": minor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note for next time: we’re using minor patches only for breaking changes while we’re on 0.x on openapi-fetch. patch includes feature additions and bugfixes before 1.0.

---

Allow specifying baseUrl per request
1 change: 1 addition & 0 deletions docs/openapi-fetch/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ client.GET("/my-url", options);
| `querySerializer` | QuerySerializer | (optional) Provide a [querySerializer](#queryserializer) |
| `bodySerializer` | BodySerializer | (optional) Provide a [bodySerializer](#bodyserializer) |
| `parseAs` | `"json"` \| `"text"` \| `"arrayBuffer"` \| `"blob"` \| `"stream"` | (optional) Parse the response using [a built-in instance method](https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods) (default: `"json"`). `"stream"` skips parsing altogether and returns the raw stream. |
| `baseUrl` | `string` | Prefix the fetch URL with this option (e.g. "https://myapi.dev/v1/") |
| `fetch` | `fetch` | Fetch instance used for requests (default: fetch from `createClient`) |
| `middleware` | `Middleware[]` | [See docs](/openapi-fetch/middleware-auth) |
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal`, …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)) |
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export type FetchResponse<T, Options, Media extends MediaType> =

export type RequestOptions<T> = ParamsOption<T> &
RequestBodyOption<T> & {
baseUrl?: string;
querySerializer?: QuerySerializer<T> | QuerySerializerOptions;
bodySerializer?: BodySerializer<T>;
parseAs?: ParseAs;
Expand Down Expand Up @@ -253,3 +254,6 @@ export declare function createFinalURL<O>(

/** Merge headers a and b, with b taking priority */
export declare function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): Headers;

/** Remove trailing slash from url */
export declare function removeTrailingSlash(url: string): string;
19 changes: 16 additions & 3 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export default function createClient(clientOptions) {
headers: baseHeaders,
...baseOptions
} = { ...clientOptions };
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length - 1);
}
baseUrl = removeTrailingSlash(baseUrl);
baseHeaders = mergeHeaders(DEFAULT_HEADERS, baseHeaders);
const middlewares = [];

Expand All @@ -53,6 +51,7 @@ export default function createClient(clientOptions) {
*/
async function coreFetch(schemaPath, fetchOptions) {
const {
baseUrl: localBaseUrl,
fetch = baseFetch,
headers,
params = {},
Expand All @@ -61,6 +60,9 @@ export default function createClient(clientOptions) {
bodySerializer = globalBodySerializer ?? defaultBodySerializer,
...init
} = fetchOptions || {};
if (localBaseUrl) {
baseUrl = removeTrailingSlash(localBaseUrl);
}

let querySerializer =
typeof globalQuerySerializer === "function"
Expand Down Expand Up @@ -484,3 +486,14 @@ export function mergeHeaders(...allHeaders) {
}
return finalHeaders;
}

/**
* Remove trailing slash from url
* @type {import("./index.js").removeTrailingSlash}
*/
export function removeTrailingSlash(url) {
if (url.endsWith("/")) {
return url.substring(0, url.length - 1);
}
return url;
}
50 changes: 50 additions & 0 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,29 @@ describe("client", () => {
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
});

it("baseUrl per request", async () => {
const localBaseUrl = "https://api.foo.bar/v1";
let client = createClient<paths>({ baseUrl });

const { getRequestUrl } = useMockRequestHandler({
baseUrl: localBaseUrl,
method: "get",
path: "/self",
status: 200,
body: { message: "OK" },
});

await client.GET("/self", { baseUrl: localBaseUrl });

// assert baseUrl and path mesh as expected
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self", localBaseUrl));

client = createClient<paths>({ baseUrl });
await client.GET("/self", { baseUrl: localBaseUrl });
// assert trailing '/' was removed
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self", localBaseUrl));
});

describe("headers", () => {
it("persist", async () => {
const headers: HeadersInit = { Authorization: "Bearer secrettoken" };
Expand Down Expand Up @@ -1272,6 +1295,33 @@ describe("client", () => {
expect(req.headers.get("onFetch")).toBe("exists");
expect(req.headers.get("onRequest")).toBe("exists");
});

it("baseUrl can be overridden", async () => {
useMockRequestHandler({
baseUrl: "https://api.foo.bar/v1/",
method: "get",
path: "/self",
status: 200,
body: {},
});

let requestBaseUrl = "";

const client = createClient<paths>({
baseUrl,
});
client.use({
onRequest({ options }) {
requestBaseUrl = options.baseUrl;
return undefined;
},
});

await client.GET("/self", {
baseUrl: "https://api.foo.bar/v1/",
});
expect(requestBaseUrl).toBe("https://api.foo.bar/v1");
});
});
});

Expand Down