1- import { AxiosRequestHeaders } from 'axios'
21import type { AxiosStatic } from 'axios'
32import copy from 'fast-copy'
4- import qs from 'qs'
53
64import asyncToken from './async-token.js'
75import rateLimitRetry from './rate-limit.js'
86import rateLimitThrottle from './rate-limit-throttle.js'
9- import type { AxiosInstance , CreateHttpClientParams , DefaultOptions } from './types.js'
10-
11- // Matches 'sub.host:port' or 'host:port' and extracts hostname and port
12- // Also enforces toplevel domain specified, no spaces and no protocol
13- const HOST_REGEX = / ^ (? ! \w + : \/ \/ ) ( [ ^ \s : ] + \. ? [ ^ \s : ] + ) (?: : ( \d + ) ) ? (? ! : ) $ /
7+ import type { AxiosInstance , CreateHttpClientParams } from './types.js'
8+ import createDefaultOptions from './create-default-options.js'
149
1510function copyHttpClientParams ( options : CreateHttpClientParams ) : CreateHttpClientParams {
1611 const copiedOptions = copy ( options )
@@ -31,89 +26,7 @@ export default function createHttpClient(
3126 axios : AxiosStatic ,
3227 options : CreateHttpClientParams ,
3328) : AxiosInstance {
34- const defaultConfig = {
35- insecure : false as const ,
36- retryOnError : true as const ,
37- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38- logHandler : ( level : string , data : any ) : void => {
39- if ( level === 'error' && data ) {
40- const title = [ data . name , data . message ] . filter ( ( a ) => a ) . join ( ' - ' )
41- console . error ( `[error] ${ title } ` )
42- console . error ( data )
43- return
44- }
45- console . log ( `[${ level } ] ${ data } ` )
46- } ,
47- // Passed to axios
48- headers : { } as AxiosRequestHeaders ,
49- httpAgent : false as const ,
50- httpsAgent : false as const ,
51- timeout : 30000 ,
52- throttle : 0 ,
53- basePath : '' ,
54- adapter : undefined ,
55- maxContentLength : 1073741824 , // 1GB
56- maxBodyLength : 1073741824 , // 1GB
57- }
58- const config = {
59- ...defaultConfig ,
60- ...options ,
61- }
62-
63- if ( ! config . accessToken ) {
64- const missingAccessTokenError = new TypeError ( 'Expected parameter accessToken' )
65- config . logHandler ( 'error' , missingAccessTokenError )
66- throw missingAccessTokenError
67- }
68-
69- // Construct axios baseURL option
70- const protocol = config . insecure ? 'http' : 'https'
71- const space = config . space ? `${ config . space } /` : ''
72- let hostname = config . defaultHostname
73- let port : number | string = config . insecure ? 80 : 443
74- if ( config . host && HOST_REGEX . test ( config . host ) ) {
75- const parsed = config . host . split ( ':' )
76- if ( parsed . length === 2 ) {
77- ; [ hostname , port ] = parsed
78- } else {
79- hostname = parsed [ 0 ]
80- }
81- }
82-
83- // Ensure that basePath does start but not end with a slash
84- if ( config . basePath ) {
85- config . basePath = `/${ config . basePath . split ( '/' ) . filter ( Boolean ) . join ( '/' ) } `
86- }
87-
88- const baseURL =
89- options . baseURL || `${ protocol } ://${ hostname } :${ port } ${ config . basePath } /spaces/${ space } `
90-
91- if ( ! config . headers . Authorization && typeof config . accessToken !== 'function' ) {
92- config . headers . Authorization = 'Bearer ' + config . accessToken
93- }
94-
95- const axiosOptions : DefaultOptions = {
96- // Axios
97- baseURL,
98- headers : config . headers ,
99- httpAgent : config . httpAgent ,
100- httpsAgent : config . httpsAgent ,
101- proxy : config . proxy ,
102- timeout : config . timeout ,
103- adapter : config . adapter ,
104- maxContentLength : config . maxContentLength ,
105- maxBodyLength : config . maxBodyLength ,
106- paramsSerializer : {
107- serialize : ( params ) => {
108- return qs . stringify ( params )
109- } ,
110- } ,
111- // Contentful
112- logHandler : config . logHandler ,
113- responseLogger : config . responseLogger ,
114- requestLogger : config . requestLogger ,
115- retryOnError : config . retryOnError ,
116- }
29+ const axiosOptions = createDefaultOptions ( options )
11730
11831 const instance = axios . create ( axiosOptions ) as AxiosInstance
11932 instance . httpClientParams = options
@@ -142,21 +55,21 @@ export default function createHttpClient(
14255 * Please note that the order of interceptors is important
14356 */
14457
145- if ( config . onBeforeRequest ) {
146- instance . interceptors . request . use ( config . onBeforeRequest )
58+ if ( options . onBeforeRequest ) {
59+ instance . interceptors . request . use ( options . onBeforeRequest )
14760 }
14861
149- if ( typeof config . accessToken === 'function' ) {
150- asyncToken ( instance , config . accessToken )
62+ if ( typeof options . accessToken === 'function' ) {
63+ asyncToken ( instance , options . accessToken )
15164 }
15265
153- if ( config . throttle ) {
154- rateLimitThrottle ( instance , config . throttle )
66+ if ( options . throttle ) {
67+ rateLimitThrottle ( instance , options . throttle )
15568 }
156- rateLimitRetry ( instance , config . retryLimit )
69+ rateLimitRetry ( instance , options . retryLimit )
15770
158- if ( config . onError ) {
159- instance . interceptors . response . use ( ( response ) => response , config . onError )
71+ if ( options . onError ) {
72+ instance . interceptors . response . use ( ( response ) => response , options . onError )
16073 }
16174
16275 return instance
0 commit comments