@paykit-sdk/react 1.1.96 → 1.1.97

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 CHANGED
@@ -41,10 +41,18 @@ function App() {
41
41
 
42
42
  ```tsx
43
43
  import * as React from 'react';
44
- import { useCustomer, useSubscription, useCheckout } from '@paykit-sdk/react';
44
+ import {
45
+ useCustomer,
46
+ useSubscription,
47
+ useCheckout,
48
+ } from '@paykit-sdk/react';
45
49
  import { CustomerView } from './customer-view';
46
50
 
47
- const CustomerDashboard = ({ customerId }: { customerId: string }) => {
51
+ const CustomerDashboard = ({
52
+ customerId,
53
+ }: {
54
+ customerId: string;
55
+ }) => {
48
56
  const { retrieve, create, update } = useCustomer();
49
57
 
50
58
  React.useEffect(() => {
@@ -71,9 +79,7 @@ Works with any PayKit provider including Stripe, Polar, Gumroad, and more.
71
79
 
72
80
  ## Documentation
73
81
 
74
- - [React Documentation](https://www.usepaykit.dev/docs/concepts/client-side-usage)
75
82
  - [PayKit Documentation](https://usepaykit.dev/docs)
76
- - [Framework Examples](https://usepaykit.dev/docs/framework-examples)
77
83
 
78
84
  ## TypeScript Support
79
85
 
@@ -1,7 +1,9 @@
1
1
  import { EndpointPath } from '@paykit-sdk/core';
2
+ import { PayKitClientError } from '../util.mjs';
2
3
 
3
- type AsyncResult<T> = [data: T, error: undefined] | [data: undefined, error: Error];
4
- declare const useAsyncFn: <Args extends unknown[], Response>(path: EndpointPath, apiUrl: string, headersEsque?: Record<string, string> | (() => Record<string, string>)) => {
4
+ type AsyncResult<T> = [data: T, error: undefined] | [data: undefined, error: PayKitClientError];
5
+ type HeadersEsque = Record<string, string> | (() => Record<string, string>);
6
+ declare const useAsyncFn: <Args extends unknown[], Response>(path: EndpointPath, apiUrl: string, headersEsque?: HeadersEsque) => {
5
7
  run: (...args: Args) => Promise<AsyncResult<Response>>;
6
8
  loading: boolean;
7
9
  };
@@ -1,7 +1,9 @@
1
1
  import { EndpointPath } from '@paykit-sdk/core';
2
+ import { PayKitClientError } from '../util.js';
2
3
 
3
- type AsyncResult<T> = [data: T, error: undefined] | [data: undefined, error: Error];
4
- declare const useAsyncFn: <Args extends unknown[], Response>(path: EndpointPath, apiUrl: string, headersEsque?: Record<string, string> | (() => Record<string, string>)) => {
4
+ type AsyncResult<T> = [data: T, error: undefined] | [data: undefined, error: PayKitClientError];
5
+ type HeadersEsque = Record<string, string> | (() => Record<string, string>);
6
+ declare const useAsyncFn: <Args extends unknown[], Response>(path: EndpointPath, apiUrl: string, headersEsque?: HeadersEsque) => {
5
7
  run: (...args: Args) => Promise<AsyncResult<Response>>;
6
8
  loading: boolean;
7
9
  };
@@ -22,6 +22,41 @@ function _interopNamespace(e) {
22
22
 
23
23
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
24
24
 
25
+ // src/hooks/use-async-fn.ts
26
+
27
+ // src/util.ts
28
+ var PayKitClientError = class extends Error {
29
+ constructor(errorData) {
30
+ super(errorData.message);
31
+ this.name = "PayKitClientError";
32
+ this.code = errorData.code;
33
+ this.statusCode = errorData.statusCode;
34
+ this.provider = errorData.provider;
35
+ this.method = errorData.method;
36
+ this.context = errorData.context;
37
+ }
38
+ };
39
+ var parsePayKitClientError = (response, errorData) => {
40
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
41
+ const data = errorData;
42
+ return new PayKitClientError({
43
+ message: data.message,
44
+ code: data.code,
45
+ statusCode: data.statusCode || response.status,
46
+ provider: data.provider,
47
+ method: data.method,
48
+ context: data.context
49
+ });
50
+ }
51
+ return new PayKitClientError({
52
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
53
+ statusCode: response.status,
54
+ provider: errorData == null ? void 0 : errorData.provider,
55
+ method: errorData == null ? void 0 : errorData.method,
56
+ context: errorData == null ? void 0 : errorData.context
57
+ });
58
+ };
59
+
25
60
  // src/hooks/use-async-fn.ts
26
61
  var useAsyncFn = (path, apiUrl, headersEsque) => {
27
62
  const [loading, setLoading] = React__namespace.useState(false);
@@ -30,22 +65,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
30
65
  setLoading(true);
31
66
  try {
32
67
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
33
- const response = await fetch(`${apiUrl}${path}`, {
68
+ const res = await fetch(`${apiUrl}${path}`, {
34
69
  method: "POST",
35
70
  headers: { "Content-Type": "application/json", ...headers },
36
71
  credentials: "include",
37
72
  body: JSON.stringify({ args })
38
73
  });
39
- if (!response.ok) {
40
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
41
- throw new Error(errorData.message || `HTTP ${response.status}`);
74
+ if (!res.ok) {
75
+ const errorData = await res.json().catch(() => ({
76
+ message: res.statusText || "Request failed"
77
+ }));
78
+ throw parsePayKitClientError(res, errorData);
42
79
  }
43
- const data = await response.json();
44
- setLoading(false);
45
- return [data.result, void 0];
46
- } catch (error) {
80
+ const response = await res.json();
81
+ if (typeof response === "object" && "result" in response) {
82
+ return [response.result, void 0];
83
+ }
84
+ throw new PayKitClientError({
85
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
86
+ statusCode: 500,
87
+ context: {
88
+ response: JSON.stringify(response, null, 2)
89
+ }
90
+ });
91
+ } catch (err) {
92
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
93
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
94
+ statusCode: err instanceof Error ? 0 : 500
95
+ });
96
+ return [void 0, error];
97
+ } finally {
47
98
  setLoading(false);
48
- return [void 0, error instanceof Error ? error : new Error(String(error))];
49
99
  }
50
100
  },
51
101
  [path, apiUrl, headersEsque]
@@ -1,5 +1,40 @@
1
1
  import * as React from 'react';
2
2
 
3
+ // src/hooks/use-async-fn.ts
4
+
5
+ // src/util.ts
6
+ var PayKitClientError = class extends Error {
7
+ constructor(errorData) {
8
+ super(errorData.message);
9
+ this.name = "PayKitClientError";
10
+ this.code = errorData.code;
11
+ this.statusCode = errorData.statusCode;
12
+ this.provider = errorData.provider;
13
+ this.method = errorData.method;
14
+ this.context = errorData.context;
15
+ }
16
+ };
17
+ var parsePayKitClientError = (response, errorData) => {
18
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
19
+ const data = errorData;
20
+ return new PayKitClientError({
21
+ message: data.message,
22
+ code: data.code,
23
+ statusCode: data.statusCode || response.status,
24
+ provider: data.provider,
25
+ method: data.method,
26
+ context: data.context
27
+ });
28
+ }
29
+ return new PayKitClientError({
30
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
31
+ statusCode: response.status,
32
+ provider: errorData == null ? void 0 : errorData.provider,
33
+ method: errorData == null ? void 0 : errorData.method,
34
+ context: errorData == null ? void 0 : errorData.context
35
+ });
36
+ };
37
+
3
38
  // src/hooks/use-async-fn.ts
4
39
  var useAsyncFn = (path, apiUrl, headersEsque) => {
5
40
  const [loading, setLoading] = React.useState(false);
@@ -8,22 +43,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
8
43
  setLoading(true);
9
44
  try {
10
45
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
11
- const response = await fetch(`${apiUrl}${path}`, {
46
+ const res = await fetch(`${apiUrl}${path}`, {
12
47
  method: "POST",
13
48
  headers: { "Content-Type": "application/json", ...headers },
14
49
  credentials: "include",
15
50
  body: JSON.stringify({ args })
16
51
  });
17
- if (!response.ok) {
18
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
19
- throw new Error(errorData.message || `HTTP ${response.status}`);
52
+ if (!res.ok) {
53
+ const errorData = await res.json().catch(() => ({
54
+ message: res.statusText || "Request failed"
55
+ }));
56
+ throw parsePayKitClientError(res, errorData);
20
57
  }
21
- const data = await response.json();
22
- setLoading(false);
23
- return [data.result, void 0];
24
- } catch (error) {
58
+ const response = await res.json();
59
+ if (typeof response === "object" && "result" in response) {
60
+ return [response.result, void 0];
61
+ }
62
+ throw new PayKitClientError({
63
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
64
+ statusCode: 500,
65
+ context: {
66
+ response: JSON.stringify(response, null, 2)
67
+ }
68
+ });
69
+ } catch (err) {
70
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
71
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
72
+ statusCode: err instanceof Error ? 0 : 500
73
+ });
74
+ return [void 0, error];
75
+ } finally {
25
76
  setLoading(false);
26
- return [void 0, error instanceof Error ? error : new Error(String(error))];
27
77
  }
28
78
  },
29
79
  [path, apiUrl, headersEsque]
package/dist/index.d.mts CHANGED
@@ -6,12 +6,13 @@ export { useCheckout } from './resources/checkout.mjs';
6
6
  export { useRefund } from './resources/refund.mjs';
7
7
  export { usePayment } from './resources/payment.mjs';
8
8
  import '@paykit-sdk/core';
9
+ import './util.mjs';
9
10
 
10
11
  interface PaykitProviderProps extends React.PropsWithChildren {
11
12
  apiUrl: string;
12
13
  headers?: Record<string, string> | (() => Record<string, string>);
13
14
  }
14
- declare const PaykitProvider: ({ apiUrl, headers, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
15
+ declare const PaykitProvider: ({ apiUrl, headers, children, }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
15
16
  declare const usePaykitContext: () => PaykitProviderProps;
16
17
 
17
18
  export { PaykitProvider, usePaykitContext };
package/dist/index.d.ts CHANGED
@@ -6,12 +6,13 @@ export { useCheckout } from './resources/checkout.js';
6
6
  export { useRefund } from './resources/refund.js';
7
7
  export { usePayment } from './resources/payment.js';
8
8
  import '@paykit-sdk/core';
9
+ import './util.js';
9
10
 
10
11
  interface PaykitProviderProps extends React.PropsWithChildren {
11
12
  apiUrl: string;
12
13
  headers?: Record<string, string> | (() => Record<string, string>);
13
14
  }
14
- declare const PaykitProvider: ({ apiUrl, headers, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
15
+ declare const PaykitProvider: ({ apiUrl, headers, children, }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
15
16
  declare const usePaykitContext: () => PaykitProviderProps;
16
17
 
17
18
  export { PaykitProvider, usePaykitContext };
package/dist/index.js CHANGED
@@ -25,16 +25,60 @@ var React__namespace = /*#__PURE__*/_interopNamespace(React);
25
25
 
26
26
  // src/context.tsx
27
27
  var PaykitContext = React__namespace.createContext(void 0);
28
- var PaykitProvider = ({ apiUrl, headers, children }) => {
29
- const value = React__namespace.useMemo(() => ({ apiUrl, headers }), [apiUrl, headers]);
28
+ var PaykitProvider = ({
29
+ apiUrl,
30
+ headers,
31
+ children
32
+ }) => {
33
+ const value = React__namespace.useMemo(
34
+ () => ({ apiUrl, headers }),
35
+ [apiUrl, headers]
36
+ );
30
37
  return /* @__PURE__ */ jsxRuntime.jsx(PaykitContext.Provider, { value, children });
31
38
  };
32
39
  var usePaykitContext = () => {
33
40
  const ctx = React__namespace.useContext(PaykitContext);
34
41
  if (!ctx)
35
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
42
+ throw new Error(
43
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
44
+ );
36
45
  return ctx;
37
46
  };
47
+
48
+ // src/util.ts
49
+ var PayKitClientError = class extends Error {
50
+ constructor(errorData) {
51
+ super(errorData.message);
52
+ this.name = "PayKitClientError";
53
+ this.code = errorData.code;
54
+ this.statusCode = errorData.statusCode;
55
+ this.provider = errorData.provider;
56
+ this.method = errorData.method;
57
+ this.context = errorData.context;
58
+ }
59
+ };
60
+ var parsePayKitClientError = (response, errorData) => {
61
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
62
+ const data = errorData;
63
+ return new PayKitClientError({
64
+ message: data.message,
65
+ code: data.code,
66
+ statusCode: data.statusCode || response.status,
67
+ provider: data.provider,
68
+ method: data.method,
69
+ context: data.context
70
+ });
71
+ }
72
+ return new PayKitClientError({
73
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
74
+ statusCode: response.status,
75
+ provider: errorData == null ? void 0 : errorData.provider,
76
+ method: errorData == null ? void 0 : errorData.method,
77
+ context: errorData == null ? void 0 : errorData.context
78
+ });
79
+ };
80
+
81
+ // src/hooks/use-async-fn.ts
38
82
  var useAsyncFn = (path, apiUrl, headersEsque) => {
39
83
  const [loading, setLoading] = React__namespace.useState(false);
40
84
  const run = React__namespace.useCallback(
@@ -42,22 +86,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
42
86
  setLoading(true);
43
87
  try {
44
88
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
45
- const response = await fetch(`${apiUrl}${path}`, {
89
+ const res = await fetch(`${apiUrl}${path}`, {
46
90
  method: "POST",
47
91
  headers: { "Content-Type": "application/json", ...headers },
48
92
  credentials: "include",
49
93
  body: JSON.stringify({ args })
50
94
  });
51
- if (!response.ok) {
52
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
53
- throw new Error(errorData.message || `HTTP ${response.status}`);
95
+ if (!res.ok) {
96
+ const errorData = await res.json().catch(() => ({
97
+ message: res.statusText || "Request failed"
98
+ }));
99
+ throw parsePayKitClientError(res, errorData);
54
100
  }
55
- const data = await response.json();
56
- setLoading(false);
57
- return [data.result, void 0];
58
- } catch (error) {
101
+ const response = await res.json();
102
+ if (typeof response === "object" && "result" in response) {
103
+ return [response.result, void 0];
104
+ }
105
+ throw new PayKitClientError({
106
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
107
+ statusCode: 500,
108
+ context: {
109
+ response: JSON.stringify(response, null, 2)
110
+ }
111
+ });
112
+ } catch (err) {
113
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
114
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
115
+ statusCode: err instanceof Error ? 0 : 500
116
+ });
117
+ return [void 0, error];
118
+ } finally {
59
119
  setLoading(false);
60
- return [void 0, error instanceof Error ? error : new Error(String(error))];
61
120
  }
62
121
  },
63
122
  [path, apiUrl, headersEsque]
package/dist/index.mjs CHANGED
@@ -3,16 +3,60 @@ import { jsx } from 'react/jsx-runtime';
3
3
 
4
4
  // src/context.tsx
5
5
  var PaykitContext = React.createContext(void 0);
6
- var PaykitProvider = ({ apiUrl, headers, children }) => {
7
- const value = React.useMemo(() => ({ apiUrl, headers }), [apiUrl, headers]);
6
+ var PaykitProvider = ({
7
+ apiUrl,
8
+ headers,
9
+ children
10
+ }) => {
11
+ const value = React.useMemo(
12
+ () => ({ apiUrl, headers }),
13
+ [apiUrl, headers]
14
+ );
8
15
  return /* @__PURE__ */ jsx(PaykitContext.Provider, { value, children });
9
16
  };
10
17
  var usePaykitContext = () => {
11
18
  const ctx = React.useContext(PaykitContext);
12
19
  if (!ctx)
13
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
20
+ throw new Error(
21
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
22
+ );
14
23
  return ctx;
15
24
  };
25
+
26
+ // src/util.ts
27
+ var PayKitClientError = class extends Error {
28
+ constructor(errorData) {
29
+ super(errorData.message);
30
+ this.name = "PayKitClientError";
31
+ this.code = errorData.code;
32
+ this.statusCode = errorData.statusCode;
33
+ this.provider = errorData.provider;
34
+ this.method = errorData.method;
35
+ this.context = errorData.context;
36
+ }
37
+ };
38
+ var parsePayKitClientError = (response, errorData) => {
39
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
40
+ const data = errorData;
41
+ return new PayKitClientError({
42
+ message: data.message,
43
+ code: data.code,
44
+ statusCode: data.statusCode || response.status,
45
+ provider: data.provider,
46
+ method: data.method,
47
+ context: data.context
48
+ });
49
+ }
50
+ return new PayKitClientError({
51
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
52
+ statusCode: response.status,
53
+ provider: errorData == null ? void 0 : errorData.provider,
54
+ method: errorData == null ? void 0 : errorData.method,
55
+ context: errorData == null ? void 0 : errorData.context
56
+ });
57
+ };
58
+
59
+ // src/hooks/use-async-fn.ts
16
60
  var useAsyncFn = (path, apiUrl, headersEsque) => {
17
61
  const [loading, setLoading] = React.useState(false);
18
62
  const run = React.useCallback(
@@ -20,22 +64,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
20
64
  setLoading(true);
21
65
  try {
22
66
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
23
- const response = await fetch(`${apiUrl}${path}`, {
67
+ const res = await fetch(`${apiUrl}${path}`, {
24
68
  method: "POST",
25
69
  headers: { "Content-Type": "application/json", ...headers },
26
70
  credentials: "include",
27
71
  body: JSON.stringify({ args })
28
72
  });
29
- if (!response.ok) {
30
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
31
- throw new Error(errorData.message || `HTTP ${response.status}`);
73
+ if (!res.ok) {
74
+ const errorData = await res.json().catch(() => ({
75
+ message: res.statusText || "Request failed"
76
+ }));
77
+ throw parsePayKitClientError(res, errorData);
32
78
  }
33
- const data = await response.json();
34
- setLoading(false);
35
- return [data.result, void 0];
36
- } catch (error) {
79
+ const response = await res.json();
80
+ if (typeof response === "object" && "result" in response) {
81
+ return [response.result, void 0];
82
+ }
83
+ throw new PayKitClientError({
84
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
85
+ statusCode: 500,
86
+ context: {
87
+ response: JSON.stringify(response, null, 2)
88
+ }
89
+ });
90
+ } catch (err) {
91
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
92
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
93
+ statusCode: err instanceof Error ? 0 : 500
94
+ });
95
+ return [void 0, error];
96
+ } finally {
37
97
  setLoading(false);
38
- return [void 0, error instanceof Error ? error : new Error(String(error))];
39
98
  }
40
99
  },
41
100
  [path, apiUrl, headersEsque]
@@ -1,20 +1,21 @@
1
+ import { PayKitClientError } from '../util.mjs';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
 
3
4
  declare const useCheckout: () => {
4
5
  create: {
5
- run: (params: _paykit_sdk_core.CreateCheckoutSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
6
+ run: (params: _paykit_sdk_core.CreateCheckoutSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  retrieve: {
9
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout | null, error: undefined]>;
10
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout | null, error: undefined]>;
10
11
  loading: boolean;
11
12
  };
12
13
  update: {
13
- run: (id: string, params: _paykit_sdk_core.UpdateCheckoutSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
14
+ run: (id: string, params: _paykit_sdk_core.UpdateCheckoutSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
14
15
  loading: boolean;
15
16
  };
16
17
  remove: {
17
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
18
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: null, error: undefined]>;
18
19
  loading: boolean;
19
20
  };
20
21
  };
@@ -1,20 +1,21 @@
1
+ import { PayKitClientError } from '../util.js';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
 
3
4
  declare const useCheckout: () => {
4
5
  create: {
5
- run: (params: _paykit_sdk_core.CreateCheckoutSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
6
+ run: (params: _paykit_sdk_core.CreateCheckoutSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  retrieve: {
9
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout | null, error: undefined]>;
10
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout | null, error: undefined]>;
10
11
  loading: boolean;
11
12
  };
12
13
  update: {
13
- run: (id: string, params: _paykit_sdk_core.UpdateCheckoutSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
14
+ run: (id: string, params: _paykit_sdk_core.UpdateCheckoutSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Checkout, error: undefined]>;
14
15
  loading: boolean;
15
16
  };
16
17
  remove: {
17
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
18
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: null, error: undefined]>;
18
19
  loading: boolean;
19
20
  };
20
21
  };
@@ -28,9 +28,46 @@ var PaykitContext = React__namespace.createContext(void 0);
28
28
  var usePaykitContext = () => {
29
29
  const ctx = React__namespace.useContext(PaykitContext);
30
30
  if (!ctx)
31
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
31
+ throw new Error(
32
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
33
+ );
32
34
  return ctx;
33
35
  };
36
+
37
+ // src/util.ts
38
+ var PayKitClientError = class extends Error {
39
+ constructor(errorData) {
40
+ super(errorData.message);
41
+ this.name = "PayKitClientError";
42
+ this.code = errorData.code;
43
+ this.statusCode = errorData.statusCode;
44
+ this.provider = errorData.provider;
45
+ this.method = errorData.method;
46
+ this.context = errorData.context;
47
+ }
48
+ };
49
+ var parsePayKitClientError = (response, errorData) => {
50
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
51
+ const data = errorData;
52
+ return new PayKitClientError({
53
+ message: data.message,
54
+ code: data.code,
55
+ statusCode: data.statusCode || response.status,
56
+ provider: data.provider,
57
+ method: data.method,
58
+ context: data.context
59
+ });
60
+ }
61
+ return new PayKitClientError({
62
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
63
+ statusCode: response.status,
64
+ provider: errorData == null ? void 0 : errorData.provider,
65
+ method: errorData == null ? void 0 : errorData.method,
66
+ context: errorData == null ? void 0 : errorData.context
67
+ });
68
+ };
69
+
70
+ // src/hooks/use-async-fn.ts
34
71
  var useAsyncFn = (path, apiUrl, headersEsque) => {
35
72
  const [loading, setLoading] = React__namespace.useState(false);
36
73
  const run = React__namespace.useCallback(
@@ -38,22 +75,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
38
75
  setLoading(true);
39
76
  try {
40
77
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
41
- const response = await fetch(`${apiUrl}${path}`, {
78
+ const res = await fetch(`${apiUrl}${path}`, {
42
79
  method: "POST",
43
80
  headers: { "Content-Type": "application/json", ...headers },
44
81
  credentials: "include",
45
82
  body: JSON.stringify({ args })
46
83
  });
47
- if (!response.ok) {
48
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
49
- throw new Error(errorData.message || `HTTP ${response.status}`);
84
+ if (!res.ok) {
85
+ const errorData = await res.json().catch(() => ({
86
+ message: res.statusText || "Request failed"
87
+ }));
88
+ throw parsePayKitClientError(res, errorData);
50
89
  }
51
- const data = await response.json();
52
- setLoading(false);
53
- return [data.result, void 0];
54
- } catch (error) {
90
+ const response = await res.json();
91
+ if (typeof response === "object" && "result" in response) {
92
+ return [response.result, void 0];
93
+ }
94
+ throw new PayKitClientError({
95
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
96
+ statusCode: 500,
97
+ context: {
98
+ response: JSON.stringify(response, null, 2)
99
+ }
100
+ });
101
+ } catch (err) {
102
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
103
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
104
+ statusCode: err instanceof Error ? 0 : 500
105
+ });
106
+ return [void 0, error];
107
+ } finally {
55
108
  setLoading(false);
56
- return [void 0, error instanceof Error ? error : new Error(String(error))];
57
109
  }
58
110
  },
59
111
  [path, apiUrl, headersEsque]