@rivil-solutions/site-kit 0.1.0 → 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/LICENSE +21 -0
- package/README.md +46 -33
- package/bin/rivil.mjs +117 -117
- package/blocks/booking/block.json +1 -1
- package/blocks/careers/block.json +1 -1
- package/blocks/events/block.json +1 -1
- package/blocks/newsletter/block.json +1 -1
- package/blocks/reviews/block.json +1 -1
- package/blocks/stay/block.json +1 -1
- package/blocks/ui/block.json +1 -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 +8 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rivil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @rivil-solutions/site-kit
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
The client for the Rivil site API (`/api/site/v1`), for Astro sites (or any Node server) built on top of Rivil ERP.
|
|
4
|
+
It runs on the server only: the site key never reaches the browser.
|
|
5
5
|
|
|
6
6
|
```ts
|
|
7
7
|
import { createContentApi, createSiteClient } from '@rivil-solutions/site-kit'
|
|
@@ -9,47 +9,55 @@ import { createContentApi, createSiteClient } from '@rivil-solutions/site-kit'
|
|
|
9
9
|
export const rivil = createSiteClient({ url: process.env.ERP_URL!, key: process.env.ERP_SITE_KEY! })
|
|
10
10
|
export const content = createContentApi(rivil)
|
|
11
11
|
|
|
12
|
-
const menus = await content.menus('
|
|
13
|
-
const page = await content.one('page', '
|
|
12
|
+
const menus = await content.menus('en')
|
|
13
|
+
const page = await content.one('page', 'about', 'en')
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
To start a new site, use the starter: [rivil-site-template](https://github.com/iorpaul3/rivil-site-template).
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
- `createContentApi`: modulul Site (setări, meniuri, redirecturi, termeni, intrări, căutare, formulare).
|
|
20
|
-
- `rivilWebhookHandler` / `verifyRivilSignature`: webhook-urile semnate (`X-Rivil-Signature`).
|
|
21
|
-
- `visitorIp` + antetul `X-Rivil-Client-IP`: limitele din ERP se aplică pe vizitator.
|
|
22
|
-
- `createI18n`: limba din prefixul adresei (`/en/...`), `t()`, `lp()`, segmente traduse, linkuri interne cu prefix.
|
|
23
|
-
- `imageAt`, `imageWidths` și `@rivil-solutions/site-kit/astro-image`: pozele din Galerie la lățimea potrivită, fără prelucrare pe server.
|
|
24
|
-
- `sitemapXml`, `robotsTxt`, `matchRedirect`: SEO și redirecturile din ERP.
|
|
25
|
-
- `consentScriptUrl`: bannerul Rivil Consent.
|
|
26
|
-
- `createShopApi`: magazinul (catalog, coș, livrare, checkout, cont de client, carduri cadou, retururi), cu limba în adresă și `purgeChanged` pentru webhook-ul `shop.changed`.
|
|
27
|
-
- `accountProxy` + `AUTH_ROUTES`: contul de client (portal) prin site, cu sesiunea în cookie httpOnly și doar rutele permise.
|
|
18
|
+
## What's inside
|
|
28
19
|
|
|
29
|
-
- `
|
|
30
|
-
- `
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
20
|
+
- `createSiteClient`: requests with the site key. Reads (`get`) are cached in memory (fresh for 60 s, then served stale and refreshed in the background), identical requests are merged, and `purge()` refreshes everything after a webhook. ERP errors come as `RivilApiError` (`status`, `code`, `message`).
|
|
21
|
+
- `createContentApi`: the Site module (settings, menus, redirects, terms, entries, search, forms).
|
|
22
|
+
- `createModulesApi`: public reads of the modules (booking, careers, reviews, events) for server-rendered pages.
|
|
23
|
+
- `siteProxy` + `MODULE_ROUTES`: the browser calls `/api/rivil/<module>/...` on the site, and the site calls the ERP with the key. Only visitor actions get through.
|
|
24
|
+
- `@rivil-solutions/site-kit/web`: what also runs in the browser (`rivilFetch`, `SiteError`, money, days, consents, `bookingPlan`).
|
|
25
|
+
- `@rivil-solutions/site-kit/react`: the logic of the module forms, with no styling (`useBooking`, `useStay`, `useApplication`, `useReview`, `useEventCheckout`).
|
|
26
|
+
- `rivilWebhookHandler` / `verifyRivilSignature`: signed webhooks (`X-Rivil-Signature`).
|
|
27
|
+
- `visitorIp` + the `X-Rivil-Client-IP` header: ERP rate limits apply per visitor, not per site.
|
|
28
|
+
- `createI18n`: the language from the URL prefix (`/en/...`), `t()`, `lp()`, translated segments, prefixed internal links.
|
|
29
|
+
- `imageAt`, `imageWidths` and `@rivil-solutions/site-kit/astro-image`: Gallery images at the right width, with no processing on the server.
|
|
30
|
+
- `sitemapXml`, `robotsTxt`, `matchRedirect`: SEO and the redirects set in the ERP.
|
|
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, `purgeChanged` for the `shop.changed` webhook and `warmProducts()` to load the whole catalog into memory at startup.
|
|
33
|
+
- `accountProxy` + `AUTH_ROUTES`: the customer account (portal) through the site, with the session in an httpOnly cookie and only the allowed routes.
|
|
34
|
+
- The `rivil` CLI: copies module pages and starting themes into a site (see below).
|
|
34
35
|
|
|
35
|
-
##
|
|
36
|
+
## Modules on a site
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
npx rivil list
|
|
40
|
+
npx rivil add booking events
|
|
41
|
+
npx rivil theme cabana
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`rivil add` copies the module's pages and styled components into the site. From then on they belong to the site and can be changed freely. The logic (free slots, holds, validation, submitting) stays in this package and is updated with it. The command also prints the scopes the site key needs and the English URL segments to add to `site.config.ts`.
|
|
45
|
+
|
|
46
|
+
Modules: `booking`, `stay`, `careers`, `reviews`, `events`, `newsletter`. Themes: `aer`, `cabana`, `curte`, `lumen`, `miez`, `solvar`.
|
|
47
|
+
|
|
48
|
+
The components call the site, never the ERP directly, through this route:
|
|
36
49
|
|
|
37
50
|
```ts
|
|
38
51
|
// src/pages/api/rivil/[...path].ts
|
|
39
52
|
import { siteProxy } from '@rivil-solutions/site-kit'
|
|
40
53
|
import { client, siteUrl } from '../../../lib/rivil'
|
|
41
54
|
|
|
42
|
-
// origins:
|
|
55
|
+
// origins: behind a reverse proxy, Astro sees the request on localhost
|
|
43
56
|
const proxy = siteProxy({ client, origins: [siteUrl()] })
|
|
44
57
|
export const ALL = (c) => proxy({ request: c.request, path: c.params.path, clientAddress: c.clientAddress })
|
|
45
58
|
```
|
|
46
59
|
|
|
47
|
-
|
|
48
|
-
npx rivil add booking events
|
|
49
|
-
npx rivil theme cabana
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Magazin
|
|
60
|
+
## Shop
|
|
53
61
|
|
|
54
62
|
```ts
|
|
55
63
|
import { createShopApi, createSiteClient } from '@rivil-solutions/site-kit'
|
|
@@ -57,10 +65,11 @@ import { createShopApi, createSiteClient } from '@rivil-solutions/site-kit'
|
|
|
57
65
|
const client = createSiteClient({ url: process.env.ERP_URL!, key: process.env.SHOP_CHANNEL_KEY!, visitorIp: () => ipStore.getStore() })
|
|
58
66
|
export const shop = createShopApi(client, { lang: () => i18n.lang() })
|
|
59
67
|
|
|
60
|
-
//
|
|
68
|
+
// at startup, once per language: await shop.warmProducts()
|
|
69
|
+
// shop.changed webhook: shop.purgeChanged(event.data.object)
|
|
61
70
|
```
|
|
62
71
|
|
|
63
|
-
##
|
|
72
|
+
## Customer account
|
|
64
73
|
|
|
65
74
|
```ts
|
|
66
75
|
// src/pages/api/account/[...path].ts
|
|
@@ -86,13 +95,17 @@ export const POST = ({ request }) => rivilWebhookHandler(process.env.ERP_WEBHOOK
|
|
|
86
95
|
})(request)
|
|
87
96
|
```
|
|
88
97
|
|
|
89
|
-
##
|
|
98
|
+
## Images in Astro
|
|
90
99
|
|
|
91
100
|
```js
|
|
92
101
|
// astro.config.mjs
|
|
93
102
|
export default defineConfig({ image: { service: { entrypoint: '@rivil-solutions/site-kit/astro-image' } } })
|
|
94
103
|
```
|
|
95
104
|
|
|
96
|
-
##
|
|
105
|
+
## Development
|
|
106
|
+
|
|
107
|
+
`pnpm test`, `pnpm check`, `pnpm build`. Publishing runs the checks first (`prepublishOnly`).
|
|
108
|
+
|
|
109
|
+
## License
|
|
97
110
|
|
|
98
|
-
|
|
111
|
+
MIT
|
package/bin/rivil.mjs
CHANGED
|
@@ -1,118 +1,118 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* `rivil add <
|
|
4
|
-
* `rivil theme <
|
|
5
|
-
*
|
|
6
|
-
*/
|
|
7
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
8
|
-
import { dirname, join, relative, sep } from 'node:path'
|
|
9
|
-
import { fileURLToPath } from 'node:url'
|
|
10
|
-
|
|
11
|
-
const KIT = join(dirname(fileURLToPath(import.meta.url)), '..')
|
|
12
|
-
const BLOCKS = join(KIT, 'blocks')
|
|
13
|
-
const THEMES = join(KIT, 'themes')
|
|
14
|
-
const cwd = process.cwd()
|
|
15
|
-
const args = process.argv.slice(2)
|
|
16
|
-
const flags = new Set(args.filter((a) => a.startsWith('--')))
|
|
17
|
-
const [cmd, ...names] = args.filter((a) => !a.startsWith('--'))
|
|
18
|
-
const force = flags.has('--force')
|
|
19
|
-
const dry = flags.has('--dry-run')
|
|
20
|
-
|
|
21
|
-
const say = (s = '') => process.stdout.write(`${s}\n`)
|
|
22
|
-
const die = (s) => {
|
|
23
|
-
process.stderr.write(`${s}\n`)
|
|
24
|
-
process.exit(1)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const blockNames = () => readdirSync(BLOCKS).filter((n) => existsSync(join(BLOCKS, n, 'block.json'))).sort()
|
|
28
|
-
const meta = (name) => JSON.parse(readFileSync(join(BLOCKS, name, 'block.json'), 'utf8'))
|
|
29
|
-
|
|
30
|
-
function walk(dir) {
|
|
31
|
-
return readdirSync(dir).flatMap((n) => {
|
|
32
|
-
const p = join(dir, n)
|
|
33
|
-
return statSync(p).isDirectory() ? walk(p) : [p]
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
function withRequires(list) {
|
|
39
|
-
const out = []
|
|
40
|
-
const visit = (n) => {
|
|
41
|
-
if (out.includes(n)) return
|
|
42
|
-
if (!blockNames().includes(n)) die(`
|
|
43
|
-
for (const r of meta(n).requires ?? []) visit(r)
|
|
44
|
-
out.push(n)
|
|
45
|
-
}
|
|
46
|
-
list.forEach(visit)
|
|
47
|
-
return out
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function copy(from, to, overwrite = force) {
|
|
51
|
-
const rel = relative(cwd, to).split(sep).join('/')
|
|
52
|
-
const had = existsSync(to)
|
|
53
|
-
if (had && !overwrite) return { rel, state: 'skipped' }
|
|
54
|
-
if (!dry) {
|
|
55
|
-
mkdirSync(dirname(to), { recursive: true })
|
|
56
|
-
writeFileSync(to, readFileSync(from))
|
|
57
|
-
}
|
|
58
|
-
return { rel, state: had ? 'replaced' : 'added' }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function needProject() {
|
|
62
|
-
if (!existsSync(join(cwd, 'package.json')) || !existsSync(join(cwd, 'src'))) die('
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (!cmd || cmd === 'help' || flags.has('--help')) {
|
|
66
|
-
say(
|
|
67
|
-
say('rivil theme <
|
|
68
|
-
say('rivil list
|
|
69
|
-
process.exit(0)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (cmd === 'list') {
|
|
73
|
-
say('
|
|
74
|
-
for (const n of blockNames()) if (!meta(n).hidden) say(` ${n.padEnd(12)} ${meta(n).title}`)
|
|
75
|
-
say('
|
|
76
|
-
for (const f of readdirSync(THEMES).filter((f) => f.endsWith('.css'))) say(` ${f.replace(/\.css$/, '')}`)
|
|
77
|
-
process.exit(0)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (cmd === 'add') {
|
|
81
|
-
needProject()
|
|
82
|
-
if (!names.length) die('
|
|
83
|
-
const all = withRequires(names)
|
|
84
|
-
const scopes = new Set()
|
|
85
|
-
const pages = []
|
|
86
|
-
const segments = {}
|
|
87
|
-
for (const n of all) {
|
|
88
|
-
const m = meta(n)
|
|
89
|
-
const files = walk(join(BLOCKS, n, 'files'))
|
|
90
|
-
for (const f of files) {
|
|
91
|
-
const r = copy(f, join(cwd, relative(join(BLOCKS, n, 'files'), f)))
|
|
92
|
-
say(`${r.state === 'skipped' ? '
|
|
93
|
-
}
|
|
94
|
-
if (m.scope) scopes.add(m.scope)
|
|
95
|
-
pages.push(...(m.pages ?? []))
|
|
96
|
-
for (const [l, map] of Object.entries(m.segments ?? {})) Object.assign((segments[l] ??= {}), map)
|
|
97
|
-
}
|
|
98
|
-
say()
|
|
99
|
-
if (dry) say('(
|
|
100
|
-
if (scopes.size) say(`
|
|
101
|
-
if (pages.length) say(`
|
|
102
|
-
if (Object.keys(segments).length) say(`
|
|
103
|
-
process.exit(0)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (cmd === 'theme') {
|
|
107
|
-
needProject()
|
|
108
|
-
const name = names[0]
|
|
109
|
-
const file = join(THEMES, `${name}.css`)
|
|
110
|
-
if (!name || !existsSync(file)) die(`
|
|
111
|
-
const to = join(cwd, 'src', 'styles', 'theme.css')
|
|
112
|
-
const blank = existsSync(to) && !readFileSync(to, 'utf8').replace(/\/\*[\s\S]*?\*\//g, '').trim()
|
|
113
|
-
const r = copy(file, to, blank)
|
|
114
|
-
say(r.state === 'skipped' ? 'src/styles/theme.css
|
|
115
|
-
process.exit(0)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
die(`
|
|
2
|
+
/**
|
|
3
|
+
* `rivil add <block...>`: copies a module's pages and components into the site (booking, stay, careers...).
|
|
4
|
+
* `rivil theme <name>`: puts a starting theme in `src/styles/theme.css`.
|
|
5
|
+
* Copied files belong to the site and can be changed freely. An existing file is not overwritten without `--force`.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import { dirname, join, relative, sep } from 'node:path'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
|
|
11
|
+
const KIT = join(dirname(fileURLToPath(import.meta.url)), '..')
|
|
12
|
+
const BLOCKS = join(KIT, 'blocks')
|
|
13
|
+
const THEMES = join(KIT, 'themes')
|
|
14
|
+
const cwd = process.cwd()
|
|
15
|
+
const args = process.argv.slice(2)
|
|
16
|
+
const flags = new Set(args.filter((a) => a.startsWith('--')))
|
|
17
|
+
const [cmd, ...names] = args.filter((a) => !a.startsWith('--'))
|
|
18
|
+
const force = flags.has('--force')
|
|
19
|
+
const dry = flags.has('--dry-run')
|
|
20
|
+
|
|
21
|
+
const say = (s = '') => process.stdout.write(`${s}\n`)
|
|
22
|
+
const die = (s) => {
|
|
23
|
+
process.stderr.write(`${s}\n`)
|
|
24
|
+
process.exit(1)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const blockNames = () => readdirSync(BLOCKS).filter((n) => existsSync(join(BLOCKS, n, 'block.json'))).sort()
|
|
28
|
+
const meta = (name) => JSON.parse(readFileSync(join(BLOCKS, name, 'block.json'), 'utf8'))
|
|
29
|
+
|
|
30
|
+
function walk(dir) {
|
|
31
|
+
return readdirSync(dir).flatMap((n) => {
|
|
32
|
+
const p = join(dir, n)
|
|
33
|
+
return statSync(p).isDirectory() ? walk(p) : [p]
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The requested blocks plus their dependencies, each once, dependencies first. */
|
|
38
|
+
function withRequires(list) {
|
|
39
|
+
const out = []
|
|
40
|
+
const visit = (n) => {
|
|
41
|
+
if (out.includes(n)) return
|
|
42
|
+
if (!blockNames().includes(n)) die(`No block named "${n}". Blocks: ${blockNames().filter((x) => !meta(x).hidden).join(', ')}.`)
|
|
43
|
+
for (const r of meta(n).requires ?? []) visit(r)
|
|
44
|
+
out.push(n)
|
|
45
|
+
}
|
|
46
|
+
list.forEach(visit)
|
|
47
|
+
return out
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function copy(from, to, overwrite = force) {
|
|
51
|
+
const rel = relative(cwd, to).split(sep).join('/')
|
|
52
|
+
const had = existsSync(to)
|
|
53
|
+
if (had && !overwrite) return { rel, state: 'skipped' }
|
|
54
|
+
if (!dry) {
|
|
55
|
+
mkdirSync(dirname(to), { recursive: true })
|
|
56
|
+
writeFileSync(to, readFileSync(from))
|
|
57
|
+
}
|
|
58
|
+
return { rel, state: had ? 'replaced' : 'added' }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function needProject() {
|
|
62
|
+
if (!existsSync(join(cwd, 'package.json')) || !existsSync(join(cwd, 'src'))) die('Run the command in the site folder (the one with package.json and src/).')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!cmd || cmd === 'help' || flags.has('--help')) {
|
|
66
|
+
say("rivil add <block...> [--force] [--dry-run] adds a module's pages")
|
|
67
|
+
say('rivil theme <name> [--force] puts a starting theme')
|
|
68
|
+
say('rivil list blocks and themes')
|
|
69
|
+
process.exit(0)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (cmd === 'list') {
|
|
73
|
+
say('Blocks:')
|
|
74
|
+
for (const n of blockNames()) if (!meta(n).hidden) say(` ${n.padEnd(12)} ${meta(n).title}`)
|
|
75
|
+
say('Themes:')
|
|
76
|
+
for (const f of readdirSync(THEMES).filter((f) => f.endsWith('.css'))) say(` ${f.replace(/\.css$/, '')}`)
|
|
77
|
+
process.exit(0)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (cmd === 'add') {
|
|
81
|
+
needProject()
|
|
82
|
+
if (!names.length) die('Name a block: rivil add booking. The list: rivil list.')
|
|
83
|
+
const all = withRequires(names)
|
|
84
|
+
const scopes = new Set()
|
|
85
|
+
const pages = []
|
|
86
|
+
const segments = {}
|
|
87
|
+
for (const n of all) {
|
|
88
|
+
const m = meta(n)
|
|
89
|
+
const files = walk(join(BLOCKS, n, 'files'))
|
|
90
|
+
for (const f of files) {
|
|
91
|
+
const r = copy(f, join(cwd, relative(join(BLOCKS, n, 'files'), f)))
|
|
92
|
+
say(`${r.state === 'skipped' ? 'exists ' : r.state === 'replaced' ? 'replaced' : 'added '} ${r.rel}`)
|
|
93
|
+
}
|
|
94
|
+
if (m.scope) scopes.add(m.scope)
|
|
95
|
+
pages.push(...(m.pages ?? []))
|
|
96
|
+
for (const [l, map] of Object.entries(m.segments ?? {})) Object.assign((segments[l] ??= {}), map)
|
|
97
|
+
}
|
|
98
|
+
say()
|
|
99
|
+
if (dry) say('(nothing written: --dry-run)')
|
|
100
|
+
if (scopes.size) say(`The site key needs: ${[...scopes].join(', ')} (ERP, Site, the site's page, Keys).`)
|
|
101
|
+
if (pages.length) say(`New pages: ${pages.join(', ')}. Add them to a menu in ERP, Site, Menus.`)
|
|
102
|
+
if (Object.keys(segments).length) say(`URLs in other languages, in src/site.config.ts under segments: ${JSON.stringify(segments)}`)
|
|
103
|
+
process.exit(0)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (cmd === 'theme') {
|
|
107
|
+
needProject()
|
|
108
|
+
const name = names[0]
|
|
109
|
+
const file = join(THEMES, `${name}.css`)
|
|
110
|
+
if (!name || !existsSync(file)) die(`Themes: ${readdirSync(THEMES).map((f) => f.replace(/\.css$/, '')).join(', ')}.`)
|
|
111
|
+
const to = join(cwd, 'src', 'styles', 'theme.css')
|
|
112
|
+
const blank = existsSync(to) && !readFileSync(to, 'utf8').replace(/\/\*[\s\S]*?\*\//g, '').trim()
|
|
113
|
+
const r = copy(file, to, blank)
|
|
114
|
+
say(r.state === 'skipped' ? 'src/styles/theme.css already exists: --force replaces it.' : `${r.state} ${r.rel}`)
|
|
115
|
+
process.exit(0)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
die(`Unknown command: ${cmd}. See: rivil help`)
|
package/blocks/events/block.json
CHANGED
package/blocks/stay/block.json
CHANGED
package/blocks/ui/block.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "title": "
|
|
1
|
+
{ "title": "Shared form components", "hidden": true }
|
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,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivil-solutions/site-kit",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Client
|
|
3
|
+
"version": "0.1.2",
|
|
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",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/iorpaul3/rivil-site-kit.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/iorpaul3/rivil-site-kit#readme",
|
|
12
|
+
"bugs": "https://github.com/iorpaul3/rivil-site-kit/issues",
|
|
7
13
|
"sideEffects": false,
|
|
8
14
|
"files": [
|
|
9
15
|
"dist",
|