UNPKG

contentful-sdk-core

Version:
1 lines 3.89 kB
{"version":3,"file":"create-http-client.js","sources":["../../src/create-http-client.ts"],"sourcesContent":["import type { AxiosStatic } from 'axios'\nimport copy from 'fast-copy'\n\nimport asyncToken from './async-token.js'\nimport rateLimitRetry from './rate-limit.js'\nimport rateLimitThrottle from './rate-limit-throttle.js'\nimport type { AxiosInstance, CreateHttpClientParams } from './types.js'\nimport createDefaultOptions from './create-default-options.js'\n\nfunction copyHttpClientParams(options: CreateHttpClientParams): CreateHttpClientParams {\n const copiedOptions = copy(options)\n // httpAgent and httpsAgent cannot be copied because they can contain private fields\n copiedOptions.httpAgent = options.httpAgent\n copiedOptions.httpsAgent = options.httpsAgent\n return copiedOptions\n}\n\n/**\n * Create pre-configured axios instance\n * @private\n * @param {AxiosStatic} axios - Axios library\n * @param {CreateHttpClientParams} options - Initialization parameters for the HTTP client\n * @return {AxiosInstance} Initialized axios instance\n */\nexport default function createHttpClient(\n axios: AxiosStatic,\n options: CreateHttpClientParams,\n): AxiosInstance {\n const axiosOptions = createDefaultOptions(options)\n\n const instance = axios.create(axiosOptions) as AxiosInstance\n instance.httpClientParams = options\n\n /**\n * Creates a new axios instance with the same default base parameters as the\n * current one, and with any overrides passed to the newParams object\n * This is useful as the SDKs use dependency injection to get the axios library\n * and the version of the library comes from different places depending\n * on whether it's a browser build or a node.js build.\n * @private\n * @param {CreateHttpClientParams} newParams - Initialization parameters for the HTTP client\n * @return {AxiosInstance} Initialized axios instance\n */\n instance.cloneWithNewParams = function (\n newParams: Partial<CreateHttpClientParams>,\n ): AxiosInstance {\n return createHttpClient(axios, {\n ...copyHttpClientParams(options),\n ...newParams,\n })\n }\n\n /**\n * Apply interceptors.\n * Please note that the order of interceptors is important\n */\n\n if (options.onBeforeRequest) {\n instance.interceptors.request.use(options.onBeforeRequest)\n }\n\n if (typeof options.accessToken === 'function') {\n asyncToken(instance, options.accessToken)\n }\n\n if (options.throttle) {\n rateLimitThrottle(instance, options.throttle)\n }\n rateLimitRetry(instance, options.retryLimit)\n\n if (options.onError) {\n instance.interceptors.response.use((response) => response, options.onError)\n }\n\n return instance\n}\n"],"names":["rateLimitRetry"],"mappings":";;;;;;AASA,SAAS,oBAAoB,CAAC,OAA+B,EAAA;AAC3D,IAAA,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC;;AAEnC,IAAA,aAAa,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS;AAC3C,IAAA,aAAa,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU;AAC7C,IAAA,OAAO,aAAa;AACtB;AAEA;;;;;;AAMG;AACW,SAAU,gBAAgB,CACtC,KAAkB,EAClB,OAA+B,EAAA;AAE/B,IAAA,MAAM,YAAY,GAAG,oBAAoB,CAAC,OAAO,CAAC;IAElD,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,YAAY,CAAkB;AAC5D,IAAA,QAAQ,CAAC,gBAAgB,GAAG,OAAO;AAEnC;;;;;;;;;AASG;AACH,IAAA,QAAQ,CAAC,kBAAkB,GAAG,UAC5B,SAA0C,EAAA;QAE1C,OAAO,gBAAgB,CAAC,KAAK,EAAE;YAC7B,GAAG,oBAAoB,CAAC,OAAO,CAAC;AAChC,YAAA,GAAG,SAAS;AACb,SAAA,CAAC;AACJ,IAAA,CAAC;AAED;;;AAGG;AAEH,IAAA,IAAI,OAAO,CAAC,eAAe,EAAE;QAC3B,QAAQ,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC;IAC5D;AAEA,IAAA,IAAI,OAAO,OAAO,CAAC,WAAW,KAAK,UAAU,EAAE;AAC7C,QAAA,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,WAAW,CAAC;IAC3C;AAEA,IAAA,IAAI,OAAO,CAAC,QAAQ,EAAE;AACpB,QAAA,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC;IAC/C;AACA,IAAAA,SAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,UAAU,CAAC;AAE5C,IAAA,IAAI,OAAO,CAAC,OAAO,EAAE;AACnB,QAAA,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,QAAQ,KAAK,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC;IAC7E;AAEA,IAAA,OAAO,QAAQ;AACjB;;;;"}