Skip to content

Commit e64dc5b

Browse files
MontiMarco92Marco Monti
andauthored
feat(next-drupal): next revalidate options (#784)
Co-authored-by: Marco Monti <marco@easytech.com.ar>
1 parent d851246 commit e64dc5b

File tree

4 files changed

+391
-9
lines changed

4 files changed

+391
-9
lines changed

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
JsonApiUpdateResourceBody,
2020
JsonApiWithAuthOption,
2121
JsonApiWithCacheOptions,
22+
JsonApiWithNextFetchOptions,
2223
JsonDeserializer,
2324
Locale,
2425
NextDrupalOptions,
@@ -248,7 +249,9 @@ export class NextDrupal extends NextDrupalBase {
248249
async getResource<T extends JsonApiResource>(
249250
type: string,
250251
uuid: string,
251-
options?: JsonApiOptions & JsonApiWithCacheOptions
252+
options?: JsonApiOptions &
253+
JsonApiWithCacheOptions &
254+
JsonApiWithNextFetchOptions
252255
): Promise<T> {
253256
options = {
254257
deserialize: true,
@@ -283,6 +286,7 @@ export class NextDrupal extends NextDrupalBase {
283286

284287
const response = await this.fetch(endpoint, {
285288
withAuth: options.withAuth,
289+
next: options.next,
286290
})
287291

288292
await this.throwIfJsonErrors(response, "Error while fetching resource: ")
@@ -301,7 +305,8 @@ export class NextDrupal extends NextDrupalBase {
301305
path: string,
302306
options?: {
303307
isVersionable?: boolean
304-
} & JsonApiOptions
308+
} & JsonApiOptions &
309+
JsonApiWithNextFetchOptions
305310
): Promise<T> {
306311
options = {
307312
deserialize: true,
@@ -370,6 +375,7 @@ export class NextDrupal extends NextDrupalBase {
370375
redirect: "follow",
371376
body: JSON.stringify(payload),
372377
withAuth: options.withAuth,
378+
next: options.next,
373379
})
374380

375381
const errorMessagePrefix = "Error while fetching resource by path:"
@@ -408,7 +414,8 @@ export class NextDrupal extends NextDrupalBase {
408414
type: string,
409415
options?: {
410416
deserialize?: boolean
411-
} & JsonApiOptions
417+
} & JsonApiOptions &
418+
JsonApiWithNextFetchOptions
412419
): Promise<T> {
413420
options = {
414421
withAuth: this.withAuth,
@@ -427,6 +434,7 @@ export class NextDrupal extends NextDrupalBase {
427434

428435
const response = await this.fetch(endpoint, {
429436
withAuth: options.withAuth,
437+
next: options.next,
430438
})
431439

432440
await this.throwIfJsonErrors(
@@ -445,6 +453,7 @@ export class NextDrupal extends NextDrupalBase {
445453
pathPrefix?: PathPrefix
446454
params?: JsonApiParams
447455
} & JsonApiWithAuthOption &
456+
JsonApiWithNextFetchOptions &
448457
(
449458
| {
450459
locales: Locale[]
@@ -490,6 +499,7 @@ export class NextDrupal extends NextDrupalBase {
490499
let opts: Parameters<NextDrupal["getResourceCollection"]>[1] = {
491500
params,
492501
withAuth: options.withAuth,
502+
next: options.next,
493503
}
494504
if (locale) {
495505
opts = {
@@ -547,7 +557,7 @@ export class NextDrupal extends NextDrupalBase {
547557

548558
async translatePath(
549559
path: string,
550-
options?: JsonApiWithAuthOption
560+
options?: JsonApiWithAuthOption & JsonApiWithNextFetchOptions
551561
): Promise<DrupalTranslatedPath | null> {
552562
options = {
553563
withAuth: this.withAuth,
@@ -562,6 +572,7 @@ export class NextDrupal extends NextDrupalBase {
562572

563573
const response = await this.fetch(endpoint, {
564574
withAuth: options.withAuth,
575+
next: options.next,
565576
})
566577

567578
if (response.status === 404) {
@@ -575,7 +586,10 @@ export class NextDrupal extends NextDrupalBase {
575586
return await response.json()
576587
}
577588

578-
async getIndex(locale?: Locale): Promise<JsonApiResponse> {
589+
async getIndex(
590+
locale?: Locale,
591+
options?: JsonApiWithNextFetchOptions
592+
): Promise<JsonApiResponse> {
579593
const endpoint = await this.buildEndpoint({
580594
locale,
581595
})
@@ -585,6 +599,7 @@ export class NextDrupal extends NextDrupalBase {
585599
const response = await this.fetch(endpoint, {
586600
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
587601
withAuth: false,
602+
next: options?.next,
588603
})
589604

590605
await this.throwIfJsonErrors(
@@ -657,7 +672,9 @@ export class NextDrupal extends NextDrupalBase {
657672

658673
async getMenu<T = DrupalMenuItem>(
659674
menuName: string,
660-
options?: JsonApiOptions & JsonApiWithCacheOptions
675+
options?: JsonApiOptions &
676+
JsonApiWithCacheOptions &
677+
JsonApiWithNextFetchOptions
661678
): Promise<{
662679
items: T[]
663680
tree: T[]
@@ -692,6 +709,7 @@ export class NextDrupal extends NextDrupalBase {
692709

693710
const response = await this.fetch(endpoint, {
694711
withAuth: options.withAuth,
712+
next: options.next,
695713
})
696714

697715
await this.throwIfJsonErrors(response, "Error while fetching menu items: ")
@@ -719,7 +737,7 @@ export class NextDrupal extends NextDrupalBase {
719737

720738
async getView<T = JsonApiResource>(
721739
name: string,
722-
options?: JsonApiOptions
740+
options?: JsonApiOptions & JsonApiWithNextFetchOptions
723741
): Promise<DrupalView<T>> {
724742
options = {
725743
withAuth: this.withAuth,
@@ -741,6 +759,7 @@ export class NextDrupal extends NextDrupalBase {
741759

742760
const response = await this.fetch(endpoint, {
743761
withAuth: options.withAuth,
762+
next: options.next,
744763
})
745764

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

760779
async getSearchIndex<T = JsonApiResource[]>(
761780
name: string,
762-
options?: JsonApiOptions
781+
options?: JsonApiOptions & JsonApiWithNextFetchOptions
763782
): Promise<T> {
764783
options = {
765784
withAuth: this.withAuth,
@@ -778,6 +797,7 @@ export class NextDrupal extends NextDrupalBase {
778797

779798
const response = await this.fetch(endpoint, {
780799
withAuth: options.withAuth,
800+
next: options.next,
781801
})
782802

783803
await this.throwIfJsonErrors(

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export type JsonApiWithCacheOptions = {
3434
cacheKey?: string
3535
}
3636

37+
export type JsonApiWithNextFetchOptions = {
38+
next?: NextFetchRequestConfig
39+
}
3740
// TODO: Properly type this.
3841
/* eslint-disable @typescript-eslint/no-explicit-any */
3942
export type JsonApiParams = Record<string, any>

0 commit comments

Comments
 (0)