Skip to content

Commit 6d5cad9

Browse files
Merge pull request #282 from auth0/feat/add-auth-params-for-fetcher
feat(core,react): add auth params to custom fetcher
2 parents 7609444 + b20af18 commit 6d5cad9

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

packages/core/src/api/api-utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @internal
55
*/
66

7-
import type { FetcherSupplier, SpaAuthConfig } from '../auth/auth-types';
7+
import type { FetcherAuthParams, FetcherSupplier, SpaAuthConfig } from '../auth/auth-types';
88

99
import { ContentType, HeaderName } from './http-constants';
1010

@@ -18,16 +18,22 @@ export const AUTH0_SCOPE_HEADER = HeaderName.Auth0Scope;
1818
* @internal
1919
*/
2020
export function createProxyFetcher(
21-
customFetcher?: (url: string, init?: RequestInit) => Promise<Response>,
21+
customFetcher?: (
22+
url: string,
23+
init?: RequestInit,
24+
authParams?: FetcherAuthParams,
25+
) => Promise<Response>,
2226
): FetcherSupplier {
23-
const fetchFn = customFetcher ?? fetch;
2427
return async (url, init, authParams) => {
2528
const headers = new Headers(init?.headers);
2629
headers.set(HeaderName.ContentType, ContentType.JSON);
2730
if (authParams?.scope?.length) {
2831
headers.set(HeaderName.Auth0Scope, authParams.scope.join(' '));
2932
}
30-
return fetchFn(url, { ...init, headers });
33+
if (customFetcher) {
34+
return customFetcher(url, { ...init, headers }, authParams);
35+
}
36+
return fetch(url, { ...init, headers });
3137
};
3238
}
3339

packages/core/src/auth/auth-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export interface AuthDetails {
133133
domain?: string | undefined;
134134
authProxyUrl?: string | undefined;
135135
contextInterface?: BasicAuth0ContextInterface | undefined;
136-
fetcher?: (url: string, init?: RequestInit) => Promise<Response>;
136+
fetcher?: (url: string, init?: RequestInit, authParams?: FetcherAuthParams) => Promise<Response>;
137137
previewMode?: boolean; // For docs - skip API client initialization
138138
}
139139

packages/core/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export * from './api';
1818

1919
export { createCoreClient } from './auth/core-client';
2020

21-
export { AuthDetails, CoreClientInterface, BasicAuth0ContextInterface } from './auth/auth-types';
21+
export {
22+
AuthDetails,
23+
CoreClientInterface,
24+
BasicAuth0ContextInterface,
25+
FetcherAuthParams,
26+
} from './auth/auth-types';
2227

2328
export * from './schemas';
2429

packages/react/src/types/auth-types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module auth-types
44
*/
55

6-
import type { AuthDetails } from '@auth0/universal-components-core';
6+
import type { AuthDetails, FetcherAuthParams } from '@auth0/universal-components-core';
77
import type * as React from 'react';
88

99
import type { QueryCacheConfig } from '@/types/cache-types';
@@ -23,7 +23,11 @@ export type Auth0ComponentProviderProps = (
2323
domain: string;
2424
proxyConfig: {
2525
baseUrl: string;
26-
fetcher?: (url: string, init?: RequestInit) => Promise<Response>;
26+
fetcher?: (
27+
url: string,
28+
init?: RequestInit,
29+
authParams?: FetcherAuthParams,
30+
) => Promise<Response>;
2731
};
2832
}
2933
) & {

0 commit comments

Comments
 (0)