@nymbalhq/sdk 1.4.0 → 1.5.0
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/chunk-333Z6VKZ.js +133 -0
- package/dist/chunk-333Z6VKZ.js.map +1 -0
- package/dist/chunk-4GP43MKT.js +19 -0
- package/dist/chunk-4GP43MKT.js.map +1 -0
- package/dist/chunk-BAWTE4QS.js +113 -0
- package/dist/chunk-BAWTE4QS.js.map +1 -0
- package/dist/chunk-EFRWJT3L.js +520 -0
- package/dist/chunk-EFRWJT3L.js.map +1 -0
- package/dist/chunk-FWEQBOEX.js +92 -0
- package/dist/chunk-FWEQBOEX.js.map +1 -0
- package/dist/chunk-KRVUYBKB.js +118 -0
- package/dist/chunk-KRVUYBKB.js.map +1 -0
- package/dist/chunk-L7EZURSK.js +130 -0
- package/dist/chunk-L7EZURSK.js.map +1 -0
- package/dist/chunk-QUSTGYBM.js +52 -0
- package/dist/chunk-QUSTGYBM.js.map +1 -0
- package/dist/chunk-U6QF2DRY.js +30 -0
- package/dist/chunk-U6QF2DRY.js.map +1 -0
- package/dist/core-DCBjP2Ev.d.ts +523 -0
- package/dist/core.d.ts +2 -0
- package/dist/core.js +17 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +3 -482
- package/dist/index.js +31 -1048
- package/dist/index.js.map +1 -1
- package/dist/ns/auth.d.ts +2 -0
- package/dist/ns/auth.js +4 -0
- package/dist/ns/auth.js.map +1 -0
- package/dist/ns/cart.d.ts +2 -0
- package/dist/ns/cart.js +4 -0
- package/dist/ns/cart.js.map +1 -0
- package/dist/ns/checkout.d.ts +2 -0
- package/dist/ns/checkout.js +5 -0
- package/dist/ns/checkout.js.map +1 -0
- package/dist/ns/product-list.d.ts +2 -0
- package/dist/ns/product-list.js +4 -0
- package/dist/ns/product-list.js.map +1 -0
- package/dist/ns/product.d.ts +2 -0
- package/dist/ns/product.js +4 -0
- package/dist/ns/product.js.map +1 -0
- package/dist/ns/search.d.ts +2 -0
- package/dist/ns/search.js +4 -0
- package/dist/ns/search.js.map +1 -0
- package/package.json +17 -2
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createStore
|
|
3
|
+
} from "./chunk-U6QF2DRY.js";
|
|
4
|
+
import {
|
|
5
|
+
defineNamespace
|
|
6
|
+
} from "./chunk-EFRWJT3L.js";
|
|
7
|
+
|
|
8
|
+
// src/store/cart-store.ts
|
|
9
|
+
function deriveItemCount(items) {
|
|
10
|
+
let count = 0;
|
|
11
|
+
for (const item of items) {
|
|
12
|
+
count += item.qty;
|
|
13
|
+
}
|
|
14
|
+
return count;
|
|
15
|
+
}
|
|
16
|
+
var INITIAL_STATE = {
|
|
17
|
+
items: [],
|
|
18
|
+
subtotalMinor: 0,
|
|
19
|
+
currency: "USD",
|
|
20
|
+
itemCount: 0,
|
|
21
|
+
loading: false,
|
|
22
|
+
error: null
|
|
23
|
+
};
|
|
24
|
+
function createCartStore(adapter) {
|
|
25
|
+
const store = createStore(INITIAL_STATE);
|
|
26
|
+
async function load() {
|
|
27
|
+
store.setState({ loading: true, error: null });
|
|
28
|
+
try {
|
|
29
|
+
const cart = await adapter.cart.get();
|
|
30
|
+
store.setState({
|
|
31
|
+
items: cart.items,
|
|
32
|
+
subtotalMinor: cart.subtotalMinor,
|
|
33
|
+
currency: cart.currency,
|
|
34
|
+
itemCount: deriveItemCount(cart.items),
|
|
35
|
+
loading: false
|
|
36
|
+
});
|
|
37
|
+
} catch (err) {
|
|
38
|
+
store.setState({
|
|
39
|
+
loading: false,
|
|
40
|
+
error: err instanceof Error ? err.message : String(err)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function addItem(variantId, qty = 1) {
|
|
45
|
+
store.setState({ loading: true, error: null });
|
|
46
|
+
try {
|
|
47
|
+
const cart = await adapter.cart.addItem(variantId, qty);
|
|
48
|
+
store.setState({
|
|
49
|
+
items: cart.items,
|
|
50
|
+
subtotalMinor: cart.subtotalMinor,
|
|
51
|
+
currency: cart.currency,
|
|
52
|
+
itemCount: deriveItemCount(cart.items),
|
|
53
|
+
loading: false
|
|
54
|
+
});
|
|
55
|
+
} catch (err) {
|
|
56
|
+
store.setState({
|
|
57
|
+
loading: false,
|
|
58
|
+
error: err instanceof Error ? err.message : String(err)
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function removeItem(variantId) {
|
|
63
|
+
store.setState({ loading: true, error: null });
|
|
64
|
+
try {
|
|
65
|
+
const cart = await adapter.cart.removeItem(variantId);
|
|
66
|
+
store.setState({
|
|
67
|
+
items: cart.items,
|
|
68
|
+
subtotalMinor: cart.subtotalMinor,
|
|
69
|
+
currency: cart.currency,
|
|
70
|
+
itemCount: deriveItemCount(cart.items),
|
|
71
|
+
loading: false
|
|
72
|
+
});
|
|
73
|
+
} catch (err) {
|
|
74
|
+
store.setState({
|
|
75
|
+
loading: false,
|
|
76
|
+
error: err instanceof Error ? err.message : String(err)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function updateQuantity(variantId, qty) {
|
|
81
|
+
store.setState({ loading: true, error: null });
|
|
82
|
+
try {
|
|
83
|
+
const cart = await adapter.cart.updateQuantity(variantId, qty);
|
|
84
|
+
store.setState({
|
|
85
|
+
items: cart.items,
|
|
86
|
+
subtotalMinor: cart.subtotalMinor,
|
|
87
|
+
currency: cart.currency,
|
|
88
|
+
itemCount: deriveItemCount(cart.items),
|
|
89
|
+
loading: false
|
|
90
|
+
});
|
|
91
|
+
} catch (err) {
|
|
92
|
+
store.setState({
|
|
93
|
+
loading: false,
|
|
94
|
+
error: err instanceof Error ? err.message : String(err)
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function clear() {
|
|
99
|
+
store.setState({ loading: true, error: null });
|
|
100
|
+
try {
|
|
101
|
+
await adapter.cart.clear();
|
|
102
|
+
store.setState({
|
|
103
|
+
items: [],
|
|
104
|
+
subtotalMinor: 0,
|
|
105
|
+
itemCount: 0,
|
|
106
|
+
loading: false
|
|
107
|
+
});
|
|
108
|
+
} catch (err) {
|
|
109
|
+
store.setState({
|
|
110
|
+
loading: false,
|
|
111
|
+
error: err instanceof Error ? err.message : String(err)
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
getState: store.getState,
|
|
117
|
+
subscribe: store.subscribe,
|
|
118
|
+
addItem,
|
|
119
|
+
removeItem,
|
|
120
|
+
updateQuantity,
|
|
121
|
+
clear,
|
|
122
|
+
load,
|
|
123
|
+
destroy: store.destroy
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/ns/cart.ts
|
|
128
|
+
defineNamespace("cart", ({ adapter }) => createCartStore(adapter));
|
|
129
|
+
|
|
130
|
+
export {
|
|
131
|
+
createCartStore
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=chunk-333Z6VKZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store/cart-store.ts","../src/ns/cart.ts"],"sourcesContent":["import type { CartItem } from '@nymbalhq/types'\nimport type { CommerceAdapter } from '../adapter/commerce-adapter.js'\nimport type { CartState } from '../types.js'\nimport { createStore } from './create-store.js'\n\nfunction deriveItemCount(items: CartItem[]): number {\n let count = 0\n for (const item of items) {\n count += item.qty\n }\n return count\n}\n\nconst INITIAL_STATE: CartState = {\n items: [],\n subtotalMinor: 0,\n currency: 'USD',\n itemCount: 0,\n loading: false,\n error: null,\n}\n\nexport interface CartStore {\n getState(): CartState\n subscribe(listener: () => void): () => void\n addItem(variantId: string, qty?: number): Promise<void>\n removeItem(variantId: string): Promise<void>\n updateQuantity(variantId: string, qty: number): Promise<void>\n clear(): Promise<void>\n load(): Promise<void>\n destroy(): void\n}\n\nexport function createCartStore(adapter: CommerceAdapter): CartStore {\n const store = createStore<CartState>(INITIAL_STATE)\n\n async function load(): Promise<void> {\n store.setState({ loading: true, error: null })\n try {\n const cart = await adapter.cart.get()\n store.setState({\n items: cart.items,\n subtotalMinor: cart.subtotalMinor,\n currency: cart.currency,\n itemCount: deriveItemCount(cart.items),\n loading: false,\n })\n } catch (err) {\n store.setState({\n loading: false,\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n async function addItem(variantId: string, qty = 1): Promise<void> {\n store.setState({ loading: true, error: null })\n try {\n const cart = await adapter.cart.addItem(variantId, qty)\n store.setState({\n items: cart.items,\n subtotalMinor: cart.subtotalMinor,\n currency: cart.currency,\n itemCount: deriveItemCount(cart.items),\n loading: false,\n })\n } catch (err) {\n store.setState({\n loading: false,\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n async function removeItem(variantId: string): Promise<void> {\n store.setState({ loading: true, error: null })\n try {\n const cart = await adapter.cart.removeItem(variantId)\n store.setState({\n items: cart.items,\n subtotalMinor: cart.subtotalMinor,\n currency: cart.currency,\n itemCount: deriveItemCount(cart.items),\n loading: false,\n })\n } catch (err) {\n store.setState({\n loading: false,\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n async function updateQuantity(variantId: string, qty: number): Promise<void> {\n store.setState({ loading: true, error: null })\n try {\n const cart = await adapter.cart.updateQuantity(variantId, qty)\n store.setState({\n items: cart.items,\n subtotalMinor: cart.subtotalMinor,\n currency: cart.currency,\n itemCount: deriveItemCount(cart.items),\n loading: false,\n })\n } catch (err) {\n store.setState({\n loading: false,\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n async function clear(): Promise<void> {\n store.setState({ loading: true, error: null })\n try {\n await adapter.cart.clear()\n store.setState({\n items: [],\n subtotalMinor: 0,\n itemCount: 0,\n loading: false,\n })\n } catch (err) {\n store.setState({\n loading: false,\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n return {\n getState: store.getState,\n subscribe: store.subscribe,\n addItem,\n removeItem,\n updateQuantity,\n clear,\n load,\n destroy: store.destroy,\n }\n}\n","import { defineNamespace } from '../client.js'\nimport { createCartStore } from '../store/cart-store.js'\n\ndefineNamespace('cart', ({ adapter }) => createCartStore(adapter))\n"],"mappings":";;;;;;;;AAKA,SAAS,gBAAgB,OAA2B;AAClD,MAAI,QAAQ;AACZ,aAAW,QAAQ,OAAO;AACxB,aAAS,KAAK;AAAA,EAChB;AACA,SAAO;AACT;AAEA,IAAM,gBAA2B;AAAA,EAC/B,OAAO,CAAC;AAAA,EACR,eAAe;AAAA,EACf,UAAU;AAAA,EACV,WAAW;AAAA,EACX,SAAS;AAAA,EACT,OAAO;AACT;AAaO,SAAS,gBAAgB,SAAqC;AACnE,QAAM,QAAQ,YAAuB,aAAa;AAElD,iBAAe,OAAsB;AACnC,UAAM,SAAS,EAAE,SAAS,MAAM,OAAO,KAAK,CAAC;AAC7C,QAAI;AACF,YAAM,OAAO,MAAM,QAAQ,KAAK,IAAI;AACpC,YAAM,SAAS;AAAA,QACb,OAAO,KAAK;AAAA,QACZ,eAAe,KAAK;AAAA,QACpB,UAAU,KAAK;AAAA,QACf,WAAW,gBAAgB,KAAK,KAAK;AAAA,QACrC,SAAS;AAAA,MACX,CAAC;AAAA,IACH,SAAS,KAAK;AACZ,YAAM,SAAS;AAAA,QACb,SAAS;AAAA,QACT,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,MACxD,CAAC;AAAA,IACH;AAAA,EACF;AAEA,iBAAe,QAAQ,WAAmB,MAAM,GAAkB;AAChE,UAAM,SAAS,EAAE,SAAS,MAAM,OAAO,KAAK,CAAC;AAC7C,QAAI;AACF,YAAM,OAAO,MAAM,QAAQ,KAAK,QAAQ,WAAW,GAAG;AACtD,YAAM,SAAS;AAAA,QACb,OAAO,KAAK;AAAA,QACZ,eAAe,KAAK;AAAA,QACpB,UAAU,KAAK;AAAA,QACf,WAAW,gBAAgB,KAAK,KAAK;AAAA,QACrC,SAAS;AAAA,MACX,CAAC;AAAA,IACH,SAAS,KAAK;AACZ,YAAM,SA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;AAAA,QACX,SAAS;AAAA,MACX,CAAC;AAAA,IACH,SAAS,KAAK;AACZ,YAAM,SAAS;AAAA,QACb,SAAS;AAAA,QACT,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,MACxD,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AAAA,IACL,UAAU,MAAM;AAAA,IAChB,WAAW,MAAM;AAAA,IACjB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,MAAM;AAAA,EACjB;AACF;;;ACzIA,gBAAgB,QAAQ,CAAC,EAAE,QAAQ,MAAM,gBAAgB,OAAO,CAAC;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/payment-drivers.ts
|
|
2
|
+
var DRIVERS_KEY = /* @__PURE__ */ Symbol.for("nymbal.paymentDrivers");
|
|
3
|
+
function drivers() {
|
|
4
|
+
const holder = globalThis;
|
|
5
|
+
holder[DRIVERS_KEY] ??= /* @__PURE__ */ new Map();
|
|
6
|
+
return holder[DRIVERS_KEY];
|
|
7
|
+
}
|
|
8
|
+
function registerPaymentDriver(name, driver) {
|
|
9
|
+
drivers().set(name, driver);
|
|
10
|
+
}
|
|
11
|
+
function getPaymentDriver(name) {
|
|
12
|
+
return drivers().get(name);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
registerPaymentDriver,
|
|
17
|
+
getPaymentDriver
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=chunk-4GP43MKT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/payment-drivers.ts"],"sourcesContent":["import type { Address } from '@nymbalhq/types'\n\n/** What a checkout already knows about the shopper, which a driver may hand its processor with the payment. */\nexport interface PaymentConfirmContext {\n email?: string\n billing?: Address\n}\n\n/** What a payment driver's `confirm` answers: paid now, accepted without a payment yet, or refused with a reason. */\nexport type PaymentConfirmation = { ok: true; confirmed: boolean } | { ok: false; error: string }\n\n/**\n * The browser half of a payments adapter, named by the `driver` of GET /api/capabilities' payments client. `mount`\n * renders whatever the shopper pays with inside `el`; `confirm` completes a payment the API left `requires_action`,\n * with the shopper's email and billing address when the checkout has them.\n */\nexport interface PaymentDriver {\n mount(el: HTMLElement, options: Record<string, string>): Promise<void> | void\n confirm(clientSecret: string, context?: PaymentConfirmContext): Promise<PaymentConfirmation>\n}\n\n// On globalThis, like the registered client: a page that loads two copies of the SDK still has one registry.\nconst DRIVERS_KEY = Symbol.for('nymbal.paymentDrivers')\n\nfunction drivers(): Map<string, PaymentDriver> {\n const holder = globalThis as Record<symbol, Map<string, PaymentDriver> | undefined>\n holder[DRIVERS_KEY] ??= new Map()\n return holder[DRIVERS_KEY]\n}\n\n/** Registers `driver` under `name`, replacing any driver of that name. */\nexport function registerPaymentDriver(name: string, driver: PaymentDriver): void {\n drivers().set(name, driver)\n}\n\nexport function getPaymentDriver(name: string): PaymentDriver | undefined {\n return drivers().get(name)\n}\n"],"mappings":";AAsBA,IAAM,cAAc,uBAAO,IAAI,uBAAuB;AAEtD,SAAS,UAAsC;AAC7C,QAAM,SAAS;AACf,SAAO,WAAW,MAAM,oBAAI,IAAI;AAChC,SAAO,OAAO,WAAW;AAC3B;AAGO,SAAS,sBAAsB,MAAc,QAA6B;AAC/E,UAAQ,EAAE,IAAI,MAAM,MAAM;AAC5B;AAEO,SAAS,iBAAiB,MAAyC;AACxE,SAAO,QAAQ,EAAE,IAAI,IAAI;AAC3B;","names":[]}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createStore
|
|
3
|
+
} from "./chunk-U6QF2DRY.js";
|
|
4
|
+
import {
|
|
5
|
+
NymbalApiError,
|
|
6
|
+
defineNamespace
|
|
7
|
+
} from "./chunk-EFRWJT3L.js";
|
|
8
|
+
|
|
9
|
+
// src/store/checkout-store.ts
|
|
10
|
+
var INITIAL_STATE = {
|
|
11
|
+
step: "contact",
|
|
12
|
+
email: "",
|
|
13
|
+
shippingAddress: null,
|
|
14
|
+
billingAddress: null,
|
|
15
|
+
shippingMethod: null,
|
|
16
|
+
paymentStatus: "idle",
|
|
17
|
+
error: null,
|
|
18
|
+
order: null,
|
|
19
|
+
placedOrderNumber: null,
|
|
20
|
+
placedOrderKey: null
|
|
21
|
+
};
|
|
22
|
+
function namedOrder(err) {
|
|
23
|
+
const context = err instanceof NymbalApiError ? err.context : void 0;
|
|
24
|
+
const orderNumber = context?.["orderNumber"];
|
|
25
|
+
const orderKey = context?.["orderKey"];
|
|
26
|
+
return typeof orderNumber === "string" ? {
|
|
27
|
+
placedOrderNumber: orderNumber,
|
|
28
|
+
placedOrderKey: typeof orderKey === "string" ? orderKey : null
|
|
29
|
+
} : { placedOrderNumber: null, placedOrderKey: null };
|
|
30
|
+
}
|
|
31
|
+
function createCheckoutStore(adapter, cart) {
|
|
32
|
+
const store = createStore(INITIAL_STATE);
|
|
33
|
+
function setEmail(email) {
|
|
34
|
+
store.setState({ email, step: "shipping" });
|
|
35
|
+
}
|
|
36
|
+
function setShippingAddress(address) {
|
|
37
|
+
store.setState({ shippingAddress: address });
|
|
38
|
+
}
|
|
39
|
+
function setBillingAddress(address) {
|
|
40
|
+
store.setState({ billingAddress: address });
|
|
41
|
+
}
|
|
42
|
+
function setShippingMethod(method) {
|
|
43
|
+
store.setState({ shippingMethod: method, step: "payment" });
|
|
44
|
+
}
|
|
45
|
+
async function submitPayment() {
|
|
46
|
+
const state = store.getState();
|
|
47
|
+
if (!state.shippingAddress || !state.billingAddress) {
|
|
48
|
+
store.setState({ error: "Shipping and billing addresses are required" });
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
store.setState({
|
|
52
|
+
paymentStatus: "processing",
|
|
53
|
+
error: null,
|
|
54
|
+
placedOrderNumber: null,
|
|
55
|
+
placedOrderKey: null
|
|
56
|
+
});
|
|
57
|
+
try {
|
|
58
|
+
const result = await adapter.checkout.create({
|
|
59
|
+
email: state.email,
|
|
60
|
+
billingAddress: state.billingAddress,
|
|
61
|
+
shippingAddress: state.shippingAddress
|
|
62
|
+
});
|
|
63
|
+
const { status, reason } = result.payment;
|
|
64
|
+
store.setState({
|
|
65
|
+
paymentStatus: status,
|
|
66
|
+
order: {
|
|
67
|
+
orderId: result.orderId,
|
|
68
|
+
orderNumber: result.orderNumber,
|
|
69
|
+
paymentIntent: result.paymentIntent
|
|
70
|
+
},
|
|
71
|
+
error: status === "failed" ? `Your payment did not go through${reason ? ` (${reason})` : ""}. Please try again.` : null,
|
|
72
|
+
...status === "succeeded" && { step: "confirmation" }
|
|
73
|
+
});
|
|
74
|
+
await cart?.load();
|
|
75
|
+
return result;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
const placed = namedOrder(err);
|
|
78
|
+
store.setState({
|
|
79
|
+
paymentStatus: "failed",
|
|
80
|
+
error: err instanceof Error ? err.message : String(err),
|
|
81
|
+
...placed
|
|
82
|
+
});
|
|
83
|
+
if (placed.placedOrderNumber !== null) await cart?.load();
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function markConfirmed() {
|
|
88
|
+
store.setState({ paymentStatus: "succeeded", step: "confirmation" });
|
|
89
|
+
}
|
|
90
|
+
function reset() {
|
|
91
|
+
store.setState(INITIAL_STATE);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
getState: store.getState,
|
|
95
|
+
subscribe: store.subscribe,
|
|
96
|
+
setEmail,
|
|
97
|
+
setShippingAddress,
|
|
98
|
+
setBillingAddress,
|
|
99
|
+
setShippingMethod,
|
|
100
|
+
submitPayment,
|
|
101
|
+
markConfirmed,
|
|
102
|
+
reset,
|
|
103
|
+
destroy: store.destroy
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/ns/checkout.ts
|
|
108
|
+
defineNamespace("checkout", ({ adapter, client }) => createCheckoutStore(adapter, client.cart));
|
|
109
|
+
|
|
110
|
+
export {
|
|
111
|
+
createCheckoutStore
|
|
112
|
+
};
|
|
113
|
+
//# sourceMappingURL=chunk-BAWTE4QS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store/checkout-store.ts","../src/ns/checkout.ts"],"sourcesContent":["import type { Address } from '@nymbalhq/types'\nimport type { CommerceAdapter } from '../adapter/commerce-adapter.js'\nimport { NymbalApiError } from '../adapter/fetch-client.js'\nimport type { CartStore } from './cart-store.js'\nimport type { CheckoutResult, CheckoutState, ShippingMethod } from '../types.js'\nimport { createStore } from './create-store.js'\n\nconst INITIAL_STATE: CheckoutState = {\n step: 'contact',\n email: '',\n shippingAddress: null,\n billingAddress: null,\n shippingMethod: null,\n paymentStatus: 'idle',\n error: null,\n order: null,\n placedOrderNumber: null,\n placedOrderKey: null,\n}\n\n/**\n * The order a refused resubmit names (409 `checkout.cart_empty` or `checkout.payment_in_progress`), if any, and the key\n * the refusal carries to read it.\n */\nfunction namedOrder(err: unknown): Pick<CheckoutState, 'placedOrderNumber' | 'placedOrderKey'> {\n const context = err instanceof NymbalApiError ? err.context : undefined\n const orderNumber = context?.['orderNumber']\n const orderKey = context?.['orderKey']\n return typeof orderNumber === 'string'\n ? {\n placedOrderNumber: orderNumber,\n placedOrderKey: typeof orderKey === 'string' ? orderKey : null,\n }\n : { placedOrderNumber: null, placedOrderKey: null }\n}\n\nexport interface CheckoutStore {\n getState(): CheckoutState\n subscribe(listener: () => void): () => void\n setEmail(email: string): void\n setShippingAddress(address: Address): void\n setBillingAddress(address: Address): void\n setShippingMethod(method: ShippingMethod): void\n /** Places the order; resolves to the server's result, or null when there is none. */\n submitPayment(): Promise<CheckoutResult | null>\n /** The provider confirmed the payment on the client (Stripe's `confirmCardPayment`). */\n markConfirmed(): void\n reset(): void\n destroy(): void\n}\n\n/**\n * `cart`, when given, is loaded again once the server has answered a submit: placing an order changes the cart on the\n * server (emptied when confirmed, kept while awaiting payment), and whatever renders the cart store must show that.\n */\nexport function createCheckoutStore(adapter: CommerceAdapter, cart?: CartStore): CheckoutStore {\n const store = createStore<CheckoutState>(INITIAL_STATE)\n\n function setEmail(email: string): void {\n store.setState({ email, step: 'shipping' })\n }\n\n function setShippingAddress(address: Address): void {\n store.setState({ shippingAddress: address })\n }\n\n function setBillingAddress(address: Address): void {\n store.setState({ billingAddress: address })\n }\n\n function setShippingMethod(method: ShippingMethod): void {\n store.setState({ shippingMethod: method, step: 'payment' })\n }\n\n async function submitPayment(): Promise<CheckoutResult | null> {\n const state = store.getState()\n if (!state.shippingAddress || !state.billingAddress) {\n store.setState({ error: 'Shipping and billing addresses are required' })\n return null\n }\n store.setState({\n paymentStatus: 'processing',\n error: null,\n placedOrderNumber: null,\n placedOrderKey: null,\n })\n try {\n const result = await adapter.checkout.create({\n email: state.email,\n billingAddress: state.billingAddress,\n shippingAddress: state.shippingAddress,\n })\n const { status, reason } = result.payment\n store.setState({\n paymentStatus: status,\n order: {\n orderId: result.orderId,\n orderNumber: result.orderNumber,\n paymentIntent: result.paymentIntent,\n },\n error:\n status === 'failed'\n ? `Your payment did not go through${reason ? ` (${reason})` : ''}. Please try again.`\n : null,\n ...(status === 'succeeded' && { step: 'confirmation' as const }),\n })\n await cart?.load()\n return result\n } catch (err) {\n const placed = namedOrder(err)\n store.setState({\n paymentStatus: 'failed',\n error: err instanceof Error ? err.message : String(err),\n ...placed,\n })\n // A refusal naming an order: the cart became (or is becoming) that order on the server.\n if (placed.placedOrderNumber !== null) await cart?.load()\n return null\n }\n }\n\n function markConfirmed(): void {\n store.setState({ paymentStatus: 'succeeded', step: 'confirmation' })\n }\n\n function reset(): void {\n store.setState(INITIAL_STATE)\n }\n\n return {\n getState: store.getState,\n subscribe: store.subscribe,\n setEmail,\n setShippingAddress,\n setBillingAddress,\n setShippingMethod,\n submitPayment,\n markConfirmed,\n reset,\n destroy: store.destroy,\n }\n}\n","import './cart.js'\nimport { defineNamespace } from '../client.js'\nimport { createCheckoutStore } from '../store/checkout-store.js'\n\n// The checkout places its order on the client's own cart, so one cart serves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