@deliverart/sdk-js-error-handler 0.0.6 → 0.0.7

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 ADDED
@@ -0,0 +1,7 @@
1
+ # @deliverart/sdk-js-error-handler
2
+
3
+ ## 0.0.7
4
+
5
+ ### Patch Changes
6
+
7
+ - ba0e953: Add form state utils
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  ApiError: () => ApiError,
24
24
  ErrorHandlerPlugin: () => ErrorHandlerPlugin,
25
+ throwableToFormState: () => throwableToFormState,
25
26
  validationErrorDataSchema: () => validationErrorDataSchema
26
27
  });
27
28
  module.exports = __toCommonJS(index_exports);
@@ -31,6 +32,34 @@ var import_axios = require("axios");
31
32
  var ApiError = class extends import_axios.AxiosError {
32
33
  };
33
34
 
35
+ // src/form-state.ts
36
+ async function throwableToFormState(fn) {
37
+ try {
38
+ const data = await fn();
39
+ return {
40
+ success: true,
41
+ data
42
+ };
43
+ } catch (e) {
44
+ if (e instanceof ApiError) {
45
+ return {
46
+ success: false,
47
+ error: e.response?.data ?? {
48
+ type: "UNKNOWN_ERROR",
49
+ value: void 0
50
+ }
51
+ };
52
+ }
53
+ return {
54
+ success: false,
55
+ error: {
56
+ type: "UNKNOWN_ERROR",
57
+ value: void 0
58
+ }
59
+ };
60
+ }
61
+ }
62
+
34
63
  // src/types.ts
35
64
  var import_zod = require("zod");
36
65
  var validationErrorDataSchema = import_zod.z.object({
@@ -78,5 +107,6 @@ var ErrorHandlerPlugin = class {
78
107
  0 && (module.exports = {
79
108
  ApiError,
80
109
  ErrorHandlerPlugin,
110
+ throwableToFormState,
81
111
  validationErrorDataSchema
82
112
  });
package/dist/index.d.cts CHANGED
@@ -53,8 +53,17 @@ 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 function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>>;
64
+
56
65
  declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
57
66
  setup(client: ApiClient): {};
58
67
  }
59
68
 
60
- export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type ValidationErrorData, validationErrorDataSchema };
69
+ export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, type ValidationErrorData, throwableToFormState, validationErrorDataSchema };
package/dist/index.d.ts CHANGED
@@ -53,8 +53,17 @@ 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 function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>>;
64
+
56
65
  declare class ErrorHandlerPlugin implements ApiClientPlugin<{}> {
57
66
  setup(client: ApiClient): {};
58
67
  }
59
68
 
60
- export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type ValidationErrorData, validationErrorDataSchema };
69
+ export { ApiError, type ApiErrorData, ErrorHandlerPlugin, type FormState, type ValidationErrorData, throwableToFormState, validationErrorDataSchema };
package/dist/index.js CHANGED
@@ -3,6 +3,34 @@ import { AxiosError } from "axios";
3
3
  var ApiError = class extends AxiosError {
4
4
  };
5
5
 
6
+ // src/form-state.ts
7
+ async function throwableToFormState(fn) {
8
+ try {
9
+ const data = await fn();
10
+ return {
11
+ success: true,
12
+ data
13
+ };
14
+ } catch (e) {
15
+ if (e instanceof ApiError) {
16
+ return {
17
+ success: false,
18
+ error: e.response?.data ?? {
19
+ type: "UNKNOWN_ERROR",
20
+ value: void 0
21
+ }
22
+ };
23
+ }
24
+ return {
25
+ success: false,
26
+ error: {
27
+ type: "UNKNOWN_ERROR",
28
+ value: void 0
29
+ }
30
+ };
31
+ }
32
+ }
33
+
6
34
  // src/types.ts
7
35
  import { z } from "zod";
8
36
  var validationErrorDataSchema = z.object({
@@ -49,5 +77,6 @@ var ErrorHandlerPlugin = class {
49
77
  export {
50
78
  ApiError,
51
79
  ErrorHandlerPlugin,
80
+ throwableToFormState,
52
81
  validationErrorDataSchema
53
82
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deliverart/sdk-js-error-handler",
3
3
  "description": "Error handling utilities for Deliverart SDK in JavaScript",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -0,0 +1,39 @@
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 async function throwableToFormState<T>(fn: () => Promise<T>): Promise<FormState<T>> {
14
+ try {
15
+ const data = await fn()
16
+ return {
17
+ success: true,
18
+ data,
19
+ }
20
+ } catch (e) {
21
+ if (e instanceof ApiError) {
22
+ return {
23
+ success: false,
24
+ error: e.response?.data ?? {
25
+ type: 'UNKNOWN_ERROR',
26
+ value: undefined,
27
+ },
28
+ }
29
+ }
30
+
31
+ return {
32
+ success: false,
33
+ error: {
34
+ type: 'UNKNOWN_ERROR',
35
+ value: undefined,
36
+ },
37
+ }
38
+ }
39
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './errors'
2
+ export * from './form-state'
2
3
  export * from './plugin'
3
4
  export * from './types'