Skip to content

Commit 98ee918

Browse files
committed
Add cache option to fetch
Fixes #809 Add support for the `cache` option in the Next Drupal client. * Add `cache` option to `JsonApiWithNextFetchOptions` type in `packages/next-drupal/src/types/options.ts`. * Update `fetch` method in `packages/next-drupal/src/next-drupal.ts` to pass the `cache` option as part of the `init` object if provided. * Add tests for the `cache` option in `fetch` and `getResource` methods in `packages/next-drupal/tests/NextDrupalBase/fetch-methods.test.ts` and `packages/next-drupal/tests/NextDrupal/resource-methods.test.ts`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/chapter-three/next-drupal/issues/809?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent a50598c commit 98ee918

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

packages/next-drupal/src/next-drupal.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class NextDrupal extends NextDrupalBase {
115115
method: "POST",
116116
body: JSON.stringify(body),
117117
withAuth: options.withAuth,
118+
cache: options.cache,
118119
})
119120

120121
await this.throwIfJsonErrors(response, "Error while creating resource: ")
@@ -158,6 +159,7 @@ export class NextDrupal extends NextDrupalBase {
158159
},
159160
body: body.data.attributes.file,
160161
withAuth: options.withAuth,
162+
cache: options.cache,
161163
})
162164

163165
await this.throwIfJsonErrors(
@@ -202,6 +204,7 @@ export class NextDrupal extends NextDrupalBase {
202204
method: "PATCH",
203205
body: JSON.stringify(body),
204206
withAuth: options.withAuth,
207+
cache: options.cache,
205208
})
206209

207210
await this.throwIfJsonErrors(response, "Error while updating resource: ")
@@ -239,6 +242,7 @@ export class NextDrupal extends NextDrupalBase {
239242
const response = await this.fetch(endpoint, {
240243
method: "DELETE",
241244
withAuth: options.withAuth,
245+
cache: options.cache,
242246
})
243247

244248
await this.throwIfJsonErrors(response, "Error while deleting resource: ")
@@ -287,6 +291,7 @@ export class NextDrupal extends NextDrupalBase {
287291
const response = await this.fetch(endpoint, {
288292
withAuth: options.withAuth,
289293
next: options.next,
294+
cache: options.cache,
290295
})
291296

292297
await this.throwIfJsonErrors(response, "Error while fetching resource: ")
@@ -376,6 +381,7 @@ export class NextDrupal extends NextDrupalBase {
376381
body: JSON.stringify(payload),
377382
withAuth: options.withAuth,
378383
next: options.next,
384+
cache: options.cache,
379385
})
380386

381387
const errorMessagePrefix = "Error while fetching resource by path:"
@@ -435,6 +441,7 @@ export class NextDrupal extends NextDrupalBase {
435441
const response = await this.fetch(endpoint, {
436442
withAuth: options.withAuth,
437443
next: options.next,
444+
cache: options.cache,
438445
})
439446

440447
await this.throwIfJsonErrors(
@@ -500,6 +507,7 @@ export class NextDrupal extends NextDrupalBase {
500507
params,
501508
withAuth: options.withAuth,
502509
next: options.next,
510+
cache: options.cache,
503511
}
504512
if (locale) {
505513
opts = {
@@ -573,6 +581,7 @@ export class NextDrupal extends NextDrupalBase {
573581
const response = await this.fetch(endpoint, {
574582
withAuth: options.withAuth,
575583
next: options.next,
584+
cache: options.cache,
576585
})
577586

578587
if (response.status === 404) {
@@ -600,6 +609,7 @@ export class NextDrupal extends NextDrupalBase {
600609
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
601610
withAuth: false,
602611
next: options?.next,
612+
cache: options?.cache,
603613
})
604614

605615
await this.throwIfJsonErrors(
@@ -710,6 +720,7 @@ export class NextDrupal extends NextDrupalBase {
710720
const response = await this.fetch(endpoint, {
711721
withAuth: options.withAuth,
712722
next: options.next,
723+
cache: options.cache,
713724
})
714725

715726
await this.throwIfJsonErrors(response, "Error while fetching menu items: ")
@@ -760,6 +771,7 @@ export class NextDrupal extends NextDrupalBase {
760771
const response = await this.fetch(endpoint, {
761772
withAuth: options.withAuth,
762773
next: options.next,
774+
cache: options.cache,
763775
})
764776

765777
await this.throwIfJsonErrors(response, "Error while fetching view: ")
@@ -798,6 +810,7 @@ export class NextDrupal extends NextDrupalBase {
798810
const response = await this.fetch(endpoint, {
799811
withAuth: options.withAuth,
800812
next: options.next,
813+
cache: options.cache,
801814
})
802815

803816
await this.throwIfJsonErrors(

packages/next-drupal/src/types/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type JsonApiWithCacheOptions = {
3636

3737
export type JsonApiWithNextFetchOptions = {
3838
next?: NextFetchRequestConfig
39+
cache?: RequestCache
3940
}
4041
// TODO: Properly type this.
4142
/* eslint-disable @typescript-eslint/no-explicit-any */

packages/next-drupal/tests/NextDrupal/resource-methods.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,29 @@ describe("getResource()", () => {
512512
})
513513
)
514514
})
515+
516+
test("makes request with cache option", async () => {
517+
const drupal = new NextDrupal(BASE_URL)
518+
const mockInit = {
519+
cache: "no-store",
520+
} as FetchOptions
521+
522+
const fetchSpy = spyOnFetch()
523+
524+
await drupal.getResource(
525+
"node--recipe",
526+
"71e04ead-4cc7-416c-b9ca-60b635fdc50f",
527+
mockInit
528+
)
529+
530+
expect(fetchSpy).toBeCalledTimes(1)
531+
expect(fetchSpy).toBeCalledWith(
532+
expect.anything(),
533+
expect.objectContaining({
534+
...mockInit,
535+
})
536+
)
537+
})
515538
})
516539

517540
describe("getResourceByPath()", () => {

packages/next-drupal/tests/NextDrupalBase/fetch-methods.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ describe("fetch()", () => {
200200
})
201201
)
202202
})
203+
204+
test("optionally adds cache option", async () => {
205+
const drupal = new NextDrupalBase(BASE_URL)
206+
const mockUrl = "/mock-url"
207+
const mockInit = {
208+
cache: "no-store",
209+
} as FetchOptions
210+
211+
const fetchSpy = spyOnFetch()
212+
213+
await drupal.fetch(mockUrl, mockInit)
214+
215+
expect(fetchSpy).toBeCalledTimes(1)
216+
expect(fetchSpy).toBeCalledWith(
217+
`${BASE_URL}${mockUrl}`,
218+
expect.objectContaining({
219+
...defaultInit,
220+
...mockInit,
221+
})
222+
)
223+
})
203224
})
204225

205226
describe("getAccessToken()", () => {

0 commit comments

Comments
 (0)