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/README.md
CHANGED
|
@@ -230,18 +230,13 @@ The above response is a Result type. It can be pattern matched using any pattern
|
|
|
230
230
|
const resultWithoutError = Result.pipe(
|
|
231
231
|
response,
|
|
232
232
|
Result.map((data) => data.data),
|
|
233
|
-
)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
Result.catchError('jsonParseError', () =>
|
|
241
|
-
console.log('failed to parse json error'),
|
|
242
|
-
),
|
|
243
|
-
)
|
|
244
|
-
.execute();
|
|
233
|
+
Result.catchError('aspiError', () => {
|
|
234
|
+
console.log('aspi error');
|
|
235
|
+
}),
|
|
236
|
+
Result.catchError('jsonParseError', () =>
|
|
237
|
+
console.log('failed to parse json error'),
|
|
238
|
+
),
|
|
239
|
+
);
|
|
245
240
|
|
|
246
241
|
// Result<AspiResultOk<AspiRequestInit, { data: any; }>, never>
|
|
247
242
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -20,13 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
Aspi: () =>
|
|
23
|
+
Aspi: () => Aspi2,
|
|
24
24
|
AspiError: () => AspiError,
|
|
25
25
|
CustomError: () => CustomError,
|
|
26
26
|
Request: () => Request,
|
|
27
27
|
Result: () => result_exports,
|
|
28
28
|
getHttpErrorStatus: () => getHttpErrorStatus,
|
|
29
|
-
httpErrors: () => httpErrors
|
|
29
|
+
httpErrors: () => httpErrors,
|
|
30
|
+
isAspiError: () => isAspiError,
|
|
31
|
+
isCustomError: () => isCustomError
|
|
30
32
|
});
|
|
31
33
|
module.exports = __toCommonJS(index_exports);
|
|
32
34
|
|
|
@@ -100,6 +102,12 @@ var CustomError = class extends Error {
|
|
|
100
102
|
this.data = data;
|
|
101
103
|
}
|
|
102
104
|
};
|
|
105
|
+
var isAspiError = (error) => {
|
|
106
|
+
return error instanceof AspiError;
|
|
107
|
+
};
|
|
108
|
+
var isCustomError = (error) => {
|
|
109
|
+
return error instanceof CustomError;
|
|
110
|
+
};
|
|
103
111
|
|
|
104
112
|
// src/result.ts
|
|
105
113
|
var result_exports = {};
|
|
@@ -266,14 +274,33 @@ function catchErrors(resultOrHandlers, handlersOrUndefined) {
|
|
|
266
274
|
};
|
|
267
275
|
}
|
|
268
276
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
277
|
+
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
|
|
278
|
+
switch (arguments.length) {
|
|
279
|
+
case 1:
|
|
280
|
+
return a;
|
|
281
|
+
case 2:
|
|
282
|
+
return ab(a);
|
|
283
|
+
case 3:
|
|
284
|
+
return bc(ab(a));
|
|
285
|
+
case 4:
|
|
286
|
+
return cd(bc(ab(a)));
|
|
287
|
+
case 6:
|
|
288
|
+
return ef(de(cd(bc(ab(a)))));
|
|
289
|
+
case 7:
|
|
290
|
+
return fg(ef(de(cd(bc(ab(a))))));
|
|
291
|
+
case 8:
|
|
292
|
+
return gh(fg(ef(de(cd(bc(ab(a)))))));
|
|
293
|
+
case 9:
|
|
294
|
+
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
|
|
295
|
+
default: {
|
|
296
|
+
let ret = arguments[0];
|
|
297
|
+
for (let i = 1; i < arguments.length; i++) {
|
|
298
|
+
ret = arguments[i](ret);
|
|
299
|
+
}
|
|
300
|
+
return ret;
|
|
301
|
+
}
|
|
272
302
|
}
|
|
273
|
-
|
|
274
|
-
run.execute = () => run(result);
|
|
275
|
-
return run;
|
|
276
|
-
};
|
|
303
|
+
}
|
|
277
304
|
|
|
278
305
|
// src/request.ts
|
|
279
306
|
var Request = class {
|
|
@@ -287,17 +314,20 @@ var Request = class {
|
|
|
287
314
|
#retryConfig;
|
|
288
315
|
#shouldBeResult = false;
|
|
289
316
|
#bodySchemaIssues = [];
|
|
317
|
+
#throwOnError = false;
|
|
290
318
|
constructor(method, path, {
|
|
291
319
|
requestConfig,
|
|
292
320
|
retryConfig,
|
|
293
321
|
middlewares,
|
|
294
|
-
errorCbs
|
|
322
|
+
errorCbs,
|
|
323
|
+
throwOnError
|
|
295
324
|
}) {
|
|
296
325
|
this.#path = path;
|
|
297
326
|
this.#middlewares = middlewares || [];
|
|
298
327
|
this.#localRequestInit = { ...requestConfig, method };
|
|
299
328
|
this.#retryConfig = retryConfig;
|
|
300
329
|
this.#customErrorCbs = errorCbs || {};
|
|
330
|
+
this.#throwOnError = throwOnError || false;
|
|
301
331
|
}
|
|
302
332
|
/**
|
|
303
333
|
* Sets the base URL for the request.
|
|
@@ -627,6 +657,22 @@ var Request = class {
|
|
|
627
657
|
this.#schema = schema;
|
|
628
658
|
return this;
|
|
629
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* Sets the request to throw an error if the response status is not successful.
|
|
662
|
+
* @returns The request instance for chaining
|
|
663
|
+
* @example
|
|
664
|
+
* const request = new Request('/users', config);
|
|
665
|
+
* const result = await request
|
|
666
|
+
* .withResult()
|
|
667
|
+
* .throwable()
|
|
668
|
+
* .json();
|
|
669
|
+
*
|
|
670
|
+
*/
|
|
671
|
+
throwable() {
|
|
672
|
+
this.#shouldBeResult = false;
|
|
673
|
+
this.#throwOnError = true;
|
|
674
|
+
return this;
|
|
675
|
+
}
|
|
630
676
|
/**
|
|
631
677
|
* Executes the request and returns the JSON response.
|
|
632
678
|
* @returns A Promise containing the Result type with either successful data or error information
|
|
@@ -634,7 +680,8 @@ var Request = class {
|
|
|
634
680
|
* const request = new Request('/users', config);
|
|
635
681
|
* const result = await request
|
|
636
682
|
* .setQueryParams({ id: '123' })
|
|
637
|
-
* .
|
|
683
|
+
* .
|
|
684
|
+
withResult()
|
|
638
685
|
* .notFound((error) => ({ message: 'User not found' }))
|
|
639
686
|
* .json<User>();
|
|
640
687
|
*
|
|
@@ -733,6 +780,7 @@ var Request = class {
|
|
|
733
780
|
* }
|
|
734
781
|
*/
|
|
735
782
|
withResult() {
|
|
783
|
+
this.#throwOnError = false;
|
|
736
784
|
this.#shouldBeResult = true;
|
|
737
785
|
return this;
|
|
738
786
|
}
|
|
@@ -740,6 +788,9 @@ var Request = class {
|
|
|
740
788
|
if (this.#shouldBeResult) {
|
|
741
789
|
return value;
|
|
742
790
|
}
|
|
791
|
+
if (this.#throwOnError) {
|
|
792
|
+
return getOrThrow(value);
|
|
793
|
+
}
|
|
743
794
|
if (isOk(value)) {
|
|
744
795
|
return [getOrNull(value), null];
|
|
745
796
|
} else {
|
|
@@ -917,11 +968,12 @@ var Request = class {
|
|
|
917
968
|
};
|
|
918
969
|
|
|
919
970
|
// src/aspi.ts
|
|
920
|
-
var
|
|
971
|
+
var Aspi2 = class {
|
|
921
972
|
#globalRequestInit;
|
|
922
973
|
#middlewares = [];
|
|
923
974
|
#retryConfig;
|
|
924
975
|
#customErrorCbs = {};
|
|
976
|
+
#throwOnError = false;
|
|
925
977
|
constructor(config) {
|
|
926
978
|
const { retryConfig, ...requestConfig } = config;
|
|
927
979
|
this.#globalRequestInit = requestConfig;
|
|
@@ -968,7 +1020,8 @@ var Aspi = class {
|
|
|
968
1020
|
},
|
|
969
1021
|
retryConfig: this.#retryConfig,
|
|
970
1022
|
middlewares: this.#middlewares,
|
|
971
|
-
errorCbs: this.#customErrorCbs
|
|
1023
|
+
errorCbs: this.#customErrorCbs,
|
|
1024
|
+
throwOnError: this.#throwOnError
|
|
972
1025
|
});
|
|
973
1026
|
}
|
|
974
1027
|
/**
|
|
@@ -1224,6 +1277,21 @@ var Aspi = class {
|
|
|
1224
1277
|
internalServerError(cb) {
|
|
1225
1278
|
return this.error("internalServerErrorError", "INTERNAL_SERVER_ERROR", cb);
|
|
1226
1279
|
}
|
|
1280
|
+
/**
|
|
1281
|
+
* Sets the aspi to throw an error if the response status is not successful.
|
|
1282
|
+
* @returns The request instance for chaining
|
|
1283
|
+
* @example
|
|
1284
|
+
* const aspi = new Aspi({baseUrl: 'https://example.com'});
|
|
1285
|
+
* const result = await aspi.get('/users')
|
|
1286
|
+
* .withResult()
|
|
1287
|
+
* .throwable()
|
|
1288
|
+
* .json();
|
|
1289
|
+
*
|
|
1290
|
+
*/
|
|
1291
|
+
throwable() {
|
|
1292
|
+
this.#throwOnError = true;
|
|
1293
|
+
return this;
|
|
1294
|
+
}
|
|
1227
1295
|
};
|
|
1228
1296
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1229
1297
|
0 && (module.exports = {
|
|
@@ -1233,5 +1301,7 @@ var Aspi = class {
|
|
|
1233
1301
|
Request,
|
|
1234
1302
|
Result,
|
|
1235
1303
|
getHttpErrorStatus,
|
|
1236
|
-
httpErrors
|
|
1304
|
+
httpErrors,
|
|
1305
|
+
isAspiError,
|
|
1306
|
+
isCustomError
|
|
1237
1307
|
});
|