@@ -228,6 +228,16 @@ export interface FiefParameters {
228
228
* @see [ID Token encryption](https://docs.fief.dev/going-further/id-token-encryption/)
229
229
*/
230
230
encryptionKey ?: string ;
231
+
232
+ /**
233
+ * Additional fetch init options for `getOpenIDConfiguration` and `getJWKS` requests.
234
+ *
235
+ * Mostly useful to control fetch cache.
236
+ *
237
+ * @see [fetch cache property](https://developer.mozilla.org/en-US/docs/Web/API/Request/cache)
238
+ * @see [Next.js fetch](https://nextjs.org/docs/app/api-reference/functions/fetch#fetchurl-options)
239
+ */
240
+ requestInit ?: RequestInit ;
231
241
}
232
242
233
243
/**
@@ -253,6 +263,8 @@ export class Fief {
253
263
254
264
private fetch : typeof fetch ;
255
265
266
+ private requestInit ?: RequestInit ;
267
+
256
268
private openIDConfiguration ?: Record < string , any > ;
257
269
258
270
private jwks ?: jose . JSONWebKeySet ;
@@ -274,6 +286,7 @@ export class Fief {
274
286
}
275
287
276
288
this . fetch = getFetch ( ) ;
289
+ this . requestInit = parameters . requestInit ;
277
290
278
291
this . crypto = getCrypto ( ) ;
279
292
}
@@ -348,6 +361,7 @@ export class Fief {
348
361
* @param redirectURI - The exact same `redirectURI` you passed to the authorization URL.
349
362
* @param codeVerifier - The raw [PKCE](https://docs.fief.dev/going-further/pkce/) code
350
363
* used to generate the code challenge during authorization.
364
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
351
365
*
352
366
* @returns A token response and user information.
353
367
*
@@ -360,6 +374,7 @@ export class Fief {
360
374
code : string ,
361
375
redirectURI : string ,
362
376
codeVerifier ?: string ,
377
+ requestInit ?: RequestInit ,
363
378
) : Promise < [ FiefTokenResponse , FiefUserInfo ] > {
364
379
const openIDConfiguration = await this . getOpenIDConfiguration ( ) ;
365
380
const payload = serializeQueryString ( {
@@ -374,9 +389,11 @@ export class Fief {
374
389
const response = await this . fetch (
375
390
openIDConfiguration . token_endpoint ,
376
391
{
392
+ ...requestInit || { } ,
377
393
method : 'POST' ,
378
394
body : payload ,
379
395
headers : {
396
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
380
397
'Content-Type' : 'application/x-www-form-urlencoded' ,
381
398
} ,
382
399
} ,
@@ -402,6 +419,7 @@ export class Fief {
402
419
* If not provided, the access token will share the same list of scopes
403
420
* as requested the first time.
404
421
* Otherwise, it should be a subset of the original list of scopes.
422
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
405
423
*
406
424
* @returns A token response and user information.
407
425
*
@@ -413,6 +431,7 @@ export class Fief {
413
431
public async authRefreshToken (
414
432
refreshToken : string ,
415
433
scope ?: string [ ] ,
434
+ requestInit ?: RequestInit ,
416
435
) : Promise < [ FiefTokenResponse , FiefUserInfo ] > {
417
436
const openIDConfiguration = await this . getOpenIDConfiguration ( ) ;
418
437
const payload = serializeQueryString ( {
@@ -425,9 +444,11 @@ export class Fief {
425
444
const response = await this . fetch (
426
445
openIDConfiguration . token_endpoint ,
427
446
{
447
+ ...requestInit || { } ,
428
448
method : 'POST' ,
429
449
body : payload ,
430
450
headers : {
451
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
431
452
'Content-Type' : 'application/x-www-form-urlencoded' ,
432
453
} ,
433
454
} ,
@@ -452,6 +473,7 @@ export class Fief {
452
473
* @param requiredScopes - Optional list of scopes to check for.
453
474
* @param requiredACR - Optional minimum ACR level required. Read more: https://docs.fief.dev/going-further/acr/
454
475
* @param requiredPermissions - Optional list of permissions to check for.
476
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
455
477
*
456
478
* @returns {@link FiefAccessTokenInfo }
457
479
* @throws {@link FiefAccessTokenInvalid } if the access token is invalid.
@@ -548,6 +570,7 @@ export class Fief {
548
570
* Return fresh {@link FiefUserInfo} from the Fief API using a valid access token.
549
571
*
550
572
* @param accessToken - A valid access token.
573
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
551
574
*
552
575
* @returns Fresh user information.
553
576
*
@@ -556,13 +579,15 @@ export class Fief {
556
579
* userinfo = await fief.userinfo('ACCESS_TOKEN');
557
580
* ```
558
581
*/
559
- public async userinfo ( accessToken : string ) : Promise < FiefUserInfo > {
582
+ public async userinfo ( accessToken : string , requestInit ?: RequestInit ) : Promise < FiefUserInfo > {
560
583
const openIDConfiguration = await this . getOpenIDConfiguration ( ) ;
561
584
const response = await this . fetch (
562
585
openIDConfiguration . userinfo_endpoint ,
563
586
{
587
+ ...requestInit || { } ,
564
588
method : 'GET' ,
565
589
headers : {
590
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
566
591
Authorization : `Bearer ${ accessToken } ` ,
567
592
} ,
568
593
} ,
@@ -577,6 +602,7 @@ export class Fief {
577
602
*
578
603
* @param accessToken - A valid access token.
579
604
* @param data - An object containing the data to update.
605
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
580
606
*
581
607
* @returns Updated user information.
582
608
*
@@ -590,14 +616,17 @@ export class Fief {
590
616
public async updateProfile (
591
617
accessToken : string ,
592
618
data : Record < string , any > ,
619
+ requestInit ?: RequestInit ,
593
620
) : Promise < FiefUserInfo > {
594
621
const updateProfileEndpoint = `${ this . baseURL } /api/profile` ;
595
622
const response = await this . fetch (
596
623
updateProfileEndpoint ,
597
624
{
625
+ ...requestInit || { } ,
598
626
method : 'PATCH' ,
599
627
body : JSON . stringify ( data ) ,
600
628
headers : {
629
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
601
630
'Content-Type' : 'application/json' ,
602
631
Authorization : `Bearer ${ accessToken } ` ,
603
632
} ,
@@ -615,6 +644,7 @@ export class Fief {
615
644
*
616
645
* @param accessToken - A valid access token.
617
646
* @param newPassword - The new password.
647
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
618
648
*
619
649
* @returns Updated user information.
620
650
*
@@ -626,14 +656,17 @@ export class Fief {
626
656
public async changePassword (
627
657
accessToken : string ,
628
658
newPassword : string ,
659
+ requestInit ?: RequestInit ,
629
660
) : Promise < FiefUserInfo > {
630
661
const updateProfileEndpoint = `${ this . baseURL } /api/password` ;
631
662
const response = await this . fetch (
632
663
updateProfileEndpoint ,
633
664
{
665
+ ...requestInit || { } ,
634
666
method : 'PATCH' ,
635
667
body : JSON . stringify ( { password : newPassword } ) ,
636
668
headers : {
669
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
637
670
'Content-Type' : 'application/json' ,
638
671
Authorization : `Bearer ${ accessToken } ` ,
639
672
} ,
@@ -654,6 +687,7 @@ export class Fief {
654
687
*
655
688
* @param accessToken - A valid access token.
656
689
* @param newPassword - The new email address.
690
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
657
691
*
658
692
* @returns Updated user information.
659
693
*
@@ -665,14 +699,17 @@ export class Fief {
665
699
public async emailChange (
666
700
accessToken : string ,
667
701
email : string ,
702
+ requestInit ?: RequestInit ,
668
703
) : Promise < FiefUserInfo > {
669
704
const updateProfileEndpoint = `${ this . baseURL } /api/email/change` ;
670
705
const response = await this . fetch (
671
706
updateProfileEndpoint ,
672
707
{
708
+ ...requestInit || { } ,
673
709
method : 'PATCH' ,
674
710
body : JSON . stringify ( { email } ) ,
675
711
headers : {
712
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
676
713
'Content-Type' : 'application/json' ,
677
714
Authorization : `Bearer ${ accessToken } ` ,
678
715
} ,
@@ -691,6 +728,7 @@ export class Fief {
691
728
*
692
729
* @param accessToken - A valid access token.
693
730
* @param newPassword - The new email address.
731
+ * @param requestInit - Additional fetch init options. Mostly useful to control fetch cache.
694
732
*
695
733
* @returns Updated user information.
696
734
*
@@ -702,14 +740,17 @@ export class Fief {
702
740
public async emailVerify (
703
741
accessToken : string ,
704
742
code : string ,
743
+ requestInit ?: RequestInit ,
705
744
) : Promise < FiefUserInfo > {
706
745
const updateProfileEndpoint = `${ this . baseURL } /api/email/verify` ;
707
746
const response = await this . fetch (
708
747
updateProfileEndpoint ,
709
748
{
749
+ ...requestInit || { } ,
710
750
method : 'POST' ,
711
751
body : JSON . stringify ( { code } ) ,
712
752
headers : {
753
+ ...requestInit && requestInit . headers ? requestInit . headers : { } ,
713
754
'Content-Type' : 'application/json' ,
714
755
Authorization : `Bearer ${ accessToken } ` ,
715
756
} ,
@@ -752,6 +793,7 @@ export class Fief {
752
793
const response = await this . fetch (
753
794
`${ this . baseURL } /.well-known/openid-configuration` ,
754
795
{
796
+ ...this . requestInit || { } ,
755
797
method : 'GET' ,
756
798
} ,
757
799
) ;
@@ -771,6 +813,7 @@ export class Fief {
771
813
const response = await this . fetch (
772
814
openIDConfiguration . jwks_uri ,
773
815
{
816
+ ...this . requestInit || { } ,
774
817
method : 'GET' ,
775
818
} ,
776
819
) ;
0 commit comments