@deliverart/sdk-js-error-handler 0.0.9 → 1.1.0
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/package.json +9 -23
- package/.changeset/config.json +0 -11
- package/.github/workflows/workflow.yml +0 -47
- package/.prettierrc +0 -7
- package/CHANGELOG.md +0 -19
- package/README.md +0 -1
- package/dist/index.cjs +0 -126
- package/dist/index.d.cts +0 -74
- package/dist/index.d.ts +0 -74
- package/dist/index.js +0 -94
- package/eslint.config.js +0 -37
- package/src/errors.ts +0 -15
- package/src/form-state.ts +0 -50
- package/src/index.ts +0 -4
- package/src/plugin.ts +0 -36
- package/src/types.ts +0 -16
- package/tsconfig.json +0 -15
package/package.json
CHANGED
|
@@ -1,33 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deliverart/sdk-js-error-handler",
|
|
3
3
|
"description": "Error handling utilities for Deliverart SDK in JavaScript",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"tsup": {
|
|
9
|
+
"tsconfig": "./tsconfig.json"
|
|
10
|
+
},
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
13
|
"import": "./dist/index.js",
|
|
11
14
|
"require": "./dist/index.cjs"
|
|
12
15
|
}
|
|
13
16
|
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
14
20
|
"dependencies": {
|
|
15
|
-
"
|
|
16
|
-
"axios": "1.9.0",
|
|
17
|
-
"@deliverart/sdk-js-core": "0.1.4"
|
|
18
|
-
},
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"@changesets/cli": "^2.29.4",
|
|
21
|
-
"@eslint/js": "9.28.0",
|
|
22
|
-
"@types/node": "22.15.30",
|
|
23
|
-
"@typescript-eslint/eslint-plugin": "8.33.1",
|
|
24
|
-
"@typescript-eslint/parser": "8.33.1",
|
|
25
|
-
"eslint": "9.28.0",
|
|
26
|
-
"eslint-config-prettier": "10.1.5",
|
|
27
|
-
"eslint-plugin-simple-import-sort": "12.1.1",
|
|
28
|
-
"prettier": "3.5.3",
|
|
29
|
-
"tsup": "8.5.0",
|
|
30
|
-
"typescript": "5.8.3"
|
|
21
|
+
"@deliverart/sdk-js-core": "1.1.0"
|
|
31
22
|
},
|
|
32
23
|
"publishConfig": {
|
|
33
24
|
"access": "public"
|
|
@@ -35,11 +26,6 @@
|
|
|
35
26
|
"scripts": {
|
|
36
27
|
"build": "tsup src/index.ts --dts --format esm,cjs",
|
|
37
28
|
"dev": "tsup src/index.ts --dts --watch",
|
|
38
|
-
"
|
|
39
|
-
"lint:fix": "eslint . --fix",
|
|
40
|
-
"prettier": "prettier --check .",
|
|
41
|
-
"prettier:fix": "prettier --write .",
|
|
42
|
-
"version": "@changesets/cli version",
|
|
43
|
-
"release": "@changesets/cli publish"
|
|
29
|
+
"clean": "rm -rf dist"
|
|
44
30
|
}
|
|
45
31
|
}
|
package/.changeset/config.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
|
|
3
|
-
"changelog": "@changesets/cli/changelog",
|
|
4
|
-
"commit": false,
|
|
5
|
-
"fixed": [],
|
|
6
|
-
"linked": [],
|
|
7
|
-
"access": "restricted",
|
|
8
|
-
"baseBranch": "main",
|
|
9
|
-
"updateInternalDependencies": "patch",
|
|
10
|
-
"ignore": []
|
|
11
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
name: Release
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
|
|
8
|
-
permissions:
|
|
9
|
-
contents: write
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
release:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v4
|
|
16
|
-
|
|
17
|
-
- uses: pnpm/action-setup@v2
|
|
18
|
-
with:
|
|
19
|
-
version: 8
|
|
20
|
-
|
|
21
|
-
- uses: actions/setup-node@v4
|
|
22
|
-
with:
|
|
23
|
-
node-version: '20'
|
|
24
|
-
registry-url: 'https://registry.npmjs.org'
|
|
25
|
-
always-auth: true
|
|
26
|
-
env:
|
|
27
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
28
|
-
|
|
29
|
-
- name: Install deps
|
|
30
|
-
run: pnpm install
|
|
31
|
-
|
|
32
|
-
- name: Build
|
|
33
|
-
run: pnpm build
|
|
34
|
-
|
|
35
|
-
- name: Create version and changelog
|
|
36
|
-
run: pnpx @changesets/cli version
|
|
37
|
-
|
|
38
|
-
- name: Publish to NPM
|
|
39
|
-
run: pnpx @changesets/cli publish
|
|
40
|
-
env:
|
|
41
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
42
|
-
|
|
43
|
-
- name: Push updated versions and changelogs
|
|
44
|
-
uses: EndBug/add-and-commit@v9
|
|
45
|
-
with:
|
|
46
|
-
message: 'chore(release): version bump'
|
|
47
|
-
add: '.'
|
package/.prettierrc
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# @deliverart/sdk-js-error-handler
|
|
2
|
-
|
|
3
|
-
## 0.0.9
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- 8932e85: increase version pkg
|
|
8
|
-
|
|
9
|
-
## 0.0.8
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- 2960c29: Add form state Error
|
|
14
|
-
|
|
15
|
-
## 0.0.7
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- ba0e953: Add form state utils
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# sdk-js-error-handler
|
package/dist/index.cjs
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
ApiError: () => ApiError,
|
|
24
|
-
ErrorHandlerPlugin: () => ErrorHandlerPlugin,
|
|
25
|
-
FormStateError: () => FormStateError,
|
|
26
|
-
formStateErrorToThrowable: () => formStateErrorToThrowable,
|
|
27
|
-
throwableToFormState: () => throwableToFormState,
|
|
28
|
-
validationErrorDataSchema: () => validationErrorDataSchema
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(index_exports);
|
|
31
|
-
|
|
32
|
-
// src/errors.ts
|
|
33
|
-
var import_axios = require("axios");
|
|
34
|
-
var ApiError = class extends import_axios.AxiosError {
|
|
35
|
-
};
|
|
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
|
-
|
|
75
|
-
// src/types.ts
|
|
76
|
-
var import_zod = require("zod");
|
|
77
|
-
var validationErrorDataSchema = import_zod.z.object({
|
|
78
|
-
status: import_zod.z.number(),
|
|
79
|
-
violations: import_zod.z.array(
|
|
80
|
-
import_zod.z.object({
|
|
81
|
-
propertyPath: import_zod.z.string(),
|
|
82
|
-
message: import_zod.z.string(),
|
|
83
|
-
code: import_zod.z.string().nullable()
|
|
84
|
-
})
|
|
85
|
-
),
|
|
86
|
-
detail: import_zod.z.string(),
|
|
87
|
-
type: import_zod.z.string(),
|
|
88
|
-
title: import_zod.z.string()
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// src/plugin.ts
|
|
92
|
-
var ErrorHandlerPlugin = class {
|
|
93
|
-
setup(client) {
|
|
94
|
-
client.http.interceptors.response.use(
|
|
95
|
-
(response) => response,
|
|
96
|
-
async (error) => {
|
|
97
|
-
if (!error.response) {
|
|
98
|
-
throw new ApiError();
|
|
99
|
-
}
|
|
100
|
-
if (error.response.status === 422) {
|
|
101
|
-
const safeParsed = validationErrorDataSchema.safeParse(error.response.data);
|
|
102
|
-
if (safeParsed.success) {
|
|
103
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
104
|
-
...error.response,
|
|
105
|
-
data: { type: "VALIDATION_ERROR", value: safeParsed.data }
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
110
|
-
...error.response,
|
|
111
|
-
data: { type: "UNKNOWN_ERROR", value: void 0 }
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
);
|
|
115
|
-
return {};
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
-
0 && (module.exports = {
|
|
120
|
-
ApiError,
|
|
121
|
-
ErrorHandlerPlugin,
|
|
122
|
-
FormStateError,
|
|
123
|
-
formStateErrorToThrowable,
|
|
124
|
-
throwableToFormState,
|
|
125
|
-
validationErrorDataSchema
|
|
126
|
-
});
|
package/dist/index.d.cts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { AxiosError } from 'axios';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { ApiClientPlugin, ApiClient } from '@deliverart/sdk-js-core';
|
|
4
|
-
|
|
5
|
-
declare const validationErrorDataSchema: z.ZodObject<{
|
|
6
|
-
status: z.ZodNumber;
|
|
7
|
-
violations: z.ZodArray<z.ZodObject<{
|
|
8
|
-
propertyPath: z.ZodString;
|
|
9
|
-
message: z.ZodString;
|
|
10
|
-
code: z.ZodNullable<z.ZodString>;
|
|
11
|
-
}, "strip", z.ZodTypeAny, {
|
|
12
|
-
code: string | null;
|
|
13
|
-
message: string;
|
|
14
|
-
propertyPath: string;
|
|
15
|
-
}, {
|
|
16
|
-
code: string | null;
|
|
17
|
-
message: string;
|
|
18
|
-
propertyPath: string;
|
|
19
|
-
}>, "many">;
|
|
20
|
-
detail: z.ZodString;
|
|
21
|
-
type: z.ZodString;
|
|
22
|
-
title: z.ZodString;
|
|
23
|
-
}, "strip", z.ZodTypeAny, {
|
|
24
|
-
status: number;
|
|
25
|
-
violations: {
|
|
26
|
-
code: string | null;
|
|
27
|
-
message: string;
|
|
28
|
-
propertyPath: string;
|
|
29
|
-
}[];
|
|
30
|
-
type: string;
|
|
31
|
-
detail: string;
|
|
32
|
-
title: string;
|
|
33
|
-
}, {
|
|
34
|
-
status: number;
|
|
35
|
-
violations: {
|
|
36
|
-
code: string | null;
|
|
37
|
-
message: string;
|
|
38
|
-
propertyPath: string;
|
|
39
|
-
}[];
|
|
40
|
-
type: string;
|
|
41
|
-
detail: string;
|
|
42
|
-
title: string;
|
|
43
|
-
}>;
|
|
44
|
-
type ValidationErrorData = z.infer<typeof validationErrorDataSchema>;
|
|
45
|
-
|
|
46
|
-
type ApiErrorData = {
|
|
47
|
-
type: 'VALIDATION_ERROR';
|
|
48
|
-
value: ValidationErrorData;
|
|
49
|
-
} | {
|
|
50
|
-
type: 'UNKNOWN_ERROR';
|
|
51
|
-
value: undefined;
|
|
52
|
-
};
|
|
53
|
-
declare class ApiError extends AxiosError<ApiErrorData> {
|
|
54
|
-
}
|
|
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
|
-
|
|
70
|
-
declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
|
|
71
|
-
setup(client: ApiClient): {};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, FormStateError, type ValidationErrorData, formStateErrorToThrowable, throwableToFormState, validationErrorDataSchema };
|
package/dist/index.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { AxiosError } from 'axios';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { ApiClientPlugin, ApiClient } from '@deliverart/sdk-js-core';
|
|
4
|
-
|
|
5
|
-
declare const validationErrorDataSchema: z.ZodObject<{
|
|
6
|
-
status: z.ZodNumber;
|
|
7
|
-
violations: z.ZodArray<z.ZodObject<{
|
|
8
|
-
propertyPath: z.ZodString;
|
|
9
|
-
message: z.ZodString;
|
|
10
|
-
code: z.ZodNullable<z.ZodString>;
|
|
11
|
-
}, "strip", z.ZodTypeAny, {
|
|
12
|
-
code: string | null;
|
|
13
|
-
message: string;
|
|
14
|
-
propertyPath: string;
|
|
15
|
-
}, {
|
|
16
|
-
code: string | null;
|
|
17
|
-
message: string;
|
|
18
|
-
propertyPath: string;
|
|
19
|
-
}>, "many">;
|
|
20
|
-
detail: z.ZodString;
|
|
21
|
-
type: z.ZodString;
|
|
22
|
-
title: z.ZodString;
|
|
23
|
-
}, "strip", z.ZodTypeAny, {
|
|
24
|
-
status: number;
|
|
25
|
-
violations: {
|
|
26
|
-
code: string | null;
|
|
27
|
-
message: string;
|
|
28
|
-
propertyPath: string;
|
|
29
|
-
}[];
|
|
30
|
-
type: string;
|
|
31
|
-
detail: string;
|
|
32
|
-
title: string;
|
|
33
|
-
}, {
|
|
34
|
-
status: number;
|
|
35
|
-
violations: {
|
|
36
|
-
code: string | null;
|
|
37
|
-
message: string;
|
|
38
|
-
propertyPath: string;
|
|
39
|
-
}[];
|
|
40
|
-
type: string;
|
|
41
|
-
detail: string;
|
|
42
|
-
title: string;
|
|
43
|
-
}>;
|
|
44
|
-
type ValidationErrorData = z.infer<typeof validationErrorDataSchema>;
|
|
45
|
-
|
|
46
|
-
type ApiErrorData = {
|
|
47
|
-
type: 'VALIDATION_ERROR';
|
|
48
|
-
value: ValidationErrorData;
|
|
49
|
-
} | {
|
|
50
|
-
type: 'UNKNOWN_ERROR';
|
|
51
|
-
value: undefined;
|
|
52
|
-
};
|
|
53
|
-
declare class ApiError extends AxiosError<ApiErrorData> {
|
|
54
|
-
}
|
|
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
|
-
|
|
70
|
-
declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
|
|
71
|
-
setup(client: ApiClient): {};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, FormStateError, type ValidationErrorData, formStateErrorToThrowable, throwableToFormState, validationErrorDataSchema };
|
package/dist/index.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// src/errors.ts
|
|
2
|
-
import { AxiosError } from "axios";
|
|
3
|
-
var ApiError = class extends AxiosError {
|
|
4
|
-
};
|
|
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
|
-
|
|
44
|
-
// src/types.ts
|
|
45
|
-
import { z } from "zod";
|
|
46
|
-
var validationErrorDataSchema = z.object({
|
|
47
|
-
status: z.number(),
|
|
48
|
-
violations: z.array(
|
|
49
|
-
z.object({
|
|
50
|
-
propertyPath: z.string(),
|
|
51
|
-
message: z.string(),
|
|
52
|
-
code: z.string().nullable()
|
|
53
|
-
})
|
|
54
|
-
),
|
|
55
|
-
detail: z.string(),
|
|
56
|
-
type: z.string(),
|
|
57
|
-
title: z.string()
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// src/plugin.ts
|
|
61
|
-
var ErrorHandlerPlugin = class {
|
|
62
|
-
setup(client) {
|
|
63
|
-
client.http.interceptors.response.use(
|
|
64
|
-
(response) => response,
|
|
65
|
-
async (error) => {
|
|
66
|
-
if (!error.response) {
|
|
67
|
-
throw new ApiError();
|
|
68
|
-
}
|
|
69
|
-
if (error.response.status === 422) {
|
|
70
|
-
const safeParsed = validationErrorDataSchema.safeParse(error.response.data);
|
|
71
|
-
if (safeParsed.success) {
|
|
72
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
73
|
-
...error.response,
|
|
74
|
-
data: { type: "VALIDATION_ERROR", value: safeParsed.data }
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
79
|
-
...error.response,
|
|
80
|
-
data: { type: "UNKNOWN_ERROR", value: void 0 }
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
return {};
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
export {
|
|
88
|
-
ApiError,
|
|
89
|
-
ErrorHandlerPlugin,
|
|
90
|
-
FormStateError,
|
|
91
|
-
formStateErrorToThrowable,
|
|
92
|
-
throwableToFormState,
|
|
93
|
-
validationErrorDataSchema
|
|
94
|
-
};
|
package/eslint.config.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
// eslint.config.js per @typescript-eslint v8
|
|
3
|
-
import js from '@eslint/js'
|
|
4
|
-
import prettier from 'eslint-config-prettier'
|
|
5
|
-
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort'
|
|
6
|
-
import tsPlugin from '@typescript-eslint/eslint-plugin'
|
|
7
|
-
import tsParser from '@typescript-eslint/parser'
|
|
8
|
-
|
|
9
|
-
export default [
|
|
10
|
-
js.configs.recommended,
|
|
11
|
-
{
|
|
12
|
-
files: ['**/*.ts'],
|
|
13
|
-
languageOptions: {
|
|
14
|
-
parser: tsParser,
|
|
15
|
-
globals: {
|
|
16
|
-
process: 'readonly',
|
|
17
|
-
},
|
|
18
|
-
parserOptions: {
|
|
19
|
-
project: ['./tsconfig.json'],
|
|
20
|
-
tsconfigRootDir: process.cwd(),
|
|
21
|
-
sourceType: 'module',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
plugins: {
|
|
25
|
-
'@typescript-eslint': tsPlugin,
|
|
26
|
-
'simple-import-sort': simpleImportSortPlugin,
|
|
27
|
-
},
|
|
28
|
-
rules: {
|
|
29
|
-
'simple-import-sort/imports': 'error',
|
|
30
|
-
'simple-import-sort/exports': 'error',
|
|
31
|
-
'@typescript-eslint/no-unused-vars': 'warn',
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
prettier,
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
export const ignores = ['dist', 'node_modules']
|
package/src/errors.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AxiosError } from 'axios'
|
|
2
|
-
|
|
3
|
-
import { ValidationErrorData } from './types'
|
|
4
|
-
|
|
5
|
-
export type ApiErrorData =
|
|
6
|
-
| {
|
|
7
|
-
type: 'VALIDATION_ERROR'
|
|
8
|
-
value: ValidationErrorData
|
|
9
|
-
}
|
|
10
|
-
| {
|
|
11
|
-
type: 'UNKNOWN_ERROR'
|
|
12
|
-
value: undefined
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class ApiError extends AxiosError<ApiErrorData> {}
|
package/src/form-state.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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
DELETED
package/src/plugin.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { ApiClient, ApiClientPlugin } from '@deliverart/sdk-js-core'
|
|
2
|
-
import { AxiosError, AxiosResponse } from 'axios'
|
|
3
|
-
|
|
4
|
-
import { ApiError } from './errors'
|
|
5
|
-
import { validationErrorDataSchema } from './types'
|
|
6
|
-
|
|
7
|
-
export class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
|
|
8
|
-
setup(client: ApiClient) {
|
|
9
|
-
client.http.interceptors.response.use(
|
|
10
|
-
(response: AxiosResponse) => response,
|
|
11
|
-
async (error: AxiosError) => {
|
|
12
|
-
if (!error.response) {
|
|
13
|
-
throw new ApiError()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (error.response.status === 422) {
|
|
17
|
-
const safeParsed = validationErrorDataSchema.safeParse(error.response.data)
|
|
18
|
-
|
|
19
|
-
if (safeParsed.success) {
|
|
20
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
21
|
-
...error.response,
|
|
22
|
-
data: { type: 'VALIDATION_ERROR', value: safeParsed.data },
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
throw new ApiError(error.message, error.code, error.config, error.request, {
|
|
28
|
-
...error.response,
|
|
29
|
-
data: { type: 'UNKNOWN_ERROR', value: undefined },
|
|
30
|
-
})
|
|
31
|
-
},
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
return {}
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod'
|
|
2
|
-
|
|
3
|
-
export const validationErrorDataSchema = z.object({
|
|
4
|
-
status: z.number(),
|
|
5
|
-
violations: z.array(
|
|
6
|
-
z.object({
|
|
7
|
-
propertyPath: z.string(),
|
|
8
|
-
message: z.string(),
|
|
9
|
-
code: z.string().nullable(),
|
|
10
|
-
}),
|
|
11
|
-
),
|
|
12
|
-
detail: z.string(),
|
|
13
|
-
type: z.string(),
|
|
14
|
-
title: z.string(),
|
|
15
|
-
})
|
|
16
|
-
export type ValidationErrorData = z.infer<typeof validationErrorDataSchema>
|
package/tsconfig.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "Node",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"forceConsistentCasingInFileNames": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"outDir": "dist",
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"declarationMap": true
|
|
13
|
-
},
|
|
14
|
-
"include": ["src"]
|
|
15
|
-
}
|