@konfiqo/storefront-react 0.6.1 → 0.8.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/README.md +54 -20
- package/dist/index.cjs +35 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -12
- package/dist/index.d.ts +26 -12
- package/dist/index.js +35 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ the payment form and the checkout challenge.
|
|
|
11
11
|
"build your own storefront" guide.
|
|
12
12
|
- No styling: components render a plain `<div>` you can give a `className`.
|
|
13
13
|
- No runtime dependencies. Peer dependencies: `@konfiqo/storefront-sdk`
|
|
14
|
-
`^0.
|
|
14
|
+
`^0.8.0` and `react` ≥ 18. Install the SDK yourself, so your code and this
|
|
15
15
|
package share one copy (`isStorefrontError` also recognises errors from a
|
|
16
16
|
second copy, but one copy keeps the bundle small).
|
|
17
17
|
- Server-rendering safe: nothing touches `window`, `document` or storage
|
|
@@ -61,7 +61,7 @@ read, and a stale answer never replaces a newer one.
|
|
|
61
61
|
|
|
62
62
|
| Hook | SDK call | Notes |
|
|
63
63
|
| --------------------- | ----------------------- | ---------------------------------------------------------------------------------------------- |
|
|
64
|
-
| `useStore()` | `store.get()` | Currency, theme,
|
|
64
|
+
| `useStore()` | `store.get()` | Currency, theme, `company`, `checkoutChallenge`. |
|
|
65
65
|
| `useCategories()` | `categories.list()` | The category tree. |
|
|
66
66
|
| `useProducts(params)` | `products.list(params)` | `{ category?, q?, page?, pageSize? }`. Reads again when a value changes (not on a new object). |
|
|
67
67
|
| `useProduct(slug)` | `products.get(slug)` | `null` / empty slug: reads nothing. Unknown slug: `error.code === 'product_not_found'`. |
|
|
@@ -80,15 +80,31 @@ In the Next.js App Router, prefer reading the catalog **on the server**
|
|
|
80
80
|
with the SDK directly (cacheable, no `Origin`); the hooks are for client
|
|
81
81
|
components.
|
|
82
82
|
|
|
83
|
+
## Blog
|
|
84
|
+
|
|
85
|
+
Read posts with `client.blog.list()` and `client.blog.get(slug)` on the
|
|
86
|
+
server, and render the body with `renderContent`. The `tree` of a post is
|
|
87
|
+
the same tree that `<Content>` renders.
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
import { renderContent } from '@konfiqo/storefront-react';
|
|
91
|
+
|
|
92
|
+
const post = await client.blog.get(slug);
|
|
93
|
+
return <article>{renderContent(post.tree)}</article>;
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`renderContent` needs a `<StorefrontProvider>` for the default components
|
|
97
|
+
(`ProductGrid`, `Image` and others).
|
|
98
|
+
|
|
83
99
|
## `useCart()`
|
|
84
100
|
|
|
85
101
|
```tsx
|
|
86
102
|
const {
|
|
87
|
-
cart, // Cart | null
|
|
103
|
+
cart, // Cart | null. The server's view; null until the buyer has one
|
|
88
104
|
status, // 'idle' (before the first read, and on the server) | 'loading' | 'ready'
|
|
89
105
|
isUpdating, // a write is in progress
|
|
90
106
|
error, // the last read / write error, or null
|
|
91
|
-
previousCartId, // string | null
|
|
107
|
+
previousCartId, // string | null. An ordered cart a write moved away from (see below)
|
|
92
108
|
itemCount, // total units (for a badge)
|
|
93
109
|
addLine, // ({ variantId, quantity, personalization? }) => Promise<Cart>
|
|
94
110
|
updateLine, // (lineId, { quantity }) => Promise<Cart>, lineId = cart line `id`
|
|
@@ -97,8 +113,8 @@ const {
|
|
|
97
113
|
setAddress, // ({ shipping, billing? }) => Promise<Cart>
|
|
98
114
|
setShippingOption, // (optionId) => Promise<Cart>
|
|
99
115
|
shippingOptions, // (options?) => Promise<ShippingOptionList>, { items: [] } without a cart
|
|
100
|
-
refresh, // () => Promise<void
|
|
101
|
-
reset, // () => void
|
|
116
|
+
refresh, // () => Promise<void>. Reads the cart again
|
|
117
|
+
reset, // () => void. Forgets the cart (e.g. after its order is paid)
|
|
102
118
|
} = useCart();
|
|
103
119
|
```
|
|
104
120
|
|
|
@@ -144,7 +160,7 @@ const {
|
|
|
144
160
|
|
|
145
161
|
## Cart storage
|
|
146
162
|
|
|
147
|
-
- **`defaultCartStorage()`**
|
|
163
|
+
- **`defaultCartStorage()`** is the provider's default storage (null on a
|
|
148
164
|
server). `getItem` reads the `__Host-konfiqo_cart` cookie first and
|
|
149
165
|
rewrites it on every read, renewing its expiry; when the cookie is gone
|
|
150
166
|
(Safari keeps script-written cookies 7 days) it reads `localStorage` and
|
|
@@ -152,7 +168,7 @@ const {
|
|
|
152
168
|
clears both and writes a `storefront:cart:<storeId>:cleared` tombstone in
|
|
153
169
|
`localStorage`; while it is there no frontend on the store origin restores
|
|
154
170
|
the old id from `localStorage`. The next `setItem` removes it.
|
|
155
|
-
- **`useCartStorage()`**
|
|
171
|
+
- **`useCartStorage()`** is a getter for the storage this provider uses
|
|
156
172
|
(call it in an effect or handler, never during render). Use it to save a
|
|
157
173
|
cart id learned out of band (`saveCartId(getStorage(), storeId, id)`) or
|
|
158
174
|
to clear one (`clearCartId(getStorage(), storeId)`).
|
|
@@ -165,10 +181,15 @@ const {
|
|
|
165
181
|
```tsx
|
|
166
182
|
const { checkout, isPending, result, error, attempts, reset } = useCheckout();
|
|
167
183
|
|
|
168
|
-
const result = await checkout({ turnstileToken }); // CheckoutResult
|
|
184
|
+
const result = await checkout({ agreementsHash, turnstileToken }); // CheckoutResult
|
|
169
185
|
```
|
|
170
186
|
|
|
171
|
-
- Calls `carts.checkout(cartId, { turnstileToken })` for the
|
|
187
|
+
- Calls `carts.checkout(cartId, { agreementsHash, turnstileToken })` for the
|
|
188
|
+
provider's cart. `agreementsHash` is required: load the agreements with
|
|
189
|
+
`client.carts.agreements(cartId)`, show them, and pass the hash. On
|
|
190
|
+
`agreements_changed`, load them again, show the new texts and call
|
|
191
|
+
`checkout()` with the new hash (see "Legal texts and agreements" in the
|
|
192
|
+
SDK README).
|
|
172
193
|
Without a cart it rejects with `checkout_incomplete`
|
|
173
194
|
(`details.missing: ['lines']`) without a request. A second call while one
|
|
174
195
|
is in flight returns the same promise.
|
|
@@ -192,6 +213,8 @@ const result = await checkout({ turnstileToken }); // CheckoutResult
|
|
|
192
213
|
break;
|
|
193
214
|
case 'out_of_stock': // error.details.variantIds
|
|
194
215
|
case 'checkout_incomplete': // error.details.missing
|
|
216
|
+
case 'agreements_changed': // load the agreements again, show them
|
|
217
|
+
case 'legal_not_confirmed': // the store takes no orders yet
|
|
195
218
|
case 'invalid_buyer_details': // error.details.field
|
|
196
219
|
case 'checkout_in_progress':
|
|
197
220
|
case 'challenge_required':
|
|
@@ -219,8 +242,8 @@ const result = await checkout({ turnstileToken }); // CheckoutResult
|
|
|
219
242
|
```
|
|
220
243
|
|
|
221
244
|
Presents payment with the SDK's `mountPayment`: the provider's embedded
|
|
222
|
-
form inside the `<div>` (its scripts run once
|
|
223
|
-
Mode, which mounts effects twice), a top-level redirect (`redirect`), or
|
|
245
|
+
form inside the `<div>` (its scripts run once, including under React
|
|
246
|
+
Strict Mode, which mounts effects twice), a top-level redirect (`redirect`), or
|
|
224
247
|
`onClosed(page)` (`closed`: `paid`, `cancelled`, `processing`, …). The form
|
|
225
248
|
is removed on unmount.
|
|
226
249
|
|
|
@@ -229,7 +252,7 @@ is removed on unmount.
|
|
|
229
252
|
or storage: the embedded content runs as script.
|
|
230
253
|
- Card data goes from the provider's form to the provider; it never
|
|
231
254
|
touches your code. Never put a Konfiqo secret (a storefront key, an API
|
|
232
|
-
key) in client code
|
|
255
|
+
key) in client code; the browser only ever needs the publishable key.
|
|
233
256
|
|
|
234
257
|
## `<CheckoutChallenge>`
|
|
235
258
|
|
|
@@ -239,6 +262,7 @@ Turnstile token:
|
|
|
239
262
|
```tsx
|
|
240
263
|
const { data: store } = useStore();
|
|
241
264
|
const [token, setToken] = useState<string | null>(null);
|
|
265
|
+
// agreementsHash: from client.carts.agreements(cartId), accepted by the buyer
|
|
242
266
|
const { checkout } = useCheckout();
|
|
243
267
|
|
|
244
268
|
<CheckoutChallenge
|
|
@@ -249,7 +273,7 @@ const { checkout } = useCheckout();
|
|
|
249
273
|
/>
|
|
250
274
|
<button
|
|
251
275
|
disabled={!!store?.checkoutChallenge && !token}
|
|
252
|
-
onClick={() => checkout({ turnstileToken: token })}
|
|
276
|
+
onClick={() => checkout({ agreementsHash, turnstileToken: token })}
|
|
253
277
|
>
|
|
254
278
|
{t('checkout.pay')}
|
|
255
279
|
</button>
|
|
@@ -290,12 +314,12 @@ const { order, error, isLoading, isPolling, refetch } = useOrder(id, token, {
|
|
|
290
314
|
|
|
291
315
|
- Reads `orders.get(orderId, token)`. Missing `orderId` or `token`: reads
|
|
292
316
|
nothing.
|
|
293
|
-
- `pollUntilFinal`: reads again with
|
|
294
|
-
15 s
|
|
317
|
+
- `pollUntilFinal`: reads again with a growing delay (2 s, 4 s, 8 s, then every
|
|
318
|
+
15 s) until the order is settled (`isOrderSettled`): any status past
|
|
295
319
|
`pendingPayment` (`paid`, `fulfilling`, `shipped`, `delivered`,
|
|
296
320
|
`cancelled`, `refunded`, …), or a pending order whose payment `failed` /
|
|
297
321
|
`expired` (the buyer checks out again). Transient errors
|
|
298
|
-
(`network_error`, `timeout`, `rate_limited`
|
|
322
|
+
(`network_error`, `timeout`, `rate_limited` (honouring `retryAfter`),
|
|
299
323
|
`order_unavailable`, `http_error`) keep polling and keep the last
|
|
300
324
|
`order`; other errors (`order_not_found`) stop it. Unmounting stops
|
|
301
325
|
polling and aborts the read.
|
|
@@ -382,9 +406,10 @@ export function Tracking({
|
|
|
382
406
|
`product_group`. Prices are in minor units, in the `currency` given to
|
|
383
407
|
`AdPixelsLoader` (ISO 4217, default `'TRY'`). Events fired before the
|
|
384
408
|
shopper's choice is read (for example from a mount effect during
|
|
385
|
-
hydration) are held, up to 20.
|
|
386
|
-
|
|
387
|
-
|
|
409
|
+
hydration) are held, up to 20. They are copied when held, so later
|
|
410
|
+
changes to the object you passed do not alter them. The first
|
|
411
|
+
configuration sends them if it carries a `marketing` grant; otherwise
|
|
412
|
+
they are dropped, and a later grant does not send them.
|
|
388
413
|
- **Which pixels and cookies a host may use.** On a default store domain,
|
|
389
414
|
filter with the SDK's `onDefaultDomain`, `pixelsForHost` and
|
|
390
415
|
`cookiesForHost`. They are server-safe, so call them in server
|
|
@@ -402,6 +427,15 @@ export function Tracking({
|
|
|
402
427
|
change: pixel scripts already loaded on the previous page read the full
|
|
403
428
|
URL (GA4 history page views, Meta automatic events) and would send the
|
|
404
429
|
secret.
|
|
430
|
+
- **Hardening for pixels loaded on an earlier page.** Meta is loaded with
|
|
431
|
+
`fbq('set', 'autoConfig', false, pixelId)` before `fbq('init', pixelId)`,
|
|
432
|
+
so Meta sends no automatic button-click or page-metadata events. Every
|
|
433
|
+
Google `config` call and every Google event the SDK sends carries an
|
|
434
|
+
explicit `page_location` that is the current page URL with the `token`
|
|
435
|
+
query parameter removed, and with the whole query string and fragment
|
|
436
|
+
removed on the secret pages above. Each event reads the URL again, so it
|
|
437
|
+
is right after a client-side route change. `scrubbedLocation()` returns
|
|
438
|
+
that URL.
|
|
405
439
|
- **Tests.** `resetAnalytics()` and `resetPixels()` forget the module
|
|
406
440
|
state between tests. They are for tests only; do not call them in
|
|
407
441
|
production code.
|
package/dist/index.cjs
CHANGED
|
@@ -53,6 +53,7 @@ __export(index_exports, {
|
|
|
53
53
|
renderContent: () => renderContent,
|
|
54
54
|
resetAnalytics: () => resetAnalytics,
|
|
55
55
|
resetPixels: () => resetPixels,
|
|
56
|
+
scrubbedLocation: () => scrubbedLocation,
|
|
56
57
|
track: () => track,
|
|
57
58
|
useAccount: () => useAccount,
|
|
58
59
|
useCart: () => useCart,
|
|
@@ -345,7 +346,7 @@ var CartStore = class {
|
|
|
345
346
|
* anyway); a second call while one is in flight returns the same
|
|
346
347
|
* promise. The cart is read again afterwards.
|
|
347
348
|
*/
|
|
348
|
-
checkout = (options
|
|
349
|
+
checkout = (options) => {
|
|
349
350
|
if (this.pendingCheckout) return this.pendingCheckout;
|
|
350
351
|
this.checkoutStarted = true;
|
|
351
352
|
this.setCheckout({ isPending: true, error: null });
|
|
@@ -362,6 +363,7 @@ var CartStore = class {
|
|
|
362
363
|
);
|
|
363
364
|
}
|
|
364
365
|
return this.client.carts.checkout(id, {
|
|
366
|
+
agreementsHash: options.agreementsHash,
|
|
365
367
|
turnstileToken: options.turnstileToken || void 0
|
|
366
368
|
});
|
|
367
369
|
});
|
|
@@ -725,7 +727,10 @@ function useCheckout() {
|
|
|
725
727
|
if (store.checkoutStarted) void store.refresh();
|
|
726
728
|
}, [store]);
|
|
727
729
|
const checkout = (0, import_react5.useCallback)(
|
|
728
|
-
(options
|
|
730
|
+
(options) => store.checkout({
|
|
731
|
+
agreementsHash: options.agreementsHash,
|
|
732
|
+
turnstileToken: options.turnstileToken ?? void 0
|
|
733
|
+
}),
|
|
729
734
|
[store]
|
|
730
735
|
);
|
|
731
736
|
return { ...snapshot, checkout, reset: store.clearCheckout };
|
|
@@ -1241,7 +1246,9 @@ function ConsentProvider({
|
|
|
1241
1246
|
const [decidedVersion, setDecidedVersion] = (0, import_react10.useState)(null);
|
|
1242
1247
|
const opener = (0, import_react10.useRef)(null);
|
|
1243
1248
|
const onDecisionRef = (0, import_react10.useRef)(onDecision);
|
|
1244
|
-
|
|
1249
|
+
(0, import_react10.useInsertionEffect)(() => {
|
|
1250
|
+
onDecisionRef.current = onDecision;
|
|
1251
|
+
});
|
|
1245
1252
|
const categories = (0, import_react10.useMemo)(
|
|
1246
1253
|
() => OPTIONAL_CATEGORIES.filter((c) => cookies.some((k) => k.category === c)),
|
|
1247
1254
|
[cookies]
|
|
@@ -1512,6 +1519,7 @@ function loadMeta(id) {
|
|
|
1512
1519
|
}
|
|
1513
1520
|
w.fbq.disablePushState = true;
|
|
1514
1521
|
addScript("https://connect.facebook.net/en_US/fbevents.js");
|
|
1522
|
+
w.fbq("set", "autoConfig", false, id);
|
|
1515
1523
|
w.fbq("init", id);
|
|
1516
1524
|
}
|
|
1517
1525
|
var TT_METHODS = [
|
|
@@ -1580,13 +1588,24 @@ function loadGoogle(g) {
|
|
|
1580
1588
|
for (const id of ids) {
|
|
1581
1589
|
w.gtag("config", id, {
|
|
1582
1590
|
send_page_view: false,
|
|
1583
|
-
cookie_domain: cookieDomain
|
|
1591
|
+
cookie_domain: cookieDomain,
|
|
1592
|
+
page_location: scrubbedLocation()
|
|
1584
1593
|
});
|
|
1585
1594
|
}
|
|
1586
1595
|
}
|
|
1587
1596
|
function onSecretPage(path = typeof location === "undefined" ? "" : location.pathname) {
|
|
1588
1597
|
return path.startsWith("/orders/") || path.startsWith("/cart/recover");
|
|
1589
1598
|
}
|
|
1599
|
+
function scrubbedLocation() {
|
|
1600
|
+
const url = new URL(location.href);
|
|
1601
|
+
if (onSecretPage(url.pathname)) {
|
|
1602
|
+
url.search = "";
|
|
1603
|
+
url.hash = "";
|
|
1604
|
+
} else {
|
|
1605
|
+
url.searchParams.delete("token");
|
|
1606
|
+
}
|
|
1607
|
+
return url.toString();
|
|
1608
|
+
}
|
|
1590
1609
|
function loadAll(p) {
|
|
1591
1610
|
if (p.meta && !loaded.has("meta")) {
|
|
1592
1611
|
loaded.add("meta");
|
|
@@ -1739,10 +1758,17 @@ function markPurchased(orderId) {
|
|
|
1739
1758
|
} catch {
|
|
1740
1759
|
}
|
|
1741
1760
|
}
|
|
1761
|
+
function copyEvent(e) {
|
|
1762
|
+
try {
|
|
1763
|
+
return structuredClone(e);
|
|
1764
|
+
} catch {
|
|
1765
|
+
return { ...e };
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1742
1768
|
function firePixel(e) {
|
|
1743
1769
|
if (!configured) {
|
|
1744
1770
|
if (typeof window === "undefined" || onSecretPage()) return;
|
|
1745
|
-
early.push(e);
|
|
1771
|
+
early.push(copyEvent(e));
|
|
1746
1772
|
if (early.length > MAX_EARLY) early.shift();
|
|
1747
1773
|
return;
|
|
1748
1774
|
}
|
|
@@ -1863,7 +1889,9 @@ function tiktok(ttq, e) {
|
|
|
1863
1889
|
);
|
|
1864
1890
|
}
|
|
1865
1891
|
}
|
|
1866
|
-
function google(
|
|
1892
|
+
function google(rawGtag, g, e) {
|
|
1893
|
+
const page_location = scrubbedLocation();
|
|
1894
|
+
const gtag = (command, name, params) => rawGtag(command, name, { ...params, page_location });
|
|
1867
1895
|
const item = (id, quantity, priceMinor) => ({
|
|
1868
1896
|
item_id: id,
|
|
1869
1897
|
quantity,
|
|
@@ -1954,6 +1982,7 @@ function AdPixelsLoader({
|
|
|
1954
1982
|
renderContent,
|
|
1955
1983
|
resetAnalytics,
|
|
1956
1984
|
resetPixels,
|
|
1985
|
+
scrubbedLocation,
|
|
1957
1986
|
track,
|
|
1958
1987
|
useAccount,
|
|
1959
1988
|
useCart,
|