contentful-sdk-core
Advanced tools
Comparing version 9.0.1 to 9.1.0
import copy from 'fast-copy'; | ||
import qs from 'qs'; | ||
import asyncToken from './async-token.js'; | ||
import rateLimitRetry from './rate-limit.js'; | ||
import rateLimitThrottle from './rate-limit-throttle.js'; | ||
// Matches 'sub.host:port' or 'host:port' and extracts hostname and port | ||
// Also enforces toplevel domain specified, no spaces and no protocol | ||
const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.?[^\s:]+)(?::(\d+))?(?!:)$/; | ||
import createDefaultOptions from './create-default-options.js'; | ||
function copyHttpClientParams(options) { | ||
@@ -24,80 +21,3 @@ const copiedOptions = copy(options); | ||
export default function createHttpClient(axios, options) { | ||
const defaultConfig = { | ||
insecure: false, | ||
retryOnError: true, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
logHandler: (level, data) => { | ||
if (level === 'error' && data) { | ||
const title = [data.name, data.message].filter((a) => a).join(' - '); | ||
console.error(`[error] ${title}`); | ||
console.error(data); | ||
return; | ||
} | ||
console.log(`[${level}] ${data}`); | ||
}, | ||
// Passed to axios | ||
headers: {}, | ||
httpAgent: false, | ||
httpsAgent: false, | ||
timeout: 30000, | ||
throttle: 0, | ||
basePath: '', | ||
adapter: undefined, | ||
maxContentLength: 1073741824, // 1GB | ||
maxBodyLength: 1073741824, // 1GB | ||
}; | ||
const config = { | ||
...defaultConfig, | ||
...options, | ||
}; | ||
if (!config.accessToken) { | ||
const missingAccessTokenError = new TypeError('Expected parameter accessToken'); | ||
config.logHandler('error', missingAccessTokenError); | ||
throw missingAccessTokenError; | ||
} | ||
// Construct axios baseURL option | ||
const protocol = config.insecure ? 'http' : 'https'; | ||
const space = config.space ? `${config.space}/` : ''; | ||
let hostname = config.defaultHostname; | ||
let port = config.insecure ? 80 : 443; | ||
if (config.host && HOST_REGEX.test(config.host)) { | ||
const parsed = config.host.split(':'); | ||
if (parsed.length === 2) { | ||
; | ||
[hostname, port] = parsed; | ||
} | ||
else { | ||
hostname = parsed[0]; | ||
} | ||
} | ||
// Ensure that basePath does start but not end with a slash | ||
if (config.basePath) { | ||
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`; | ||
} | ||
const baseURL = options.baseURL || `${protocol}://${hostname}:${port}${config.basePath}/spaces/${space}`; | ||
if (!config.headers.Authorization && typeof config.accessToken !== 'function') { | ||
config.headers.Authorization = 'Bearer ' + config.accessToken; | ||
} | ||
const axiosOptions = { | ||
// Axios | ||
baseURL, | ||
headers: config.headers, | ||
httpAgent: config.httpAgent, | ||
httpsAgent: config.httpsAgent, | ||
proxy: config.proxy, | ||
timeout: config.timeout, | ||
adapter: config.adapter, | ||
maxContentLength: config.maxContentLength, | ||
maxBodyLength: config.maxBodyLength, | ||
paramsSerializer: { | ||
serialize: (params) => { | ||
return qs.stringify(params); | ||
}, | ||
}, | ||
// Contentful | ||
logHandler: config.logHandler, | ||
responseLogger: config.responseLogger, | ||
requestLogger: config.requestLogger, | ||
retryOnError: config.retryOnError, | ||
}; | ||
const axiosOptions = createDefaultOptions(options); | ||
const instance = axios.create(axiosOptions); | ||
@@ -125,16 +45,16 @@ instance.httpClientParams = options; | ||
*/ | ||
if (config.onBeforeRequest) { | ||
instance.interceptors.request.use(config.onBeforeRequest); | ||
if (options.onBeforeRequest) { | ||
instance.interceptors.request.use(options.onBeforeRequest); | ||
} | ||
if (typeof config.accessToken === 'function') { | ||
asyncToken(instance, config.accessToken); | ||
if (typeof options.accessToken === 'function') { | ||
asyncToken(instance, options.accessToken); | ||
} | ||
if (config.throttle) { | ||
rateLimitThrottle(instance, config.throttle); | ||
if (options.throttle) { | ||
rateLimitThrottle(instance, options.throttle); | ||
} | ||
rateLimitRetry(instance, config.retryLimit); | ||
if (config.onError) { | ||
instance.interceptors.response.use((response) => response, config.onError); | ||
rateLimitRetry(instance, options.retryLimit); | ||
if (options.onError) { | ||
instance.interceptors.response.use((response) => response, options.onError); | ||
} | ||
return instance; | ||
} |
@@ -8,1 +8,2 @@ export { default as createHttpClient } from './create-http-client.js'; | ||
export { default as errorHandler } from './error-handler.js'; | ||
export { default as createDefaultOptions } from './create-default-options.js'; |
@@ -8,2 +8,3 @@ export { default as createHttpClient } from './create-http-client.js'; | ||
export { default as errorHandler } from './error-handler.js'; | ||
export type { AxiosInstance, CreateHttpClientParams } from './types.js'; | ||
export { default as createDefaultOptions } from './create-default-options.js'; | ||
export type { AxiosInstance, CreateHttpClientParams, DefaultOptions } from './types.js'; |
{ | ||
"name": "contentful-sdk-core", | ||
"version": "9.0.1", | ||
"version": "9.1.0", | ||
"description": "Core modules for the Contentful JS SDKs", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.contentful.com/developers/docs/javascript/", |
33333
31
702