@realnation/content-config 0.8.0 → 0.9.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 CHANGED
@@ -65,6 +65,45 @@ export default {
65
65
  - 出口规则算法在 `@realnation/content-config/exits`(`computeExits`、`checkExitRules`),模块构建与建活动 skill 用同一份。
66
66
  - 产物 `content-config.json` 多一个可选的 `dynamicExits` 键,`format` 仍是 1:读取方不认识就当没有。
67
67
 
68
+ ## 引导步骤 bootstrap(0.9.0 起)
69
+
70
+ 有的模块保存内容配置之前,要先在活动里建一套内容、拿到编号填进请求体。例如 Digital Signage:素材库与播放列表挂在活动上
71
+ (`POST /partner/contents/digital-signage/{projectId}/signages` 回 `{ signageId }`),没有活动编号建不了,出厂默认里
72
+ `detail.signageId` 只能是空的,标着 `x-fill-before-save`。配置页是第一次打开时自己调这个接口;建活动 skill 不打开配置页,
73
+ 就照说明里的 `bootstrap` 在活动建好、存内容配置之前调一次,把返回值填进请求体再存。
74
+
75
+ ```ts
76
+ export default {
77
+ endpoint: 'common',
78
+ defaultBody: () => ({ configId: 0, detail: createEmptyDetail() }), // detail.signageId 是 null
79
+ schema: { /* detail.signageId:type integer、minimum 1、'x-fill-before-save': true */ },
80
+ bootstrap: [
81
+ {
82
+ method: 'POST',
83
+ path: '/partner/contents/digital-signage/{projectId}/signages',
84
+ body: {},
85
+ fills: { 'detail.signageId': 'signageId' },
86
+ description: 'Creates the signage content set (library, playlists, screen settings) for this campaign.'
87
+ }
88
+ ]
89
+ } satisfies ContentConfigSpec;
90
+ ```
91
+
92
+ - `method`:只认 `POST`、`PUT`。
93
+ - `path`:平台接口路径,以 `/partner/` 开头、必须带 `{projectId}`;占位只认 `{projectId}`(活动编号,`p_…`)、`{versionId}`(草稿版本编号)。
94
+ 路径照配置页调的那个接口写,接口名引用代码里的同一个常量更好。
95
+ - `body`:请求体,可省(省了发 `{}`);原样发,不带占位。
96
+ - `fills`:`{ 内容配置请求体里的路径: 返回 data 里的路径 }`,都用点号分隔;返回值取平台 `ApiResponse` 的 `data`(如 `{ data: { signageId: 12 } }` 写 `signageId`)。
97
+ 请求体那一格只能沿 `properties` 走(不能在数组、以 id 为键的表里),在字段说明里要标 `x-fill-before-save`。
98
+ - `description`:可省,英文。
99
+ - skill 的做法:校验要提交的请求体时,`fills` 指的格空着也放过(别的 `x-fill-before-save` 照样拦);活动建好后只在那一格还空着
100
+ (缺席、null、空串、空数组)时调这一步,已经填了(人用 `--config-file` 给了编号)就跳过;填好再按说明严格校验一遍才存。
101
+ 模块默认(save-default)里带着的这一格是做模板那个活动的编号,skill 换回出厂的空值,再照引导步骤给这个活动新建。
102
+ - 构建时核对:键认得、方法与路径合规、`fills` 指的格在说明里且标了 `x-fill-before-save`、同一格只填一次。不过关构建就失败。
103
+ 核对函数在 `@realnation/content-config/bootstrap`(`checkBootstrap`)。
104
+ - 产物 `content-config.json` 多一个可选的 `bootstrap` 键,`format` 仍是 1:读取方不认识就当没有(老 skill 照旧在校验处停下点名)。
105
+ - 只给「没有活动就建不了」的东西用。能放进出厂默认的值照旧写进 `defaultBody`,不要拿引导步骤绕过 `x-fill-before-save`(例如要人上传的视频)。
106
+
68
107
  ## 写字段说明的规矩
69
108
 
70
109
  - **默认值与范围引用代码里的同一份常量**(默认值函数、LIMITS、枚举表),不照抄数字;改代码说明自动跟上。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realnation/content-config",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "qiksense 模块内容配置的机器可读说明:构建时导出 content-config.json,并用字段说明校验默认请求体;0.5.0 起另导出画布说明 canvas-config.json(体验端)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,6 +19,10 @@
19
19
  "./canvas": {
20
20
  "types": "./src/canvas.d.ts",
21
21
  "import": "./src/canvas.mjs"
22
+ },
23
+ "./bootstrap": {
24
+ "types": "./src/bootstrap.d.ts",
25
+ "import": "./src/bootstrap.mjs"
22
26
  }
23
27
  },
24
28
  "files": [
@@ -0,0 +1,27 @@
1
+ import type { JsonSchema } from './validate';
2
+
3
+ /**
4
+ * 引导步骤(0.9.0 起):保存内容配置之前要先在活动里建一套内容的模块(如 Digital Signage 先建 signage 拿 signageId),
5
+ * 建活动 skill 在活动建好、存内容配置之前照它调一次接口,把返回 data 里的值填进请求体。fills 指的格已经填了就跳过。
6
+ */
7
+ export interface BootstrapStep {
8
+ /** 只认 POST、PUT */
9
+ method: 'POST' | 'PUT';
10
+ /** 平台接口路径,以 /partner/ 开头、带 {projectId};占位只认 {projectId}、{versionId} */
11
+ path: string;
12
+ /** 请求体,可省(省了发 {}) */
13
+ body?: Record<string, unknown>;
14
+ /** { 内容配置请求体里的路径: 返回 data 里的路径 }(点号分隔);请求体那一格要标 x-fill-before-save */
15
+ fills: Record<string, string>;
16
+ /** 英文,给人与 AI 看这一步建的是什么 */
17
+ description?: string;
18
+ }
19
+
20
+ export declare const BOOTSTRAP_METHODS: readonly ['POST', 'PUT'];
21
+ export declare const BOOTSTRAP_PLACEHOLDERS: readonly ['projectId', 'versionId'];
22
+ /** 字段说明里点号路径指的那一层(只沿 properties 走) */
23
+ export declare function schemaAt(schema: JsonSchema, path: string): JsonSchema | undefined;
24
+ /** 引导步骤要填的请求体路径 */
25
+ export declare function bootstrapTargets(steps: readonly BootstrapStep[] | undefined): string[];
26
+ /** 检查引导步骤的写法;给了字段说明时再核对 fills 指的格。返回问题清单,空数组 = 通过 */
27
+ export declare function checkBootstrap(steps: unknown, options?: { schema?: JsonSchema }): string[];
@@ -0,0 +1,73 @@
1
+ // 引导步骤(0.9.0 起):有的模块保存内容配置之前,要先在活动里建一套内容、拿到编号填进请求体
2
+ // (Digital Signage 要先 POST /partner/contents/digital-signage/{projectId}/signages 拿 signageId;signage 挂在活动上,
3
+ // 没有活动编号建不了,所以出厂默认里这一格只能是空的)。配置页是自己在第一次打开时调这个接口;
4
+ // 建活动 skill 不打开配置页,就照说明里的 bootstrap 在活动建好、存内容配置之前调一次,把返回值填进请求体。
5
+ //
6
+ // 一步写成:
7
+ // { method: 'POST', path: '/partner/contents/digital-signage/{projectId}/signages', body: {}, fills: { 'detail.signageId': 'signageId' } }
8
+ // method 只认 POST、PUT
9
+ // path 平台接口路径,以 /partner/ 开头、带 {projectId};占位只认 {projectId}、{versionId}(活动编号、草稿版本编号)
10
+ // body 请求体,可省(省了发 {});只能是原样的 JSON 对象,不带占位
11
+ // fills { 内容配置请求体里的路径: 返回 data 里的路径 }(点号分隔)。请求体那一格要在字段说明里、标了 x-fill-before-save
12
+ // description 可省,英文,给人与 AI 看这一步建的是什么
13
+ // skill 只在 fills 指的格还空着(缺席、null、空串、空数组)时调这一步;已经填了(人用 --config-file 给了编号)就跳过。
14
+
15
+ export const BOOTSTRAP_METHODS = ['POST', 'PUT']
16
+ export const BOOTSTRAP_PLACEHOLDERS = ['projectId', 'versionId']
17
+ const STEP_KEYS = new Set(['method', 'path', 'body', 'fills', 'description'])
18
+ const DOT_PATH = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/
19
+ const PATH_CHARS = /^\/partner\/[A-Za-z0-9/_{}.-]+$/
20
+
21
+ const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v)
22
+
23
+ // 字段说明里点号路径指的那一层(只沿 properties 走;数组里的、以 id 为键的表里的格不能当引导目标)
24
+ export function schemaAt(schema, path) {
25
+ let cur = schema
26
+ for (const seg of path.split('.')) {
27
+ cur = isPlainObject(cur?.properties) ? cur.properties[seg] : undefined
28
+ if (!isPlainObject(cur)) return undefined
29
+ }
30
+ return cur
31
+ }
32
+
33
+ // 引导步骤要填的请求体路径
34
+ export const bootstrapTargets = (steps) => (Array.isArray(steps) ? steps.flatMap((s) => (isPlainObject(s?.fills) ? Object.keys(s.fills) : [])) : [])
35
+
36
+ // 检查引导步骤的写法;给了字段说明时再核对 fills 指的格在说明里、标了 x-fill-before-save。返回问题清单,空数组 = 通过
37
+ export function checkBootstrap(steps, { schema } = {}) {
38
+ const errors = []
39
+ if (!Array.isArray(steps) || !steps.length) return ['bootstrap 要写成至少一步的数组']
40
+ const seen = new Set()
41
+ steps.forEach((step, i) => {
42
+ const at = `bootstrap[${i}]`
43
+ if (!isPlainObject(step)) return errors.push(`${at}:每一步是一个对象`)
44
+ for (const k of Object.keys(step)) if (!STEP_KEYS.has(k)) errors.push(`${at}:认不出的键 ${k}(能写 ${[...STEP_KEYS].join('、')})`)
45
+ if (!BOOTSTRAP_METHODS.includes(step.method)) errors.push(`${at}.method:只认 ${BOOTSTRAP_METHODS.join('、')},收到 ${JSON.stringify(step.method)}`)
46
+ if (typeof step.path !== 'string' || !PATH_CHARS.test(step.path) || step.path.includes('..')) {
47
+ errors.push(`${at}.path:要以 /partner/ 开头,只含字母、数字、/ _ - . 与占位,收到 ${JSON.stringify(step.path)}`)
48
+ } else {
49
+ const names = [...step.path.matchAll(/\{([^{}]*)\}/g)].map((m) => m[1])
50
+ for (const n of names) if (!BOOTSTRAP_PLACEHOLDERS.includes(n)) errors.push(`${at}.path:占位只认 ${BOOTSTRAP_PLACEHOLDERS.map((p) => `{${p}}`).join('、')},收到 {${n}}`)
51
+ if (!names.includes('projectId')) errors.push(`${at}.path:要带 {projectId}(引导建的东西挂在活动上)`)
52
+ if (/[{}]/.test(step.path.replace(/\{[^{}]*\}/g, ''))) errors.push(`${at}.path:花括号没配对`)
53
+ }
54
+ if (step.body !== undefined && !isPlainObject(step.body)) errors.push(`${at}.body:要是一个 JSON 对象(可省,省了发 {})`)
55
+ if (step.description !== undefined && typeof step.description !== 'string') errors.push(`${at}.description:要是字符串`)
56
+ if (!isPlainObject(step.fills) || !Object.keys(step.fills).length) {
57
+ errors.push(`${at}.fills:写成 { 请求体里的路径: 返回 data 里的路径 },至少一项`)
58
+ return
59
+ }
60
+ for (const [target, from] of Object.entries(step.fills)) {
61
+ if (!DOT_PATH.test(target)) errors.push(`${at}.fills:请求体路径 ${JSON.stringify(target)} 要写成点号分隔的字段名(如 detail.signageId)`)
62
+ if (typeof from !== 'string' || !DOT_PATH.test(from)) errors.push(`${at}.fills.${target}:返回 data 里的路径要写成点号分隔的字段名(如 signageId),收到 ${JSON.stringify(from)}`)
63
+ if (seen.has(target)) errors.push(`${at}.fills.${target}:前面的步骤已经填这一格`)
64
+ seen.add(target)
65
+ if (schema && DOT_PATH.test(target)) {
66
+ const field = schemaAt(schema, target)
67
+ if (!field) errors.push(`${at}.fills.${target}:字段说明里沿 properties 找不到这一格`)
68
+ else if (field['x-fill-before-save'] !== true) errors.push(`${at}.fills.${target}:这一格要标 x-fill-before-save(出厂默认是空的,保存前由引导步骤填)`)
69
+ }
70
+ }
71
+ })
72
+ return errors
73
+ }
package/src/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import type { Plugin } from 'vite';
2
2
  import type { JsonSchema } from './validate';
3
3
  import type { DynamicExitRule } from './exits';
4
+ import type { BootstrapStep } from './bootstrap';
4
5
 
5
6
  export type { JsonSchema } from './validate';
6
7
  export { validate, SUPPORTED_KEYWORDS } from './validate';
7
8
  export type { DynamicExitRule, FixedExitRule, EachExitRule, DynamicExit } from './exits';
8
9
  export { computeExits, checkExitRules } from './exits';
10
+ export type { BootstrapStep } from './bootstrap';
11
+ export { checkBootstrap, bootstrapTargets, schemaAt, BOOTSTRAP_METHODS, BOOTSTRAP_PLACEHOLDERS } from './bootstrap';
9
12
 
10
13
  /** 模块 src/content-config.ts 默认导出的形状 */
11
14
  export interface ContentConfigSpec<Body = unknown> {
@@ -17,6 +20,8 @@ export interface ContentConfigSpec<Body = unknown> {
17
20
  schema: JsonSchema;
18
21
  /** 配置页按内容配置算出、保存时推给宿主 setAccessDynamicPorts 的出口,用数据写成的同一套算法(0.4.0 起);出口全在模块目录里的模块不写 */
19
22
  dynamicExits?: readonly DynamicExitRule[];
23
+ /** 保存前要先在活动里建一套内容的模块:建活动 skill 在活动建好、存内容配置之前照它调接口、把返回值填进请求体(0.9.0 起);不用的模块不写 */
24
+ bootstrap?: readonly BootstrapStep[];
20
25
  }
21
26
 
22
27
  /** 构建产物 content-config.json 的形状 */
@@ -27,6 +32,8 @@ export interface ContentConfigFile<Body = unknown> {
27
32
  schema: JsonSchema;
28
33
  /** 模块写了 dynamicExits 才有 */
29
34
  dynamicExits?: readonly DynamicExitRule[];
35
+ /** 模块写了 bootstrap 才有 */
36
+ bootstrap?: readonly BootstrapStep[];
30
37
  }
31
38
 
32
39
  export interface ContentConfigPluginOptions {
package/src/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default, buildContentConfig, loadSpec, checkLabels, collectLabels, FORMAT } from './vite-plugin.mjs'
2
2
  export { validate, SUPPORTED_KEYWORDS } from './validate.mjs'
3
3
  export { computeExits, checkExitRules } from './exits.mjs'
4
+ export { checkBootstrap, bootstrapTargets, schemaAt, BOOTSTRAP_METHODS, BOOTSTRAP_PLACEHOLDERS } from './bootstrap.mjs'
@@ -8,6 +8,7 @@ import { pathToFileURL } from 'node:url'
8
8
  import { validate } from './validate.mjs'
9
9
  import { checkLabels } from './labels.mjs'
10
10
  import { checkExitRules } from './exits.mjs'
11
+ import { checkBootstrap } from './bootstrap.mjs'
11
12
 
12
13
  export { checkLabels, collectLabels } from './labels.mjs'
13
14
 
@@ -53,19 +54,22 @@ export async function loadSpec(root, entry) {
53
54
  export async function buildContentConfig(root, entry = 'src/content-config.ts', { labelDirs = ['src'], labelFiles } = {}) {
54
55
  const spec = await loadSpec(root, entry)
55
56
  if (!spec || typeof spec.endpoint !== 'string' || !spec.endpoint || !spec.schema || spec.defaultBody === undefined) {
56
- throw new Error(`content-config:${entry} 要默认导出 { endpoint, defaultBody, schema }(有动态出口的再加 dynamicExits)`)
57
+ throw new Error(`content-config:${entry} 要默认导出 { endpoint, defaultBody, schema }(有动态出口的再加 dynamicExits,保存前要先建内容的再加 bootstrap)`)
57
58
  }
58
59
  const defaultBody = typeof spec.defaultBody === 'function' ? await spec.defaultBody() : spec.defaultBody
59
60
  // 出厂默认可以留空「保存前必须填」的字段(x-fill-before-save),其余照常校验
60
61
  const errors = validate(spec.schema, defaultBody, '$', { allowUnfilled: true })
61
62
  if (labelDirs.length) errors.push(...checkLabels(spec.schema, root, { dirs: labelDirs, exclude: [entry], files: labelFiles }))
62
63
  if (spec.dynamicExits !== undefined) errors.push(...checkExitRules(spec.dynamicExits, { schema: spec.schema, body: defaultBody }))
64
+ if (spec.bootstrap !== undefined) errors.push(...checkBootstrap(spec.bootstrap, { schema: spec.schema }))
63
65
  if (errors.length) {
64
66
  throw new Error(`content-config:字段说明没通过检查(${errors.length} 处)\n ${errors.join('\n ')}`)
65
67
  }
66
68
  // dynamicExits 是 0.4.0 加的可选键,格式号不变:读取方不认识就当没有(建活动 skill 读到才补出口)
67
69
  const doc = { format: FORMAT, endpoint: spec.endpoint, defaultBody, schema: spec.schema }
68
70
  if (spec.dynamicExits !== undefined) doc.dynamicExits = spec.dynamicExits
71
+ // bootstrap 是 0.9.0 加的可选键,格式号同样不变(建活动 skill 读到才在存内容配置之前照它建)
72
+ if (spec.bootstrap !== undefined) doc.bootstrap = spec.bootstrap
69
73
  return doc
70
74
  }
71
75