@deliverart/sdk-js-error-handler 0.0.6 → 0.0.8
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/CHANGELOG.md +13 -0
- package/dist/index.cjs +44 -0
- package/dist/index.d.cts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +41 -0
- package/package.json +1 -1
- package/src/form-state.ts +50 -0
- package/src/index.ts +1 -0
package/CHANGELOG.md
ADDED
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,9 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
ApiError: () => ApiError,
|
|
24
24
|
ErrorHandlerPlugin: () => ErrorHandlerPlugin,
|
|
25
|
+
FormStateError: () => FormStateError,
|
|
26
|
+
formStateErrorToThrowable: () => formStateErrorToThrowable,
|
|
27
|
+
throwableToFormState: () => throwableToFormState,
|
|
25
28
|
validationErrorDataSchema: () => validationErrorDataSchema
|
|
26
29
|
});
|
|
27
30
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -31,6 +34,44 @@ var import_axios = require("axios");
|
|
|
31
34
|
var ApiError = class extends import_axios.AxiosError {
|
|
32
35
|
};
|
|
33
36
|
|
|
37
|
+
// src/form-state.ts
|
|
38
|
+
var FormStateError = class extends Error {
|
|
39
|
+
constructor(error) {
|
|
40
|
+
super(error.type === "VALIDATION_ERROR" ? "Validation error" : "Unknown error");
|
|
41
|
+
this.error = error;
|
|
42
|
+
this.name = "FormStateError";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
function formStateErrorToThrowable(error) {
|
|
46
|
+
return new FormStateError(error);
|
|
47
|
+
}
|
|
48
|
+
async function throwableToFormState(fn) {
|
|
49
|
+
try {
|
|
50
|
+
const data = await fn();
|
|
51
|
+
return {
|
|
52
|
+
success: true,
|
|
53
|
+
data
|
|
54
|
+
};
|
|
55
|
+
} catch (e) {
|
|
56
|
+
if (e instanceof ApiError) {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
error: e.response?.data ?? {
|
|
60
|
+
type: "UNKNOWN_ERROR",
|
|
61
|
+
value: void 0
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
success: false,
|
|
67
|
+
error: {
|
|
68
|
+
type: "UNKNOWN_ERROR",
|
|
69
|
+
value: void 0
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
34
75
|
// src/types.ts
|
|
35
76
|
var import_zod = require("zod");
|
|
36
77
|
var validationErrorDataSchema = import_zod.z.object({
|
|
@@ -78,5 +119,8 @@ var ErrorHandlerPlugin = class {
|
|
|
78
119
|
0 && (module.exports = {
|
|
79
120
|
ApiError,
|
|
80
121
|
ErrorHandlerPlugin,
|
|
122
|
+
FormStateError,
|
|
123
|
+
formStateErrorToThrowable,
|
|
124
|
+
throwableToFormState,
|
|
81
125
|
validationErrorDataSchema
|
|
82
126
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -53,8 +53,22 @@ type ApiErrorData = {
|
|
|
53
53
|
declare class ApiError extends AxiosError<ApiErrorData> {
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
type FormState<T> = {
|
|
57
|
+
success: true;
|
|
58
|
+
data: T;
|
|
59
|
+
} | {
|
|
60
|
+
success: false;
|
|
61
|
+
error: ApiErrorData;
|
|
62
|
+
};
|
|
63
|
+
declare class FormStateError extends Error {
|
|
64
|
+
readonly error: ApiErrorData;
|
|
65
|
+
constructor(error: ApiErrorData);
|
|
66
|
+
}
|
|
67
|
+
declare function formStateErrorToThrowable(error: ApiErrorData): FormStateError;
|
|
68
|
+
declare function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>>;
|
|
69
|
+
|
|
56
70
|
declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
|
|
57
71
|
setup(client: ApiClient): {};
|
|
58
72
|
}
|
|
59
73
|
|
|
60
|
-
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type ValidationErrorData, validationErrorDataSchema };
|
|
74
|
+
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, FormStateError, type ValidationErrorData, formStateErrorToThrowable, throwableToFormState, validationErrorDataSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -53,8 +53,22 @@ type ApiErrorData = {
|
|
|
53
53
|
declare class ApiError extends AxiosError<ApiErrorData> {
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
type FormState<T> = {
|
|
57
|
+
success: true;
|
|
58
|
+
data: T;
|
|
59
|
+
} | {
|
|
60
|
+
success: false;
|
|
61
|
+
error: ApiErrorData;
|
|
62
|
+
};
|
|
63
|
+
declare class FormStateError extends Error {
|
|
64
|
+
readonly error: ApiErrorData;
|
|
65
|
+
constructor(error: ApiErrorData);
|
|
66
|
+
}
|
|
67
|
+
declare function formStateErrorToThrowable(error: ApiErrorData): FormStateError;
|
|
68
|
+
declare function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>>;
|
|
69
|
+
|
|
56
70
|
declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
|
|
57
71
|
setup(client: ApiClient): {};
|
|
58
72
|
}
|
|
59
73
|
|
|
60
|
-
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type ValidationErrorData, validationErrorDataSchema };
|
|
74
|
+
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, FormStateError, type ValidationErrorData, formStateErrorToThrowable, throwableToFormState, validationErrorDataSchema };
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,44 @@ import { AxiosError } from "axios";
|
|
|
3
3
|
var ApiError = class extends AxiosError {
|
|
4
4
|
};
|
|
5
5
|
|
|
6
|
+
// src/form-state.ts
|
|
7
|
+
var FormStateError = class extends Error {
|
|
8
|
+
constructor(error) {
|
|
9
|
+
super(error.type === "VALIDATION_ERROR" ? "Validation error" : "Unknown error");
|
|
10
|
+
this.error = error;
|
|
11
|
+
this.name = "FormStateError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function formStateErrorToThrowable(error) {
|
|
15
|
+
return new FormStateError(error);
|
|
16
|
+
}
|
|
17
|
+
async function throwableToFormState(fn) {
|
|
18
|
+
try {
|
|
19
|
+
const data = await fn();
|
|
20
|
+
return {
|
|
21
|
+
success: true,
|
|
22
|
+
data
|
|
23
|
+
};
|
|
24
|
+
} catch (e) {
|
|
25
|
+
if (e instanceof ApiError) {
|
|
26
|
+
return {
|
|
27
|
+
success: false,
|
|
28
|
+
error: e.response?.data ?? {
|
|
29
|
+
type: "UNKNOWN_ERROR",
|
|
30
|
+
value: void 0
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
success: false,
|
|
36
|
+
error: {
|
|
37
|
+
type: "UNKNOWN_ERROR",
|
|
38
|
+
value: void 0
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
6
44
|
// src/types.ts
|
|
7
45
|
import { z } from "zod";
|
|
8
46
|
var validationErrorDataSchema = z.object({
|
|
@@ -49,5 +87,8 @@ var ErrorHandlerPlugin = class {
|
|
|
49
87
|
export {
|
|
50
88
|
ApiError,
|
|
51
89
|
ErrorHandlerPlugin,
|
|
90
|
+
FormStateError,
|
|
91
|
+
formStateErrorToThrowable,
|
|
92
|
+
throwableToFormState,
|
|
52
93
|
validationErrorDataSchema
|
|
53
94
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ApiError, ApiErrorData } from './errors'
|
|
2
|
+
|
|
3
|
+
export type FormState<T> =
|
|
4
|
+
| {
|
|
5
|
+
success: true
|
|
6
|
+
data: T
|
|
7
|
+
}
|
|
8
|
+
| {
|
|
9
|
+
success: false
|
|
10
|
+
error: ApiErrorData
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class FormStateError extends Error {
|
|
14
|
+
constructor(public readonly error: ApiErrorData) {
|
|
15
|
+
super(error.type === 'VALIDATION_ERROR' ? 'Validation error' : 'Unknown error')
|
|
16
|
+
this.name = 'FormStateError'
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function formStateErrorToThrowable(error: ApiErrorData): FormStateError {
|
|
21
|
+
return new FormStateError(error)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>> {
|
|
25
|
+
try {
|
|
26
|
+
const data = await fn()
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
data,
|
|
30
|
+
}
|
|
31
|
+
} catch (e) {
|
|
32
|
+
if (e instanceof ApiError) {
|
|
33
|
+
return {
|
|
34
|
+
success: false,
|
|
35
|
+
error: e.response?.data ?? {
|
|
36
|
+
type: 'UNKNOWN_ERROR',
|
|
37
|
+
value: undefined,
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
success: false,
|
|
44
|
+
error: {
|
|
45
|
+
type: 'UNKNOWN_ERROR',
|
|
46
|
+
value: undefined,
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
CHANGED