-
Notifications
You must be signed in to change notification settings - Fork 149
/
FetchToken.tsx
36 lines (30 loc) · 1.58 KB
/
FetchToken.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Fetch, OidcClient } from '@axa-fr/oidc-client';
import { useCallback } from 'react';
export interface ComponentWithOidcFetchProps {
fetch?: Fetch;
}
const defaultConfigurationName = 'default';
const fetchWithToken = (fetch: Fetch, getOidcWithConfigurationName: () => OidcClient | null, demonstrating_proof_of_possession=false) => async (...params: Parameters<Fetch>) => {
const oidc = getOidcWithConfigurationName();
const newFetch = oidc.fetchWithTokens(fetch, demonstrating_proof_of_possession);
return await newFetch(...params);
};
export const withOidcFetch = (fetch: Fetch = null, configurationName = defaultConfigurationName, demonstrating_proof_of_possession:boolean=false) => (
WrappedComponent,
) => (props: ComponentWithOidcFetchProps) => {
const { fetch: newFetch } = useOidcFetch(fetch || props.fetch, configurationName, demonstrating_proof_of_possession);
return <WrappedComponent {...props} fetch={newFetch} />;
};
export const useOidcFetch = (fetch: Fetch = null, configurationName = defaultConfigurationName, demonstrating_proof_of_possession:boolean=false) => {
const previousFetch = fetch || window.fetch;
const getOidc = OidcClient.get;
const memoizedFetchCallback = useCallback(
(input: RequestInfo | URL, init?: RequestInit) => {
const getOidcWithConfigurationName = () => getOidc(configurationName);
const newFetch = fetchWithToken(previousFetch, getOidcWithConfigurationName, demonstrating_proof_of_possession);
return newFetch(input, init);
},
[previousFetch, configurationName],
);
return { fetch: memoizedFetchCallback };
};