-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcontentstackHTTPClient.js
More file actions
242 lines (225 loc) · 7.7 KB
/
contentstackHTTPClient.js
File metadata and controls
242 lines (225 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import axios from 'axios'
import clonedeep from 'lodash/cloneDeep'
import Qs from 'qs'
import { ConcurrencyQueue } from './concurrency-queue'
import { isHost, normalizePlugins } from './Util'
import { ERROR_MESSAGES } from './errorMessages'
export default function contentstackHttpClient (options) {
const defaultConfig = {
insecure: false,
retryOnError: true,
logHandler: (level, data) => {
if (level === 'error' && data) {
const title = [data.name, data.message].filter((a) => a).join(' - ')
console.error(ERROR_MESSAGES.ERROR_WITH_TITLE(title))
return
}
console.log(`[${level}] ${data}`)
},
retryCondition: (error) => {
if (error.response && error.response.status === 429) {
return true
}
return false
},
headers: {},
basePath: '',
proxy: false,
httpAgent: false,
httpsAgent: false,
adapter: false,
timeout: 30000
}
const config = {
...defaultConfig,
...clonedeep(options)
}
if (config.apiKey) {
config.headers['apiKey'] = config.apiKey
}
if (config.accessToken) {
config.headers['accessToken'] = config.accessToken
}
if (config.early_access) {
config.headers['x-header-ea'] = config.early_access
}
const protocol = config.insecure ? 'http' : 'https'
let hostname = config.defaultHostName
let port = config.port || 443
const version = config.version || 'v3'
if (config.region) {
config.host = config.defaultHostName // set region on priority
}
if (isHost(config.host)) {
const parsed = config.host.split(':')
if (parsed.length === 2) {
[hostname, port] = parsed
} else {
hostname = parsed[0]
}
}
if (config.basePath) {
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
}
const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/{api-version}`
let uiHostName = hostname
let developerHubBaseUrl = hostname
if (uiHostName?.endsWith('io')) {
uiHostName = uiHostName.replace('io', 'com')
}
if (uiHostName) {
uiHostName = uiHostName.replace('api', 'app')
}
const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}`
developerHubBaseUrl = developerHubBaseUrl
?.replace('api', 'developerhub-api')
.replace(/^dev\d+/, 'dev') // Replaces any 'dev1', 'dev2', etc. with 'dev'
.replace('io', 'com')
.replace(/^http/, '') // Removing `http` if already present
.replace(/^/, 'https://') // Adds 'https://' at the start if not already there
// set ui host name
const axiosOptions = {
// Axios
baseURL,
uiBaseUrl,
developerHubBaseUrl,
...config,
paramsSerializer: function (params) {
var query = params.query
delete params.query
var qs = Qs.stringify(params, { arrayFormat: 'brackets' })
if (query) {
qs = qs + `&query=${encodeURIComponent(JSON.stringify(query))}`
}
params.query = query
return qs
},
versioningStrategy: 'path'
}
const instance = axios.create(axiosOptions)
instance.httpClientParams = options
// Normalize and store plugins before ConcurrencyQueue so plugin interceptors
// run after the queue's (plugin sees responses/errors before they reach the queue).
// Use options.plugins so hooks run against the same plugin references (spies work in tests).
const plugins = normalizePlugins(options.plugins || config.plugins)
// Request interceptor for versioning strategy (must run first)
instance.interceptors.request.use((request) => {
if (request.versioningStrategy && request.versioningStrategy === 'path') {
request.baseURL = request.baseURL.replace('{api-version}', version)
} else {
request.baseURL = request.baseURL.replace('/{api-version}', '')
}
return request
})
// Request interceptor for plugins (runs after versioning)
if (plugins.length > 0) {
instance.interceptors.request.use(
(request) => {
// Run all onRequest hooks sequentially, using return values
let currentRequest = request
for (const plugin of plugins) {
try {
if (typeof plugin.onRequest === 'function') {
const result = plugin.onRequest(currentRequest)
// Use returned value if provided, otherwise use current request
if (result !== undefined) {
currentRequest = result
}
}
} catch (error) {
// Log error and continue with next plugin
if (config.logHandler) {
config.logHandler('error', {
name: 'PluginError',
message: `Error in plugin onRequest: ${error.message}`,
error: error
})
}
}
}
return currentRequest
},
(error) => {
// Handle request errors - run plugins even on error
let currentConfig = error.config
for (const plugin of plugins) {
try {
if (typeof plugin.onRequest === 'function' && currentConfig) {
const result = plugin.onRequest(currentConfig)
// Use returned value if provided, otherwise use current config
if (result !== undefined) {
currentConfig = result
error.config = currentConfig
}
}
} catch (pluginError) {
if (config.logHandler) {
config.logHandler('error', {
name: 'PluginError',
message: `Error in plugin onRequest (error handler): ${pluginError.message}`,
error: pluginError
})
}
}
}
return Promise.reject(error)
}
)
// Response interceptor for plugins
instance.interceptors.response.use(
(response) => {
// Run all onResponse hooks sequentially for successful responses
// Use return values from plugins
let currentResponse = response
for (const plugin of plugins) {
try {
if (typeof plugin.onResponse === 'function') {
const result = plugin.onResponse(currentResponse)
// Use returned value if provided, otherwise use current response
if (result !== undefined) {
currentResponse = result
}
}
} catch (error) {
// Log error and continue with next plugin
if (config.logHandler) {
config.logHandler('error', {
name: 'PluginError',
message: `Error in plugin onResponse: ${error.message}`,
error: error
})
}
}
}
return currentResponse
},
(error) => {
// Handle response errors - run plugins even on error
// Pass the error object (which may contain error.response if server responded)
let currentError = error
for (const plugin of plugins) {
try {
if (typeof plugin.onResponse === 'function') {
const result = plugin.onResponse(currentError)
// Use returned value if provided, otherwise use current error
if (result !== undefined) {
currentError = result
}
}
} catch (pluginError) {
if (config.logHandler) {
config.logHandler('error', {
name: 'PluginError',
message: `Error in plugin onResponse (error handler): ${pluginError.message}`,
error: pluginError
})
}
}
}
return Promise.reject(currentError)
}
)
}
instance.concurrencyQueue = new ConcurrencyQueue({ axios: instance, config, plugins })
return instance
}