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(next-drupal): next revalidate options #784

Merged
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
36 changes: 28 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 Down Expand Up @@ -283,6 +286,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 +305,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 +375,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 +414,8 @@ export class NextDrupal extends NextDrupalBase {
type: string,
options?: {
deserialize?: boolean
} & JsonApiOptions
} & JsonApiOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand All @@ -427,6 +434,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 +453,7 @@ export class NextDrupal extends NextDrupalBase {
pathPrefix?: PathPrefix
params?: JsonApiParams
} & JsonApiWithAuthOption &
JsonApiWithNextFetchOptions &
(
| {
locales: Locale[]
Expand Down Expand Up @@ -490,6 +499,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 +557,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 +572,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 +586,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 +599,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 +672,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 +709,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 +737,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 +759,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 +778,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 +797,7 @@ export class NextDrupal extends NextDrupalBase {

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

await this.throwIfJsonErrors(
Expand Down
3 changes: 3 additions & 0 deletions packages/next-drupal/src/types/options.ts
Original file line number Diff line number Diff line change
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
Loading