UNPKG

@samuraitruong/php-cookie-challenge

Version:

Axios wrapper with automatic cookie challenge detection and processing

65 lines (56 loc) 2.27 kB
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; /** * Axios response object with cookie challenge */ export interface CookieChallengeResponse extends AxiosResponse { data: string | any; } /** * Function to detect if a response contains a cookie challenge */ export type DetectCookieChallengeFn = (response: AxiosResponse) => boolean; /** * Function to process a cookie challenge */ export type ProcessCookieChallengeFn = ( response: AxiosResponse, client: AxiosInstance ) => Promise<AxiosResponse>; /** * Detects if a response contains a cookie challenge * @param response - Axios response object * @returns True if cookie challenge is detected */ export function detectCookieChallenge(response: AxiosResponse): boolean; /** * Processes a cookie challenge by making necessary requests and updating cookies * @param response - Axios response object containing the challenge * @param client - Axios client instance to make follow-up requests * @returns Promise that resolves to the retried response with cookies handled */ export function processCookieChallenge( response: AxiosResponse, client: AxiosInstance ): Promise<AxiosResponse>; /** * Creates a cookie challenge interceptor function that can be used with client.interceptors.use() * @param client - Axios client instance (will be used for sequential API calls and retry) * @param detectFn - Optional custom detection function (defaults to detectCookieChallenge) * @param processFn - Optional custom processing function (defaults to processCookieChallenge) * @returns Interceptor function for use with axios interceptors */ export function createCookieChallengeInterceptor( client: AxiosInstance, detectFn?: DetectCookieChallengeFn, processFn?: ProcessCookieChallengeFn ): (response: AxiosResponse) => Promise<AxiosResponse>; /** * Creates an axios client with automatic cookie challenge detection and processing * @param config - Axios configuration object * @param challengeHandler - Optional custom challenge handler function * @returns Configured axios instance with interceptors */ export function createAxiosClient( config?: AxiosRequestConfig, challengeHandler?: (response: AxiosResponse) => Promise<AxiosResponse> ): AxiosInstance;