@rivil-solutions/site-kit 0.1.1 → 0.1.2
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 +2 -1
- package/dist/client.d.ts +1 -0
- package/dist/client.js +8 -1
- package/dist/shop.d.ts +6 -1
- package/dist/shop.js +33 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ To start a new site, use the starter: [rivil-site-template](https://github.com/i
|
|
|
29
29
|
- `imageAt`, `imageWidths` and `@rivil-solutions/site-kit/astro-image`: Gallery images at the right width, with no processing on the server.
|
|
30
30
|
- `sitemapXml`, `robotsTxt`, `matchRedirect`: SEO and the redirects set in the ERP.
|
|
31
31
|
- `consentScriptUrl`: the Rivil Consent banner.
|
|
32
|
-
- `createShopApi`: the shop (catalog, cart, shipping, checkout, customer account, gift cards, returns), with the language in the URL
|
|
32
|
+
- `createShopApi`: the shop (catalog, cart, shipping, checkout, customer account, gift cards, returns), with the language in the URL, `purgeChanged` for the `shop.changed` webhook and `warmProducts()` to load the whole catalog into memory at startup.
|
|
33
33
|
- `accountProxy` + `AUTH_ROUTES`: the customer account (portal) through the site, with the session in an httpOnly cookie and only the allowed routes.
|
|
34
34
|
- The `rivil` CLI: copies module pages and starting themes into a site (see below).
|
|
35
35
|
|
|
@@ -65,6 +65,7 @@ import { createShopApi, createSiteClient } from '@rivil-solutions/site-kit'
|
|
|
65
65
|
const client = createSiteClient({ url: process.env.ERP_URL!, key: process.env.SHOP_CHANNEL_KEY!, visitorIp: () => ipStore.getStore() })
|
|
66
66
|
export const shop = createShopApi(client, { lang: () => i18n.lang() })
|
|
67
67
|
|
|
68
|
+
// at startup, once per language: await shop.warmProducts()
|
|
68
69
|
// shop.changed webhook: shop.purgeChanged(event.data.object)
|
|
69
70
|
```
|
|
70
71
|
|
package/dist/client.d.ts
CHANGED
|
@@ -51,6 +51,7 @@ export declare function createSiteClient(o: SiteClientOptions): {
|
|
|
51
51
|
request: (path: string, r?: RequestOptions) => Promise<Response>;
|
|
52
52
|
call: <T = unknown>(path: string, r?: RequestOptions) => Promise<T>;
|
|
53
53
|
get: <T = unknown>(path: string) => Promise<T | null>;
|
|
54
|
+
seed: (path: string, value: unknown) => void;
|
|
54
55
|
purge: (match?: (path: string, value: unknown) => boolean) => void;
|
|
55
56
|
forget: (match: (path: string) => boolean) => void;
|
|
56
57
|
clear: () => void;
|
package/dist/client.js
CHANGED
|
@@ -137,6 +137,13 @@ export function createSiteClient(o) {
|
|
|
137
137
|
if (match(path))
|
|
138
138
|
memo.delete(path);
|
|
139
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Pune în memorie o valoare deja primită pe altă cale (de exemplu o fișă de produs dintr-o listă cu fișe complete), ca prima citire a ei
|
|
142
|
+
* să nu mai întrebe ERP-ul. Se reîmprospătează ca orice citire.
|
|
143
|
+
*/
|
|
144
|
+
function seed(path, value) {
|
|
145
|
+
memo.set(path, { at: Date.now(), v: value });
|
|
146
|
+
}
|
|
140
147
|
/** Golește memoria de tot (pentru teste sau o schimbare de cheie). */
|
|
141
148
|
function clear() {
|
|
142
149
|
memo.clear();
|
|
@@ -149,5 +156,5 @@ export function createSiteClient(o) {
|
|
|
149
156
|
return '';
|
|
150
157
|
return `${origin}/api/img/${tenant}/${id}${size ? `?size=${size}` : ''}`;
|
|
151
158
|
}
|
|
152
|
-
return { origin, base, request, call, get, purge, forget, clear, imageUrl };
|
|
159
|
+
return { origin, base, request, call, get, seed, purge, forget, clear, imageUrl };
|
|
153
160
|
}
|
package/dist/shop.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type SiteClient } from './client.js';
|
|
2
2
|
/** Un obiect din API-ul magazinului; forma exactă e în documentația API-ului de site. */
|
|
3
3
|
export type ShopData = Record<string, any>;
|
|
4
4
|
export type ShopList = {
|
|
@@ -100,6 +100,11 @@ export declare function createShopApi(client: SiteClient, o?: ShopApiOptions): {
|
|
|
100
100
|
giftStatus: (token: string) => Promise<ShopData>;
|
|
101
101
|
requestQuote: (body: unknown) => Promise<ShopData>;
|
|
102
102
|
requestReturn: (body: unknown) => Promise<ShopData>;
|
|
103
|
+
/**
|
|
104
|
+
* Încarcă în memorie tot catalogul, fișă cu fișă, împreună cu recenziile (50 de produse pe cerere, una după alta), ca primul
|
|
105
|
+
* vizitator al unei fișe să nu aștepte ERP-ul. Cât ERP-ul e ocupat (429 sau eroare de server), așteaptă și reia.
|
|
106
|
+
*/
|
|
107
|
+
warmProducts(): Promise<number>;
|
|
103
108
|
/**
|
|
104
109
|
* Webhook-ul `shop.changed`: reîmprospătează în fundal doar ce s-a schimbat. Cu `product_ids`, intrările care conțin acele produse
|
|
105
110
|
* (fișa, listele, colecțiile); cu `prefixes`, părțile din vitrină numite de ERP (`page:`, `menus`, `redirects`...); fără ele, tot magazinul.
|
package/dist/shop.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RivilApiError } from './client.js';
|
|
1
2
|
const enc = encodeURIComponent;
|
|
2
3
|
const UUID = /^[0-9a-f-]{36}$/i;
|
|
3
4
|
const SESSION = 'X-Shop-Session';
|
|
@@ -138,6 +139,24 @@ export function createShopApi(client, o = {}) {
|
|
|
138
139
|
giftStatus: (token) => send(`/gift-cards/${enc(token)}`),
|
|
139
140
|
requestQuote: (body) => send('/quote-requests', json(body)),
|
|
140
141
|
requestReturn: (body) => send('/returns', json(body)),
|
|
142
|
+
/**
|
|
143
|
+
* Încarcă în memorie tot catalogul, fișă cu fișă, împreună cu recenziile (50 de produse pe cerere, una după alta), ca primul
|
|
144
|
+
* vizitator al unei fișe să nu aștepte ERP-ul. Cât ERP-ul e ocupat (429 sau eroare de server), așteaptă și reia.
|
|
145
|
+
*/
|
|
146
|
+
async warmProducts() {
|
|
147
|
+
let cursor;
|
|
148
|
+
let count = 0;
|
|
149
|
+
do {
|
|
150
|
+
const r = await patient(() => send(`/product-details${query({ limit: 50, cursor })}`));
|
|
151
|
+
for (const { product, reviews } of r?.items ?? []) {
|
|
152
|
+
client.seed(p(`/products/${enc(product.slug)}`), product);
|
|
153
|
+
client.seed(p(`/products/${enc(product.slug)}/reviews`), reviews);
|
|
154
|
+
count++;
|
|
155
|
+
}
|
|
156
|
+
cursor = r?.next_cursor ?? undefined;
|
|
157
|
+
} while (cursor);
|
|
158
|
+
return count;
|
|
159
|
+
},
|
|
141
160
|
/**
|
|
142
161
|
* Webhook-ul `shop.changed`: reîmprospătează în fundal doar ce s-a schimbat. Cu `product_ids`, intrările care conțin acele produse
|
|
143
162
|
* (fișa, listele, colecțiile); cu `prefixes`, părțile din vitrină numite de ERP (`page:`, `menus`, `redirects`...); fără ele, tot magazinul.
|
|
@@ -163,5 +182,19 @@ export function createShopApi(client, o = {}) {
|
|
|
163
182
|
},
|
|
164
183
|
};
|
|
165
184
|
}
|
|
185
|
+
/** Reia o cerere cât ERP-ul răspunde că e ocupat (429) sau cade (5xx), cu pauza cerută de el. */
|
|
186
|
+
async function patient(fn, tries = 5) {
|
|
187
|
+
for (;;) {
|
|
188
|
+
try {
|
|
189
|
+
return await fn();
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
if (--tries <= 0 || !(e instanceof RivilApiError) || e.status < 429)
|
|
193
|
+
throw e;
|
|
194
|
+
const wait = e.details?.retry_after_sec ?? 5;
|
|
195
|
+
await new Promise((r) => setTimeout(r, wait * 1000));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
166
199
|
/** Prefixul unei chei de vitrină din `shop.changed` -> începutul căii din API: `page:` -> `/shop/pages/`, `menus` -> `/shop/menus`. */
|
|
167
200
|
export const prefixPath = (prefix) => `/shop/${prefix.endsWith(':') ? `${prefix.slice(0, -1)}s/` : prefix}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivil-solutions/site-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Client for the Rivil site API (/api/site/v1): content, modules, cache cleared by webhook, images, languages, sitemap, blocks for the starter.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|