@driveflux/fetch 6.0.10 → 6.1.1

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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Problem } from '@driveflux/problem';
2
- import { Result } from '@driveflux/result';
1
+ import { type Problem } from '@driveflux/problem';
2
+ import { type Result } from '@driveflux/result';
3
3
  type EnhancedRequestInit = Omit<RequestInit, 'body'> & {
4
4
  body?: RequestInit['body'] | object;
5
5
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,OAAO,EAA+C,MAAM,oBAAoB,CAAA;AACtG,OAAO,EAAW,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAEnD,KAAK,mBAAmB,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG;IACrD,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,CAAA;CACpC,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC5B,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,aAAa,CAAC,CAAA;AAavF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAU,CAAC,WAAW,CAAC,iBAC1C,MAAM,oBACO,mBAAmB,KACpC,OAAO,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAgF7C,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,OAAO,EAEZ,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAExD,KAAK,mBAAmB,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG;IACtD,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,CAAA;CACnC,CAAA;AAED,KAAK,aAAa,GAAG;IACpB,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC5B,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,IAAI,MAAM,CACvD,CAAC,EACD,CAAC,GAAG,OAAO,EACX,aAAa,CACb,CAAA;AAaD;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAU,CAAC,WAAW,CAAC,iBAC3C,MAAM,oBACO,mBAAmB,KACnC,OAAO,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAgF7C,CAAA"}
package/dist/index.js CHANGED
@@ -1,81 +1,360 @@
1
- // src/index.ts
2
- import { makeProblem, PROBLEM_INVALID_DATA, PROBLEM_NETWORK_ERROR } from "@driveflux/problem";
3
- import { Err, Ok } from "@driveflux/result";
4
- var isSuccessful = (response) => {
5
- return response.status >= 200 && response.status < 300;
6
- };
7
- var enhancedFetch = async (url, providedOptions) => {
8
- const { body, ...rest } = providedOptions || {};
9
- const options = {
10
- credentials: "include",
11
- ...rest,
12
- headers: {
13
- "Content-Type": "application/json; charset=utf-8",
14
- Accept: "application/json",
15
- ...rest?.headers
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
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);
16
13
  }
17
- };
18
- let finalBody = body;
19
- if (finalBody && typeof finalBody !== "string") {
20
- if (finalBody instanceof FormData) {
21
- options.headers = {
22
- ...options.headers,
23
- "Content-Type": "application/x-www-form-urlencoded"
24
- };
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
+ });
25
38
  } else {
26
- finalBody = JSON.stringify(finalBody);
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
+ });
27
62
  }
28
- }
29
- options.body = finalBody;
30
- let response;
31
- try {
32
- response = await fetch(url, options);
33
- } catch (e) {
34
- return new Err(
35
- makeProblem({
36
- code: PROBLEM_NETWORK_ERROR,
37
- message: e.message,
38
- privateMetadata: {
39
- error: e
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
+ });
40
73
  }
41
- })
42
- );
43
- }
44
- const metadata = {
45
- headers: response.headers,
46
- status: response.status
47
- };
48
- let responseBody;
49
- const textBody = await response.text();
50
- try {
51
- responseBody = await JSON.parse(textBody);
52
- } catch (e) {
53
- if (response.status > 200 && response.status < 206) {
54
- responseBody = null;
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));
55
82
  } else {
56
- return new Err(
57
- makeProblem({
58
- code: PROBLEM_INVALID_DATA,
59
- message: `Unable to parse json`,
60
- statusCode: response.status,
61
- metadata: {
62
- response: {
63
- status: response.status,
64
- statusTest: response.statusText,
65
- body: textBody
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, g, _ = {
118
+ label: 0,
119
+ sent: function() {
120
+ if (t[0] & 1) throw t[1];
121
+ return t[1];
122
+ },
123
+ trys: [],
124
+ ops: []
125
+ };
126
+ return g = {
127
+ next: verb(0),
128
+ "throw": verb(1),
129
+ "return": verb(2)
130
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
131
+ return this;
132
+ }), g;
133
+ function verb(n) {
134
+ return function(v) {
135
+ return step([
136
+ n,
137
+ v
138
+ ]);
139
+ };
140
+ }
141
+ function step(op) {
142
+ if (f) throw new TypeError("Generator is already executing.");
143
+ while(_)try {
144
+ 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;
145
+ if (y = 0, t) op = [
146
+ op[0] & 2,
147
+ t.value
148
+ ];
149
+ switch(op[0]){
150
+ case 0:
151
+ case 1:
152
+ t = op;
153
+ break;
154
+ case 4:
155
+ _.label++;
156
+ return {
157
+ value: op[1],
158
+ done: false
159
+ };
160
+ case 5:
161
+ _.label++;
162
+ y = op[1];
163
+ op = [
164
+ 0
165
+ ];
166
+ continue;
167
+ case 7:
168
+ op = _.ops.pop();
169
+ _.trys.pop();
170
+ continue;
171
+ default:
172
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
173
+ _ = 0;
174
+ continue;
175
+ }
176
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
177
+ _.label = op[1];
178
+ break;
179
+ }
180
+ if (op[0] === 6 && _.label < t[1]) {
181
+ _.label = t[1];
182
+ t = op;
183
+ break;
184
+ }
185
+ if (t && _.label < t[2]) {
186
+ _.label = t[2];
187
+ _.ops.push(op);
188
+ break;
189
+ }
190
+ if (t[2]) _.ops.pop();
191
+ _.trys.pop();
192
+ continue;
66
193
  }
67
- }
68
- }),
69
- metadata
70
- );
194
+ op = body.call(thisArg, _);
195
+ } catch (e) {
196
+ op = [
197
+ 6,
198
+ e
199
+ ];
200
+ y = 0;
201
+ } finally{
202
+ f = t = 0;
203
+ }
204
+ if (op[0] & 5) throw op[1];
205
+ return {
206
+ value: op[0] ? op[1] : void 0,
207
+ done: true
208
+ };
71
209
  }
72
- }
73
- if (!isSuccessful(response)) {
74
- return new Err(responseBody, metadata);
75
- }
76
- return new Ok(responseBody, metadata);
210
+ }
211
+ import { PROBLEM_INVALID_DATA, PROBLEM_NETWORK_ERROR, makeProblem } from '@driveflux/problem';
212
+ import { Err, Ok } from '@driveflux/result';
213
+ /**
214
+ * Checks if a network request came back fine, and throws an error if not
215
+ *
216
+ * @param response A response from a network request
217
+ *
218
+ * @return Returns either the response, or throws an error
219
+ */ var isSuccessful = function(response) {
220
+ return response.status >= 200 && response.status < 300;
77
221
  };
78
- export {
79
- enhancedFetch
80
- };
81
- //# sourceMappingURL=index.js.map
222
+ /**
223
+ * Requests a URL, returning a promise
224
+ *
225
+ * @param url The URL we want to request
226
+ * @param [options] The options we want to pass to "fetch"
227
+ *
228
+ * @return The response data
229
+ */ export var enhancedFetch = function() {
230
+ var _ref = _async_to_generator(function(url, providedOptions) {
231
+ var _ref, body, rest, options, finalBody, response, e, metadata, responseBody, textBody, e1;
232
+ return _ts_generator(this, function(_state) {
233
+ switch(_state.label){
234
+ case 0:
235
+ _ref = providedOptions || {}, body = _ref.body, rest = _object_without_properties(_ref, [
236
+ "body"
237
+ ]);
238
+ options = _object_spread_props(_object_spread({
239
+ credentials: 'include'
240
+ }, rest), {
241
+ headers: _object_spread({
242
+ 'Content-Type': 'application/json; charset=utf-8',
243
+ Accept: 'application/json'
244
+ }, rest === null || rest === void 0 ? void 0 : rest.headers)
245
+ });
246
+ finalBody = body;
247
+ if (finalBody && typeof finalBody !== 'string') {
248
+ if (_instanceof(finalBody, FormData)) {
249
+ options.headers = _object_spread_props(_object_spread({}, options.headers), {
250
+ 'Content-Type': 'application/x-www-form-urlencoded'
251
+ });
252
+ } else {
253
+ finalBody = JSON.stringify(finalBody);
254
+ }
255
+ }
256
+ options.body = finalBody;
257
+ _state.label = 1;
258
+ case 1:
259
+ _state.trys.push([
260
+ 1,
261
+ 3,
262
+ ,
263
+ 4
264
+ ]);
265
+ return [
266
+ 4,
267
+ fetch(url, options)
268
+ ];
269
+ case 2:
270
+ response = _state.sent();
271
+ return [
272
+ 3,
273
+ 4
274
+ ];
275
+ case 3:
276
+ e = _state.sent();
277
+ return [
278
+ 2,
279
+ new Err(makeProblem({
280
+ code: PROBLEM_NETWORK_ERROR,
281
+ message: e.message,
282
+ privateMetadata: {
283
+ error: e
284
+ }
285
+ }))
286
+ ];
287
+ case 4:
288
+ metadata = {
289
+ headers: response.headers,
290
+ status: response.status
291
+ };
292
+ return [
293
+ 4,
294
+ response.text()
295
+ ];
296
+ case 5:
297
+ textBody = _state.sent();
298
+ _state.label = 6;
299
+ case 6:
300
+ _state.trys.push([
301
+ 6,
302
+ 8,
303
+ ,
304
+ 9
305
+ ]);
306
+ return [
307
+ 4,
308
+ JSON.parse(textBody)
309
+ ];
310
+ case 7:
311
+ responseBody = _state.sent();
312
+ return [
313
+ 3,
314
+ 9
315
+ ];
316
+ case 8:
317
+ e1 = _state.sent();
318
+ if (response.status > 200 && response.status < 206) {
319
+ responseBody = null;
320
+ } else {
321
+ return [
322
+ 2,
323
+ new Err(makeProblem({
324
+ code: PROBLEM_INVALID_DATA,
325
+ message: 'Unable to parse json',
326
+ statusCode: response.status,
327
+ metadata: {
328
+ response: {
329
+ status: response.status,
330
+ statusTest: response.statusText,
331
+ body: textBody
332
+ }
333
+ }
334
+ }), metadata)
335
+ ];
336
+ }
337
+ return [
338
+ 3,
339
+ 9
340
+ ];
341
+ case 9:
342
+ // We return the body as E if the fetch is not successful
343
+ if (!isSuccessful(response)) {
344
+ return [
345
+ 2,
346
+ new Err(responseBody, metadata)
347
+ ];
348
+ }
349
+ // We return the body as S if the fetch is successful
350
+ return [
351
+ 2,
352
+ new Ok(responseBody, metadata)
353
+ ];
354
+ }
355
+ });
356
+ });
357
+ return function enhancedFetch(url, providedOptions) {
358
+ return _ref.apply(this, arguments);
359
+ };
360
+ }();
package/package.json CHANGED
@@ -1,33 +1,37 @@
1
1
  {
2
2
  "name": "@driveflux/fetch",
3
- "version": "6.0.10",
3
+ "version": "6.1.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./package.json": "./package.json",
7
7
  ".": {
8
- "import": "./dist/index.js",
9
- "require": "./dist/cjs/index.js",
10
- "types": "./dist/index.d.ts"
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
11
10
  }
12
11
  },
13
- "main": "dist/cjs/index.js",
14
- "module": "dist/index.js",
15
- "types": "dist/index.d.ts",
12
+ "files": [
13
+ "dist"
14
+ ],
16
15
  "dependencies": {
17
- "@driveflux/problem": "4.0.5",
18
- "@driveflux/result": "4.0.9"
16
+ "@driveflux/problem": "4.1.2",
17
+ "@driveflux/result": "4.1.1"
19
18
  },
20
19
  "devDependencies": {
20
+ "@driveflux/fab": "2.1.1",
21
+ "@driveflux/tsconfig": "1.1.2",
22
+ "@types/node": "^20.14.11",
21
23
  "del-cli": "^5.1.0",
22
- "esbuild": "^0.21.5",
23
- "esbuild-register": "^3.5.0",
24
- "typescript": "^5.5.2"
24
+ "typescript": "^5.5.3"
25
25
  },
26
26
  "scripts": {
27
- "build": "npm run clean && npm run build:types && npm run build:js",
28
- "build:js": "node -r esbuild-register build.js",
29
- "build:types": "tsc --build",
30
- "clean": "del dist/**/*.js",
31
- "type-check": "tsc --noEmit"
27
+ "build": "fab",
28
+ "build:js": "fab -t esm",
29
+ "build:types": "fab -t types",
30
+ "clean": "fab clean",
31
+ "type-check": "tsc --noEmit",
32
+ "check": "biome check --write ./src",
33
+ "lint:errors": "pnpm lint --diagnostic-level error",
34
+ "format": "biome format --write ./src",
35
+ "lint": "biome lint --write --unsafe ./src"
32
36
  }
33
37
  }
@@ -1,18 +0,0 @@
1
-  WARN  Issue while reading "/Users/flux/Projects/flux-master/.npmrc". Failed to replace env in config: ${NPM_TOKEN}
2
-
3
- > @driveflux/fetch@6.0.9 build /Users/flux/Projects/flux-master/packages/fetch
4
- > npm run clean && npm run build:types && npm run build:js
5
-
6
-
7
- > @driveflux/fetch@6.0.9 clean
8
- > del dist/**/*.js
9
-
10
- \
11
- > @driveflux/fetch@6.0.9 build:types
12
- > tsc --build
13
-
14
- \
15
- > @driveflux/fetch@6.0.9 build:js
16
- > node -r esbuild-register build.js
17
-
18
- \
@@ -1,5 +0,0 @@
1
- @driveflux/fetch:build:js: cache hit, replaying output 31fdccd0c9d784cb
2
- @driveflux/fetch:build:js: 
3
- @driveflux/fetch:build:js: > @driveflux/fetch@2.0.6 build:js /Users/flux/Projects/flux/packages/fetch
4
- @driveflux/fetch:build:js: > node -r esbuild-register build.js
5
- @driveflux/fetch:build:js: 
@@ -1,5 +0,0 @@
1
- @driveflux/fetch:build:types: cache hit, replaying output c80bb862651433c7
2
- @driveflux/fetch:build:types: 
3
- @driveflux/fetch:build:types: > @driveflux/fetch@2.0.6 build:types /Users/flux/Projects/flux/packages/fetch
4
- @driveflux/fetch:build:types: > tsc --build
5
- @driveflux/fetch:build:types: 
@@ -1,5 +0,0 @@
1
- @driveflux/fetch:clean: cache hit, replaying output cd08b1d08c3494fb
2
- @driveflux/fetch:clean: 
3
- @driveflux/fetch:clean: > @driveflux/fetch@2.0.6 clean /Users/flux/Projects/flux/packages/fetch
4
- @driveflux/fetch:clean: > del dist/**/*.js
5
- @driveflux/fetch:clean: