Skip to content

Commit

Permalink
feat(next-drupal): add next revalidate options to resource-methods an…
Browse files Browse the repository at this point in the history
…d custom fetch
  • Loading branch information
Marco Monti committed Jun 27, 2024
1 parent f56c243 commit 1679890
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 10 deletions.
37 changes: 29 additions & 8 deletions packages/next-drupal/src/next-drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
JsonApiUpdateResourceBody,
JsonApiWithAuthOption,
JsonApiWithCacheOptions,
JsonApiWithNextFetchOptions,
JsonDeserializer,
Locale,
NextDrupalOptions,
Expand Down Expand Up @@ -248,7 +249,9 @@ export class NextDrupal extends NextDrupalBase {
async getResource<T extends JsonApiResource>(
type: string,
uuid: string,
options?: JsonApiOptions & JsonApiWithCacheOptions
options?: JsonApiOptions &
JsonApiWithCacheOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand All @@ -257,6 +260,7 @@ export class NextDrupal extends NextDrupalBase {
params: {},
...options,
}
// Not sure about this node-cache

/* c8 ignore next 11 */
if (options.withCache) {
Expand All @@ -283,6 +287,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching resource: ")
Expand All @@ -301,7 +306,8 @@ export class NextDrupal extends NextDrupalBase {
path: string,
options?: {
isVersionable?: boolean
} & JsonApiOptions
} & JsonApiOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -370,6 +376,7 @@ export class NextDrupal extends NextDrupalBase {
redirect: "follow",
body: JSON.stringify(payload),
withAuth: options.withAuth,
next: options.next,
})

const errorMessagePrefix = "Error while fetching resource by path:"
Expand Down Expand Up @@ -408,7 +415,8 @@ export class NextDrupal extends NextDrupalBase {
type: string,
options?: {
deserialize?: boolean
} & JsonApiOptions
} & JsonApiOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand All @@ -427,6 +435,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(
Expand All @@ -445,6 +454,7 @@ export class NextDrupal extends NextDrupalBase {
pathPrefix?: PathPrefix
params?: JsonApiParams
} & JsonApiWithAuthOption &
JsonApiWithNextFetchOptions &
(
| {
locales: Locale[]
Expand Down Expand Up @@ -490,6 +500,7 @@ export class NextDrupal extends NextDrupalBase {
let opts: Parameters<NextDrupal["getResourceCollection"]>[1] = {
params,
withAuth: options.withAuth,
next: options.next,
}
if (locale) {
opts = {
Expand Down Expand Up @@ -547,7 +558,7 @@ export class NextDrupal extends NextDrupalBase {

async translatePath(
path: string,
options?: JsonApiWithAuthOption
options?: JsonApiWithAuthOption & JsonApiWithNextFetchOptions
): Promise<DrupalTranslatedPath | null> {
options = {
withAuth: this.withAuth,
Expand All @@ -562,6 +573,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

if (response.status === 404) {
Expand All @@ -575,7 +587,10 @@ export class NextDrupal extends NextDrupalBase {
return await response.json()
}

async getIndex(locale?: Locale): Promise<JsonApiResponse> {
async getIndex(
locale?: Locale,
options?: JsonApiWithNextFetchOptions
): Promise<JsonApiResponse> {
const endpoint = await this.buildEndpoint({
locale,
})
Expand All @@ -585,6 +600,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
withAuth: false,
next: options?.next,
})

await this.throwIfJsonErrors(
Expand Down Expand Up @@ -657,7 +673,9 @@ export class NextDrupal extends NextDrupalBase {

async getMenu<T = DrupalMenuItem>(
menuName: string,
options?: JsonApiOptions & JsonApiWithCacheOptions
options?: JsonApiOptions &
JsonApiWithCacheOptions &
JsonApiWithNextFetchOptions
): Promise<{
items: T[]
tree: T[]
Expand Down Expand Up @@ -692,6 +710,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching menu items: ")
Expand Down Expand Up @@ -719,7 +738,7 @@ export class NextDrupal extends NextDrupalBase {

async getView<T = JsonApiResource>(
name: string,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<DrupalView<T>> {
options = {
withAuth: this.withAuth,
Expand All @@ -741,6 +760,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching view: ")
Expand All @@ -759,7 +779,7 @@ export class NextDrupal extends NextDrupalBase {

async getSearchIndex<T = JsonApiResource[]>(
name: string,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand All @@ -778,6 +798,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(
Expand Down
5 changes: 4 additions & 1 deletion packages/next-drupal/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Locale = string

export type PathPrefix = string

export interface FetchOptions extends RequestInit {
export interface FetchOptions extends RequestInit, JsonApiWithNextFetchOptions {
withAuth?: boolean | NextDrupalAuth
}

Expand Down Expand Up @@ -34,6 +34,9 @@ export type JsonApiWithCacheOptions = {
cacheKey?: string
}

export type JsonApiWithNextFetchOptions = {
next?: NextFetchRequestConfig
}
// TODO: Properly type this.
/* eslint-disable @typescript-eslint/no-explicit-any */
export type JsonApiParams = Record<string, any>
Loading

0 comments on commit 1679890

Please sign in to comment.