@moonbase.sh/react 1.1.0 → 1.2.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 +127 -16
- package/dist/index.cjs +467 -88
- package/dist/index.d.cts +256 -16
- package/dist/index.d.ts +256 -16
- package/dist/index.js +440 -66
- package/package.json +2 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput,
|
|
1
|
+
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, OwnedProduct, Activation, License, Subscription, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput, ActivationMethod, Download, SubscriptionCancellationFeedback, Page, PriceFormat, ProductReviewSort, SubmitReviewRequest, ReviewVoteDirection, Vendor, Voucher } from '@moonbase.sh/storefront-api';
|
|
2
2
|
export * from '@moonbase.sh/storefront-api';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -31,7 +31,13 @@ interface StorefrontAuth {
|
|
|
31
31
|
setPassword: (currentPassword: string, newPassword: string) => Promise<void>;
|
|
32
32
|
forgotPassword: (email: string) => Promise<void>;
|
|
33
33
|
resetPassword: (email: string, newPassword: string, code: string) => Promise<void>;
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Confirms a new account from the code in its welcome email, which signs the customer in. A code works
|
|
36
|
+
* once, so calling this again with the same email and code, while the first call is in flight or for
|
|
37
|
+
* a few seconds after, hands back the first call's result rather than asking Moonbase twice: a page
|
|
38
|
+
* confirming as it loads can be run twice by React. The same goes for `confirmEmail` and
|
|
39
|
+
* `confirmEmailChange`.
|
|
40
|
+
*/
|
|
35
41
|
confirmAccount: (email: string, code: string) => Promise<UserAccountConfirmed>;
|
|
36
42
|
confirmEmail: (email: string, code: string) => Promise<void>;
|
|
37
43
|
confirmEmailChange: (email: string, code: string) => Promise<User | null>;
|
|
@@ -127,8 +133,41 @@ interface StorefrontCart {
|
|
|
127
133
|
removeCartOffer: () => Promise<void>;
|
|
128
134
|
}
|
|
129
135
|
|
|
136
|
+
type AttributionConsentStatus = 'granted' | 'refused' | 'unknown';
|
|
137
|
+
/** Where the visitor's consent stands for each ad platform a store can connect in Moonbase. */
|
|
138
|
+
interface AttributionConsentStatusByPlatform {
|
|
139
|
+
meta: AttributionConsentStatus;
|
|
140
|
+
googleAnalytics: AttributionConsentStatus;
|
|
141
|
+
}
|
|
130
142
|
/** Everything a storefront knows that a page draws from. Replaced, never changed in place, on every update. */
|
|
131
143
|
interface StorefrontState {
|
|
144
|
+
/**
|
|
145
|
+
* The visitor's answer to the site's cookie banner, given with `answerAttributionConsent` and
|
|
146
|
+
* remembered in this browser across visits, or `null` until they answer. The storefront starts from it
|
|
147
|
+
* when `attributionConsent` isn't passed to `createStorefront`.
|
|
148
|
+
*/
|
|
149
|
+
attributionConsentAnswer: boolean | null;
|
|
150
|
+
/**
|
|
151
|
+
* Whether the store asks this visitor to agree to marketing cookies before it keeps the ad click IDs
|
|
152
|
+
* they arrived with, as its "Ad and campaign tracking" setting in Moonbase decides for where they are.
|
|
153
|
+
* `undefined` until a catalog loaded in this browser says: one a server fetched, such as
|
|
154
|
+
* `initialCatalog`, describes where the server is. A cookie banner asks when this is `true` and
|
|
155
|
+
* `attributionConsentStatus` is still `'unknown'`.
|
|
156
|
+
*/
|
|
157
|
+
attributionConsentRequired: boolean | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* Where the visitor's consent to marketing cookies stands: `'refused'` once the site reports a refusal,
|
|
160
|
+
* `'granted'` once the site reports consent or the store's own setting lets them go unasked, and
|
|
161
|
+
* `'unknown'` until either says. A browser that sends Global Privacy Control counts as refusing, unless the
|
|
162
|
+
* store set every ad platform it connected in Moonbase to ignore it.
|
|
163
|
+
*/
|
|
164
|
+
attributionConsentStatus: AttributionConsentStatus;
|
|
165
|
+
/**
|
|
166
|
+
* Where the visitor's consent stands for each ad platform a store can connect in Moonbase: as
|
|
167
|
+
* `attributionConsentStatus`, except that Global Privacy Control refuses only a platform the store has
|
|
168
|
+
* it honored for (`gpc` on `catalog.tracking.meta` and `.googleAnalytics`).
|
|
169
|
+
*/
|
|
170
|
+
attributionConsentStatusByPlatform: AttributionConsentStatusByPlatform;
|
|
132
171
|
/**
|
|
133
172
|
* The store's catalog for this visitor: products, bundles, offers and promotions, priced in their
|
|
134
173
|
* currency, with `owned` worked out for whoever is signed in. Starts from the copy this browser
|
|
@@ -185,7 +224,11 @@ interface StorefrontEvents {
|
|
|
185
224
|
* called), and `order` is the order it was open for, when the checkout sent it.
|
|
186
225
|
*/
|
|
187
226
|
checkoutClosed: (intent: string | undefined, completed: boolean, order: Order | undefined) => void;
|
|
188
|
-
/**
|
|
227
|
+
/**
|
|
228
|
+
* A fresh catalog arrived from Moonbase. The first one can arrive before a component's effects run, so
|
|
229
|
+
* a component that needs what it says reads it from the state instead: `catalog`, or
|
|
230
|
+
* `attributionConsentRequired` for whether to ask for consent.
|
|
231
|
+
*/
|
|
189
232
|
catalogUpdated: (catalog: Storefront) => void;
|
|
190
233
|
/**
|
|
191
234
|
* Moonbase refused to renew the buyer's session: they signed out of it elsewhere, changed their
|
|
@@ -220,6 +263,8 @@ declare class MoonbaseStorefront {
|
|
|
220
263
|
static readonly storefrontKey = "moonbase_storefront";
|
|
221
264
|
static readonly utmKey = "moonbase_utm";
|
|
222
265
|
static readonly arrivalKey = "moonbase_arrival";
|
|
266
|
+
static readonly adConsentReportedKey = "moonbase_ad_consent_reported";
|
|
267
|
+
static readonly attributionConsentKey = "moonbase_attribution_consent";
|
|
223
268
|
/**
|
|
224
269
|
* Whether the cart attaches eligible item-scope offers by itself. See
|
|
225
270
|
* `StorefrontOptions.autoApplyItemOffers`. Read on every cart change.
|
|
@@ -255,6 +300,12 @@ declare class MoonbaseStorefront {
|
|
|
255
300
|
private storeAllowsAttribution;
|
|
256
301
|
private refreshOrderAbortController;
|
|
257
302
|
private pushTimeout;
|
|
303
|
+
/** Sends the cart change `pushTimeout` is waiting to send. */
|
|
304
|
+
private pendingPush;
|
|
305
|
+
/** Aborted by `dispose`, which takes the storefront's window listeners with it. */
|
|
306
|
+
private readonly lifetime;
|
|
307
|
+
/** Whether the storefront took the hosted checkout's parameters off the address as it was made. */
|
|
308
|
+
private tookCheckoutReturn;
|
|
258
309
|
private catalogRefreshesStarted;
|
|
259
310
|
private catalogRefreshLanded;
|
|
260
311
|
constructor(configuration: MoonbaseConfiguration, client: MoonbaseClient, options?: Pick<StorefrontOptions, 'initialCatalog' | 'autoApplyItemOffers'>);
|
|
@@ -270,9 +321,29 @@ declare class MoonbaseStorefront {
|
|
|
270
321
|
readonly subscribe: (listener: () => void) => (() => void);
|
|
271
322
|
/**
|
|
272
323
|
* Calls `callback` each time the storefront reports `event`, and returns the function that stops it.
|
|
273
|
-
* In a component, `useStorefrontEvent` does the subscribing and the cleaning up.
|
|
324
|
+
* `dispose` stops it too. In a component, `useStorefrontEvent` does the subscribing and the cleaning up.
|
|
274
325
|
*/
|
|
275
326
|
on<K extends keyof StorefrontEvents>(event: K, callback: StorefrontEvents[K]): () => void;
|
|
327
|
+
/** Whether `dispose` has stopped the storefront. */
|
|
328
|
+
get disposed(): boolean;
|
|
329
|
+
/**
|
|
330
|
+
* Stops the storefront, for a test or for a page that swaps it for another: it stops following other
|
|
331
|
+
* tabs, the checkout and the customer's session, stops calling what `on` subscribed, stops renewing
|
|
332
|
+
* their sign-in, and ignores what requests still in flight bring back. A cart change still waiting to
|
|
333
|
+
* be sent goes at once. What it stored in the browser stays, for the next storefront to pick up.
|
|
334
|
+
* `getStorefront` makes a new one after this.
|
|
335
|
+
*/
|
|
336
|
+
dispose(): void;
|
|
337
|
+
/**
|
|
338
|
+
* Takes the hosted checkout's parameters off the address again, when the storefront took them off as
|
|
339
|
+
* it was made and a router has since put them back, and has the router read the address again.
|
|
340
|
+
* `<StorefrontProvider>` calls it once the page has hydrated, which is all a page needs.
|
|
341
|
+
*
|
|
342
|
+
* Next.js's router is why: it reads the address before any of the page's own code runs, and writes it
|
|
343
|
+
* back as the page hydrates. It is told with a `popstate`, as for Back, since it ignores an address
|
|
344
|
+
* changed under the history entry it wrote, and `useSearchParams()` would go on listing the parameters.
|
|
345
|
+
*/
|
|
346
|
+
clearCheckoutReturn(): void;
|
|
276
347
|
get hasUtm(): boolean;
|
|
277
348
|
get hasUtmOrReferrer(): boolean;
|
|
278
349
|
/** Whether the storefront may keep and send ad click IDs and record an arrival before any action. */
|
|
@@ -298,10 +369,19 @@ declare class MoonbaseStorefront {
|
|
|
298
369
|
* browser's storage again. What has already been sent stays with the order.
|
|
299
370
|
*/
|
|
300
371
|
setAttributionConsent(granted: boolean | AdConsentReport): void;
|
|
372
|
+
/**
|
|
373
|
+
* Records the visitor's answer to the site's own cookie banner: remembered in this browser, so the
|
|
374
|
+
* storefront starts from it on their next visit, and passed on with `setAttributionConsent`. Other tabs
|
|
375
|
+
* follow it. `useAttributionConsent` hands it out. A banner that asks for Google's two consent mode
|
|
376
|
+
* signals separately passes them to `setAttributionConsent` instead.
|
|
377
|
+
*/
|
|
378
|
+
readonly answerAttributionConsent: (granted: boolean) => void;
|
|
301
379
|
/**
|
|
302
380
|
* What the storefront's requests say of the visit for ad measurement, beside `utm`: what the site last
|
|
303
381
|
* reported of the visitor's consent, left out until it says, and when and on which page the visit's
|
|
304
|
-
* tagged arrival landed, left out for a visit without tags.
|
|
382
|
+
* tagged arrival landed, left out for a visit without tags. Where the store connected Meta or Google
|
|
383
|
+
* Analytics in Moonbase, and while the visitor's consent lets click IDs go, also their browser IDs and
|
|
384
|
+
* the page the request is made on.
|
|
305
385
|
*/
|
|
306
386
|
adSignals(): AdSignals;
|
|
307
387
|
/** Asks Moonbase who is signed in. `state.user` is null when nobody is, or the session has run out. */
|
|
@@ -352,8 +432,23 @@ declare class MoonbaseStorefront {
|
|
|
352
432
|
private storeLanding;
|
|
353
433
|
/** `utm` as the storefront may keep and send it: with its ad click IDs only once the visitor agreed. */
|
|
354
434
|
private consentedUtm;
|
|
435
|
+
/**
|
|
436
|
+
* The visitor's Meta and Google Analytics browser IDs, and this page, for a store that connected either
|
|
437
|
+
* in Moonbase, while the visitor's consent lets click IDs go. A browser that sends Global Privacy Control
|
|
438
|
+
* gives only those of a platform the store set to ignore it. Read as each request is made, since the
|
|
439
|
+
* vendors' tags write their cookies after the storefront starts.
|
|
440
|
+
*/
|
|
441
|
+
private browserIdentifiers;
|
|
442
|
+
private consentStatus;
|
|
443
|
+
/**
|
|
444
|
+
* Tells Moonbase when the site reports that a signed-in customer refuses ad tracking, which renewals
|
|
445
|
+
* and other events sent without a visit then follow. Once per tab for each customer and report, as a
|
|
446
|
+
* site's banner often reports the same choice on every page.
|
|
447
|
+
*/
|
|
448
|
+
private reportAdConsentRefusal;
|
|
355
449
|
private handleStorageUpdate;
|
|
356
450
|
private refreshOrder;
|
|
451
|
+
private static parseAnswer;
|
|
357
452
|
private static parseCachedUtm;
|
|
358
453
|
private static parseCachedOrder;
|
|
359
454
|
/**
|
|
@@ -369,14 +464,17 @@ declare class MoonbaseStorefront {
|
|
|
369
464
|
/** The store's catalog for this visitor, with `useStorefrontState(state => state.catalogLoaded)` saying whether it has loaded. */
|
|
370
465
|
declare function useCatalog(): Storefront;
|
|
371
466
|
declare function useProducts(): StorefrontProduct[];
|
|
372
|
-
/**
|
|
373
|
-
|
|
467
|
+
/**
|
|
468
|
+
* The product with this ID in the catalog, or null, as before the catalog has loaded. Null for no ID, so a
|
|
469
|
+
* component that may have none, such as a cart line holding a bundle, can still call it.
|
|
470
|
+
*/
|
|
471
|
+
declare function useProduct(productId: string | null | undefined): StorefrontProduct | null;
|
|
374
472
|
declare function useBundles(): StorefrontBundle[];
|
|
375
|
-
/** The bundle with this ID in the catalog, or null. */
|
|
376
|
-
declare function useBundle(bundleId: string): StorefrontBundle | null;
|
|
473
|
+
/** The bundle with this ID in the catalog, or null, also for no ID. */
|
|
474
|
+
declare function useBundle(bundleId: string | null | undefined): StorefrontBundle | null;
|
|
377
475
|
declare function useOffers(): StorefrontOffer[];
|
|
378
|
-
/** The offer with this ID in the catalog, or null. */
|
|
379
|
-
declare function useOffer(offerId: string): StorefrontOffer | null;
|
|
476
|
+
/** The offer with this ID in the catalog, or null, also for no ID. */
|
|
477
|
+
declare function useOffer(offerId: string | null | undefined): StorefrontOffer | null;
|
|
380
478
|
/**
|
|
381
479
|
* The sales currently running across the store's catalog.
|
|
382
480
|
*
|
|
@@ -427,6 +525,50 @@ declare function useLockedOffers(): LockedOffer[];
|
|
|
427
525
|
/** What the cart's offer meter shows: the cart offer the buyer has earned, the next one to reach, or both. */
|
|
428
526
|
declare function useCartOffer(): CartOfferState | null;
|
|
429
527
|
|
|
528
|
+
/** Part of the signed-in customer's account, as a hook loads it. */
|
|
529
|
+
interface AccountData<T> {
|
|
530
|
+
/**
|
|
531
|
+
* The data, or null: while it loads, with nobody signed in, and for no ID. A component that mounts
|
|
532
|
+
* again gets what was loaded before at once, and the hook asks for it again in the background.
|
|
533
|
+
*/
|
|
534
|
+
data: T | null;
|
|
535
|
+
/**
|
|
536
|
+
* Whether it is loading: while a request is in flight, a background refresh of `data` included, and
|
|
537
|
+
* until the storefront knows who is signed in. Show a placeholder while this is true and `data` is null.
|
|
538
|
+
*/
|
|
539
|
+
loading: boolean;
|
|
540
|
+
/** Why the last request failed, until one succeeds. */
|
|
541
|
+
error: Error | null;
|
|
542
|
+
/** Asks for it again, as after changing it. Settles once the answer is in, and never rejects. */
|
|
543
|
+
reload: () => Promise<void>;
|
|
544
|
+
}
|
|
545
|
+
/** A list in the signed-in customer's account, which Moonbase hands out a page at a time. */
|
|
546
|
+
interface PagedAccountData<T> extends AccountData<T[]> {
|
|
547
|
+
/**
|
|
548
|
+
* Every item loaded so far, or null. `reload` goes back to the first page. A page can hold more items
|
|
549
|
+
* than Moonbase's page size, so count what is here rather than pages.
|
|
550
|
+
*/
|
|
551
|
+
data: T[] | null;
|
|
552
|
+
/** Whether Moonbase has more after what is loaded. */
|
|
553
|
+
hasMore: boolean;
|
|
554
|
+
/** Adds the next page to `data`. Settles once it is in, and never rejects. */
|
|
555
|
+
loadMore: () => Promise<void>;
|
|
556
|
+
}
|
|
557
|
+
/** The products the signed-in customer owns, or has a trial of, a page at a time. */
|
|
558
|
+
declare function useOwnedProducts(): PagedAccountData<OwnedProduct>;
|
|
559
|
+
/** One product the signed-in customer owns, with its downloads. Null for no ID. */
|
|
560
|
+
declare function useOwnedProduct(productId: string | null | undefined): AccountData<OwnedProduct>;
|
|
561
|
+
/** The computers the signed-in customer's licenses for a product are activated on, a page at a time. */
|
|
562
|
+
declare function useProductActivations(productId: string | null | undefined): PagedAccountData<Activation>;
|
|
563
|
+
/** The signed-in customer's licenses for a product, a page at a time. */
|
|
564
|
+
declare function useProductLicenses(productId: string | null | undefined): PagedAccountData<License>;
|
|
565
|
+
/** The signed-in customer's completed orders, newest first, a page at a time. */
|
|
566
|
+
declare function useOrderHistory(): PagedAccountData<CompletedOrder>;
|
|
567
|
+
/** The signed-in customer's subscriptions, a page at a time. */
|
|
568
|
+
declare function useSubscriptions(): PagedAccountData<Subscription>;
|
|
569
|
+
/** One of the signed-in customer's subscriptions. Null for no ID. */
|
|
570
|
+
declare function useSubscription(subscriptionId: string | null | undefined): AccountData<Subscription>;
|
|
571
|
+
|
|
430
572
|
interface ActivationRequestState {
|
|
431
573
|
/** The request the app on the customer's computer made, or null until it has loaded. */
|
|
432
574
|
activationRequest: ActivationRequest | null;
|
|
@@ -445,12 +587,46 @@ interface ActivationRequestState {
|
|
|
445
587
|
/** Turns the request down, as a customer who didn't make it would. */
|
|
446
588
|
cancel: () => Promise<void>;
|
|
447
589
|
}
|
|
590
|
+
interface ActivationRequestOptions {
|
|
591
|
+
/**
|
|
592
|
+
* Activates with a license the signed-in customer owns as soon as the request says they can: once
|
|
593
|
+
* `activationRequest.licenseEligibility.eligible` is true while it is still `Requested`. Tried once per
|
|
594
|
+
* request. A failure is left in `error`, and `fulfillLicense` tries again.
|
|
595
|
+
*/
|
|
596
|
+
autoFulfill?: boolean;
|
|
597
|
+
}
|
|
448
598
|
/**
|
|
449
599
|
* Activating a product in the browser: the page an app opens with a `token` when its customer chooses
|
|
450
600
|
* to activate online. Loads the request, again when someone signs in, and once it has been granted
|
|
451
|
-
* waits for the app to pick it up. Without a token, as on a link that lost it, it loads nothing.
|
|
601
|
+
* waits for the app to pick it up. Without a token, as on a link that lost it, it loads nothing. With
|
|
602
|
+
* `autoFulfill`, it activates with the customer's license as soon as the request says they can.
|
|
603
|
+
*/
|
|
604
|
+
declare function useActivationRequest(token: string | null | undefined, options?: ActivationRequestOptions): ActivationRequestState;
|
|
605
|
+
|
|
606
|
+
interface AttributionConsent {
|
|
607
|
+
/**
|
|
608
|
+
* Whether the store asks this visitor to agree to marketing cookies, as its "Ad and campaign tracking"
|
|
609
|
+
* setting decides for where they are, or `undefined` until the browser has loaded the catalog for them.
|
|
610
|
+
*/
|
|
611
|
+
required: boolean | undefined;
|
|
612
|
+
/** The visitor's answer, remembered in this browser across visits, or `null` until they answer. */
|
|
613
|
+
answer: boolean | null;
|
|
614
|
+
/** Records the visitor's answer, remembers it, and has the storefront follow it. */
|
|
615
|
+
setAnswer: (granted: boolean) => void;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* What a cookie banner of the site's own needs: ask when `required` is `true` and `answer` is `null`, and
|
|
619
|
+
* hand the visitor's choice to `setAnswer`. The storefront remembers it in this browser and starts from it
|
|
620
|
+
* on their next visit, unless `attributionConsent` is passed to `createStorefront`. The banner itself is
|
|
621
|
+
* the site's.
|
|
622
|
+
*
|
|
623
|
+
* ```tsx
|
|
624
|
+
* const { required, answer, setAnswer } = useAttributionConsent()
|
|
625
|
+
* if (required && answer === null)
|
|
626
|
+
* return <Banner onAccept={() => setAnswer(true)} onDecline={() => setAnswer(false)} />
|
|
627
|
+
* ```
|
|
452
628
|
*/
|
|
453
|
-
declare function
|
|
629
|
+
declare function useAttributionConsent(): AttributionConsent;
|
|
454
630
|
|
|
455
631
|
interface Auth extends StorefrontAuth {
|
|
456
632
|
/** The signed-in customer, or null. */
|
|
@@ -487,6 +663,17 @@ interface Cart extends StorefrontCart {
|
|
|
487
663
|
}
|
|
488
664
|
/** The cart, and changing it. Every function keeps its identity across renders. */
|
|
489
665
|
declare function useCart(): Cart;
|
|
666
|
+
/**
|
|
667
|
+
* The currency the cart shows and checks out in: the one the store suggests for this visitor. Renders again
|
|
668
|
+
* only when that changes, not on every change to the cart, so a product card can price itself without
|
|
669
|
+
* `useCart`.
|
|
670
|
+
*/
|
|
671
|
+
declare function useCurrency(): string;
|
|
672
|
+
/**
|
|
673
|
+
* Changing the cart, `addToCart` and the rest, without reading it: a component using only this never
|
|
674
|
+
* renders again because the cart changed. The same object every time.
|
|
675
|
+
*/
|
|
676
|
+
declare function useCartActions(): StorefrontCart;
|
|
490
677
|
|
|
491
678
|
/**
|
|
492
679
|
* Buying straight away, without the cart: the function a "Buy now" button calls.
|
|
@@ -579,6 +766,29 @@ interface Orders {
|
|
|
579
766
|
/** The signed-in customer's purchases, for an account area of your own. */
|
|
580
767
|
declare function useOrders(): Orders;
|
|
581
768
|
|
|
769
|
+
/** A pricing option's price in the visitor's currency, written the way the checkout writes it. */
|
|
770
|
+
interface VariationPrice {
|
|
771
|
+
/** The visitor's currency, which the cart and the checkout use too. */
|
|
772
|
+
currency: string;
|
|
773
|
+
/** What it costs, or undefined until the catalog has a price for it in this currency. */
|
|
774
|
+
price: string | undefined;
|
|
775
|
+
/** What it cost before its discount, when it has one. */
|
|
776
|
+
originalPrice: string | undefined;
|
|
777
|
+
/** What its discount takes off, when it has one. */
|
|
778
|
+
saving: string | undefined;
|
|
779
|
+
/** What it costs as a number, for adding prices up. */
|
|
780
|
+
amount: number | undefined;
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* A pricing option's price, before and after its discount, in the visitor's currency. Renders again when
|
|
784
|
+
* the currency or the option changes, not when the cart does.
|
|
785
|
+
*
|
|
786
|
+
* ```tsx
|
|
787
|
+
* const { price, originalPrice } = usePrice(product.defaultVariation)
|
|
788
|
+
* ```
|
|
789
|
+
*/
|
|
790
|
+
declare function usePrice(variation: PricingVariation | null | undefined, format?: PriceFormat): VariationPrice;
|
|
791
|
+
|
|
582
792
|
type Reviews = MoonbaseClient['reviews'];
|
|
583
793
|
interface ProductReviews {
|
|
584
794
|
/** Whether the store takes reviews. Off until the catalog has loaded, and on older backends. */
|
|
@@ -623,7 +833,10 @@ interface Vouchers {
|
|
|
623
833
|
declare function useVoucher(): Vouchers;
|
|
624
834
|
|
|
625
835
|
interface StorefrontProviderProps {
|
|
626
|
-
/**
|
|
836
|
+
/**
|
|
837
|
+
* The storefront: one `createStorefront` made outside of any component, or one `getStorefront` hands
|
|
838
|
+
* back in a component.
|
|
839
|
+
*/
|
|
627
840
|
storefront: MoonbaseStorefront;
|
|
628
841
|
children?: ReactNode;
|
|
629
842
|
}
|
|
@@ -645,6 +858,11 @@ declare function useStorefrontState<T>(selector: (state: StorefrontState) => T):
|
|
|
645
858
|
* ```tsx
|
|
646
859
|
* useStorefrontEvent('checkoutCompleted', order => setThanks(order))
|
|
647
860
|
* ```
|
|
861
|
+
*
|
|
862
|
+
* It starts listening once the component has mounted, after hydration, so it misses what the storefront
|
|
863
|
+
* reported before then, such as the first `catalogUpdated` on a slow phone. Read what has to be known
|
|
864
|
+
* from the start from the state instead, with `useStorefrontState`, or subscribe with `storefront.on`
|
|
865
|
+
* where the storefront is made.
|
|
648
866
|
*/
|
|
649
867
|
declare function useStorefrontEvent<K extends keyof StorefrontEvents>(event: K, handler: StorefrontEvents[K]): void;
|
|
650
868
|
|
|
@@ -663,8 +881,30 @@ declare function useStorefrontEvent<K extends keyof StorefrontEvents>(event: K,
|
|
|
663
881
|
* ```
|
|
664
882
|
*
|
|
665
883
|
* Coming back from the hosted checkout, it takes Moonbase's parameters off the address as it is made,
|
|
666
|
-
* so make it before a router that reads the address.
|
|
884
|
+
* so make it before a router that reads the address. To make it in a component instead, such as from
|
|
885
|
+
* a catalog the server fetched, use `getStorefront`.
|
|
667
886
|
*/
|
|
668
887
|
declare function createStorefront(endpoint: string, options?: StorefrontOptions): MoonbaseStorefront;
|
|
888
|
+
/**
|
|
889
|
+
* The storefront for `endpoint`: in a browser, made the first time it is asked for and the same one every
|
|
890
|
+
* time after, and on a server, a new one each time, as each render there is for another visitor. For
|
|
891
|
+
* making the storefront in a component, such as a Next.js layout's providers starting it from a catalog
|
|
892
|
+
* the server fetched:
|
|
893
|
+
*
|
|
894
|
+
* ```tsx
|
|
895
|
+
* 'use client'
|
|
896
|
+
*
|
|
897
|
+
* export function Providers({ catalog, children }: { catalog: Storefront, children: ReactNode }) {
|
|
898
|
+
* const storefront = getStorefront('https://demo.moonbase.sh', { initialCatalog: catalog })
|
|
899
|
+
* return <StorefrontProvider storefront={storefront}>{children}</StorefrontProvider>
|
|
900
|
+
* }
|
|
901
|
+
* ```
|
|
902
|
+
*
|
|
903
|
+
* React can render a component, and run a `useState` initializer, more than once for one page, and each
|
|
904
|
+
* `createStorefront` there would be another storefront following the tab. `options` only count when the
|
|
905
|
+
* storefront is made, so a later `initialCatalog` is ignored: the storefront already has a fresher
|
|
906
|
+
* catalog by then. Once the storefront is disposed, the next call makes a new one.
|
|
907
|
+
*/
|
|
908
|
+
declare function getStorefront(endpoint: string, options?: StorefrontOptions): MoonbaseStorefront;
|
|
669
909
|
|
|
670
|
-
export { type ActivationRequestState, type AddToCartOptions, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VendorState, type Vouchers, createStorefront, useActivationRequest, useAuth, useBundle, useBundles, useCart, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrders, useProduct, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useVendor, useVoucher };
|
|
910
|
+
export { type AccountData, type ActivationRequestOptions, type ActivationRequestState, type AddToCartOptions, type AttributionConsent, type AttributionConsentStatus, type AttributionConsentStatusByPlatform, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type PagedAccountData, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VariationPrice, type VendorState, type Vouchers, createStorefront, getStorefront, useActivationRequest, useAttributionConsent, useAuth, useBundle, useBundles, useCart, useCartActions, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useCurrency, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrderHistory, useOrders, useOwnedProduct, useOwnedProducts, usePrice, useProduct, useProductActivations, useProductLicenses, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useSubscription, useSubscriptions, useVendor, useVoucher };
|