@driveflux/fetch 7.0.1 → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +77 -336
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,210 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
try {
|
|
3
|
-
var info = gen[key](arg);
|
|
4
|
-
var value = info.value;
|
|
5
|
-
} catch (error) {
|
|
6
|
-
reject(error);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
if (info.done) {
|
|
10
|
-
resolve(value);
|
|
11
|
-
} else {
|
|
12
|
-
Promise.resolve(value).then(_next, _throw);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _async_to_generator(fn) {
|
|
16
|
-
return function() {
|
|
17
|
-
var self = this, args = arguments;
|
|
18
|
-
return new Promise(function(resolve, reject) {
|
|
19
|
-
var gen = fn.apply(self, args);
|
|
20
|
-
function _next(value) {
|
|
21
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
-
}
|
|
23
|
-
function _throw(err) {
|
|
24
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
-
}
|
|
26
|
-
_next(undefined);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function _define_property(obj, key, value) {
|
|
31
|
-
if (key in obj) {
|
|
32
|
-
Object.defineProperty(obj, key, {
|
|
33
|
-
value: value,
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true
|
|
37
|
-
});
|
|
38
|
-
} else {
|
|
39
|
-
obj[key] = value;
|
|
40
|
-
}
|
|
41
|
-
return obj;
|
|
42
|
-
}
|
|
43
|
-
function _instanceof(left, right) {
|
|
44
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
45
|
-
return !!right[Symbol.hasInstance](left);
|
|
46
|
-
} else {
|
|
47
|
-
return left instanceof right;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function _object_spread(target) {
|
|
51
|
-
for(var i = 1; i < arguments.length; i++){
|
|
52
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
53
|
-
var ownKeys = Object.keys(source);
|
|
54
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
55
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
56
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
57
|
-
}));
|
|
58
|
-
}
|
|
59
|
-
ownKeys.forEach(function(key) {
|
|
60
|
-
_define_property(target, key, source[key]);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
return target;
|
|
64
|
-
}
|
|
65
|
-
function ownKeys(object, enumerableOnly) {
|
|
66
|
-
var keys = Object.keys(object);
|
|
67
|
-
if (Object.getOwnPropertySymbols) {
|
|
68
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
69
|
-
if (enumerableOnly) {
|
|
70
|
-
symbols = symbols.filter(function(sym) {
|
|
71
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
keys.push.apply(keys, symbols);
|
|
75
|
-
}
|
|
76
|
-
return keys;
|
|
77
|
-
}
|
|
78
|
-
function _object_spread_props(target, source) {
|
|
79
|
-
source = source != null ? source : {};
|
|
80
|
-
if (Object.getOwnPropertyDescriptors) {
|
|
81
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
82
|
-
} else {
|
|
83
|
-
ownKeys(Object(source)).forEach(function(key) {
|
|
84
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
return target;
|
|
88
|
-
}
|
|
89
|
-
function _object_without_properties(source, excluded) {
|
|
90
|
-
if (source == null) return {};
|
|
91
|
-
var target = _object_without_properties_loose(source, excluded);
|
|
92
|
-
var key, i;
|
|
93
|
-
if (Object.getOwnPropertySymbols) {
|
|
94
|
-
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
95
|
-
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
96
|
-
key = sourceSymbolKeys[i];
|
|
97
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
98
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
99
|
-
target[key] = source[key];
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return target;
|
|
103
|
-
}
|
|
104
|
-
function _object_without_properties_loose(source, excluded) {
|
|
105
|
-
if (source == null) return {};
|
|
106
|
-
var target = {};
|
|
107
|
-
var sourceKeys = Object.keys(source);
|
|
108
|
-
var key, i;
|
|
109
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
110
|
-
key = sourceKeys[i];
|
|
111
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
112
|
-
target[key] = source[key];
|
|
113
|
-
}
|
|
114
|
-
return target;
|
|
115
|
-
}
|
|
116
|
-
function _ts_generator(thisArg, body) {
|
|
117
|
-
var f, y, t, _ = {
|
|
118
|
-
label: 0,
|
|
119
|
-
sent: function() {
|
|
120
|
-
if (t[0] & 1) throw t[1];
|
|
121
|
-
return t[1];
|
|
122
|
-
},
|
|
123
|
-
trys: [],
|
|
124
|
-
ops: []
|
|
125
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
126
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
127
|
-
return this;
|
|
128
|
-
}), g;
|
|
129
|
-
function verb(n) {
|
|
130
|
-
return function(v) {
|
|
131
|
-
return step([
|
|
132
|
-
n,
|
|
133
|
-
v
|
|
134
|
-
]);
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
function step(op) {
|
|
138
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
139
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
140
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
141
|
-
if (y = 0, t) op = [
|
|
142
|
-
op[0] & 2,
|
|
143
|
-
t.value
|
|
144
|
-
];
|
|
145
|
-
switch(op[0]){
|
|
146
|
-
case 0:
|
|
147
|
-
case 1:
|
|
148
|
-
t = op;
|
|
149
|
-
break;
|
|
150
|
-
case 4:
|
|
151
|
-
_.label++;
|
|
152
|
-
return {
|
|
153
|
-
value: op[1],
|
|
154
|
-
done: false
|
|
155
|
-
};
|
|
156
|
-
case 5:
|
|
157
|
-
_.label++;
|
|
158
|
-
y = op[1];
|
|
159
|
-
op = [
|
|
160
|
-
0
|
|
161
|
-
];
|
|
162
|
-
continue;
|
|
163
|
-
case 7:
|
|
164
|
-
op = _.ops.pop();
|
|
165
|
-
_.trys.pop();
|
|
166
|
-
continue;
|
|
167
|
-
default:
|
|
168
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
169
|
-
_ = 0;
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
173
|
-
_.label = op[1];
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
177
|
-
_.label = t[1];
|
|
178
|
-
t = op;
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
if (t && _.label < t[2]) {
|
|
182
|
-
_.label = t[2];
|
|
183
|
-
_.ops.push(op);
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
if (t[2]) _.ops.pop();
|
|
187
|
-
_.trys.pop();
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
op = body.call(thisArg, _);
|
|
191
|
-
} catch (e) {
|
|
192
|
-
op = [
|
|
193
|
-
6,
|
|
194
|
-
e
|
|
195
|
-
];
|
|
196
|
-
y = 0;
|
|
197
|
-
} finally{
|
|
198
|
-
f = t = 0;
|
|
199
|
-
}
|
|
200
|
-
if (op[0] & 5) throw op[1];
|
|
201
|
-
return {
|
|
202
|
-
value: op[0] ? op[1] : void 0,
|
|
203
|
-
done: true
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
import { PROBLEM_INVALID_DATA, PROBLEM_NETWORK_ERROR, makeProblem } from '@driveflux/problem';
|
|
1
|
+
import { PROBLEM_INVALID_DATA, PROBLEM_NETWORK_ERROR, makeProblem, } from '@driveflux/problem';
|
|
208
2
|
import { Err, Ok } from '@driveflux/result';
|
|
209
3
|
/**
|
|
210
4
|
* Checks if a network request came back fine, and throws an error if not
|
|
@@ -212,7 +6,8 @@ import { Err, Ok } from '@driveflux/result';
|
|
|
212
6
|
* @param response A response from a network request
|
|
213
7
|
*
|
|
214
8
|
* @return Returns either the response, or throws an error
|
|
215
|
-
*/
|
|
9
|
+
*/
|
|
10
|
+
const isSuccessful = (response) => {
|
|
216
11
|
return response.status >= 200 && response.status < 300;
|
|
217
12
|
};
|
|
218
13
|
/**
|
|
@@ -222,132 +17,78 @@ import { Err, Ok } from '@driveflux/result';
|
|
|
222
17
|
* @param [options] The options we want to pass to "fetch"
|
|
223
18
|
*
|
|
224
19
|
* @return The response data
|
|
225
|
-
*/
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
8,
|
|
299
|
-
,
|
|
300
|
-
9
|
|
301
|
-
]);
|
|
302
|
-
return [
|
|
303
|
-
4,
|
|
304
|
-
JSON.parse(textBody)
|
|
305
|
-
];
|
|
306
|
-
case 7:
|
|
307
|
-
responseBody = _state.sent();
|
|
308
|
-
return [
|
|
309
|
-
3,
|
|
310
|
-
9
|
|
311
|
-
];
|
|
312
|
-
case 8:
|
|
313
|
-
e1 = _state.sent();
|
|
314
|
-
if (response.status > 200 && response.status < 206) {
|
|
315
|
-
responseBody = null;
|
|
316
|
-
} else {
|
|
317
|
-
return [
|
|
318
|
-
2,
|
|
319
|
-
new Err(makeProblem({
|
|
320
|
-
code: PROBLEM_INVALID_DATA,
|
|
321
|
-
message: 'Unable to parse json',
|
|
322
|
-
statusCode: response.status,
|
|
323
|
-
metadata: {
|
|
324
|
-
response: {
|
|
325
|
-
status: response.status,
|
|
326
|
-
statusTest: response.statusText,
|
|
327
|
-
body: textBody
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}), metadata)
|
|
331
|
-
];
|
|
332
|
-
}
|
|
333
|
-
return [
|
|
334
|
-
3,
|
|
335
|
-
9
|
|
336
|
-
];
|
|
337
|
-
case 9:
|
|
338
|
-
// We return the body as E if the fetch is not successful
|
|
339
|
-
if (!isSuccessful(response)) {
|
|
340
|
-
return [
|
|
341
|
-
2,
|
|
342
|
-
new Err(responseBody, metadata)
|
|
343
|
-
];
|
|
344
|
-
}
|
|
345
|
-
// We return the body as S if the fetch is successful
|
|
346
|
-
return [
|
|
347
|
-
2,
|
|
348
|
-
new Ok(responseBody, metadata)
|
|
349
|
-
];
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
})();
|
|
20
|
+
*/
|
|
21
|
+
export const enhancedFetch = async (url, providedOptions) => {
|
|
22
|
+
const { body, ...rest } = providedOptions || {};
|
|
23
|
+
const options = {
|
|
24
|
+
credentials: 'include',
|
|
25
|
+
...rest,
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
28
|
+
Accept: 'application/json',
|
|
29
|
+
...rest?.headers,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
let finalBody = body;
|
|
33
|
+
if (finalBody && typeof finalBody !== 'string') {
|
|
34
|
+
if (finalBody instanceof FormData) {
|
|
35
|
+
options.headers = {
|
|
36
|
+
...options.headers,
|
|
37
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
finalBody = JSON.stringify(finalBody);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
options.body = finalBody;
|
|
45
|
+
let response;
|
|
46
|
+
try {
|
|
47
|
+
response = await fetch(url, options);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
return new Err(makeProblem({
|
|
51
|
+
code: PROBLEM_NETWORK_ERROR,
|
|
52
|
+
message: e.message,
|
|
53
|
+
privateMetadata: {
|
|
54
|
+
error: e,
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
const metadata = {
|
|
59
|
+
headers: response.headers,
|
|
60
|
+
status: response.status,
|
|
61
|
+
};
|
|
62
|
+
let responseBody;
|
|
63
|
+
// Will parse JSON body regardless of the response status
|
|
64
|
+
const textBody = await response.text();
|
|
65
|
+
try {
|
|
66
|
+
responseBody = await JSON.parse(textBody);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (response.status > 200 && response.status < 206) {
|
|
70
|
+
responseBody = null;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return new Err(makeProblem({
|
|
74
|
+
code: PROBLEM_INVALID_DATA,
|
|
75
|
+
message: 'Unable to parse json',
|
|
76
|
+
statusCode: response.status,
|
|
77
|
+
metadata: {
|
|
78
|
+
response: {
|
|
79
|
+
status: response.status,
|
|
80
|
+
statusTest: response.statusText,
|
|
81
|
+
body: textBody,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}), metadata);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// We return the body as E if the fetch is not successful
|
|
88
|
+
if (!isSuccessful(response)) {
|
|
89
|
+
return new Err(responseBody, metadata);
|
|
90
|
+
}
|
|
91
|
+
// We return the body as S if the fetch is successful
|
|
92
|
+
return new Ok(responseBody, metadata);
|
|
353
93
|
};
|
|
94
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driveflux/fetch",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./package.json": "./package.json",
|
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@driveflux/problem": "5.0.
|
|
17
|
-
"@driveflux/result": "5.0.
|
|
16
|
+
"@driveflux/problem": "5.0.2",
|
|
17
|
+
"@driveflux/result": "5.0.2"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@driveflux/fab": "3.0.
|
|
21
|
-
"@driveflux/tsconfig": "2.0.
|
|
20
|
+
"@driveflux/fab": "3.0.2",
|
|
21
|
+
"@driveflux/tsconfig": "2.0.2",
|
|
22
22
|
"@types/node": "^22.15.16",
|
|
23
23
|
"del-cli": "^6.0.0",
|
|
24
24
|
"typescript": "^5.8.3"
|