@webround/commerce 0.0.0-stage → 0.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.
Files changed (170) hide show
  1. package/README.md +192 -2
  2. package/dist/account/account.d.ts +54 -0
  3. package/dist/account/account.js +40 -0
  4. package/dist/account/index.d.ts +3 -0
  5. package/dist/account/index.js +2 -0
  6. package/dist/addresses/api.d.ts +16 -0
  7. package/dist/addresses/api.js +13 -0
  8. package/dist/addresses/book.d.ts +40 -0
  9. package/dist/addresses/book.js +144 -0
  10. package/dist/addresses/index.d.ts +8 -0
  11. package/dist/addresses/index.js +5 -0
  12. package/dist/addresses/rules.d.ts +4 -0
  13. package/dist/addresses/rules.js +9 -0
  14. package/dist/addresses/types.d.ts +48 -0
  15. package/dist/addresses/types.js +6 -0
  16. package/dist/auth/api.d.ts +43 -0
  17. package/dist/auth/api.js +22 -0
  18. package/dist/auth/auth.d.ts +32 -0
  19. package/dist/auth/auth.js +23 -0
  20. package/dist/auth/cookies.d.ts +46 -0
  21. package/dist/auth/cookies.js +156 -0
  22. package/dist/auth/index.d.ts +14 -0
  23. package/dist/auth/index.js +8 -0
  24. package/dist/auth/renewal.d.ts +4 -0
  25. package/dist/auth/renewal.js +37 -0
  26. package/dist/auth/store.d.ts +70 -0
  27. package/dist/auth/store.js +284 -0
  28. package/dist/auth/types.d.ts +105 -0
  29. package/dist/auth/types.js +5 -0
  30. package/dist/auth/wake.d.ts +18 -0
  31. package/dist/auth/wake.js +23 -0
  32. package/dist/cache.d.ts +49 -0
  33. package/dist/cache.js +144 -0
  34. package/dist/canonical.d.ts +7 -0
  35. package/dist/canonical.js +32 -0
  36. package/dist/cart/api.d.ts +25 -0
  37. package/dist/cart/api.js +16 -0
  38. package/dist/cart/index.d.ts +10 -0
  39. package/dist/cart/index.js +6 -0
  40. package/dist/cart/lines.d.ts +15 -0
  41. package/dist/cart/lines.js +23 -0
  42. package/dist/cart/storage.d.ts +30 -0
  43. package/dist/cart/storage.js +82 -0
  44. package/dist/cart/store.d.ts +58 -0
  45. package/dist/cart/store.js +218 -0
  46. package/dist/cart/types.d.ts +194 -0
  47. package/dist/cart/types.js +11 -0
  48. package/dist/catalog/cached.d.ts +47 -0
  49. package/dist/catalog/cached.js +52 -0
  50. package/dist/catalog/catalog.d.ts +10 -0
  51. package/dist/catalog/catalog.js +7 -0
  52. package/dist/catalog/named.d.ts +3 -0
  53. package/dist/catalog/named.js +7 -0
  54. package/dist/catalog/products.d.ts +28 -0
  55. package/dist/catalog/products.js +12 -0
  56. package/dist/catalog/store-data.d.ts +13 -0
  57. package/dist/catalog/store-data.js +6 -0
  58. package/dist/catalog/types.d.ts +430 -0
  59. package/dist/catalog/types.js +4 -0
  60. package/dist/checkout/api.d.ts +14 -0
  61. package/dist/checkout/api.js +7 -0
  62. package/dist/checkout/errors.d.ts +23 -0
  63. package/dist/checkout/errors.js +18 -0
  64. package/dist/checkout/index.d.ts +10 -0
  65. package/dist/checkout/index.js +6 -0
  66. package/dist/checkout/return-urls.d.ts +7 -0
  67. package/dist/checkout/return-urls.js +11 -0
  68. package/dist/checkout/store.d.ts +98 -0
  69. package/dist/checkout/store.js +179 -0
  70. package/dist/checkout/types.d.ts +225 -0
  71. package/dist/checkout/types.js +28 -0
  72. package/dist/commerce.d.ts +55 -0
  73. package/dist/commerce.js +55 -0
  74. package/dist/customer-resources.d.ts +18 -0
  75. package/dist/customer-resources.js +40 -0
  76. package/dist/errors.d.ts +65 -0
  77. package/dist/errors.js +96 -0
  78. package/dist/http.d.ts +49 -0
  79. package/dist/http.js +94 -0
  80. package/dist/index.d.ts +32 -0
  81. package/dist/index.js +22 -0
  82. package/dist/orders/api.d.ts +16 -0
  83. package/dist/orders/api.js +13 -0
  84. package/dist/orders/index.d.ts +6 -0
  85. package/dist/orders/index.js +3 -0
  86. package/dist/orders/orders.d.ts +19 -0
  87. package/dist/orders/orders.js +18 -0
  88. package/dist/orders/types.d.ts +88 -0
  89. package/dist/orders/types.js +4 -0
  90. package/dist/react/index.d.ts +23 -0
  91. package/dist/react/index.js +15 -0
  92. package/dist/react/use-account.d.ts +12 -0
  93. package/dist/react/use-account.js +9 -0
  94. package/dist/react/use-addresses.d.ts +20 -0
  95. package/dist/react/use-addresses.js +20 -0
  96. package/dist/react/use-auth.d.ts +24 -0
  97. package/dist/react/use-auth.js +20 -0
  98. package/dist/react/use-cart.d.ts +27 -0
  99. package/dist/react/use-cart.js +23 -0
  100. package/dist/react/use-checkout.d.ts +37 -0
  101. package/dist/react/use-checkout.js +33 -0
  102. package/dist/react/use-customer-resource.d.ts +5 -0
  103. package/dist/react/use-customer-resource.js +9 -0
  104. package/dist/react/use-orders.d.ts +14 -0
  105. package/dist/react/use-orders.js +6 -0
  106. package/dist/react/use-resource.d.ts +25 -0
  107. package/dist/react/use-resource.js +60 -0
  108. package/dist/react/use-returns.d.ts +14 -0
  109. package/dist/react/use-returns.js +7 -0
  110. package/dist/react/use-reviews.d.ts +14 -0
  111. package/dist/react/use-reviews.js +10 -0
  112. package/dist/react/use-shop-address.d.ts +12 -0
  113. package/dist/react/use-shop-address.js +39 -0
  114. package/dist/react/use-shop.d.ts +34 -0
  115. package/dist/react/use-shop.js +31 -0
  116. package/dist/react/use-wishlist.d.ts +15 -0
  117. package/dist/react/use-wishlist.js +12 -0
  118. package/dist/resource.d.ts +19 -0
  119. package/dist/resource.js +15 -0
  120. package/dist/returns/api.d.ts +27 -0
  121. package/dist/returns/api.js +19 -0
  122. package/dist/returns/index.d.ts +6 -0
  123. package/dist/returns/index.js +3 -0
  124. package/dist/returns/returns.d.ts +30 -0
  125. package/dist/returns/returns.js +31 -0
  126. package/dist/returns/rules.d.ts +7 -0
  127. package/dist/returns/rules.js +45 -0
  128. package/dist/returns/types.d.ts +99 -0
  129. package/dist/returns/types.js +2 -0
  130. package/dist/reviews/api.d.ts +19 -0
  131. package/dist/reviews/api.js +15 -0
  132. package/dist/reviews/index.d.ts +9 -0
  133. package/dist/reviews/index.js +5 -0
  134. package/dist/reviews/reviews.d.ts +23 -0
  135. package/dist/reviews/reviews.js +37 -0
  136. package/dist/reviews/rules.d.ts +8 -0
  137. package/dist/reviews/rules.js +7 -0
  138. package/dist/reviews/types.d.ts +54 -0
  139. package/dist/reviews/types.js +10 -0
  140. package/dist/shop/address-store.d.ts +14 -0
  141. package/dist/shop/address-store.js +28 -0
  142. package/dist/shop/address.d.ts +12 -0
  143. package/dist/shop/address.js +95 -0
  144. package/dist/shop/index.d.ts +16 -0
  145. package/dist/shop/index.js +9 -0
  146. package/dist/shop/question.d.ts +15 -0
  147. package/dist/shop/question.js +12 -0
  148. package/dist/shop/range.d.ts +7 -0
  149. package/dist/shop/range.js +38 -0
  150. package/dist/shop/sort.d.ts +19 -0
  151. package/dist/shop/sort.js +18 -0
  152. package/dist/shop/state.d.ts +47 -0
  153. package/dist/shop/state.js +51 -0
  154. package/dist/shop/store.d.ts +26 -0
  155. package/dist/shop/store.js +47 -0
  156. package/dist/shop/to-search.d.ts +14 -0
  157. package/dist/shop/to-search.js +53 -0
  158. package/dist/site.d.ts +7 -0
  159. package/dist/site.js +41 -0
  160. package/dist/store-kit.d.ts +39 -0
  161. package/dist/store-kit.js +81 -0
  162. package/dist/wishlist/api.d.ts +12 -0
  163. package/dist/wishlist/api.js +10 -0
  164. package/dist/wishlist/index.d.ts +7 -0
  165. package/dist/wishlist/index.js +4 -0
  166. package/dist/wishlist/types.d.ts +30 -0
  167. package/dist/wishlist/types.js +4 -0
  168. package/dist/wishlist/wishlist.d.ts +45 -0
  169. package/dist/wishlist/wishlist.js +84 -0
  170. package/package.json +67 -5
@@ -0,0 +1,84 @@
1
+ import { CommerceError, toCommerceError } from '../errors.js';
2
+ import { createSerialQueue, createStateHolder, followAuth } from '../store-kit.js';
3
+ import { WISHLIST_ERROR_CODES } from './types.js';
4
+ const idsOf = (items) => items.flatMap((i) => i.product.variants.map((v) => v.id));
5
+ /** Whether the store said the wishlist is off (its 403): a refusal that is not about the customer. */
6
+ const isDisabled = (error) => error.status === 403 && /disabled/i.test(error.message);
7
+ export function createWishlist(options) {
8
+ const { api, auth } = options;
9
+ const holder = createStateHolder({ owner: 'unknown', items: [], variantIds: [], count: 0, loaded: false, enabled: null, busy: false, error: null });
10
+ const set = holder.set;
11
+ const queue = createSerialQueue((busy) => set({ busy }));
12
+ // Counts the times the wishlist changed hands: an answer that comes after that is about a wishlist that is not this one any more
13
+ let epoch = 0;
14
+ async function loadCustomer() {
15
+ const mine = epoch;
16
+ try {
17
+ const list = await api.list();
18
+ if (epoch !== mine)
19
+ return;
20
+ set({ items: list.data, variantIds: idsOf(list.data), count: list.data.length, loaded: true, enabled: true, error: null });
21
+ }
22
+ catch (error) {
23
+ if (epoch !== mine)
24
+ return;
25
+ const failure = toCommerceError(error);
26
+ set(isDisabled(failure) ? { items: [], variantIds: [], count: 0, loaded: true, enabled: false, error: null } : { error: failure });
27
+ }
28
+ }
29
+ function toGuest() {
30
+ epoch++;
31
+ set({ owner: 'guest', items: [], variantIds: [], count: 0, loaded: true, error: null });
32
+ }
33
+ function toCustomer() {
34
+ epoch++;
35
+ set({ owner: 'customer', items: [], variantIds: [], count: 0, loaded: false, error: null });
36
+ void queue.run(loadCustomer);
37
+ }
38
+ const following = followAuth(auth, { current: () => holder.get().owner, toGuest, toCustomer });
39
+ return {
40
+ getState: holder.get,
41
+ subscribe: holder.subscribe,
42
+ start: () => following.start(),
43
+ async toggle(variantId) {
44
+ if (holder.get().owner === 'unknown')
45
+ await following.start();
46
+ if (holder.get().owner !== 'customer')
47
+ throw new CommerceError({ kind: 'http', status: 401, code: WISHLIST_ERROR_CODES.loginRequired, message: 'Log in to use the wishlist.' });
48
+ const mine = epoch;
49
+ // What a heart shows changes at once: the store settles it
50
+ const before = holder.get().variantIds;
51
+ const next = before.includes(variantId) ? before.filter((id) => id !== variantId) : [...before, variantId];
52
+ set({ variantIds: next, count: next.length });
53
+ return queue.run(async () => {
54
+ try {
55
+ const answer = await api.toggle(variantId);
56
+ return { wasAdded: answer.wasAdded };
57
+ }
58
+ catch (error) {
59
+ const failure = toCommerceError(error);
60
+ if (epoch === mine) {
61
+ // What was shown at once was not so: it goes back now, whether or not the read that follows can be made
62
+ set(isDisabled(failure) ? { variantIds: before, count: before.length, enabled: false } : { variantIds: before, count: before.length });
63
+ }
64
+ throw failure;
65
+ }
66
+ finally {
67
+ // What is shown is what the store holds, a refusal included
68
+ if (epoch === mine && queue.pending() === 1)
69
+ await loadCustomer();
70
+ }
71
+ });
72
+ },
73
+ async refresh() {
74
+ if (holder.get().owner === 'unknown')
75
+ await following.start();
76
+ if (holder.get().owner === 'customer')
77
+ await queue.run(loadCustomer);
78
+ },
79
+ dispose: following.stop,
80
+ };
81
+ }
82
+ /** Whether a variant is in the wishlist (what a heart asks). */
83
+ export const isInWishlist = (state, variantId) => state.variantIds.includes(variantId);
84
+ //# sourceMappingURL=wishlist.js.map
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
- "name": "@webround/commerce",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@webround/commerce",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "development": "./src/index.ts"
13
+ },
14
+ "./react": {
15
+ "import": "./dist/react/index.js",
16
+ "types": "./dist/react/index.d.ts",
17
+ "development": "./src/react/index.ts"
18
+ },
19
+ "./site": {
20
+ "import": "./dist/site.js",
21
+ "types": "./dist/site.d.ts",
22
+ "development": "./src/site.ts"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "!dist/**/*.map"
28
+ ],
29
+ "scripts": {
30
+ "build": "tsc",
31
+ "dev": "tsc --watch",
32
+ "test": "node --test test/*.test.ts",
33
+ "typecheck": "tsc --noEmit -p tsconfig.test.json",
34
+ "smoke:catalog": "node smoke/catalog.smoke.ts",
35
+ "smoke:auth": "node smoke/auth.smoke.ts",
36
+ "smoke:cart": "node smoke/cart.smoke.ts",
37
+ "smoke:checkout": "node smoke/checkout.smoke.ts",
38
+ "smoke:addresses": "node smoke/addresses.smoke.ts",
39
+ "smoke:orders": "node smoke/orders.smoke.ts",
40
+ "smoke:returns": "node smoke/returns.smoke.ts",
41
+ "smoke:wishlist": "node smoke/wishlist.smoke.ts",
42
+ "smoke:reviews": "node smoke/reviews.smoke.ts"
43
+ },
44
+ "peerDependencies": {
45
+ "react": ">=19",
46
+ "@webround/core": "^0.2.0"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "react": {
50
+ "optional": true
51
+ },
52
+ "@webround/core": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "devDependencies": {
57
+ "@types/jsdom": "^30.0.0",
58
+ "@types/react": "^19.3.0",
59
+ "@types/react-dom": "^19.3.0",
60
+ "jsdom": "^26.1.0",
61
+ "react": "^19.3.0",
62
+ "react-dom": "^19.3.0",
63
+ "typescript": "^5.5.0"
64
+ },
65
+ "publishConfig": {
66
+ "access": "public"
67
+ }
68
+ }