1
- import { useOidc , useOidcFetch } from "@axa-fr/react-oidc" ;
1
+ import {
2
+ useOidc ,
3
+ useOidcAccessToken ,
4
+ useOidcIdToken ,
5
+ } from "@axa-fr/react-oidc" ;
2
6
import loadConfig from "@utils/config" ;
3
7
import { usePathname } from "next/navigation" ;
4
8
import useSWR from "swr" ;
@@ -32,8 +36,32 @@ async function apiRequest<T>(
32
36
return ( await res . json ( ) ) as T ;
33
37
}
34
38
39
+ export function useNetBirdFetch ( ) {
40
+ const tokenSource = config . tokenSource || "accessToken" ;
41
+ const { idToken } = useOidcIdToken ( ) ;
42
+ const { accessToken } = useOidcAccessToken ( ) ;
43
+
44
+ const nativeFetch = async ( input : RequestInfo , init ?: RequestInit ) => {
45
+ const token =
46
+ tokenSource . toLowerCase ( ) == "idtoken" ? idToken : accessToken ;
47
+ const headers = {
48
+ "Content-Type" : "application/json" ,
49
+ Accept : "application/json" ,
50
+ Authorization : `Bearer ${ token } ` ,
51
+ } ;
52
+ return fetch ( input , {
53
+ ...init ,
54
+ headers,
55
+ } ) ;
56
+ } ;
57
+
58
+ return {
59
+ fetch : nativeFetch ,
60
+ } ;
61
+ }
62
+
35
63
export default function useFetchApi < T > ( url : string ) {
36
- const { fetch } = useOidcFetch ( ) ;
64
+ const { fetch } = useNetBirdFetch ( ) ;
37
65
const handleErrors = useApiErrorHandling ( ) ;
38
66
39
67
const { data, error, isLoading, isValidating, mutate } = useSWR (
@@ -58,7 +86,7 @@ export default function useFetchApi<T>(url: string) {
58
86
}
59
87
60
88
export function useApiCall < T > ( url : string ) {
61
- const { fetch } = useOidcFetch ( ) ;
89
+ const { fetch } = useNetBirdFetch ( ) ;
62
90
const handleErrors = useApiErrorHandling ( ) ;
63
91
64
92
return {
0 commit comments