@paykit-sdk/react 1.1.95 → 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.
@@ -6,9 +6,46 @@ var PaykitContext = React.createContext(void 0);
6
6
  var usePaykitContext = () => {
7
7
  const ctx = React.useContext(PaykitContext);
8
8
  if (!ctx)
9
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
9
+ throw new Error(
10
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
11
+ );
10
12
  return ctx;
11
13
  };
14
+
15
+ // src/util.ts
16
+ var PayKitClientError = class extends Error {
17
+ constructor(errorData) {
18
+ super(errorData.message);
19
+ this.name = "PayKitClientError";
20
+ this.code = errorData.code;
21
+ this.statusCode = errorData.statusCode;
22
+ this.provider = errorData.provider;
23
+ this.method = errorData.method;
24
+ this.context = errorData.context;
25
+ }
26
+ };
27
+ var parsePayKitClientError = (response, errorData) => {
28
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
29
+ const data = errorData;
30
+ return new PayKitClientError({
31
+ message: data.message,
32
+ code: data.code,
33
+ statusCode: data.statusCode || response.status,
34
+ provider: data.provider,
35
+ method: data.method,
36
+ context: data.context
37
+ });
38
+ }
39
+ return new PayKitClientError({
40
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
41
+ statusCode: response.status,
42
+ provider: errorData == null ? void 0 : errorData.provider,
43
+ method: errorData == null ? void 0 : errorData.method,
44
+ context: errorData == null ? void 0 : errorData.context
45
+ });
46
+ };
47
+
48
+ // src/hooks/use-async-fn.ts
12
49
  var useAsyncFn = (path, apiUrl, headersEsque) => {
13
50
  const [loading, setLoading] = React.useState(false);
14
51
  const run = React.useCallback(
@@ -16,22 +53,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
16
53
  setLoading(true);
17
54
  try {
18
55
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
19
- const response = await fetch(`${apiUrl}${path}`, {
56
+ const res = await fetch(`${apiUrl}${path}`, {
20
57
  method: "POST",
21
58
  headers: { "Content-Type": "application/json", ...headers },
22
59
  credentials: "include",
23
60
  body: JSON.stringify({ args })
24
61
  });
25
- if (!response.ok) {
26
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
27
- throw new Error(errorData.message || `HTTP ${response.status}`);
62
+ if (!res.ok) {
63
+ const errorData = await res.json().catch(() => ({
64
+ message: res.statusText || "Request failed"
65
+ }));
66
+ throw parsePayKitClientError(res, errorData);
28
67
  }
29
- const data = await response.json();
30
- setLoading(false);
31
- return [data.result, void 0];
32
- } catch (error) {
68
+ const response = await res.json();
69
+ if (typeof response === "object" && "result" in response) {
70
+ return [response.result, void 0];
71
+ }
72
+ throw new PayKitClientError({
73
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
74
+ statusCode: 500,
75
+ context: {
76
+ response: JSON.stringify(response, null, 2)
77
+ }
78
+ });
79
+ } catch (err) {
80
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
81
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
82
+ statusCode: err instanceof Error ? 0 : 500
83
+ });
84
+ return [void 0, error];
85
+ } finally {
33
86
  setLoading(false);
34
- return [void 0, error instanceof Error ? error : new Error(String(error))];
35
87
  }
36
88
  },
37
89
  [path, apiUrl, headersEsque]
@@ -1,20 +1,21 @@
1
1
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
+ import { PayKitClientError } from '../util.mjs';
2
3
 
3
4
  declare const useCustomer: () => {
4
5
  retrieve: {
5
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer | null, error: undefined]>;
6
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer | null, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  create: {
9
- run: (params: _paykit_sdk_core.CreateCustomerParams) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer, error: undefined]>;
10
+ run: (params: _paykit_sdk_core.CreateCustomerParams<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer, error: undefined]>;
10
11
  loading: boolean;
11
12
  };
12
13
  update: {
13
- run: (id: string, params: _paykit_sdk_core.UpdateCustomerParams) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer, error: undefined]>;
14
+ run: (id: string, params: _paykit_sdk_core.UpdateCustomerParams<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer, 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
1
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
+ import { PayKitClientError } from '../util.js';
2
3
 
3
4
  declare const useCustomer: () => {
4
5
  retrieve: {
5
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer | null, error: undefined]>;
6
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer | null, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  create: {
9
- run: (params: _paykit_sdk_core.CreateCustomerParams) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer, error: undefined]>;
10
+ run: (params: _paykit_sdk_core.CreateCustomerParams<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer, error: undefined]>;
10
11
  loading: boolean;
11
12
  };
12
13
  update: {
13
- run: (id: string, params: _paykit_sdk_core.UpdateCustomerParams) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Customer, error: undefined]>;
14
+ run: (id: string, params: _paykit_sdk_core.UpdateCustomerParams<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Customer, 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]
@@ -6,9 +6,46 @@ var PaykitContext = React.createContext(void 0);
6
6
  var usePaykitContext = () => {
7
7
  const ctx = React.useContext(PaykitContext);
8
8
  if (!ctx)
9
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
9
+ throw new Error(
10
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
11
+ );
10
12
  return ctx;
11
13
  };
14
+
15
+ // src/util.ts
16
+ var PayKitClientError = class extends Error {
17
+ constructor(errorData) {
18
+ super(errorData.message);
19
+ this.name = "PayKitClientError";
20
+ this.code = errorData.code;
21
+ this.statusCode = errorData.statusCode;
22
+ this.provider = errorData.provider;
23
+ this.method = errorData.method;
24
+ this.context = errorData.context;
25
+ }
26
+ };
27
+ var parsePayKitClientError = (response, errorData) => {
28
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
29
+ const data = errorData;
30
+ return new PayKitClientError({
31
+ message: data.message,
32
+ code: data.code,
33
+ statusCode: data.statusCode || response.status,
34
+ provider: data.provider,
35
+ method: data.method,
36
+ context: data.context
37
+ });
38
+ }
39
+ return new PayKitClientError({
40
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
41
+ statusCode: response.status,
42
+ provider: errorData == null ? void 0 : errorData.provider,
43
+ method: errorData == null ? void 0 : errorData.method,
44
+ context: errorData == null ? void 0 : errorData.context
45
+ });
46
+ };
47
+
48
+ // src/hooks/use-async-fn.ts
12
49
  var useAsyncFn = (path, apiUrl, headersEsque) => {
13
50
  const [loading, setLoading] = React.useState(false);
14
51
  const run = React.useCallback(
@@ -16,22 +53,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
16
53
  setLoading(true);
17
54
  try {
18
55
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
19
- const response = await fetch(`${apiUrl}${path}`, {
56
+ const res = await fetch(`${apiUrl}${path}`, {
20
57
  method: "POST",
21
58
  headers: { "Content-Type": "application/json", ...headers },
22
59
  credentials: "include",
23
60
  body: JSON.stringify({ args })
24
61
  });
25
- if (!response.ok) {
26
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
27
- throw new Error(errorData.message || `HTTP ${response.status}`);
62
+ if (!res.ok) {
63
+ const errorData = await res.json().catch(() => ({
64
+ message: res.statusText || "Request failed"
65
+ }));
66
+ throw parsePayKitClientError(res, errorData);
28
67
  }
29
- const data = await response.json();
30
- setLoading(false);
31
- return [data.result, void 0];
32
- } catch (error) {
68
+ const response = await res.json();
69
+ if (typeof response === "object" && "result" in response) {
70
+ return [response.result, void 0];
71
+ }
72
+ throw new PayKitClientError({
73
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
74
+ statusCode: 500,
75
+ context: {
76
+ response: JSON.stringify(response, null, 2)
77
+ }
78
+ });
79
+ } catch (err) {
80
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
81
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
82
+ statusCode: err instanceof Error ? 0 : 500
83
+ });
84
+ return [void 0, error];
85
+ } finally {
33
86
  setLoading(false);
34
- return [void 0, error instanceof Error ? error : new Error(String(error))];
35
87
  }
36
88
  },
37
89
  [path, apiUrl, headersEsque]
@@ -1,29 +1,30 @@
1
+ import { PayKitClientError } from '../util.mjs';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
  import { Payment } from '@paykit-sdk/core';
3
4
 
4
5
  declare const usePayment: () => {
5
6
  create: {
6
- run: (params: _paykit_sdk_core.CreatePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
7
+ run: (params: _paykit_sdk_core.CreatePaymentSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
7
8
  loading: boolean;
8
9
  };
9
10
  retrieve: {
10
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: Payment | null, error: undefined]>;
11
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment | null, error: undefined]>;
11
12
  loading: boolean;
12
13
  };
13
14
  update: {
14
- run: (id: string, params: _paykit_sdk_core.UpdatePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
15
+ run: (id: string, params: _paykit_sdk_core.UpdatePaymentSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
15
16
  loading: boolean;
16
17
  };
17
18
  remove: {
18
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
19
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: null, error: undefined]>;
19
20
  loading: boolean;
20
21
  };
21
22
  capture: {
22
- run: (id: string, params: _paykit_sdk_core.CapturePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
23
+ run: (id: string, params: _paykit_sdk_core.CapturePaymentSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
23
24
  loading: boolean;
24
25
  };
25
26
  cancel: {
26
- run: (args_0: string) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
27
+ run: (args_0: string) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
27
28
  loading: boolean;
28
29
  };
29
30
  };
@@ -1,29 +1,30 @@
1
+ import { PayKitClientError } from '../util.js';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
  import { Payment } from '@paykit-sdk/core';
3
4
 
4
5
  declare const usePayment: () => {
5
6
  create: {
6
- run: (params: _paykit_sdk_core.CreatePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
7
+ run: (params: _paykit_sdk_core.CreatePaymentSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
7
8
  loading: boolean;
8
9
  };
9
10
  retrieve: {
10
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: Payment | null, error: undefined]>;
11
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment | null, error: undefined]>;
11
12
  loading: boolean;
12
13
  };
13
14
  update: {
14
- run: (id: string, params: _paykit_sdk_core.UpdatePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
15
+ run: (id: string, params: _paykit_sdk_core.UpdatePaymentSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
15
16
  loading: boolean;
16
17
  };
17
18
  remove: {
18
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
19
+ run: (id: string) => Promise<[data: undefined, error: PayKitClientError] | [data: null, error: undefined]>;
19
20
  loading: boolean;
20
21
  };
21
22
  capture: {
22
- run: (id: string, params: _paykit_sdk_core.CapturePaymentSchema) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
23
+ run: (id: string, params: _paykit_sdk_core.CapturePaymentSchema) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
23
24
  loading: boolean;
24
25
  };
25
26
  cancel: {
26
- run: (args_0: string) => Promise<[data: undefined, error: Error] | [data: Payment, error: undefined]>;
27
+ run: (args_0: string) => Promise<[data: undefined, error: PayKitClientError] | [data: Payment, error: undefined]>;
27
28
  loading: boolean;
28
29
  };
29
30
  };
@@ -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]
@@ -6,9 +6,46 @@ var PaykitContext = React.createContext(void 0);
6
6
  var usePaykitContext = () => {
7
7
  const ctx = React.useContext(PaykitContext);
8
8
  if (!ctx)
9
- throw new Error("Your app must be wrapped in PayKitProvider to use PayKit hooks.");
9
+ throw new Error(
10
+ "Your app must be wrapped in PayKitProvider to use PayKit hooks."
11
+ );
10
12
  return ctx;
11
13
  };
14
+
15
+ // src/util.ts
16
+ var PayKitClientError = class extends Error {
17
+ constructor(errorData) {
18
+ super(errorData.message);
19
+ this.name = "PayKitClientError";
20
+ this.code = errorData.code;
21
+ this.statusCode = errorData.statusCode;
22
+ this.provider = errorData.provider;
23
+ this.method = errorData.method;
24
+ this.context = errorData.context;
25
+ }
26
+ };
27
+ var parsePayKitClientError = (response, errorData) => {
28
+ if (errorData && typeof errorData === "object" && "message" in errorData) {
29
+ const data = errorData;
30
+ return new PayKitClientError({
31
+ message: data.message,
32
+ code: data.code,
33
+ statusCode: data.statusCode || response.status,
34
+ provider: data.provider,
35
+ method: data.method,
36
+ context: data.context
37
+ });
38
+ }
39
+ return new PayKitClientError({
40
+ message: typeof errorData === "string" ? errorData : (errorData == null ? void 0 : errorData.message) || `HTTP ${response.status}: ${response.statusText}`,
41
+ statusCode: response.status,
42
+ provider: errorData == null ? void 0 : errorData.provider,
43
+ method: errorData == null ? void 0 : errorData.method,
44
+ context: errorData == null ? void 0 : errorData.context
45
+ });
46
+ };
47
+
48
+ // src/hooks/use-async-fn.ts
12
49
  var useAsyncFn = (path, apiUrl, headersEsque) => {
13
50
  const [loading, setLoading] = React.useState(false);
14
51
  const run = React.useCallback(
@@ -16,22 +53,37 @@ var useAsyncFn = (path, apiUrl, headersEsque) => {
16
53
  setLoading(true);
17
54
  try {
18
55
  const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
19
- const response = await fetch(`${apiUrl}${path}`, {
56
+ const res = await fetch(`${apiUrl}${path}`, {
20
57
  method: "POST",
21
58
  headers: { "Content-Type": "application/json", ...headers },
22
59
  credentials: "include",
23
60
  body: JSON.stringify({ args })
24
61
  });
25
- if (!response.ok) {
26
- const errorData = await response.json().catch(() => ({ message: "Request failed" }));
27
- throw new Error(errorData.message || `HTTP ${response.status}`);
62
+ if (!res.ok) {
63
+ const errorData = await res.json().catch(() => ({
64
+ message: res.statusText || "Request failed"
65
+ }));
66
+ throw parsePayKitClientError(res, errorData);
28
67
  }
29
- const data = await response.json();
30
- setLoading(false);
31
- return [data.result, void 0];
32
- } catch (error) {
68
+ const response = await res.json();
69
+ if (typeof response === "object" && "result" in response) {
70
+ return [response.result, void 0];
71
+ }
72
+ throw new PayKitClientError({
73
+ message: "Unknown response format (API must return JSON with a top-level 'result' property)",
74
+ statusCode: 500,
75
+ context: {
76
+ response: JSON.stringify(response, null, 2)
77
+ }
78
+ });
79
+ } catch (err) {
80
+ const error = err instanceof PayKitClientError ? err : new PayKitClientError({
81
+ message: err instanceof Error ? err.message : "An unexpected error occurred",
82
+ statusCode: err instanceof Error ? 0 : 500
83
+ });
84
+ return [void 0, error];
85
+ } finally {
33
86
  setLoading(false);
34
- return [void 0, error instanceof Error ? error : new Error(String(error))];
35
87
  }
36
88
  },
37
89
  [path, apiUrl, headersEsque]
@@ -1,8 +1,9 @@
1
+ import { PayKitClientError } from '../util.mjs';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
 
3
4
  declare const useRefund: () => {
4
5
  create: {
5
- run: (params: _paykit_sdk_core.CreateRefundSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Refund, error: undefined]>;
6
+ run: (params: _paykit_sdk_core.CreateRefundSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Refund, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  };
@@ -1,8 +1,9 @@
1
+ import { PayKitClientError } from '../util.js';
1
2
  import * as _paykit_sdk_core from '@paykit-sdk/core';
2
3
 
3
4
  declare const useRefund: () => {
4
5
  create: {
5
- run: (params: _paykit_sdk_core.CreateRefundSchema) => Promise<[data: undefined, error: Error] | [data: _paykit_sdk_core.Refund, error: undefined]>;
6
+ run: (params: _paykit_sdk_core.CreateRefundSchema<Record<string, unknown>>) => Promise<[data: undefined, error: PayKitClientError] | [data: _paykit_sdk_core.Refund, error: undefined]>;
6
7
  loading: boolean;
7
8
  };
8
9
  };