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: add cache option to fetch #817

Merged
merged 6 commits into from
Nov 19, 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
21 changes: 17 additions & 4 deletions packages/next-drupal/src/next-drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class NextDrupal extends NextDrupalBase {
async createResource<T extends JsonApiResource>(
type: string,
body: JsonApiCreateResourceBody,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand All @@ -115,6 +115,7 @@ export class NextDrupal extends NextDrupalBase {
method: "POST",
body: JSON.stringify(body),
withAuth: options.withAuth,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while creating resource: ")
Expand All @@ -129,7 +130,7 @@ export class NextDrupal extends NextDrupalBase {
async createFileResource<T = DrupalFile>(
type: string,
body: JsonApiCreateFileResourceBody,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -158,6 +159,7 @@ export class NextDrupal extends NextDrupalBase {
},
body: body.data.attributes.file,
withAuth: options.withAuth,
cache: options.cache,
})

await this.throwIfJsonErrors(
Expand All @@ -174,7 +176,7 @@ export class NextDrupal extends NextDrupalBase {
type: string,
uuid: string,
body: JsonApiUpdateResourceBody,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -202,6 +204,7 @@ export class NextDrupal extends NextDrupalBase {
method: "PATCH",
body: JSON.stringify(body),
withAuth: options.withAuth,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while updating resource: ")
Expand All @@ -216,7 +219,7 @@ export class NextDrupal extends NextDrupalBase {
async deleteResource(
type: string,
uuid: string,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<boolean> {
options = {
withAuth: true,
Expand All @@ -239,6 +242,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
method: "DELETE",
withAuth: options.withAuth,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while deleting resource: ")
Expand Down Expand Up @@ -287,6 +291,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while fetching resource: ")
Expand Down Expand Up @@ -376,6 +381,7 @@ export class NextDrupal extends NextDrupalBase {
body: JSON.stringify(payload),
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

const errorMessagePrefix = "Error while fetching resource by path:"
Expand Down Expand Up @@ -435,6 +441,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

await this.throwIfJsonErrors(
Expand Down Expand Up @@ -500,6 +507,7 @@ export class NextDrupal extends NextDrupalBase {
params,
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
}
if (locale) {
opts = {
Expand Down Expand Up @@ -573,6 +581,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

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

await this.throwIfJsonErrors(
Expand Down Expand Up @@ -710,6 +720,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while fetching menu items: ")
Expand Down Expand Up @@ -760,6 +771,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

await this.throwIfJsonErrors(response, "Error while fetching view: ")
Expand Down Expand Up @@ -798,6 +810,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
cache: options.cache,
})

await this.throwIfJsonErrors(
Expand Down
1 change: 1 addition & 0 deletions packages/next-drupal/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type JsonApiWithCacheOptions = {

export type JsonApiWithNextFetchOptions = {
next?: NextFetchRequestConfig
cache?: RequestCache
}
// TODO: Properly type this.
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
165 changes: 165 additions & 0 deletions packages/next-drupal/tests/NextDrupal/resource-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,25 @@ describe("getMenu()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.getMenu("main", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getResource()", () => {
Expand Down Expand Up @@ -512,6 +531,29 @@ describe("getResource()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.getResource(
"node--recipe",
"71e04ead-4cc7-416c-b9ca-60b635fdc50f",
mockInit
)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getResourceByPath()", () => {
Expand Down Expand Up @@ -830,6 +872,32 @@ describe("getResourceByPath()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch({
status: 207,
statusText: "Multi-Status",
responseBody: mocks.resources.subRequests.ok,
})

await drupal.getResourceByPath<DrupalNode>(
"/recipes/deep-mediterranean-quiche",
mockInit
)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getResourceCollection()", () => {
Expand Down Expand Up @@ -957,6 +1025,25 @@ describe("getResourceCollection()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.getResourceCollection("node--recipe", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getResourceCollectionPathSegments()", () => {
Expand Down Expand Up @@ -1116,6 +1203,27 @@ describe("getResourceCollectionPathSegments()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch({
responseBody: { data: [] },
})

await drupal.getResourceCollectionPathSegments("node--article", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getSearchIndex()", () => {
Expand Down Expand Up @@ -1246,6 +1354,25 @@ describe("getSearchIndex()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.getSearchIndex("recipes", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("getView()", () => {
Expand Down Expand Up @@ -1369,6 +1496,25 @@ describe("getView()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.getView("featured_articles--page_1", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})

describe("translatePath()", () => {
Expand Down Expand Up @@ -1478,4 +1624,23 @@ describe("translatePath()", () => {
})
)
})

test("makes request with cache option", async () => {
const drupal = new NextDrupal(BASE_URL)
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.translatePath("recipes/deep-mediterranean-quiche", mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
expect.anything(),
expect.objectContaining({
...mockInit,
})
)
})
})
21 changes: 21 additions & 0 deletions packages/next-drupal/tests/NextDrupalBase/fetch-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,27 @@ describe("fetch()", () => {
})
)
})

test("optionally adds cache option", async () => {
const drupal = new NextDrupalBase(BASE_URL)
const mockUrl = "/mock-url"
const mockInit = {
cache: "no-store",
} as FetchOptions

const fetchSpy = spyOnFetch()

await drupal.fetch(mockUrl, mockInit)

expect(fetchSpy).toBeCalledTimes(1)
expect(fetchSpy).toBeCalledWith(
`${BASE_URL}${mockUrl}`,
expect.objectContaining({
...defaultInit,
...mockInit,
})
)
})
})

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