-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrequest.js
More file actions
executable file
·143 lines (123 loc) · 5.36 KB
/
request.js
File metadata and controls
executable file
·143 lines (123 loc) · 5.36 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
import * as Utils from "./utils.js";
import fetch from "runtime/http.js";
//JS SDK version
let version = '{{VERSION}}';
let environment,
api_key;
export default function Request(stack, fetchOptions) {
let requestParams = stack.requestParams;
return new Promise(function(resolve, reject) {
let queryParams;
const params = new URLSearchParams();
let serialize = function (obj, prefix) {
if (typeof obj === 'object' && obj.length !== undefined) {
for (let i = 0, _i = obj.length; i < _i; i++) {
params.append(prefix + '[]', obj[i]);
}
} else {
for (const p in obj) {
let k = prefix ? prefix + '[' + p + ']' : p,
v = obj[p];
v !== null && typeof v === 'object' && p !== 'query'
? serialize(v, k)
: params.append(k, p !== 'query' ? v : JSON.stringify(v));
}
}
return params.toString();
};
// setting headers
requestParams.headers['Content-Type'] = 'application/json; charset=UTF-8';
requestParams.headers['X-User-Agent'] = 'contentstack-delivery-javascript-{{PLATFORM}}/' + version;
if (requestParams.body && typeof requestParams.body === 'object') {
delete requestParams.body._method;
if (typeof requestParams.body.query === "object" && Object.keys(requestParams.body.query).length === 0) delete requestParams.body.query;
queryParams = serialize(requestParams.body);
}
return fetchRetry(stack, queryParams,
fetchOptions,
resolve,
reject,
fetchOptions.retryDelay,
fetchOptions.retryLimit)
});
}
function wait(retryDelay) {
return new Promise((resolve) => {
setTimeout(resolve, retryDelay)
});
}
function fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay = 300, retryLimit = 5) {
const requestParams = stack.requestParams,
url = requestParams.url + '?' + queryParams,
headers = requestParams.headers
const option = Utils.mergeDeep({
method: 'GET',
headers: headers,
timeout: 30000,
},
fetchOptions);
function onError (error) {
if (retryLimit === 0) {
if (fetchOptions.debug) fetchOptions.logHandler('error', error);
reject(error);
}else {
var msDelay = retryDelay
retryLimit = retryLimit - 1
var retryCount = (fetchOptions.retryLimit - retryLimit)
if (fetchOptions.retryDelayOptions) {
if (fetchOptions.retryDelayOptions.base) {
msDelay = fetchOptions.retryDelayOptions.base * retryCount
} else if (fetchOptions.retryDelayOptions.customBackoff) {
msDelay = fetchOptions.retryDelayOptions.customBackoff(retryCount, error)
}
}
wait(msDelay)
.then(() => {
return fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay, retryLimit)
})
.catch(() => {
return fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay, retryLimit)
})
}
}
if (fetchOptions.debug) fetchOptions.logHandler('info', { url: url, option: option});
let request = {url, option};
let plugins = stack.plugins;
if (plugins && plugins !== undefined) {
for (let index = 0; index < plugins.length; index++) {
if (typeof plugins[index].onRequest === 'function') {
request = plugins[index].onRequest(stack, request)
}
}
}
fetch(request.url, request.option)
.then( function(response) {
if (fetchOptions.debug) fetchOptions.logHandler('info', response);
const data = response.json();
if (response.ok && response.status === 200) {
data.then(json => {
for (let index = 0; index < plugins.length && typeof plugins[index].onResponse === 'function'; index++)
json = plugins[index].onResponse(stack, request , response, json)
resolve(json);
})
} else {
const {status, statusText} = response
data.then((json) => {
const {error_message, error_code, errors} = json
const errorDetails = { error_message, error_code, errors, status, statusText }
if (fetchOptions.retryCondition && fetchOptions.retryCondition(response)) {
onError(errorDetails)
} else {
if (fetchOptions.debug) fetchOptions.logHandler('error', errorDetails);
reject(errorDetails)
}
}).catch(() => {
if (fetchOptions.debug) fetchOptions.logHandler('error', {status, statusText});
reject({status, statusText})
});
}
}).catch((error) => {
if (fetchOptions.debug) fetchOptions.logHandler('error', error);
reject(error)
});
}