55 lines
2.8 KiB
TypeScript
55 lines
2.8 KiB
TypeScript
import { GetTokenSilentlyVerboseResponse } from './global';
|
|
export type ResponseHeaders = Record<string, string | null | undefined> | [string, string][] | {
|
|
get(name: string): string | null | undefined;
|
|
};
|
|
export type CustomFetchMinimalOutput = {
|
|
status: number;
|
|
headers: ResponseHeaders;
|
|
};
|
|
export type CustomFetchImpl<TOutput extends CustomFetchMinimalOutput> = (req: Request) => Promise<TOutput>;
|
|
export type AuthParams = {
|
|
scope?: string[];
|
|
audience?: string;
|
|
};
|
|
type AccessTokenFactory = (authParams?: AuthParams) => Promise<string | GetTokenSilentlyVerboseResponse>;
|
|
export type FetcherConfig<TOutput extends CustomFetchMinimalOutput> = {
|
|
getAccessToken?: AccessTokenFactory;
|
|
baseUrl?: string;
|
|
fetch?: CustomFetchImpl<TOutput>;
|
|
dpopNonceId?: string;
|
|
};
|
|
export type FetcherHooks = {
|
|
isDpopEnabled: () => boolean;
|
|
getAccessToken: AccessTokenFactory;
|
|
getDpopNonce: () => Promise<string | undefined>;
|
|
setDpopNonce: (nonce: string) => Promise<void>;
|
|
generateDpopProof: (params: {
|
|
url: string;
|
|
method: string;
|
|
nonce?: string;
|
|
accessToken: string;
|
|
}) => Promise<string>;
|
|
};
|
|
export type FetchWithAuthCallbacks<TOutput> = {
|
|
onUseDpopNonceError?(): Promise<TOutput>;
|
|
};
|
|
export declare class Fetcher<TOutput extends CustomFetchMinimalOutput> {
|
|
protected readonly config: Omit<FetcherConfig<TOutput>, 'fetch'> & Required<Pick<FetcherConfig<TOutput>, 'fetch'>>;
|
|
protected readonly hooks: FetcherHooks;
|
|
constructor(config: FetcherConfig<TOutput>, hooks: FetcherHooks);
|
|
protected isAbsoluteUrl(url: string): boolean;
|
|
protected buildUrl(baseUrl: string | undefined, url: string | undefined): string;
|
|
protected getAccessToken(authParams?: AuthParams): Promise<string | GetTokenSilentlyVerboseResponse>;
|
|
protected extractUrl(info: RequestInfo | URL): string;
|
|
protected buildBaseRequest(info: RequestInfo | URL, init: RequestInit | undefined): Request;
|
|
protected setAuthorizationHeader(request: Request, accessToken: string, tokenType?: string): void;
|
|
protected setDpopProofHeader(request: Request, accessToken: string): Promise<void>;
|
|
protected prepareRequest(request: Request, authParams?: AuthParams): Promise<void>;
|
|
protected getHeader(headers: ResponseHeaders, name: string): string;
|
|
protected hasUseDpopNonceError(response: TOutput): boolean;
|
|
protected handleResponse(response: TOutput, callbacks: FetchWithAuthCallbacks<TOutput>): Promise<TOutput>;
|
|
protected internalFetchWithAuth(info: RequestInfo | URL, init: RequestInit | undefined, callbacks: FetchWithAuthCallbacks<TOutput>, authParams?: AuthParams): Promise<TOutput>;
|
|
fetchWithAuth(info: RequestInfo | URL, init?: RequestInit, authParams?: AuthParams): Promise<TOutput>;
|
|
}
|
|
export {};
|