@usethrottle/catalog 1.1.0 → 1.3.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +135 -24
  3. package/dist/admin.cjs +16 -16
  4. package/dist/admin.d.cts +6 -2
  5. package/dist/admin.d.ts +6 -2
  6. package/dist/admin.js +1 -1
  7. package/dist/{catalog-error-s1BINNkt.d.cts → catalog-error-BOaSKgY3.d.cts} +12 -2
  8. package/dist/{catalog-error-C_zLOtYA.d.ts → catalog-error-CSoPS0-d.d.ts} +12 -2
  9. package/dist/{chunk-4ORXUW2U.cjs → chunk-2YQNMBDI.cjs} +119 -33
  10. package/dist/{chunk-JK2UY4CD.js → chunk-3DLH56OX.js} +99 -13
  11. package/dist/chunk-CADIHOIR.js +496 -0
  12. package/dist/chunk-GU63WFXI.cjs +496 -0
  13. package/dist/{client-c2F-eCaL.d.ts → client-6j1Iz-iJ.d.cts} +2 -9
  14. package/dist/{client-BAoq6jmM.d.cts → client-B3mL2-xJ.d.ts} +2 -9
  15. package/dist/{contract-types-DhiThMEr.d.cts → contract-types-BRyonOYH.d.cts} +23 -21
  16. package/dist/{contract-types-DhiThMEr.d.ts → contract-types-BRyonOYH.d.ts} +23 -21
  17. package/dist/index.cjs +24 -2
  18. package/dist/index.d.cts +13 -7
  19. package/dist/index.d.ts +13 -7
  20. package/dist/index.js +25 -3
  21. package/dist/react-extras.cjs +42 -0
  22. package/dist/react-extras.d.cts +23 -0
  23. package/dist/react-extras.d.ts +23 -0
  24. package/dist/react-extras.js +42 -0
  25. package/dist/react.cjs +286 -491
  26. package/dist/react.d.cts +44 -38
  27. package/dist/react.d.ts +44 -38
  28. package/dist/react.js +289 -494
  29. package/dist/server.cjs +19 -14
  30. package/dist/server.d.cts +4 -3
  31. package/dist/server.d.ts +4 -3
  32. package/dist/server.js +7 -2
  33. package/dist/{types-DsjoG8ce.d.cts → types-BfveENg1.d.ts} +76 -3
  34. package/dist/{types-xQLID-4j.d.ts → types-D0PywKnN.d.cts} +76 -3
  35. package/dist/url.d.cts +2 -1
  36. package/dist/url.d.ts +2 -1
  37. package/dist/use-query-CR7r5tam.d.ts +40 -0
  38. package/dist/use-query-UWf6Pi5g.d.cts +40 -0
  39. package/dist/version-ev3p03v2.d.cts +8 -0
  40. package/dist/version-ev3p03v2.d.ts +8 -0
  41. package/package.json +13 -2
package/dist/react.d.cts CHANGED
@@ -1,15 +1,32 @@
1
- import { CatalogClient } from './index.cjs';
2
- export { S as SDK_VERSION } from './client-BAoq6jmM.cjs';
1
+ export { ReviewEligibility } from './index.cjs';
3
2
  import { ReactNode } from 'react';
3
+ import { CatalogClient } from './index.cjs';
4
+ import { C as CustomerCartSource } from './types-D0PywKnN.cjs';
5
+ import { CategoryTreeDepth, ProductsQuery, ThrottleCart, CartWarning } from './index.cjs';
6
+ export { O as OpenCartRef } from './types-D0PywKnN.cjs';
7
+ import { Q as QueryOptions, a as QueryResult } from './use-query-UWf6Pi5g.cjs';
8
+ export { c as catalogQueryKey } from './use-query-UWf6Pi5g.cjs';
4
9
  import { CatalogError } from './admin.cjs';
5
- import { CategoryTreeDepth, ProductsQuery, ThrottleCart } from './index.cjs';
6
10
  import { ReviewSubmitAnswer, ReviewSubmitInput } from './index.cjs';
11
+ export { S as SDK_VERSION } from './version-ev3p03v2.cjs';
12
+ import '@usethrottle/money';
7
13
 
8
- /** Where `useCart` keeps the cart session id (P6-R15). The default is memory only. */
14
+ /**
15
+ * Where `useCart` keeps the cart session id (P6-R15). The default is memory
16
+ * only. The optional per-currency slots let a guest's `switchCurrency` find
17
+ * the cart it already has in that currency (MC2-R41); without them a guest
18
+ * switch makes a new cart. They hold a guest's carts only: `useCart` never
19
+ * writes them while signed in, and forgets them on a sign-in, a sign-out or
20
+ * a customer change (`removeFor`, else `setFor(currency, '')`).
21
+ */
9
22
  type CartStorage = {
10
23
  get(): string | null | Promise<string | null>;
11
24
  set(id: string): void | Promise<void>;
12
25
  remove(): void | Promise<void>;
26
+ /** The session id last saved for `currency` (upper case), or `null`. */
27
+ getFor?(currency: string): string | null | Promise<string | null>;
28
+ setFor?(currency: string, id: string): void | Promise<void>;
29
+ removeFor?(currency: string): void | Promise<void>;
13
30
  };
14
31
  /** In memory only: the cart is forgotten on reload. The default `cartStorage`. */
15
32
  declare function createMemoryCartStorage(): CartStorage;
@@ -22,6 +39,7 @@ type CartUrls = {
22
39
  cancelUrl?: string;
23
40
  storefrontUrl?: string;
24
41
  };
42
+
25
43
  type CatalogProviderProps = {
26
44
  client: CatalogClient;
27
45
  /**
@@ -31,6 +49,8 @@ type CatalogProviderProps = {
31
49
  */
32
50
  customerKey?: string | null;
33
51
  cartStorage?: CartStorage;
52
+ /** Resumes the signed-in customer's cart on any device (P8-R14). */
53
+ customerCart?: CustomerCartSource;
34
54
  cartUrls?: CartUrls;
35
55
  throttleBaseUrl?: string;
36
56
  throttleCheckoutUrl?: string;
@@ -42,37 +62,11 @@ type CatalogProviderProps = {
42
62
  * limit is the visitor's, not the customer's.
43
63
  */
44
64
  declare function CatalogProvider(props: CatalogProviderProps): ReactNode;
65
+ /** The provider's `customerKey` (`null` when signed out). */
66
+ declare function useCustomerKey(): string | null;
45
67
  /** The provider's client. */
46
68
  declare function useCatalog(): CatalogClient;
47
69
 
48
- type QueryResult<T> = {
49
- data: T | undefined;
50
- error: CatalogError | undefined;
51
- /** No data and no error yet, and the hook is enabled. */
52
- isLoading: boolean;
53
- /** A request for this key is in flight (first load or revalidation). */
54
- isValidating: boolean;
55
- /** Fetches again now (unless a rate-limit pause holds every read). Never rejects. */
56
- refetch(): Promise<void>;
57
- };
58
- type QueryOptions<T> = {
59
- /** `false` makes no request; `data` stays `undefined`. */
60
- enabled?: boolean;
61
- /**
62
- * Shown at once (SSR hand-off). Treated as fetched at mount when the
63
- * provider has no `customerKey`; under a signed-in customer it is
64
- * revalidated at once, since a server render is usually anonymous (P6-R51).
65
- */
66
- initialData?: T;
67
- /** Cached data younger than this is not fetched again; default `STALE_MS`. */
68
- staleTimeMs?: number;
69
- };
70
- /**
71
- * The cache key of a read: the route name and its canonical parameters.
72
- * Hooks prefix it with the provider's `customerKey` (P6-R14).
73
- */
74
- declare function catalogQueryKey(route: string, params: unknown): string;
75
-
76
70
  type Answer<F extends (...args: never[]) => unknown> = Awaited<ReturnType<F>>;
77
71
  type Products = CatalogClient['products'];
78
72
  declare function useProducts(query?: ProductsQuery, options?: QueryOptions<Answer<Products['list']>>): QueryResult<Answer<Products['list']>>;
@@ -116,6 +110,12 @@ type CartState = {
116
110
  error: CatalogError | undefined;
117
111
  /** A change is queued or running. */
118
112
  pending: boolean;
113
+ /**
114
+ * What the last `switchCurrency` could not carry over: `lines_removed`
115
+ * (lines with no price in the new currency, left behind) and
116
+ * `discount_dropped` (a code the new currency refuses). Empty otherwise.
117
+ */
118
+ warnings: CartWarning[];
119
119
  };
120
120
  type CartOptions = {
121
121
  currency?: string;
@@ -138,14 +138,20 @@ type UseCartResult = CartState & {
138
138
  checkoutUrl: string;
139
139
  }>;
140
140
  /**
141
- * A new cart with the current lines in `currency` (BC4); the old one is
142
- * left to Throttle. It lands whole or not at all: if the new cart cannot be
143
- * made, read back or saved, it rejects and the old cart, its stored id and
144
- * the old currency stay (the unused new cart is left to Throttle).
141
+ * Moves the cart into `currency`, keeping one cart per currency (MC2-R41):
142
+ * the shopper's open cart in `currency` (the signed-in customer's from
143
+ * `customerCart`, a guest's from `cartStorage.getFor`), else a new empty
144
+ * one, absorbs the current cart, which Throttle then closes as merged. Lines
145
+ * with no price in `currency` stay behind and a discount it refuses is
146
+ * dropped; both show in `warnings`. If anything fails before the absorb
147
+ * lands, it rejects and the old cart, its stored id and the old currency
148
+ * stay (an unused new cart is left to Throttle).
145
149
  */
146
150
  switchCurrency(currency: string): Promise<void>;
147
151
  /** Forgets the cart (no request). */
148
152
  clear(): Promise<void>;
153
+ /** `formatMoney(minor, currency)` in the cart's currency (before a cart, the hook's or the client's). */
154
+ format(minor: number): string;
149
155
  };
150
156
  /**
151
157
  * The provider's cart (P6-R15). The session id is kept in the provider's
@@ -178,5 +184,5 @@ type SubmitReviewResult = SubmitReviewState & {
178
184
  */
179
185
  declare function useSubmitReview(handle: string): SubmitReviewResult;
180
186
 
181
- export { CatalogProvider, catalogQueryKey, createMemoryCartStorage, useCart, useCatalog, useCategories, useCollection, useConfig, useFilterValues, useProduct, useProducts, useRelated, useReviews, useSubmitReview, useSuggest };
182
- export type { CartOptions, CartState, CartStorage, CartUrls, CatalogProviderProps, QueryOptions, QueryResult, SubmitReviewResult, SubmitReviewState, UseCartResult };
187
+ export { CatalogProvider, CustomerCartSource, QueryOptions, QueryResult, createMemoryCartStorage, useCart, useCatalog, useCategories, useCollection, useConfig, useCustomerKey, useFilterValues, useProduct, useProducts, useRelated, useReviews, useSubmitReview, useSuggest };
188
+ export type { CartOptions, CartState, CartStorage, CartUrls, CatalogProviderProps, SubmitReviewResult, SubmitReviewState, UseCartResult };
package/dist/react.d.ts CHANGED
@@ -1,15 +1,32 @@
1
- import { CatalogClient } from './index.js';
2
- export { S as SDK_VERSION } from './client-c2F-eCaL.js';
1
+ export { ReviewEligibility } from './index.js';
3
2
  import { ReactNode } from 'react';
3
+ import { CatalogClient } from './index.js';
4
+ import { C as CustomerCartSource } from './types-BfveENg1.js';
5
+ import { CategoryTreeDepth, ProductsQuery, ThrottleCart, CartWarning } from './index.js';
6
+ export { O as OpenCartRef } from './types-BfveENg1.js';
7
+ import { Q as QueryOptions, a as QueryResult } from './use-query-CR7r5tam.js';
8
+ export { c as catalogQueryKey } from './use-query-CR7r5tam.js';
4
9
  import { CatalogError } from './admin.js';
5
- import { CategoryTreeDepth, ProductsQuery, ThrottleCart } from './index.js';
6
10
  import { ReviewSubmitAnswer, ReviewSubmitInput } from './index.js';
11
+ export { S as SDK_VERSION } from './version-ev3p03v2.js';
12
+ import '@usethrottle/money';
7
13
 
8
- /** Where `useCart` keeps the cart session id (P6-R15). The default is memory only. */
14
+ /**
15
+ * Where `useCart` keeps the cart session id (P6-R15). The default is memory
16
+ * only. The optional per-currency slots let a guest's `switchCurrency` find
17
+ * the cart it already has in that currency (MC2-R41); without them a guest
18
+ * switch makes a new cart. They hold a guest's carts only: `useCart` never
19
+ * writes them while signed in, and forgets them on a sign-in, a sign-out or
20
+ * a customer change (`removeFor`, else `setFor(currency, '')`).
21
+ */
9
22
  type CartStorage = {
10
23
  get(): string | null | Promise<string | null>;
11
24
  set(id: string): void | Promise<void>;
12
25
  remove(): void | Promise<void>;
26
+ /** The session id last saved for `currency` (upper case), or `null`. */
27
+ getFor?(currency: string): string | null | Promise<string | null>;
28
+ setFor?(currency: string, id: string): void | Promise<void>;
29
+ removeFor?(currency: string): void | Promise<void>;
13
30
  };
14
31
  /** In memory only: the cart is forgotten on reload. The default `cartStorage`. */
15
32
  declare function createMemoryCartStorage(): CartStorage;
@@ -22,6 +39,7 @@ type CartUrls = {
22
39
  cancelUrl?: string;
23
40
  storefrontUrl?: string;
24
41
  };
42
+
25
43
  type CatalogProviderProps = {
26
44
  client: CatalogClient;
27
45
  /**
@@ -31,6 +49,8 @@ type CatalogProviderProps = {
31
49
  */
32
50
  customerKey?: string | null;
33
51
  cartStorage?: CartStorage;
52
+ /** Resumes the signed-in customer's cart on any device (P8-R14). */
53
+ customerCart?: CustomerCartSource;
34
54
  cartUrls?: CartUrls;
35
55
  throttleBaseUrl?: string;
36
56
  throttleCheckoutUrl?: string;
@@ -42,37 +62,11 @@ type CatalogProviderProps = {
42
62
  * limit is the visitor's, not the customer's.
43
63
  */
44
64
  declare function CatalogProvider(props: CatalogProviderProps): ReactNode;
65
+ /** The provider's `customerKey` (`null` when signed out). */
66
+ declare function useCustomerKey(): string | null;
45
67
  /** The provider's client. */
46
68
  declare function useCatalog(): CatalogClient;
47
69
 
48
- type QueryResult<T> = {
49
- data: T | undefined;
50
- error: CatalogError | undefined;
51
- /** No data and no error yet, and the hook is enabled. */
52
- isLoading: boolean;
53
- /** A request for this key is in flight (first load or revalidation). */
54
- isValidating: boolean;
55
- /** Fetches again now (unless a rate-limit pause holds every read). Never rejects. */
56
- refetch(): Promise<void>;
57
- };
58
- type QueryOptions<T> = {
59
- /** `false` makes no request; `data` stays `undefined`. */
60
- enabled?: boolean;
61
- /**
62
- * Shown at once (SSR hand-off). Treated as fetched at mount when the
63
- * provider has no `customerKey`; under a signed-in customer it is
64
- * revalidated at once, since a server render is usually anonymous (P6-R51).
65
- */
66
- initialData?: T;
67
- /** Cached data younger than this is not fetched again; default `STALE_MS`. */
68
- staleTimeMs?: number;
69
- };
70
- /**
71
- * The cache key of a read: the route name and its canonical parameters.
72
- * Hooks prefix it with the provider's `customerKey` (P6-R14).
73
- */
74
- declare function catalogQueryKey(route: string, params: unknown): string;
75
-
76
70
  type Answer<F extends (...args: never[]) => unknown> = Awaited<ReturnType<F>>;
77
71
  type Products = CatalogClient['products'];
78
72
  declare function useProducts(query?: ProductsQuery, options?: QueryOptions<Answer<Products['list']>>): QueryResult<Answer<Products['list']>>;
@@ -116,6 +110,12 @@ type CartState = {
116
110
  error: CatalogError | undefined;
117
111
  /** A change is queued or running. */
118
112
  pending: boolean;
113
+ /**
114
+ * What the last `switchCurrency` could not carry over: `lines_removed`
115
+ * (lines with no price in the new currency, left behind) and
116
+ * `discount_dropped` (a code the new currency refuses). Empty otherwise.
117
+ */
118
+ warnings: CartWarning[];
119
119
  };
120
120
  type CartOptions = {
121
121
  currency?: string;
@@ -138,14 +138,20 @@ type UseCartResult = CartState & {
138
138
  checkoutUrl: string;
139
139
  }>;
140
140
  /**
141
- * A new cart with the current lines in `currency` (BC4); the old one is
142
- * left to Throttle. It lands whole or not at all: if the new cart cannot be
143
- * made, read back or saved, it rejects and the old cart, its stored id and
144
- * the old currency stay (the unused new cart is left to Throttle).
141
+ * Moves the cart into `currency`, keeping one cart per currency (MC2-R41):
142
+ * the shopper's open cart in `currency` (the signed-in customer's from
143
+ * `customerCart`, a guest's from `cartStorage.getFor`), else a new empty
144
+ * one, absorbs the current cart, which Throttle then closes as merged. Lines
145
+ * with no price in `currency` stay behind and a discount it refuses is
146
+ * dropped; both show in `warnings`. If anything fails before the absorb
147
+ * lands, it rejects and the old cart, its stored id and the old currency
148
+ * stay (an unused new cart is left to Throttle).
145
149
  */
146
150
  switchCurrency(currency: string): Promise<void>;
147
151
  /** Forgets the cart (no request). */
148
152
  clear(): Promise<void>;
153
+ /** `formatMoney(minor, currency)` in the cart's currency (before a cart, the hook's or the client's). */
154
+ format(minor: number): string;
149
155
  };
150
156
  /**
151
157
  * The provider's cart (P6-R15). The session id is kept in the provider's
@@ -178,5 +184,5 @@ type SubmitReviewResult = SubmitReviewState & {
178
184
  */
179
185
  declare function useSubmitReview(handle: string): SubmitReviewResult;
180
186
 
181
- export { CatalogProvider, catalogQueryKey, createMemoryCartStorage, useCart, useCatalog, useCategories, useCollection, useConfig, useFilterValues, useProduct, useProducts, useRelated, useReviews, useSubmitReview, useSuggest };
182
- export type { CartOptions, CartState, CartStorage, CartUrls, CatalogProviderProps, QueryOptions, QueryResult, SubmitReviewResult, SubmitReviewState, UseCartResult };
187
+ export { CatalogProvider, CustomerCartSource, QueryOptions, QueryResult, createMemoryCartStorage, useCart, useCatalog, useCategories, useCollection, useConfig, useCustomerKey, useFilterValues, useProduct, useProducts, useRelated, useReviews, useSubmitReview, useSuggest };
188
+ export type { CartOptions, CartState, CartStorage, CartUrls, CatalogProviderProps, SubmitReviewResult, SubmitReviewState, UseCartResult };