@wix/origami-site 1.0.43 → 1.0.45

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-site",
3
- "version": "1.0.43",
3
+ "version": "1.0.45",
4
4
  "description": "Runs an exported Wix origami site: file-based routing, Wix dynamic pages, popups and build checks. Use with @wix/origami-site-astro or @wix/origami-site-express.",
5
5
  "type": "module",
6
6
  "files": [
@@ -48,11 +48,11 @@
48
48
  "dependencies": {
49
49
  "@wix/origami-types": "1.4.2",
50
50
  "@wix/builder-error-pages": "^1.1.6",
51
- "@wix/origami-transforms": "0.1.51",
51
+ "@wix/origami-transforms": "0.1.52",
52
52
  "@babel/parser": "^7.25.0",
53
53
  "@wix/origami-services": "1.2.7",
54
54
  "@wix/services-manager-react": "^1.0.8",
55
- "@wix/origami-bi": "0.19.0"
55
+ "@wix/origami-bi": "0.20.0"
56
56
  },
57
57
  "exports": {
58
58
  "./server": {
@@ -93,5 +93,5 @@
93
93
  "optional": true
94
94
  }
95
95
  },
96
- "falconPackageHash": "d1d5fc0d1c2dddbbcf258955ce7ec534dfdbbbf5bfc38df40ea73f7e"
96
+ "falconPackageHash": "77f602d52bb1bc0c8ad5725452fab1373d760437c23ad786906d7b91"
97
97
  }
@@ -7,7 +7,9 @@ export function routerRequest({ router, suffix, url, token, wixApiBaseUrl }) {
7
7
  const { config } = router
8
8
  const pageRoles = Object.fromEntries(Object.values(router.rolePages).map(page =>
9
9
  [page.role, { id: page.pageId ?? page.role, title: page.title }]))
10
- return new Request(new URL(`${config.routerServiceUrl}/pages`, wixApiBaseUrl), {
10
+ // DM writes events' routerServiceUrl with a trailing slash, /_api/events-tpa-router/: one slash before pages.
11
+ const pagesUrl = new URL(`${config.routerServiceUrl.replace(/\/+$/, '')}/pages`, wixApiBaseUrl)
12
+ return new Request(pagesUrl, {
11
13
  method: 'POST',
12
14
  // The raw token, no "Bearer", as piler sent it.
13
15
  headers: { 'content-type': 'application/json', authorization: token },
@@ -4,7 +4,7 @@ import { basename, dirname, resolve } from 'node:path'
4
4
  import { parse } from '@babel/parser'
5
5
  import { findItems, pageUrl } from '@wix/origami-types'
6
6
  import { SiteTreeError } from '../requests/errors.js'
7
- import { canonicalJson, readRouteJson } from './routeJson.js'
7
+ import { readRouteJsons } from './routeJson.js'
8
8
 
9
9
  /** @type {Map<string, { mtimeMs: number, metadata: { title?: string, pageId?: string } }>} */
10
10
  const metadataCache = new Map()
@@ -65,9 +65,10 @@ const isBracket = name => /^\[[^[\]]+\]$/.test(name)
65
65
  const isPageFolder = entry => entry.isDirectory() && !entry.name.startsWith('.') && !['_generated', 'node_modules'].includes(entry.name)
66
66
  const hasPageFiles = entries => entries.some(entry => entry.isFile() && !entry.name.startsWith('.'))
67
67
 
68
- /** A router prefix: `pages/<prefix>/` with only `[slug]` role page folders, each with an identical route.json.
68
+ /** A router prefix: `pages/<prefix>/` with only `[slug]` role page folders, each with a route.json of the same router.
69
+ * @param {string[]} items every item folder of the site, as findItems lists them
69
70
  * @returns {import('@wix/origami-types').RouterRoute} */
70
- function readRouter(folder, itemPath, entries) {
71
+ function readRouter(folder, itemPath, entries, items) {
71
72
  const path = `src/${itemPath}`
72
73
  const children = entries.filter(isPageFolder)
73
74
  const prefix = basename(folder)
@@ -81,14 +82,15 @@ function readRouter(folder, itemPath, entries) {
81
82
  const others = children.filter(child => !isBracket(child.name))
82
83
  if (others.length)
83
84
  throw new SiteTreeError('PREFIX_COLLIDES_WITH_PAGE', others.map(child => `${path}/${child.name}`), `Every URL under /${prefix} goes to the Wix router, so it can't hold other pages.`)
84
- /** @type {{ slug: string, itemPath: string, path: string, config: import('@wix/origami-types').RouteConfig, title: string }[]} */
85
+ /** @type {{ slug: string, itemPath: string, path: string, file: string, title: string }[]} */
85
86
  const roleFolders = []
86
87
  for (const child of children) {
87
88
  const childPath = `${path}/${child.name}`
88
89
  const childFolder = resolve(folder, child.name)
89
90
  const childEntries = readdirSync(childFolder, { withFileTypes: true })
90
- const nested = childEntries.filter(isPageFolder)
91
- if (nested.length) throw new SiteTreeError('ROLE_PAGE_HAS_CHILDREN', nested.map(entry => `${childPath}/${entry.name}`), 'A [page] folder cannot hold other pages.')
91
+ // A folder of the page's own files, such as servicesConfigs/, is part of it; only a folder with an item is a page.
92
+ const nested = items.filter(item => item.startsWith(`${itemPath}/${child.name}/`))
93
+ if (nested.length) throw new SiteTreeError('ROLE_PAGE_HAS_CHILDREN', nested.map(item => `src/${item}`), 'A [page] folder cannot hold other pages.')
92
94
  if (!childEntries.some(entry => entry.name === 'route.json'))
93
95
  throw new SiteTreeError('ROUTE_JSON_INVALID', [`${childPath}/route.json`], 'Every [page] folder of a router needs its route.json.')
94
96
  // Without them the browser would import a page entry that doesn't exist, once the router picks this page.
@@ -96,15 +98,11 @@ function readRouter(folder, itemPath, entries) {
96
98
  throw new SiteTreeError('ROLE_PAGE_HAS_NO_FILES', [childPath], 'This [page] folder has a route.json but no page files.')
97
99
  roleFolders.push({
98
100
  slug: child.name.slice(1, -1), itemPath: `${itemPath}/${child.name}`, path: childPath,
99
- config: readRouteJson(resolve(childFolder, 'route.json'), `${childPath}/route.json`),
101
+ file: resolve(childFolder, 'route.json'),
100
102
  title: pageMetadata(childFolder).title,
101
103
  })
102
104
  }
103
- const [first] = roleFolders
104
- const differ = roleFolders.find(folder => canonicalJson(folder.config) !== canonicalJson(first.config))
105
- if (differ) throw new SiteTreeError('ROUTE_JSON_SIBLINGS_DIFFER', [`${first.path}/route.json`, `${differ.path}/route.json`],
106
- 'Role pages of one router must have identical route.json files.')
107
- const { config } = first
105
+ const config = readRouteJsons(roleFolders.map(({ slug, file, path }) => ({ slug, file, path: `${path}/route.json` })))
108
106
  const unnamed = roleFolders.filter(folder => !Object.values(config.pages).includes(folder.slug))
109
107
  if (unnamed.length) throw new SiteTreeError('ROLE_FOLDER_NOT_IN_PAGES', unnamed.map(folder => folder.path), 'No role in route.json "pages" names this folder.')
110
108
  /** @type {Record<string, import('@wix/origami-types').RolePage>} */
@@ -135,7 +133,7 @@ export const readSiteTree = async root => {
135
133
  for (const prefix of prefixes) {
136
134
  if (prefix === 'pages/error404')
137
135
  throw new SiteTreeError('NOT_FOUND_HAS_CHILDREN', [`src/${prefix}`], 'The not-found page cannot have children.')
138
- routers.push(readRouter(resolve(src, prefix), prefix, readdirSync(resolve(src, prefix), { withFileTypes: true })))
136
+ routers.push(readRouter(resolve(src, prefix), prefix, readdirSync(resolve(src, prefix), { withFileTypes: true }), items))
139
137
  }
140
138
  for (const path of routerPaths.filter(path => basename(path) === 'route.json')) {
141
139
  if (!routers.some(router => Object.values(router.rolePages).some(page => path === `${page.itemPath}/route.json`)))
@@ -4,23 +4,52 @@ import { SiteTreeError } from '../requests/errors.js'
4
4
  const isRecord = value => typeof value === 'object' && value !== null && !Array.isArray(value)
5
5
  const isStringRecord = value => isRecord(value) && Object.values(value).every(item => typeof item === 'string' && item !== '')
6
6
 
7
- /** Reads and checks one route.json. The core reads only routerServiceUrl, pages and pageIds; the rest passes through.
8
- * @param {string} file absolute path @param {string} path site-relative path for errors
9
- * @returns {import('@wix/origami-types').RouteConfig} */
10
- export function readRouteJson(file, path) {
11
- let config
12
- try { config = JSON.parse(readFileSync(file, 'utf8')) } catch (error) {
7
+ /** @param {string} file absolute path @param {string} path site-relative path for errors */
8
+ function readJson(file, path) {
9
+ let json
10
+ try { json = JSON.parse(readFileSync(file, 'utf8')) } catch (error) {
13
11
  throw new SiteTreeError('ROUTE_JSON_INVALID', [path], `Not valid JSON: ${/** @type {Error} */ (error).message}`)
14
12
  }
13
+ if (!isRecord(json)) throw new SiteTreeError('ROUTE_JSON_INVALID', [path], 'route.json must hold a JSON object.')
14
+ return json
15
+ }
16
+
17
+ /** Reads a router's route.json files: DM exports one per role page, with the router's fields and the page's own
18
+ * pageRoles and pageId (document-management#37725). This is the one bridge from that format to the RouteConfig the
19
+ * core reads: pages maps each role to its page's slug, pageIds to its Wix page id. pageIds go once routers answer with the role.
20
+ * @param {{ slug: string, file: string, path: string }[]} folders the router's [slug] folders
21
+ * @returns {import('@wix/origami-types').RouteConfig} */
22
+ export function readRouteJsons(folders) {
23
+ const files = folders.map(folder => ({ ...folder, json: readJson(folder.file, folder.path) }))
24
+ const routerOf = ({ pageRoles, pageId, ...router }) => router
25
+ const [first] = files
26
+ const differ = files.find(file => canonicalJson(routerOf(file.json)) !== canonicalJson(routerOf(first.json)))
27
+ if (differ) throw new SiteTreeError('ROUTE_JSON_SIBLINGS_DIFFER', [first.path, differ.path],
28
+ 'Role pages of one router must have the same router in route.json; only pageRoles and pageId differ.')
29
+ /** @type {Record<string, string>} */ const pages = {}
30
+ /** @type {Record<string, string>} */ const pageIds = {}
31
+ for (const { slug, path, json } of files) {
32
+ if (!Array.isArray(json.pageRoles) || !json.pageRoles.length || !json.pageRoles.every(role => typeof role === 'string' && role))
33
+ throw new SiteTreeError('ROUTE_JSON_INVALID', [path], '"pageRoles" must list the roles this page serves, e.g. ["member"].')
34
+ for (const role of json.pageRoles) {
35
+ if (role in pages) throw new SiteTreeError('ROUTE_JSON_INVALID', [path], `Role "${role}" is also in the route.json of [${pages[role]}]; each role belongs to one page.`)
36
+ pages[role] = slug
37
+ if (json.pageId !== undefined) pageIds[role] = json.pageId
38
+ }
39
+ }
40
+ return checkRouteConfig({ ...routerOf(first.json), pages, ...(Object.keys(pageIds).length ? { pageIds } : {}), roleVariations: {} }, first.path)
41
+ }
42
+
43
+ /** Checks a router's config. The core reads only routerServiceUrl, pages and pageIds; the rest passes through.
44
+ * @param {Record<string, unknown>} config @param {string} path site-relative path for errors
45
+ * @returns {import('@wix/origami-types').RouteConfig} */
46
+ function checkRouteConfig(config, path) {
15
47
  const invalid = message => new SiteTreeError('ROUTE_JSON_INVALID', [path], message)
16
- if (!isRecord(config)) throw invalid('route.json must hold a JSON object.')
17
48
  if (typeof config.routerServiceUrl !== 'string' || !config.routerServiceUrl)
18
49
  throw new SiteTreeError('ROUTE_JSON_MISSING_SERVICE_URL', [path], 'route.json has no routerServiceUrl, so the Wix router cannot be called. Export the site again.')
19
- if (!isStringRecord(config.pages) || !Object.keys(config.pages).length) throw invalid('"pages" must map each role to its page folder name, e.g. { "member": "team-member" }.')
20
- if (config.pageIds !== undefined && !isStringRecord(config.pageIds)) throw invalid('"pageIds" must map each role to a Wix page id.')
50
+ if (config.pageIds !== undefined && !isStringRecord(config.pageIds)) throw invalid('"pageId" must be a Wix page id.')
21
51
  if (!('config' in config)) throw invalid('"config" is missing.')
22
- if (!isRecord(config.roleVariations)) throw invalid('"roleVariations" must be an object.')
23
- return config
52
+ return /** @type {import('@wix/origami-types').RouteConfig} */ (config)
24
53
  }
25
54
 
26
55
  /** Key order and whitespace don't count when siblings are compared. */