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

NextJs options added to fetch #1571

Closed
Closed
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/rare-pets-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fixed fetch options used by NextJs to be passed to fetch.
4 changes: 4 additions & 0 deletions packages/openapi-fetch/examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ async function getFact() {
params: {
query: { max_length: 500 },
},
next: {
revalidate: 10,
tags: ["cat"],
},
});
}

Expand Down
7 changes: 6 additions & 1 deletion packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export type RequestBodyOption<T> =
: { body: OperationRequestBodyContent<T> };

export type FetchOptions<T> = RequestOptions<T> &
Omit<RequestInit, "body" | "headers">;
Omit<RequestInit, "body" | "headers"> &
NextJsFetchOptions;

export type NextJsFetchOptions = {
next?: { revalidate?: false | 0 | number; tags?: string[] };
};

/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
export type MaybeOptionalInit<P extends {}, M extends keyof P> =
Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export default function createClient(clientOptions) {
}

// fetch!
let response = await fetch(request);
let response = await fetch(
request,
init.next ? { next: init.next } : undefined,
);

// middleware (response)
// execute in reverse-array order (first priority gets last transform)
Expand Down
25 changes: 25 additions & 0 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,31 @@ describe("client", () => {
const req = fetchMocker.mock.calls[0][0];
expect(req.credentials).toBe("include");
});

it("passes NextJs specific options", async () => {
function createCustomFetch(data: any) {
const response = {
clone: () => ({ ...response }),
headers: new Headers(),
json: async () => data,
status: 200,
ok: true,
} as Response;
return async (
input: RequestInfo | URL,
init?: RequestInit | undefined,
) => {
expect(init).toEqual({ next: { tags: ["tag"], revalidate: 10 } });
return Promise.resolve(response);
};
}

const customFetch = createCustomFetch({});
const client = createClient<paths>({ fetch: customFetch });

mockFetchOnce({ status: 200, body: "{}" });
client.GET("/self", { next: { tags: ["tag"], revalidate: 10 } });
});
});

describe("responses", () => {
Expand Down
Loading