@paykit-sdk/react 1.1.9 → 1.1.91

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
@@ -14,9 +14,9 @@ npm install @paykit-sdk/react
14
14
 
15
15
  ```tsx
16
16
  import { PayKit } from '@paykit-sdk/core';
17
- import { local } from '@paykit-sdk/local';
17
+ import { paypal } from '@paykit-sdk/paypal';
18
18
 
19
- const provider = local();
19
+ const provider = paypal();
20
20
  const paykit = new PayKit(provider);
21
21
 
22
22
  export { provider, paykit };
@@ -0,0 +1,9 @@
1
+ import { EndpointPath } from '@paykit-sdk/core';
2
+
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>)) => {
5
+ run: (...args: Args) => Promise<AsyncResult<Response>>;
6
+ loading: boolean;
7
+ };
8
+
9
+ export { useAsyncFn };
@@ -0,0 +1,9 @@
1
+ import { EndpointPath } from '@paykit-sdk/core';
2
+
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>)) => {
5
+ run: (...args: Args) => Promise<AsyncResult<Response>>;
6
+ loading: boolean;
7
+ };
8
+
9
+ export { useAsyncFn };
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ function _interopNamespace(e) {
6
+ if (e && e.__esModule) return e;
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
24
+
25
+ // src/hooks/use-async-fn.ts
26
+ var useAsyncFn = (path, apiUrl, headersEsque) => {
27
+ const [loading, setLoading] = React__namespace.useState(false);
28
+ const run = React__namespace.useCallback(
29
+ async (...args) => {
30
+ setLoading(true);
31
+ try {
32
+ const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
33
+ const response = await fetch(`${apiUrl}${path}`, {
34
+ method: "POST",
35
+ headers: { "Content-Type": "application/json", ...headers },
36
+ credentials: "include",
37
+ body: JSON.stringify({ args })
38
+ });
39
+ if (!response.ok) {
40
+ const errorData = await response.json().catch(() => ({ message: "Request failed" }));
41
+ throw new Error(errorData.message || `HTTP ${response.status}`);
42
+ }
43
+ const data = await response.json();
44
+ setLoading(false);
45
+ return [data.result, void 0];
46
+ } catch (error) {
47
+ setLoading(false);
48
+ return [void 0, error instanceof Error ? error : new Error(String(error))];
49
+ }
50
+ },
51
+ [path, apiUrl, headersEsque]
52
+ );
53
+ return { run, loading };
54
+ };
55
+
56
+ exports.useAsyncFn = useAsyncFn;
@@ -0,0 +1,34 @@
1
+ import * as React from 'react';
2
+
3
+ // src/hooks/use-async-fn.ts
4
+ var useAsyncFn = (path, apiUrl, headersEsque) => {
5
+ const [loading, setLoading] = React.useState(false);
6
+ const run = React.useCallback(
7
+ async (...args) => {
8
+ setLoading(true);
9
+ try {
10
+ const headers = typeof headersEsque === "function" ? headersEsque() : headersEsque != null ? headersEsque : {};
11
+ const response = await fetch(`${apiUrl}${path}`, {
12
+ method: "POST",
13
+ headers: { "Content-Type": "application/json", ...headers },
14
+ credentials: "include",
15
+ body: JSON.stringify({ args })
16
+ });
17
+ if (!response.ok) {
18
+ const errorData = await response.json().catch(() => ({ message: "Request failed" }));
19
+ throw new Error(errorData.message || `HTTP ${response.status}`);
20
+ }
21
+ const data = await response.json();
22
+ setLoading(false);
23
+ return [data.result, void 0];
24
+ } catch (error) {
25
+ setLoading(false);
26
+ return [void 0, error instanceof Error ? error : new Error(String(error))];
27
+ }
28
+ },
29
+ [path, apiUrl, headersEsque]
30
+ );
31
+ return { run, loading };
32
+ };
33
+
34
+ export { useAsyncFn };
package/dist/index.d.mts CHANGED
@@ -1,153 +1,16 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import { PayKitProvider } from '@paykit-sdk/core';
3
+ export { useCustomer } from './resources/customer.mjs';
4
+ export { useSubscription } from './resources/subscription.mjs';
5
+ export { useCheckout } from './resources/checkout.mjs';
6
+ export { useRefund } from './resources/refund.mjs';
7
+ import '@paykit-sdk/core';
4
8
 
5
- interface PaykitProviderProps {
6
- children: React.ReactNode;
7
- provider: PayKitProvider;
9
+ interface PaykitProviderProps extends React.PropsWithChildren {
10
+ apiUrl: string;
11
+ headers: Record<string, string> | (() => Record<string, string>);
8
12
  }
9
- declare const PaykitProvider: ({ provider, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
10
- declare const usePaykitContext: () => {
11
- provider: PayKitProvider;
12
- } | undefined;
13
+ declare const PaykitProvider: ({ apiUrl, headers, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
14
+ declare const usePaykitContext: () => PaykitProviderProps;
13
15
 
14
- declare const useCustomer: () => {
15
- retrieve: {
16
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
17
- id: string;
18
- email?: string | undefined;
19
- metadata?: Record<string, string> | undefined;
20
- name?: string | undefined;
21
- } | null, error: undefined]>;
22
- loading: boolean;
23
- };
24
- create: {
25
- run: (params: {
26
- email: string;
27
- metadata?: Record<string, string> | undefined;
28
- name?: string | undefined;
29
- }) => Promise<[data: undefined, error: Error] | [data: {
30
- id: string;
31
- email?: string | undefined;
32
- metadata?: Record<string, string> | undefined;
33
- name?: string | undefined;
34
- }, error: undefined]>;
35
- loading: boolean;
36
- };
37
- update: {
38
- run: (id: string, params: {
39
- email?: string | undefined;
40
- metadata?: Record<string, string> | undefined;
41
- name?: string | undefined;
42
- }) => Promise<[data: undefined, error: Error] | [data: {
43
- id: string;
44
- email?: string | undefined;
45
- metadata?: Record<string, string> | undefined;
46
- name?: string | undefined;
47
- }, error: undefined]>;
48
- loading: boolean;
49
- };
50
- };
51
-
52
- declare const useSubscription: () => {
53
- retrieve: {
54
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
55
- customer_id: string;
56
- status: "active" | "past_due" | "canceled" | "expired";
57
- item_id: string;
58
- billing_interval: "day" | "week" | "month" | "year";
59
- billing_interval_count: number;
60
- id: string;
61
- currency: string;
62
- amount: number;
63
- current_period_start: Date;
64
- current_period_end: Date;
65
- current_cycle: number;
66
- total_cycles: number;
67
- custom_fields: Record<string, any> | null;
68
- metadata?: Record<string, string> | null | undefined;
69
- } | null, error: undefined]>;
70
- loading: boolean;
71
- };
72
- update: {
73
- run: (id: string, params: {
74
- metadata?: Record<string, string> | undefined;
75
- }) => Promise<[data: undefined, error: Error] | [data: {
76
- customer_id: string;
77
- status: "active" | "past_due" | "canceled" | "expired";
78
- item_id: string;
79
- billing_interval: "day" | "week" | "month" | "year";
80
- billing_interval_count: number;
81
- id: string;
82
- currency: string;
83
- amount: number;
84
- current_period_start: Date;
85
- current_period_end: Date;
86
- current_cycle: number;
87
- total_cycles: number;
88
- custom_fields: Record<string, any> | null;
89
- metadata?: Record<string, string> | null | undefined;
90
- }, error: undefined]>;
91
- loading: boolean;
92
- };
93
- cancel: {
94
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
95
- loading: boolean;
96
- };
97
- };
98
-
99
- declare const useCheckout: () => {
100
- create: {
101
- run: (params: {
102
- customer_id: string;
103
- metadata: Record<string, string>;
104
- session_type: "one_time" | "recurring";
105
- item_id: string;
106
- quantity: number;
107
- subscription?: {
108
- billing_interval: "day" | "week" | "month" | "year";
109
- billing_interval_count: number;
110
- } | undefined;
111
- provider_metadata?: Record<string, string> | undefined;
112
- }) => Promise<[data: undefined, error: Error] | [data: {
113
- customer_id: string;
114
- session_type: "one_time" | "recurring";
115
- id: string;
116
- payment_url: string;
117
- products: {
118
- quantity: number;
119
- id: string;
120
- }[];
121
- currency: string;
122
- amount: number;
123
- metadata?: Record<string, string> | null | undefined;
124
- subscription?: {
125
- billing_interval: "day" | "week" | "month" | "year";
126
- billing_interval_count: number;
127
- } | null | undefined;
128
- }, error: undefined]>;
129
- loading: boolean;
130
- };
131
- retrieve: {
132
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
133
- customer_id: string;
134
- session_type: "one_time" | "recurring";
135
- id: string;
136
- payment_url: string;
137
- products: {
138
- quantity: number;
139
- id: string;
140
- }[];
141
- currency: string;
142
- amount: number;
143
- metadata?: Record<string, string> | null | undefined;
144
- subscription?: {
145
- billing_interval: "day" | "week" | "month" | "year";
146
- billing_interval_count: number;
147
- } | null | undefined;
148
- } | null, error: undefined]>;
149
- loading: boolean;
150
- };
151
- };
152
-
153
- export { PaykitProvider, useCheckout, useCustomer, usePaykitContext, useSubscription };
16
+ export { PaykitProvider, usePaykitContext };
package/dist/index.d.ts CHANGED
@@ -1,153 +1,16 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import { PayKitProvider } from '@paykit-sdk/core';
3
+ export { useCustomer } from './resources/customer.js';
4
+ export { useSubscription } from './resources/subscription.js';
5
+ export { useCheckout } from './resources/checkout.js';
6
+ export { useRefund } from './resources/refund.js';
7
+ import '@paykit-sdk/core';
4
8
 
5
- interface PaykitProviderProps {
6
- children: React.ReactNode;
7
- provider: PayKitProvider;
9
+ interface PaykitProviderProps extends React.PropsWithChildren {
10
+ apiUrl: string;
11
+ headers: Record<string, string> | (() => Record<string, string>);
8
12
  }
9
- declare const PaykitProvider: ({ provider, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
10
- declare const usePaykitContext: () => {
11
- provider: PayKitProvider;
12
- } | undefined;
13
+ declare const PaykitProvider: ({ apiUrl, headers, children }: PaykitProviderProps) => react_jsx_runtime.JSX.Element;
14
+ declare const usePaykitContext: () => PaykitProviderProps;
13
15
 
14
- declare const useCustomer: () => {
15
- retrieve: {
16
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
17
- id: string;
18
- email?: string | undefined;
19
- metadata?: Record<string, string> | undefined;
20
- name?: string | undefined;
21
- } | null, error: undefined]>;
22
- loading: boolean;
23
- };
24
- create: {
25
- run: (params: {
26
- email: string;
27
- metadata?: Record<string, string> | undefined;
28
- name?: string | undefined;
29
- }) => Promise<[data: undefined, error: Error] | [data: {
30
- id: string;
31
- email?: string | undefined;
32
- metadata?: Record<string, string> | undefined;
33
- name?: string | undefined;
34
- }, error: undefined]>;
35
- loading: boolean;
36
- };
37
- update: {
38
- run: (id: string, params: {
39
- email?: string | undefined;
40
- metadata?: Record<string, string> | undefined;
41
- name?: string | undefined;
42
- }) => Promise<[data: undefined, error: Error] | [data: {
43
- id: string;
44
- email?: string | undefined;
45
- metadata?: Record<string, string> | undefined;
46
- name?: string | undefined;
47
- }, error: undefined]>;
48
- loading: boolean;
49
- };
50
- };
51
-
52
- declare const useSubscription: () => {
53
- retrieve: {
54
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
55
- customer_id: string;
56
- status: "active" | "past_due" | "canceled" | "expired";
57
- item_id: string;
58
- billing_interval: "day" | "week" | "month" | "year";
59
- billing_interval_count: number;
60
- id: string;
61
- currency: string;
62
- amount: number;
63
- current_period_start: Date;
64
- current_period_end: Date;
65
- current_cycle: number;
66
- total_cycles: number;
67
- custom_fields: Record<string, any> | null;
68
- metadata?: Record<string, string> | null | undefined;
69
- } | null, error: undefined]>;
70
- loading: boolean;
71
- };
72
- update: {
73
- run: (id: string, params: {
74
- metadata?: Record<string, string> | undefined;
75
- }) => Promise<[data: undefined, error: Error] | [data: {
76
- customer_id: string;
77
- status: "active" | "past_due" | "canceled" | "expired";
78
- item_id: string;
79
- billing_interval: "day" | "week" | "month" | "year";
80
- billing_interval_count: number;
81
- id: string;
82
- currency: string;
83
- amount: number;
84
- current_period_start: Date;
85
- current_period_end: Date;
86
- current_cycle: number;
87
- total_cycles: number;
88
- custom_fields: Record<string, any> | null;
89
- metadata?: Record<string, string> | null | undefined;
90
- }, error: undefined]>;
91
- loading: boolean;
92
- };
93
- cancel: {
94
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: null, error: undefined]>;
95
- loading: boolean;
96
- };
97
- };
98
-
99
- declare const useCheckout: () => {
100
- create: {
101
- run: (params: {
102
- customer_id: string;
103
- metadata: Record<string, string>;
104
- session_type: "one_time" | "recurring";
105
- item_id: string;
106
- quantity: number;
107
- subscription?: {
108
- billing_interval: "day" | "week" | "month" | "year";
109
- billing_interval_count: number;
110
- } | undefined;
111
- provider_metadata?: Record<string, string> | undefined;
112
- }) => Promise<[data: undefined, error: Error] | [data: {
113
- customer_id: string;
114
- session_type: "one_time" | "recurring";
115
- id: string;
116
- payment_url: string;
117
- products: {
118
- quantity: number;
119
- id: string;
120
- }[];
121
- currency: string;
122
- amount: number;
123
- metadata?: Record<string, string> | null | undefined;
124
- subscription?: {
125
- billing_interval: "day" | "week" | "month" | "year";
126
- billing_interval_count: number;
127
- } | null | undefined;
128
- }, error: undefined]>;
129
- loading: boolean;
130
- };
131
- retrieve: {
132
- run: (id: string) => Promise<[data: undefined, error: Error] | [data: {
133
- customer_id: string;
134
- session_type: "one_time" | "recurring";
135
- id: string;
136
- payment_url: string;
137
- products: {
138
- quantity: number;
139
- id: string;
140
- }[];
141
- currency: string;
142
- amount: number;
143
- metadata?: Record<string, string> | null | undefined;
144
- subscription?: {
145
- billing_interval: "day" | "week" | "month" | "year";
146
- billing_interval_count: number;
147
- } | null | undefined;
148
- } | null, error: undefined]>;
149
- loading: boolean;
150
- };
151
- };
152
-
153
- export { PaykitProvider, useCheckout, useCustomer, usePaykitContext, useSubscription };
16
+ export { PaykitProvider, usePaykitContext };