@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.
- package/README.md +192 -2
- package/dist/account/account.d.ts +54 -0
- package/dist/account/account.js +40 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/index.js +2 -0
- package/dist/addresses/api.d.ts +16 -0
- package/dist/addresses/api.js +13 -0
- package/dist/addresses/book.d.ts +40 -0
- package/dist/addresses/book.js +144 -0
- package/dist/addresses/index.d.ts +8 -0
- package/dist/addresses/index.js +5 -0
- package/dist/addresses/rules.d.ts +4 -0
- package/dist/addresses/rules.js +9 -0
- package/dist/addresses/types.d.ts +48 -0
- package/dist/addresses/types.js +6 -0
- package/dist/auth/api.d.ts +43 -0
- package/dist/auth/api.js +22 -0
- package/dist/auth/auth.d.ts +32 -0
- package/dist/auth/auth.js +23 -0
- package/dist/auth/cookies.d.ts +46 -0
- package/dist/auth/cookies.js +156 -0
- package/dist/auth/index.d.ts +14 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/renewal.d.ts +4 -0
- package/dist/auth/renewal.js +37 -0
- package/dist/auth/store.d.ts +70 -0
- package/dist/auth/store.js +284 -0
- package/dist/auth/types.d.ts +105 -0
- package/dist/auth/types.js +5 -0
- package/dist/auth/wake.d.ts +18 -0
- package/dist/auth/wake.js +23 -0
- package/dist/cache.d.ts +49 -0
- package/dist/cache.js +144 -0
- package/dist/canonical.d.ts +7 -0
- package/dist/canonical.js +32 -0
- package/dist/cart/api.d.ts +25 -0
- package/dist/cart/api.js +16 -0
- package/dist/cart/index.d.ts +10 -0
- package/dist/cart/index.js +6 -0
- package/dist/cart/lines.d.ts +15 -0
- package/dist/cart/lines.js +23 -0
- package/dist/cart/storage.d.ts +30 -0
- package/dist/cart/storage.js +82 -0
- package/dist/cart/store.d.ts +58 -0
- package/dist/cart/store.js +218 -0
- package/dist/cart/types.d.ts +194 -0
- package/dist/cart/types.js +11 -0
- package/dist/catalog/cached.d.ts +47 -0
- package/dist/catalog/cached.js +52 -0
- package/dist/catalog/catalog.d.ts +10 -0
- package/dist/catalog/catalog.js +7 -0
- package/dist/catalog/named.d.ts +3 -0
- package/dist/catalog/named.js +7 -0
- package/dist/catalog/products.d.ts +28 -0
- package/dist/catalog/products.js +12 -0
- package/dist/catalog/store-data.d.ts +13 -0
- package/dist/catalog/store-data.js +6 -0
- package/dist/catalog/types.d.ts +430 -0
- package/dist/catalog/types.js +4 -0
- package/dist/checkout/api.d.ts +14 -0
- package/dist/checkout/api.js +7 -0
- package/dist/checkout/errors.d.ts +23 -0
- package/dist/checkout/errors.js +18 -0
- package/dist/checkout/index.d.ts +10 -0
- package/dist/checkout/index.js +6 -0
- package/dist/checkout/return-urls.d.ts +7 -0
- package/dist/checkout/return-urls.js +11 -0
- package/dist/checkout/store.d.ts +98 -0
- package/dist/checkout/store.js +179 -0
- package/dist/checkout/types.d.ts +225 -0
- package/dist/checkout/types.js +28 -0
- package/dist/commerce.d.ts +55 -0
- package/dist/commerce.js +55 -0
- package/dist/customer-resources.d.ts +18 -0
- package/dist/customer-resources.js +40 -0
- package/dist/errors.d.ts +65 -0
- package/dist/errors.js +96 -0
- package/dist/http.d.ts +49 -0
- package/dist/http.js +94 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +22 -0
- package/dist/orders/api.d.ts +16 -0
- package/dist/orders/api.js +13 -0
- package/dist/orders/index.d.ts +6 -0
- package/dist/orders/index.js +3 -0
- package/dist/orders/orders.d.ts +19 -0
- package/dist/orders/orders.js +18 -0
- package/dist/orders/types.d.ts +88 -0
- package/dist/orders/types.js +4 -0
- package/dist/react/index.d.ts +23 -0
- package/dist/react/index.js +15 -0
- package/dist/react/use-account.d.ts +12 -0
- package/dist/react/use-account.js +9 -0
- package/dist/react/use-addresses.d.ts +20 -0
- package/dist/react/use-addresses.js +20 -0
- package/dist/react/use-auth.d.ts +24 -0
- package/dist/react/use-auth.js +20 -0
- package/dist/react/use-cart.d.ts +27 -0
- package/dist/react/use-cart.js +23 -0
- package/dist/react/use-checkout.d.ts +37 -0
- package/dist/react/use-checkout.js +33 -0
- package/dist/react/use-customer-resource.d.ts +5 -0
- package/dist/react/use-customer-resource.js +9 -0
- package/dist/react/use-orders.d.ts +14 -0
- package/dist/react/use-orders.js +6 -0
- package/dist/react/use-resource.d.ts +25 -0
- package/dist/react/use-resource.js +60 -0
- package/dist/react/use-returns.d.ts +14 -0
- package/dist/react/use-returns.js +7 -0
- package/dist/react/use-reviews.d.ts +14 -0
- package/dist/react/use-reviews.js +10 -0
- package/dist/react/use-shop-address.d.ts +12 -0
- package/dist/react/use-shop-address.js +39 -0
- package/dist/react/use-shop.d.ts +34 -0
- package/dist/react/use-shop.js +31 -0
- package/dist/react/use-wishlist.d.ts +15 -0
- package/dist/react/use-wishlist.js +12 -0
- package/dist/resource.d.ts +19 -0
- package/dist/resource.js +15 -0
- package/dist/returns/api.d.ts +27 -0
- package/dist/returns/api.js +19 -0
- package/dist/returns/index.d.ts +6 -0
- package/dist/returns/index.js +3 -0
- package/dist/returns/returns.d.ts +30 -0
- package/dist/returns/returns.js +31 -0
- package/dist/returns/rules.d.ts +7 -0
- package/dist/returns/rules.js +45 -0
- package/dist/returns/types.d.ts +99 -0
- package/dist/returns/types.js +2 -0
- package/dist/reviews/api.d.ts +19 -0
- package/dist/reviews/api.js +15 -0
- package/dist/reviews/index.d.ts +9 -0
- package/dist/reviews/index.js +5 -0
- package/dist/reviews/reviews.d.ts +23 -0
- package/dist/reviews/reviews.js +37 -0
- package/dist/reviews/rules.d.ts +8 -0
- package/dist/reviews/rules.js +7 -0
- package/dist/reviews/types.d.ts +54 -0
- package/dist/reviews/types.js +10 -0
- package/dist/shop/address-store.d.ts +14 -0
- package/dist/shop/address-store.js +28 -0
- package/dist/shop/address.d.ts +12 -0
- package/dist/shop/address.js +95 -0
- package/dist/shop/index.d.ts +16 -0
- package/dist/shop/index.js +9 -0
- package/dist/shop/question.d.ts +15 -0
- package/dist/shop/question.js +12 -0
- package/dist/shop/range.d.ts +7 -0
- package/dist/shop/range.js +38 -0
- package/dist/shop/sort.d.ts +19 -0
- package/dist/shop/sort.js +18 -0
- package/dist/shop/state.d.ts +47 -0
- package/dist/shop/state.js +51 -0
- package/dist/shop/store.d.ts +26 -0
- package/dist/shop/store.js +47 -0
- package/dist/shop/to-search.d.ts +14 -0
- package/dist/shop/to-search.js +53 -0
- package/dist/site.d.ts +7 -0
- package/dist/site.js +41 -0
- package/dist/store-kit.d.ts +39 -0
- package/dist/store-kit.js +81 -0
- package/dist/wishlist/api.d.ts +12 -0
- package/dist/wishlist/api.js +10 -0
- package/dist/wishlist/index.d.ts +7 -0
- package/dist/wishlist/index.js +4 -0
- package/dist/wishlist/types.d.ts +30 -0
- package/dist/wishlist/types.js +4 -0
- package/dist/wishlist/wishlist.d.ts +45 -0
- package/dist/wishlist/wishlist.js +84 -0
- 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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
+
}
|