Skip to content

Commit aa523d2

Browse files
committed
feat: allow cache and next properties to pass through to fetch
1 parent f9d8ce7 commit aa523d2

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ export type ApplyDefaults<Defaults, Target> = {
2323
[K in keyof Target as K extends keyof Defaults ? K : never]: Target[K]
2424
};
2525

26+
declare global {
27+
interface RequestInit {
28+
next?: {
29+
revalidate?: boolean | number;
30+
tags?: string[];
31+
};
32+
}
33+
}
34+
2635
export type RouteOptions = {
2736
headers?: HeadersInit;
2837
params?: unknown;
@@ -37,6 +46,8 @@ export type DefaultRouteOptions = {
3746
payload?: never;
3847
query?: never;
3948
response?: unknown;
49+
cache?: RequestInit["cache"];
50+
next?: RequestInit["next"];
4051
};
4152

4253
export type RouteDefinition = Record<string, RouteOptions>;
@@ -114,7 +125,7 @@ export class Client<T extends RouteTable> {
114125
}
115126
}
116127

117-
async #fetch<O extends RouteOptions = RouteOptions>(method: RequestMethod, path: string, options?: WithoutResponse<O>): Promise<ClientResponse<O["response"]>> {
128+
async #fetch<O extends DefaultRouteOptions = DefaultRouteOptions>(method: RequestMethod, path: string, options?: WithoutResponse<O>): Promise<ClientResponse<O["response"]>> {
118129
const realPath = options && "params" in options
119130
? replaceParams(path, options.params as Record<string, string>)
120131
: path;
@@ -167,6 +178,8 @@ export class Client<T extends RouteTable> {
167178
method,
168179
headers,
169180
body: options && "payload" in options ? JSON.stringify(options.payload) : undefined,
181+
cache: options?.cache,
182+
next: options?.next,
170183
});
171184

172185
const isJsonResponse = fetchResponse.headers.get("content-type")?.startsWith("application/json");

test/fetch.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ import { test, type TestContext } from "node:test";
22
import { server, Client } from "./fixture.ts";
33
import type { RouteMap } from "./generated.ts";
44

5-
await test("GET - fetch", async (t) => {
6-
let client: Client;
7-
t.before(async () => {
8-
await server.start();
9-
client = new Client({ url: server.info.uri });
10-
});
5+
await test("fetch()", async (t) => {
6+
await server.start();
7+
const client = new Client({ url: server.info.uri });
118

129
t.after(async () => {
1310
await server.stop();
1411
});
1512

13+
await t.test("flags: allows passing `next` property to fetch", async (t) => {
14+
const fetchSpy = t.mock.method(global, "fetch");
15+
t.after(() => {
16+
fetchSpy.mock.restore();
17+
});
18+
19+
const res = await client.get("/simple", { next: { revalidate: 10 } });
20+
t.assert.equal(res.status, 200);
21+
t.assert.equal(fetchSpy.mock.callCount(), 1);
22+
const firstCall = fetchSpy.mock.calls[0];
23+
t.assert.partialDeepStrictEqual(firstCall.arguments, [{ next: { revalidate: 10 } }]);
24+
});
25+
1626
await t.test("/simple", async (t: TestContext) => {
1727
const res = await client.get("/simple");
1828
t.assert.equal(res.status, 200);

0 commit comments

Comments
 (0)