@cohostvip/cohost-react 0.0.4 → 0.0.12
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 +10 -0
- package/dist/context/CohostCheckoutContext.d.ts +21 -0
- package/dist/context/CohostCheckoutContext.d.ts.map +1 -0
- package/dist/context/CohostCheckoutContext.js +80 -0
- package/dist/context/CohostContext.d.ts +14 -6
- package/dist/context/CohostContext.d.ts.map +1 -1
- package/dist/context/CohostContext.js +3 -3
- package/dist/context/CohostEventContext.d.ts +34 -0
- package/dist/context/CohostEventContext.d.ts.map +1 -0
- package/dist/context/CohostEventContext.js +72 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -84,6 +84,16 @@ See its documentation for available methods like:
|
|
|
84
84
|
|
|
85
85
|
---
|
|
86
86
|
|
|
87
|
+
## 🗒️ Changelog
|
|
88
|
+
See the latest changes [here](./CHANGELOG.md).
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## 🚧 Roadmap
|
|
93
|
+
Planned features and improvements are tracked [here](./ROADMAP.md).
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
87
97
|
## 🛠️ Support
|
|
88
98
|
|
|
89
99
|
If you're building something custom or need help integrating, reach out at [cohost.vip](https://cohost.vip) or open an issue.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { CartSession, UpdatableCartSession } from '@cohostvip/cohost-node';
|
|
3
|
+
export type CohostCheckoutProviderProps = {
|
|
4
|
+
cartSessionId: string;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
};
|
|
7
|
+
export type CohostCheckoutContextType = {
|
|
8
|
+
cartSessionId: string;
|
|
9
|
+
cartSession: CartSession | null;
|
|
10
|
+
updateItem: (offeringId: string, quantity: number) => Promise<void>;
|
|
11
|
+
updateCartSession: (data: Partial<UpdatableCartSession>) => Promise<void>;
|
|
12
|
+
placeOrder: () => Promise<unknown>;
|
|
13
|
+
};
|
|
14
|
+
export declare const CohostCheckoutContext: React.Context<CohostCheckoutContextType | null>;
|
|
15
|
+
export declare const CohostCheckoutProvider: React.FC<CohostCheckoutProviderProps>;
|
|
16
|
+
/**
|
|
17
|
+
* Hook to access the current CohostCheckoutContext
|
|
18
|
+
* Must be used inside a <CohostCheckoutProvider>
|
|
19
|
+
*/
|
|
20
|
+
export declare const useCohostCheckout: () => CohostCheckoutContextType;
|
|
21
|
+
//# sourceMappingURL=CohostCheckoutContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CohostCheckoutContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostCheckoutContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAEhF,MAAM,MAAM,2BAA2B,GAAG;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACpC,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC;IAChC,UAAU,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACpE,iBAAiB,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,oBAAoB,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,UAAU,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;CACtC,CAAC;AAGF,eAAO,MAAM,qBAAqB,iDAAwD,CAAC;AAE3F,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,2BAA2B,CAkFxE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,yBAIpC,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createContext, useContext, useEffect } from 'react';
|
|
4
|
+
import { useCohostClient } from './CohostContext';
|
|
5
|
+
export const CohostCheckoutContext = createContext(null);
|
|
6
|
+
export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
7
|
+
const { client } = useCohostClient();
|
|
8
|
+
const [cartSession, setCartSession] = React.useState(null);
|
|
9
|
+
const assertCartSession = () => {
|
|
10
|
+
if (!cartSession) {
|
|
11
|
+
console.error("CohostCheckoutProvider requires a cartSession");
|
|
12
|
+
throw new Error("CohostCheckoutProvider requires a cartSession");
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const updateItem = async (offeringId, quantity) => {
|
|
16
|
+
assertCartSession();
|
|
17
|
+
try {
|
|
18
|
+
const updatedCart = await client.cart.updateItem(cartSessionId, { offeringId, quantity });
|
|
19
|
+
setCartSession(updatedCart);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.error("Error updating cart item:", error);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const updateCartSession = async (data) => {
|
|
26
|
+
assertCartSession();
|
|
27
|
+
try {
|
|
28
|
+
const updatedCart = await client.cart.update(cartSessionId, data);
|
|
29
|
+
setCartSession(updatedCart);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error("Error updating cart session:", error);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const placeOrder = async () => {
|
|
36
|
+
assertCartSession();
|
|
37
|
+
try {
|
|
38
|
+
const res = await client.cart.placeOrder(cartSessionId, {});
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error("Error placing order:", error);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (!cartSessionId) {
|
|
47
|
+
console.error("CohostCheckoutProvider requires a cartSessionId");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const fetchCartSession = async () => {
|
|
51
|
+
try {
|
|
52
|
+
const cart = await client.cart.get(cartSessionId);
|
|
53
|
+
setCartSession(cart);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error("Error fetching cart session:", error);
|
|
57
|
+
// rethrow the error to be handled by the caller
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
fetchCartSession();
|
|
62
|
+
}, [cartSessionId]);
|
|
63
|
+
return (_jsx(CohostCheckoutContext.Provider, { value: {
|
|
64
|
+
cartSessionId,
|
|
65
|
+
cartSession,
|
|
66
|
+
updateItem,
|
|
67
|
+
updateCartSession,
|
|
68
|
+
placeOrder,
|
|
69
|
+
}, children: children }));
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Hook to access the current CohostCheckoutContext
|
|
73
|
+
* Must be used inside a <CohostCheckoutProvider>
|
|
74
|
+
*/
|
|
75
|
+
export const useCohostCheckout = () => {
|
|
76
|
+
const ctx = useContext(CohostCheckoutContext);
|
|
77
|
+
if (!ctx)
|
|
78
|
+
throw new Error("useCohostCheckout must be used within a CohostCheckoutProvider");
|
|
79
|
+
return ctx;
|
|
80
|
+
};
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { type CohostClient } from '@cohostvip/cohost-node';
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { CohostClientSettings, type CohostClient } from '@cohostvip/cohost-node';
|
|
3
|
+
export type CohostProviderProps = {
|
|
4
|
+
settings?: CohostClientSettings;
|
|
5
5
|
children: React.ReactNode;
|
|
6
|
-
}
|
|
6
|
+
} & ({
|
|
7
|
+
token: string;
|
|
8
|
+
client?: CohostClient;
|
|
9
|
+
} | {
|
|
10
|
+
token?: string;
|
|
11
|
+
client: CohostClient;
|
|
12
|
+
});
|
|
7
13
|
export declare const CohostProvider: React.FC<CohostProviderProps>;
|
|
8
|
-
export declare const useCohostClient: () =>
|
|
14
|
+
export declare const useCohostClient: () => {
|
|
15
|
+
client: CohostClient;
|
|
16
|
+
};
|
|
9
17
|
//# sourceMappingURL=CohostContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohostContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostContext.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"CohostContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,oBAAoB,EAAsB,KAAK,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAGrG,MAAM,MAAM,mBAAmB,GAAG;IAC9B,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,GAAG,CAAC;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,YAAY,CAAC;CACzB,GAAG;IACA,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,YAAY,CAAC;CACxB,CAAC,CAAC;AAMH,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAaxD,CAAC;AAEF,eAAO,MAAM,eAAe,QAAO;IAAE,MAAM,EAAE,YAAY,CAAA;CAIxD,CAAC"}
|
|
@@ -2,9 +2,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { createContext, useContext } from 'react';
|
|
3
3
|
import { createCohostClient } from '@cohostvip/cohost-node';
|
|
4
4
|
const CohostContext = createContext(null);
|
|
5
|
-
export const CohostProvider = ({ token, children, }) => {
|
|
6
|
-
const client = createCohostClient({ token }); // assumes a factory fn in cohost-node
|
|
7
|
-
return (_jsx(CohostContext.Provider, { value: client, children: children }));
|
|
5
|
+
export const CohostProvider = ({ client: providedClient, settings, token: providedToken, children, }) => {
|
|
6
|
+
const client = providedClient ?? createCohostClient({ token: providedToken, settings }); // assumes a factory fn in cohost-node
|
|
7
|
+
return (_jsx(CohostContext.Provider, { value: { client }, children: children }));
|
|
8
8
|
};
|
|
9
9
|
export const useCohostClient = () => {
|
|
10
10
|
const ctx = useContext(CohostContext);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { EventProfile, Ticket } from '@cohostvip/cohost-node';
|
|
3
|
+
export type CohostEventProviderProps = {
|
|
4
|
+
loadTickets?: boolean;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
} & ({
|
|
7
|
+
/** If event is provided, skip fetching. Optionally provide eventId for ticket loading. */
|
|
8
|
+
event: EventProfile;
|
|
9
|
+
eventId?: string;
|
|
10
|
+
} | {
|
|
11
|
+
/** If event not provided, must provide eventId for fetching. */
|
|
12
|
+
event?: EventProfile;
|
|
13
|
+
eventId: string;
|
|
14
|
+
});
|
|
15
|
+
export type CohostEventContextType = {
|
|
16
|
+
state: 'initial' | 'loading' | 'loading-tickets' | 'ready';
|
|
17
|
+
/**
|
|
18
|
+
* The event instance, will be undefined while loading
|
|
19
|
+
*/
|
|
20
|
+
event?: EventProfile;
|
|
21
|
+
/**
|
|
22
|
+
* If `loadTickets` is true, this will be populated with the tickets for the event
|
|
23
|
+
* If `loadTickets` is false, this will be null
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
tickets?: Ticket[] | null;
|
|
27
|
+
};
|
|
28
|
+
export declare const CohostEventProvider: React.FC<CohostEventProviderProps>;
|
|
29
|
+
/**
|
|
30
|
+
* Hook to access the current CohostEventContext
|
|
31
|
+
* Must be used inside a <CohostEventProvider>
|
|
32
|
+
*/
|
|
33
|
+
export declare const useCohostEvent: () => CohostEventContextType;
|
|
34
|
+
//# sourceMappingURL=CohostEventContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CohostEventContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostEventContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EACH,YAAY,EACZ,MAAM,EACT,MAAM,wBAAwB,CAAC;AAGhC,MAAM,MAAM,wBAAwB,GAAG;IACnC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,GAAG,CACM;IACE,0FAA0F;IAC1F,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB,GACC;IACE,gEAAgE;IAChE,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACnB,CACJ,CAAC;AAEN,MAAM,MAAM,sBAAsB,GAAG;IACjC,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,iBAAiB,GAAG,OAAO,CAAC;IAE3D;;OAEG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;CAC7B,CAAA;AAID,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,EAAE,CAAC,wBAAwB,CAwElE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,QAAO,sBAIjC,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createContext, useContext } from 'react';
|
|
4
|
+
import { useCohostClient } from './CohostContext';
|
|
5
|
+
const CohostEventContext = createContext(null);
|
|
6
|
+
export const CohostEventProvider = ({ event: providedEvent, eventId, loadTickets = false, children, }) => {
|
|
7
|
+
const { client } = useCohostClient();
|
|
8
|
+
const [state, setState] = React.useState('initial');
|
|
9
|
+
const [event, setEvent] = React.useState(providedEvent);
|
|
10
|
+
const [tickets, setTickets] = React.useState(undefined);
|
|
11
|
+
// Use provided event's ID or fallback to eventId prop
|
|
12
|
+
const resolvedEventId = providedEvent?.id || eventId;
|
|
13
|
+
/**
|
|
14
|
+
* Load event from API if not provided.
|
|
15
|
+
* Skip loading if event was passed via props.
|
|
16
|
+
*/
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
if (providedEvent) {
|
|
19
|
+
// Use the provided event and skip fetching
|
|
20
|
+
setEvent(providedEvent);
|
|
21
|
+
setState('ready');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!eventId)
|
|
25
|
+
return; // Safety guard if eventId is undefined
|
|
26
|
+
setState('loading');
|
|
27
|
+
client.events.fetch(eventId)
|
|
28
|
+
.then((fetchedEvent) => {
|
|
29
|
+
setEvent(fetchedEvent);
|
|
30
|
+
setState('ready');
|
|
31
|
+
})
|
|
32
|
+
.catch(() => {
|
|
33
|
+
// On error, reset to initial state
|
|
34
|
+
setState('initial');
|
|
35
|
+
});
|
|
36
|
+
}, [client, eventId, providedEvent]);
|
|
37
|
+
/**
|
|
38
|
+
* Optionally load tickets once event is available and loadTickets is true.
|
|
39
|
+
* If event is not loaded yet, this waits until it becomes available.
|
|
40
|
+
*/
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
if (!loadTickets || !event?.id) {
|
|
43
|
+
setTickets(null);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
setState('loading-tickets');
|
|
47
|
+
client.events.tickets(event.id)
|
|
48
|
+
.then((tickets) => {
|
|
49
|
+
setTickets(tickets);
|
|
50
|
+
setState('ready');
|
|
51
|
+
})
|
|
52
|
+
.catch(() => {
|
|
53
|
+
// Fail silently and continue with ready state
|
|
54
|
+
setState('ready');
|
|
55
|
+
});
|
|
56
|
+
}, [client, event?.id, loadTickets]);
|
|
57
|
+
return (_jsx(CohostEventContext.Provider, { value: {
|
|
58
|
+
state,
|
|
59
|
+
event,
|
|
60
|
+
tickets,
|
|
61
|
+
}, children: children }));
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Hook to access the current CohostEventContext
|
|
65
|
+
* Must be used inside a <CohostEventProvider>
|
|
66
|
+
*/
|
|
67
|
+
export const useCohostEvent = () => {
|
|
68
|
+
const ctx = useContext(CohostEventContext);
|
|
69
|
+
if (!ctx)
|
|
70
|
+
throw new Error("useCohostEvent must be used within a CohostEventProvider");
|
|
71
|
+
return ctx;
|
|
72
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { CohostCheckoutProvider, CohostCheckoutContext, useCohostCheckout } from './context/CohostCheckoutContext';
|
|
2
|
+
export { CohostEventProvider, useCohostEvent } from './context/CohostEventContext';
|
|
3
|
+
export { CohostProvider, type CohostProviderProps } from './context/CohostContext';
|
|
2
4
|
export { useCohost } from './hooks/useCohost';
|
|
3
5
|
//# 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,cAAc,EAAE,MAAM,yBAAyB,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,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
export { CohostCheckoutProvider, CohostCheckoutContext, useCohostCheckout } from './context/CohostCheckoutContext';
|
|
2
|
+
export { CohostEventProvider, useCohostEvent } from './context/CohostEventContext';
|
|
1
3
|
export { CohostProvider } from './context/CohostContext';
|
|
2
4
|
export { useCohost } from './hooks/useCohost';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cohostvip/cohost-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "React bindings for the Cohost API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"require": "./dist/index.js",
|
|
11
|
-
"import": "./dist/index.js"
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@cohostvip/cohost-node": "0.0.
|
|
30
|
+
"@cohostvip/cohost-node": "0.0.12"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@testing-library/jest-dom": "6.6.3",
|