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

Allow unsetting default headers #1314

Merged
merged 1 commit into from
Aug 20, 2023
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/gentle-socks-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Make headers typing friendlier
5 changes: 5 additions & 0 deletions .changeset/weak-games-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Allow unsetting headers
10 changes: 10 additions & 0 deletions packages/openapi-fetch/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ describe("client", () => {
);
});

it("allows unsetting headers", async () => {
const client = createClient<paths>({ headers: { "Content-Type": null } });
mockFetchOnce({ status: 200, body: JSON.stringify({ email: "[email protected]" }) });
await client.GET("/self", { params: {} });

// assert default headers were passed
const options = fetchMocker.mock.calls[0][1];
expect(options?.headers).toEqual(new Headers());
});

it("accepts a custom fetch function", async () => {
const data = { works: true };
const customFetch = {
Expand Down
18 changes: 9 additions & 9 deletions packages/openapi-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const TRAILING_SLASH_RE = /\/*$/;
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */

/** options for each client instance */
interface ClientOptions extends RequestInit {
interface ClientOptions extends Omit<RequestInit, "headers"> {
/** set the common root URL for all API requests */
baseUrl?: string;
/** custom fetch (defaults to globalThis.fetch) */
Expand All @@ -20,7 +20,10 @@ interface ClientOptions extends RequestInit {
querySerializer?: QuerySerializer<unknown>;
/** global bodySerializer */
bodySerializer?: BodySerializer<unknown>;
// headers override to make typing friendlier
headers?: HeadersOptions;
}
export type HeadersOptions = HeadersInit | Record<string, string | number | boolean | null | undefined>;
export type QuerySerializer<T> = (query: T extends { parameters: any } ? NonNullable<T["parameters"]["query"]> : Record<string, unknown>) => string;
export type BodySerializer<T> = (body: OperationRequestBodyContent<T>) => any;
export type ParseAs = "json" | "text" | "blob" | "arrayBuffer" | "stream";
Expand All @@ -43,17 +46,12 @@ export type RequestOptions<T> = ParamsOption<T> &
export default function createClient<Paths extends {}>(clientOptions: ClientOptions = {}) {
const { fetch = globalThis.fetch, querySerializer: globalQuerySerializer, bodySerializer: globalBodySerializer, ...options } = clientOptions;

const defaultHeaders = new Headers({
...DEFAULT_HEADERS,
...(options.headers ?? {}),
});

async function coreFetch<P extends keyof Paths, M extends HttpMethod>(url: P, fetchOptions: FetchOptions<M extends keyof Paths[P] ? Paths[P][M] : never>): Promise<FetchResponse<M extends keyof Paths[P] ? Paths[P][M] : unknown>> {
const { headers, body: requestBody, params = {}, parseAs = "json", querySerializer = globalQuerySerializer ?? defaultQuerySerializer, bodySerializer = globalBodySerializer ?? defaultBodySerializer, ...init } = fetchOptions || {};

// URL
const finalURL = createFinalURL(url as string, { baseUrl: options.baseUrl, params, querySerializer });
const finalHeaders = mergeHeaders(defaultHeaders as any, headers as any, (params as any).header);
const finalHeaders = mergeHeaders(DEFAULT_HEADERS, clientOptions?.headers, headers, (params as any).header);

// fetch!
const requestInit: RequestInit = { redirect: "follow", ...options, ...init, headers: finalHeaders };
Expand Down Expand Up @@ -157,13 +155,15 @@ export function createFinalURL<O>(url: string, options: { baseUrl?: string; para
}

/** merge headers a and b, with b taking priority */
export function mergeHeaders(...allHeaders: (Record<string, unknown> | Headers)[]): Headers {
export function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): Headers {
const headers = new Headers();
for (const headerSet of allHeaders) {
if (!headerSet || typeof headerSet !== "object") continue;
const iterator = headerSet instanceof Headers ? headerSet.entries() : Object.entries(headerSet);
for (const [k, v] of iterator) {
if (v !== undefined && v !== null) {
if (v === null) {
headers.delete(k);
} else if (v !== undefined) {
headers.set(k, v as any);
}
}
Expand Down