@cohostvip/cohost-react 0.2.0 → 0.2.1
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/dist/context/CohostCheckoutContext.d.ts +2 -1
- package/dist/context/CohostCheckoutContext.d.ts.map +1 -1
- package/dist/context/CohostCheckoutContext.js +38 -26
- package/dist/hooks/useCohostStartCheckout.d.ts +31 -0
- package/dist/hooks/useCohostStartCheckout.d.ts.map +1 -1
- package/dist/hooks/useCohostStartCheckout.js +66 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/package.json +3 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
2
|
import type { CartSession, Customer, PersonAddress, UpdatableCartSession } from '@cohostvip/cohost-node';
|
|
3
3
|
export type CohostCheckoutProviderProps = {
|
|
4
4
|
cartSessionId: string;
|
|
@@ -18,6 +18,7 @@ export type CohostCheckoutContextType = {
|
|
|
18
18
|
removeCoupon: (id: string) => Promise<void>;
|
|
19
19
|
setCustomer: (customer: Partial<Customer>) => Promise<void>;
|
|
20
20
|
setBillingAddress: (address: Partial<PersonAddress>) => Promise<void>;
|
|
21
|
+
preValidate: (data: unknown) => Promise<unknown>;
|
|
21
22
|
};
|
|
22
23
|
export declare const CohostCheckoutContext: React.Context<CohostCheckoutContextType | null>;
|
|
23
24
|
export declare const CohostCheckoutProvider: React.FC<CohostCheckoutProviderProps>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohostCheckoutContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostCheckoutContext.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"CohostCheckoutContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostCheckoutContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAwD,MAAM,OAAO,CAAC;AAE7E,OAAO,KAAK,EAAE,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E,CAAC,OAAO,EAAE,OAAO,CAAC,aAAa,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,WAAW,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CACpD,CAAC;AAGF,eAAO,MAAM,qBAAqB,iDAAwD,CAAC;AAE3F,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,2BAA2B,CAmPxE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,yBAIpC,CAAC"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
3
|
-
import { createContext, useContext, useEffect } from 'react';
|
|
2
|
+
import React, { createContext, useContext, useEffect, useMemo } from 'react';
|
|
4
3
|
import { useCohostClient } from './CohostContext';
|
|
5
4
|
export const CohostCheckoutContext = createContext(null);
|
|
6
5
|
export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
@@ -34,6 +33,16 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
34
33
|
throw error;
|
|
35
34
|
}
|
|
36
35
|
};
|
|
36
|
+
const preValidate = async (data) => {
|
|
37
|
+
assertCartSession();
|
|
38
|
+
try {
|
|
39
|
+
return client.cart.preValidate(cartSessionId, data);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error("Error removing coupon:", error);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
37
46
|
const joinGroup = async (groupId) => {
|
|
38
47
|
assertCartSession();
|
|
39
48
|
try {
|
|
@@ -120,14 +129,15 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
120
129
|
};
|
|
121
130
|
const setBillingAddress = async (address) => {
|
|
122
131
|
assertCartSession();
|
|
132
|
+
const billingAddress = {
|
|
133
|
+
...cartSession?.customer?.billingAddress,
|
|
134
|
+
...address,
|
|
135
|
+
first: address.first || cartSession?.customer?.billingAddress?.first || cartSession?.customer?.first || '',
|
|
136
|
+
last: address.last || cartSession?.customer?.billingAddress?.last || cartSession?.customer?.last || '',
|
|
137
|
+
};
|
|
123
138
|
const customer = {
|
|
124
139
|
...cartSession?.customer,
|
|
125
|
-
billingAddress
|
|
126
|
-
...cartSession?.customer?.billingAddress,
|
|
127
|
-
...address,
|
|
128
|
-
first: address.first || cartSession?.customer?.billingAddress?.first || cartSession?.customer?.first || '',
|
|
129
|
-
last: address.last || cartSession?.customer?.billingAddress?.last || cartSession?.customer?.last || '',
|
|
130
|
-
},
|
|
140
|
+
billingAddress,
|
|
131
141
|
};
|
|
132
142
|
return setCustomer(customer);
|
|
133
143
|
};
|
|
@@ -169,24 +179,26 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
169
179
|
};
|
|
170
180
|
fetchCartSession();
|
|
171
181
|
}, [cartSessionId]);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
182
|
+
const stateValue = useMemo(() => ({
|
|
183
|
+
cartSessionId,
|
|
184
|
+
cartSession,
|
|
185
|
+
joinGroup,
|
|
186
|
+
updateItem,
|
|
187
|
+
incrementItem,
|
|
188
|
+
decrementItem,
|
|
189
|
+
updateCartSession,
|
|
190
|
+
placeOrder,
|
|
191
|
+
processPayment,
|
|
192
|
+
applyCoupon,
|
|
193
|
+
removeCoupon,
|
|
194
|
+
setCustomer,
|
|
195
|
+
setBillingAddress,
|
|
196
|
+
preValidate,
|
|
197
|
+
}), [
|
|
198
|
+
cartSessionId,
|
|
199
|
+
cartSession,
|
|
200
|
+
]);
|
|
201
|
+
return (_jsx(CohostCheckoutContext.Provider, { value: stateValue, children: children }));
|
|
190
202
|
};
|
|
191
203
|
/**
|
|
192
204
|
* Hook to access the current CohostCheckoutContext
|
|
@@ -1 +1,32 @@
|
|
|
1
|
+
export interface StartCheckoutOptions {
|
|
2
|
+
items: Array<{
|
|
3
|
+
offeringId: string;
|
|
4
|
+
quantity: number;
|
|
5
|
+
options?: Record<string, any>;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hook to start the checkout process.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const { startCheckout, isStarting } = useCohostStartCheckout();
|
|
14
|
+
*
|
|
15
|
+
* const handleCheckout = async () => {
|
|
16
|
+
* const cartSessionId = await startCheckout({
|
|
17
|
+
* items: [
|
|
18
|
+
* { offeringId: 'ticket_123', quantity: 2 }
|
|
19
|
+
* ]
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* if (cartSessionId) {
|
|
23
|
+
* router.push(`/checkout/${cartSessionId}`);
|
|
24
|
+
* }
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const useCohostStartCheckout: () => {
|
|
29
|
+
startCheckout: (options: StartCheckoutOptions) => Promise<string | null>;
|
|
30
|
+
contextId: string;
|
|
31
|
+
};
|
|
1
32
|
//# sourceMappingURL=useCohostStartCheckout.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCohostStartCheckout.d.ts","sourceRoot":"","sources":["../../src/hooks/useCohostStartCheckout.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"useCohostStartCheckout.d.ts","sourceRoot":"","sources":["../../src/hooks/useCohostStartCheckout.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,oBAAoB;IACjC,KAAK,EAAE,KAAK,CAAC;QACT,UAAU,EAAE,MAAM,CAAC;QACnB,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KACjC,CAAC,CAAC;CACN;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,sBAAsB;6BAgBO,oBAAoB,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;;CAgCtF,CAAC"}
|
|
@@ -1 +1,66 @@
|
|
|
1
|
-
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { CohostStartCheckoutContext } from '../context/CohostStartCheckoutContext';
|
|
3
|
+
import { useCohostClient } from '../context/CohostContext';
|
|
4
|
+
/**
|
|
5
|
+
* Hook to start the checkout process.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const { startCheckout, isStarting } = useCohostStartCheckout();
|
|
10
|
+
*
|
|
11
|
+
* const handleCheckout = async () => {
|
|
12
|
+
* const cartSessionId = await startCheckout({
|
|
13
|
+
* items: [
|
|
14
|
+
* { offeringId: 'ticket_123', quantity: 2 }
|
|
15
|
+
* ]
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* if (cartSessionId) {
|
|
19
|
+
* router.push(`/checkout/${cartSessionId}`);
|
|
20
|
+
* }
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const useCohostStartCheckout = () => {
|
|
25
|
+
const ctx = useContext(CohostStartCheckoutContext);
|
|
26
|
+
const { client } = useCohostClient();
|
|
27
|
+
if (!ctx) {
|
|
28
|
+
throw new Error('useCohostStartCheckout must be used within a CohostStartCheckoutProvider');
|
|
29
|
+
}
|
|
30
|
+
const { contextId, getCartSessionId } = ctx;
|
|
31
|
+
/**
|
|
32
|
+
* Start the checkout process by creating a cart session and adding items to it.
|
|
33
|
+
*
|
|
34
|
+
* @param options - The items to add to the cart
|
|
35
|
+
* @returns The cart session ID or null if the operation failed
|
|
36
|
+
*/
|
|
37
|
+
const startCheckout = async (options) => {
|
|
38
|
+
try {
|
|
39
|
+
// Get or create a cart session
|
|
40
|
+
let cartSessionId = await getCartSessionId();
|
|
41
|
+
if (!cartSessionId) {
|
|
42
|
+
console.error('Failed to create cart session');
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
// Add items to the cart session
|
|
46
|
+
for (const item of options.items) {
|
|
47
|
+
if (item.quantity > 0) {
|
|
48
|
+
await client.cart.updateItem(cartSessionId, {
|
|
49
|
+
itemId: item.offeringId,
|
|
50
|
+
quantity: item.quantity,
|
|
51
|
+
options: item.options,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return cartSessionId;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error('Error starting checkout:', error);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
startCheckout,
|
|
64
|
+
contextId,
|
|
65
|
+
};
|
|
66
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export { PaymentElementProvider, usePaymentElement } from './context/PaymentElem
|
|
|
5
5
|
export { CohostStartCheckoutProvider } from './provider/CohostStartCheckoutProvider';
|
|
6
6
|
export { CreditCardInformation } from './lib/tokenizers/types';
|
|
7
7
|
export { useCohost } from './hooks/useCohost';
|
|
8
|
+
export { useCohostStartCheckout } from './hooks/useCohostStartCheckout';
|
|
8
9
|
export { formatCurrency } from './lib/utils';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACnH,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnF,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC5F,OAAO,EAAE,2BAA2B,EAAE,MAAM,wCAAwC,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACnH,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnF,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC5F,OAAO,EAAE,2BAA2B,EAAE,MAAM,wCAAwC,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,4 +4,5 @@ export { CohostProvider } from './context/CohostContext';
|
|
|
4
4
|
export { PaymentElementProvider, usePaymentElement } from './context/PaymentElementContext';
|
|
5
5
|
export { CohostStartCheckoutProvider } from './provider/CohostStartCheckoutProvider';
|
|
6
6
|
export { useCohost } from './hooks/useCohost';
|
|
7
|
+
export { useCohostStartCheckout } from './hooks/useCohostStartCheckout';
|
|
7
8
|
export { formatCurrency } from './lib/utils';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cohostvip/cohost-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "React bindings for the Cohost API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -35,6 +35,8 @@
|
|
|
35
35
|
"@types/react": "19.2.0",
|
|
36
36
|
"@types/react-dom": "19.2.0",
|
|
37
37
|
"jsdom": "26.0.0",
|
|
38
|
+
"react": "catalog:frontend",
|
|
39
|
+
"react-dom": "catalog:frontend",
|
|
38
40
|
"typescript": "5.9.3",
|
|
39
41
|
"vitest": "^3.2.0"
|
|
40
42
|
},
|