UNPKG

3.8 kBTypeScriptView Raw
1import { AxiosHeaderValue, AxiosRequestHeaders, InternalAxiosRequestConfig } from 'axios';
2import type { AxiosInstance as OriginalAxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
3export type DefaultOptions = AxiosRequestConfig & {
4 logHandler: (level: string, data?: Error | string) => void;
5 responseLogger?: (response: AxiosResponse<any> | Error) => unknown;
6 requestLogger?: (request: AxiosRequestConfig | Error) => unknown;
7 retryOnError?: boolean;
8};
9export type AxiosInstance = OriginalAxiosInstance & {
10 httpClientParams: CreateHttpClientParams;
11 cloneWithNewParams: (params: Partial<CreateHttpClientParams>) => AxiosInstance;
12 defaults: DefaultOptions;
13};
14export type CreateHttpClientParams = {
15 /** Access Token or an async function that returns Access Token */
16 accessToken: string | (() => Promise<string>);
17 /** Space ID */
18 space?: string;
19 /**
20 * Requests will be made over http instead of the default https
21 * @default false
22 */
23 insecure?: boolean;
24 /**
25 * API host
26 */
27 host?: string;
28 /** HTTP agent for node */
29 httpAgent?: AxiosRequestConfig['httpAgent'];
30 /** HTTPS agent for node */
31 httpsAgent?: AxiosRequestConfig['httpsAgent'];
32 /** Axios adapter to handle requests */
33 adapter?: AxiosRequestConfig['adapter'];
34 /** Axios proxy config */
35 proxy?: AxiosRequestConfig['proxy'];
36 /** Gets called on every request triggered by the SDK, takes the axios request config as an argument */
37 requestLogger?: DefaultOptions['requestLogger'];
38 /** Gets called on every response, takes axios response object as an argument */
39 responseLogger?: DefaultOptions['responseLogger'];
40 /** Request interceptor */
41 onBeforeRequest?: (value: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
42 /** Error handler */
43 onError?: (error: any) => any;
44 /** A log handler function to process given log messages & errors. Receives the log level (error, warning & info) and the actual log data (Error object or string). (Default can be found here: https://github.com/contentful/contentful-sdk-core/blob/master/lib/create-http-client.js) */
45 logHandler?: DefaultOptions['logHandler'];
46 /** Optional additional headers */
47 headers?: AxiosRequestHeaders | Record<string, AxiosHeaderValue>;
48 defaultHostname?: string;
49 /**
50 * If we should retry on errors and 429 rate limit exceptions
51 * @default true
52 */
53 retryOnError?: boolean;
54 /**
55 * Optional number of retries before failure
56 * @default 5
57 */
58 retryLimit?: number;
59 /**
60 * Optional number of milliseconds before the request times out.
61 * @default 30000
62 */
63 timeout?: number;
64 basePath?: string;
65 baseURL?: string;
66 /**
67 * Optional maximum content length in bytes
68 * @default 1073741824 i.e 1GB
69 */
70 maxContentLength?: number;
71 /**
72 * Optional maximum body length in bytes
73 * @default 1073741824 i.e 1GB
74 */
75 maxBodyLength?: number;
76 /**
77 * Optional maximum number of requests per second (rate-limit)
78 * @desc should represent the max of your current plan's rate limit
79 * @default 0 = no throttling
80 * @param 1-30 (fixed number of limit), 'auto' (calculated limit based on current tier), '0%' - '100%' (calculated % limit based on tier)
81 */
82 throttle?: 'auto' | string | number;
83 /**
84 * Optional how often the current request has been retried
85 * @default 0
86 */
87 attempt?: number;
88};
89export type ContentfulErrorData = {
90 status?: number;
91 statusText?: string;
92 requestId?: string;
93 message: string;
94 details: Record<string, any>;
95 request?: Record<string, any>;
96 sys?: {
97 id?: string;
98 };
99};