aspi 1.1.0-beta.0 → 1.2.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/README.md +7 -12
- package/dist/index.cjs +84 -14
- package/dist/index.d.cts +128 -109
- package/dist/index.d.ts +128 -109
- package/dist/index.js +81 -13
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -74,6 +74,12 @@ var CustomError = class extends Error {
|
|
|
74
74
|
this.data = data;
|
|
75
75
|
}
|
|
76
76
|
};
|
|
77
|
+
var isAspiError = (error) => {
|
|
78
|
+
return error instanceof AspiError;
|
|
79
|
+
};
|
|
80
|
+
var isCustomError = (error) => {
|
|
81
|
+
return error instanceof CustomError;
|
|
82
|
+
};
|
|
77
83
|
|
|
78
84
|
// src/result.ts
|
|
79
85
|
var result_exports = {};
|
|
@@ -240,14 +246,33 @@ function catchErrors(resultOrHandlers, handlersOrUndefined) {
|
|
|
240
246
|
};
|
|
241
247
|
}
|
|
242
248
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
249
|
+
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
|
|
250
|
+
switch (arguments.length) {
|
|
251
|
+
case 1:
|
|
252
|
+
return a;
|
|
253
|
+
case 2:
|
|
254
|
+
return ab(a);
|
|
255
|
+
case 3:
|
|
256
|
+
return bc(ab(a));
|
|
257
|
+
case 4:
|
|
258
|
+
return cd(bc(ab(a)));
|
|
259
|
+
case 6:
|
|
260
|
+
return ef(de(cd(bc(ab(a)))));
|
|
261
|
+
case 7:
|
|
262
|
+
return fg(ef(de(cd(bc(ab(a))))));
|
|
263
|
+
case 8:
|
|
264
|
+
return gh(fg(ef(de(cd(bc(ab(a)))))));
|
|
265
|
+
case 9:
|
|
266
|
+
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
|
|
267
|
+
default: {
|
|
268
|
+
let ret = arguments[0];
|
|
269
|
+
for (let i = 1; i < arguments.length; i++) {
|
|
270
|
+
ret = arguments[i](ret);
|
|
271
|
+
}
|
|
272
|
+
return ret;
|
|
273
|
+
}
|
|
246
274
|
}
|
|
247
|
-
|
|
248
|
-
run.execute = () => run(result);
|
|
249
|
-
return run;
|
|
250
|
-
};
|
|
275
|
+
}
|
|
251
276
|
|
|
252
277
|
// src/request.ts
|
|
253
278
|
var Request = class {
|
|
@@ -261,17 +286,20 @@ var Request = class {
|
|
|
261
286
|
#retryConfig;
|
|
262
287
|
#shouldBeResult = false;
|
|
263
288
|
#bodySchemaIssues = [];
|
|
289
|
+
#throwOnError = false;
|
|
264
290
|
constructor(method, path, {
|
|
265
291
|
requestConfig,
|
|
266
292
|
retryConfig,
|
|
267
293
|
middlewares,
|
|
268
|
-
errorCbs
|
|
294
|
+
errorCbs,
|
|
295
|
+
throwOnError
|
|
269
296
|
}) {
|
|
270
297
|
this.#path = path;
|
|
271
298
|
this.#middlewares = middlewares || [];
|
|
272
299
|
this.#localRequestInit = { ...requestConfig, method };
|
|
273
300
|
this.#retryConfig = retryConfig;
|
|
274
301
|
this.#customErrorCbs = errorCbs || {};
|
|
302
|
+
this.#throwOnError = throwOnError || false;
|
|
275
303
|
}
|
|
276
304
|
/**
|
|
277
305
|
* Sets the base URL for the request.
|
|
@@ -601,6 +629,22 @@ var Request = class {
|
|
|
601
629
|
this.#schema = schema;
|
|
602
630
|
return this;
|
|
603
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* Sets the request to throw an error if the response status is not successful.
|
|
634
|
+
* @returns The request instance for chaining
|
|
635
|
+
* @example
|
|
636
|
+
* const request = new Request('/users', config);
|
|
637
|
+
* const result = await request
|
|
638
|
+
* .withResult()
|
|
639
|
+
* .throwable()
|
|
640
|
+
* .json();
|
|
641
|
+
*
|
|
642
|
+
*/
|
|
643
|
+
throwable() {
|
|
644
|
+
this.#shouldBeResult = false;
|
|
645
|
+
this.#throwOnError = true;
|
|
646
|
+
return this;
|
|
647
|
+
}
|
|
604
648
|
/**
|
|
605
649
|
* Executes the request and returns the JSON response.
|
|
606
650
|
* @returns A Promise containing the Result type with either successful data or error information
|
|
@@ -608,7 +652,8 @@ var Request = class {
|
|
|
608
652
|
* const request = new Request('/users', config);
|
|
609
653
|
* const result = await request
|
|
610
654
|
* .setQueryParams({ id: '123' })
|
|
611
|
-
* .
|
|
655
|
+
* .
|
|
656
|
+
withResult()
|
|
612
657
|
* .notFound((error) => ({ message: 'User not found' }))
|
|
613
658
|
* .json<User>();
|
|
614
659
|
*
|
|
@@ -707,6 +752,7 @@ var Request = class {
|
|
|
707
752
|
* }
|
|
708
753
|
*/
|
|
709
754
|
withResult() {
|
|
755
|
+
this.#throwOnError = false;
|
|
710
756
|
this.#shouldBeResult = true;
|
|
711
757
|
return this;
|
|
712
758
|
}
|
|
@@ -714,6 +760,9 @@ var Request = class {
|
|
|
714
760
|
if (this.#shouldBeResult) {
|
|
715
761
|
return value;
|
|
716
762
|
}
|
|
763
|
+
if (this.#throwOnError) {
|
|
764
|
+
return getOrThrow(value);
|
|
765
|
+
}
|
|
717
766
|
if (isOk(value)) {
|
|
718
767
|
return [getOrNull(value), null];
|
|
719
768
|
} else {
|
|
@@ -891,11 +940,12 @@ var Request = class {
|
|
|
891
940
|
};
|
|
892
941
|
|
|
893
942
|
// src/aspi.ts
|
|
894
|
-
var
|
|
943
|
+
var Aspi2 = class {
|
|
895
944
|
#globalRequestInit;
|
|
896
945
|
#middlewares = [];
|
|
897
946
|
#retryConfig;
|
|
898
947
|
#customErrorCbs = {};
|
|
948
|
+
#throwOnError = false;
|
|
899
949
|
constructor(config) {
|
|
900
950
|
const { retryConfig, ...requestConfig } = config;
|
|
901
951
|
this.#globalRequestInit = requestConfig;
|
|
@@ -942,7 +992,8 @@ var Aspi = class {
|
|
|
942
992
|
},
|
|
943
993
|
retryConfig: this.#retryConfig,
|
|
944
994
|
middlewares: this.#middlewares,
|
|
945
|
-
errorCbs: this.#customErrorCbs
|
|
995
|
+
errorCbs: this.#customErrorCbs,
|
|
996
|
+
throwOnError: this.#throwOnError
|
|
946
997
|
});
|
|
947
998
|
}
|
|
948
999
|
/**
|
|
@@ -1198,13 +1249,30 @@ var Aspi = class {
|
|
|
1198
1249
|
internalServerError(cb) {
|
|
1199
1250
|
return this.error("internalServerErrorError", "INTERNAL_SERVER_ERROR", cb);
|
|
1200
1251
|
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Sets the aspi to throw an error if the response status is not successful.
|
|
1254
|
+
* @returns The request instance for chaining
|
|
1255
|
+
* @example
|
|
1256
|
+
* const aspi = new Aspi({baseUrl: 'https://example.com'});
|
|
1257
|
+
* const result = await aspi.get('/users')
|
|
1258
|
+
* .withResult()
|
|
1259
|
+
* .throwable()
|
|
1260
|
+
* .json();
|
|
1261
|
+
*
|
|
1262
|
+
*/
|
|
1263
|
+
throwable() {
|
|
1264
|
+
this.#throwOnError = true;
|
|
1265
|
+
return this;
|
|
1266
|
+
}
|
|
1201
1267
|
};
|
|
1202
1268
|
export {
|
|
1203
|
-
Aspi,
|
|
1269
|
+
Aspi2 as Aspi,
|
|
1204
1270
|
AspiError,
|
|
1205
1271
|
CustomError,
|
|
1206
1272
|
Request,
|
|
1207
1273
|
result_exports as Result,
|
|
1208
1274
|
getHttpErrorStatus,
|
|
1209
|
-
httpErrors
|
|
1275
|
+
httpErrors,
|
|
1276
|
+
isAspiError,
|
|
1277
|
+
isCustomError
|
|
1210
1278
|
};
|
package/package.json
CHANGED