Skip to content

Commit 0591e4f

Browse files
authored
feat(sdk): add support for custom headers in React SDK (#1318)
1 parent a54fded commit 0591e4f

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

sdks/js/packages/core/react/contexts/FrontierProvider.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,24 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
77
import { TransportProvider } from '@connectrpc/connect-query';
88
import { createConnectTransport } from '@connectrpc/connect-web';
99
import { useMemo } from 'react';
10+
import { createFetchWithCreds } from '../utils/fetch';
1011

1112
export const multipleFrontierProvidersError =
1213
"Frontier: You've added multiple <FrontierProvider> components in your React component tree. Wrap your components in a single <FrontierProvider>.";
1314

14-
const fetchWithCreds: typeof fetch = (input, init) => {
15-
return fetch(input, {
16-
...init,
17-
credentials: 'include'
18-
});
19-
};
20-
2115
export const queryClient = new QueryClient();
2216

2317
export const FrontierProvider = (props: FrontierProviderProps) => {
24-
const { children, initialState, config, theme, ...options } = props;
18+
const { children, initialState, config, theme, customHeaders, ...options } =
19+
props;
2520

2621
const transport = useMemo(
2722
() =>
2823
createConnectTransport({
2924
baseUrl: config.connectEndpoint || '/frontier-connect',
30-
fetch: fetchWithCreds
25+
fetch: createFetchWithCreds(customHeaders)
3126
}),
32-
[config.connectEndpoint]
27+
[config.connectEndpoint, customHeaders]
3328
);
3429

3530
return (
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CustomHeaderValue } from '../../shared/types';
2+
3+
export const createFetchWithCreds = (
4+
customHeaders?: Record<string, CustomHeaderValue>
5+
): typeof fetch => {
6+
return (input, init) => {
7+
const headers = new Headers(init?.headers);
8+
if (customHeaders) {
9+
for (const [key, value] of Object.entries(customHeaders)) {
10+
const headerValue = typeof value === 'function' ? value() : value;
11+
headers.set(key, headerValue);
12+
}
13+
}
14+
return fetch(input, {
15+
...init,
16+
credentials: 'include',
17+
headers
18+
});
19+
};
20+
};

sdks/js/packages/core/shared/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ export interface InitialState {
5252
sessionId?: string | null;
5353
}
5454

55+
export type CustomHeaderValue = string | (() => string);
56+
5557
export interface FrontierProviderProps {
5658
config: FrontierClientOptions;
5759
children: React.ReactNode;
5860
initialState?: InitialState;
61+
customHeaders?: Record<string, CustomHeaderValue>;
5962
theme?: ThemeProviderProps;
6063
}

sdks/js/packages/sdk-demo/src/App.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ import AuthContextProvider from '@/contexts/auth/provider';
33
import { customFetch } from '@/utils/custom-fetch';
44
import { FrontierProvider } from '@raystack/frontier/react';
55
import Router from './Router';
6+
import { v4 as uuid } from 'uuid';
7+
8+
const customHeaders = {
9+
'X-Request-ID': () => `sdk-demo:${uuid()}`
10+
};
611

712
function App() {
813
return (
9-
<FrontierProvider config={config} customFetch={customFetch}>
14+
<FrontierProvider
15+
config={config}
16+
customFetch={customFetch}
17+
customHeaders={customHeaders}
18+
>
1019
<AuthContextProvider>
1120
<Router />
1221
</AuthContextProvider>

0 commit comments

Comments
 (0)