@overspace-app/client 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/client/index.d.ts +110 -0
- package/dist/client/index.js +168 -0
- package/dist/css.d.ts +8 -0
- package/dist/css.js +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/preview.d.ts +45 -0
- package/dist/preview.js +51 -0
- package/dist/revalidate/index.d.ts +24 -0
- package/dist/revalidate/index.js +39 -0
- package/dist/schema/compile.d.ts +88 -0
- package/dist/schema/compile.js +247 -0
- package/dist/schema/fields.d.ts +157 -0
- package/dist/schema/fields.js +32 -0
- package/dist/schema/hash.d.ts +12 -0
- package/dist/schema/hash.js +20 -0
- package/dist/schema/href.d.ts +12 -0
- package/dist/schema/href.js +29 -0
- package/dist/schema/index.d.ts +6 -0
- package/dist/schema/index.js +6 -0
- package/dist/schema/normalize.d.ts +30 -0
- package/dist/schema/normalize.js +143 -0
- package/dist/schema/site.d.ts +49 -0
- package/dist/schema/site.js +3 -0
- package/dist/video.d.ts +11 -0
- package/dist/video.js +44 -0
- package/package.json +42 -4
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { FIELD_TYPES } from './fields.js';
|
|
2
|
+
export const SCHEMA_FORMAT_VERSION = 1;
|
|
3
|
+
export const LIMITS = {
|
|
4
|
+
pages: 100,
|
|
5
|
+
sections: 200,
|
|
6
|
+
fieldsPerSection: 100,
|
|
7
|
+
fieldsPerListItem: 30,
|
|
8
|
+
listItems: 100,
|
|
9
|
+
galleryImages: 100,
|
|
10
|
+
selectOptions: 200,
|
|
11
|
+
categories: 50,
|
|
12
|
+
locales: 10,
|
|
13
|
+
label: 120,
|
|
14
|
+
hint: 500,
|
|
15
|
+
path: 200,
|
|
16
|
+
key: 50,
|
|
17
|
+
textMaxLength: 100_000,
|
|
18
|
+
};
|
|
19
|
+
export const SECTION_KEY_RE = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
20
|
+
export const FIELD_KEY_RE = /^[a-zA-Z][a-zA-Z0-9_]*$/;
|
|
21
|
+
export const LOCALE_RE = /^[a-z]{2}(?:-[A-Z]{2})?$/;
|
|
22
|
+
export const PAGE_PATH_RE = /^\/(?:[A-Za-z0-9._~%!$&'()*+,;=:@-]+(?:\/[A-Za-z0-9._~%!$&'()*+,;=:@-]+)*\/?)?$/;
|
|
23
|
+
const isReservedKey = (key) => key in Object.prototype || key === 'prototype';
|
|
24
|
+
const ENCODED_PATH_CHARS = /%2e|%2f|%5c/i;
|
|
25
|
+
export const MAX_PROBLEMS = 50;
|
|
26
|
+
export class SchemaError extends Error {
|
|
27
|
+
problems;
|
|
28
|
+
constructor(problems) {
|
|
29
|
+
super('Schéma invalide :\n- ' + problems.join('\n- '));
|
|
30
|
+
this.problems = problems;
|
|
31
|
+
this.name = 'SchemaError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function compileSite(def) {
|
|
35
|
+
const compiled = {
|
|
36
|
+
formatVersion: SCHEMA_FORMAT_VERSION,
|
|
37
|
+
locales: { default: def.locales.default, others: [...(def.locales.others ?? [])] },
|
|
38
|
+
pages: Object.entries(def.pages).map(([key, p]) => ({ key, label: p.label, path: p.path })),
|
|
39
|
+
sections: Object.entries(def.sections).map(([key, s]) => ({
|
|
40
|
+
key,
|
|
41
|
+
label: s.label,
|
|
42
|
+
page: s.page,
|
|
43
|
+
...(s.renamedFrom ? { renamedFrom: s.renamedFrom } : {}),
|
|
44
|
+
fields: Object.entries(s.fields).map(([fkey, f]) => compileField(fkey, f)),
|
|
45
|
+
})),
|
|
46
|
+
articles: { enabled: def.articles?.enabled ?? false, categories: [...(def.articles?.categories ?? [])] },
|
|
47
|
+
};
|
|
48
|
+
const problems = checkCompiledSchema(compiled);
|
|
49
|
+
if (problems.length > 0)
|
|
50
|
+
throw new SchemaError(problems);
|
|
51
|
+
return compiled;
|
|
52
|
+
}
|
|
53
|
+
function printable(v) {
|
|
54
|
+
return String(v).replace(/[\u0000-\u001f\u007f-\u009f]/g, '').slice(0, 80);
|
|
55
|
+
}
|
|
56
|
+
function compileField(key, f) {
|
|
57
|
+
if (f.type === 'list') {
|
|
58
|
+
const { item, ...rest } = f;
|
|
59
|
+
return { key, ...rest, item: Object.entries(item).map(([k, sub]) => ({ key: k, ...sub })) };
|
|
60
|
+
}
|
|
61
|
+
return { key, ...f };
|
|
62
|
+
}
|
|
63
|
+
export function checkCompiledSchema(s) {
|
|
64
|
+
const problems = [];
|
|
65
|
+
const p = (msg) => {
|
|
66
|
+
if (problems.length < MAX_PROBLEMS)
|
|
67
|
+
problems.push(msg);
|
|
68
|
+
};
|
|
69
|
+
if (s.formatVersion !== SCHEMA_FORMAT_VERSION)
|
|
70
|
+
p(`format ${String(s.formatVersion)} non pris en charge (attendu : ${SCHEMA_FORMAT_VERSION})`);
|
|
71
|
+
const locales = [s.locales.default, ...s.locales.others];
|
|
72
|
+
for (const l of locales)
|
|
73
|
+
if (!LOCALE_RE.test(l))
|
|
74
|
+
p(`langue « ${l} » invalide (ex. fr, en, pt-BR)`);
|
|
75
|
+
if (new Set(locales).size !== locales.length)
|
|
76
|
+
p('une langue est déclarée deux fois');
|
|
77
|
+
if (locales.length > LIMITS.locales)
|
|
78
|
+
p(`trop de langues (${locales.length} > ${LIMITS.locales})`);
|
|
79
|
+
if (s.pages.length === 0)
|
|
80
|
+
p('aucune page déclarée');
|
|
81
|
+
if (s.pages.length > LIMITS.pages)
|
|
82
|
+
p(`trop de pages (${s.pages.length} > ${LIMITS.pages})`);
|
|
83
|
+
const pageKeys = new Set();
|
|
84
|
+
const paths = new Set();
|
|
85
|
+
for (const page of s.pages) {
|
|
86
|
+
checkKey(page.key, SECTION_KEY_RE, `page « ${page.key} »`, p);
|
|
87
|
+
if (pageKeys.has(page.key))
|
|
88
|
+
p(`page « ${page.key} » déclarée deux fois`);
|
|
89
|
+
pageKeys.add(page.key);
|
|
90
|
+
checkLabel(page.label, `page « ${page.key} »`, p);
|
|
91
|
+
if (page.path.length > LIMITS.path || !PAGE_PATH_RE.test(page.path) || page.path.includes('..') || ENCODED_PATH_CHARS.test(page.path)) {
|
|
92
|
+
p(`page « ${printable(page.key)} » : chemin « ${printable(page.path)} » invalide (absolu, ex. /offre)`);
|
|
93
|
+
}
|
|
94
|
+
if (paths.has(page.path))
|
|
95
|
+
p(`chemin « ${page.path} » utilisé par deux pages`);
|
|
96
|
+
paths.add(page.path);
|
|
97
|
+
}
|
|
98
|
+
if (s.sections.length > LIMITS.sections)
|
|
99
|
+
p(`trop de sections (${s.sections.length} > ${LIMITS.sections})`);
|
|
100
|
+
const sectionKeys = new Set();
|
|
101
|
+
for (const sec of s.sections) {
|
|
102
|
+
const where = `section « ${sec.key} »`;
|
|
103
|
+
checkKey(sec.key, SECTION_KEY_RE, where, p);
|
|
104
|
+
if (sectionKeys.has(sec.key))
|
|
105
|
+
p(`${where} déclarée deux fois`);
|
|
106
|
+
sectionKeys.add(sec.key);
|
|
107
|
+
checkLabel(sec.label, where, p);
|
|
108
|
+
if (!pageKeys.has(sec.page))
|
|
109
|
+
p(`${where} : page « ${sec.page} » inconnue`);
|
|
110
|
+
if (sec.renamedFrom !== undefined)
|
|
111
|
+
checkKey(sec.renamedFrom, SECTION_KEY_RE, `${where} (renamedFrom)`, p);
|
|
112
|
+
if (sec.fields.length === 0)
|
|
113
|
+
p(`${where} : aucun champ`);
|
|
114
|
+
if (sec.fields.length > LIMITS.fieldsPerSection)
|
|
115
|
+
p(`${where} : trop de champs (> ${LIMITS.fieldsPerSection})`);
|
|
116
|
+
checkFields(sec.fields, where, p);
|
|
117
|
+
}
|
|
118
|
+
const sectionSources = new Set();
|
|
119
|
+
for (const sec of s.sections) {
|
|
120
|
+
if (!sec.renamedFrom)
|
|
121
|
+
continue;
|
|
122
|
+
if (sectionKeys.has(sec.renamedFrom))
|
|
123
|
+
p(`section « ${sec.key} » : renamedFrom « ${sec.renamedFrom} » est encore une section du schéma`);
|
|
124
|
+
if (sectionSources.has(sec.renamedFrom))
|
|
125
|
+
p(`renamedFrom « ${sec.renamedFrom} » utilisé par deux sections`);
|
|
126
|
+
sectionSources.add(sec.renamedFrom);
|
|
127
|
+
}
|
|
128
|
+
const cats = s.articles.categories;
|
|
129
|
+
if (cats.length > LIMITS.categories)
|
|
130
|
+
p(`trop de catégories d'articles (> ${LIMITS.categories})`);
|
|
131
|
+
if (new Set(cats).size !== cats.length)
|
|
132
|
+
p("une catégorie d'article est déclarée deux fois");
|
|
133
|
+
for (const c of cats) {
|
|
134
|
+
if (c.trim() === '' || c.length > LIMITS.label || c !== c.trim().normalize('NFC'))
|
|
135
|
+
p(`catégorie d'article « ${printable(c)} » invalide`);
|
|
136
|
+
}
|
|
137
|
+
return problems;
|
|
138
|
+
}
|
|
139
|
+
function checkFields(fields, where, p, inList = false) {
|
|
140
|
+
const keys = new Set();
|
|
141
|
+
const sources = new Set();
|
|
142
|
+
for (const f of fields) {
|
|
143
|
+
const at = `${where}, champ « ${printable(f.key)} »`;
|
|
144
|
+
checkKey(f.key, FIELD_KEY_RE, at, p);
|
|
145
|
+
if (keys.has(f.key))
|
|
146
|
+
p(`${at} déclaré deux fois`);
|
|
147
|
+
keys.add(f.key);
|
|
148
|
+
if (!FIELD_TYPES.includes(f.type)) {
|
|
149
|
+
p(`${at} : type « ${String(f.type)} » inconnu`);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
checkLabel(f.label, at, p);
|
|
153
|
+
if (f.hint !== undefined && f.hint.length > LIMITS.hint)
|
|
154
|
+
p(`${at} : aide trop longue (> ${LIMITS.hint})`);
|
|
155
|
+
if (f.renamedFrom !== undefined) {
|
|
156
|
+
checkKey(f.renamedFrom, FIELD_KEY_RE, `${at} (renamedFrom)`, p);
|
|
157
|
+
if (fields.some((o) => o.key === f.renamedFrom))
|
|
158
|
+
p(`${at} : renamedFrom « ${f.renamedFrom} » est encore un champ de la section`);
|
|
159
|
+
if (sources.has(f.renamedFrom))
|
|
160
|
+
p(`${at} : renamedFrom « ${f.renamedFrom} » utilisé par deux champs`);
|
|
161
|
+
sources.add(f.renamedFrom);
|
|
162
|
+
}
|
|
163
|
+
if (f.type === 'list') {
|
|
164
|
+
if (inList) {
|
|
165
|
+
p(`${at} : une liste ne peut pas contenir de liste`);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (!Array.isArray(f.item)) {
|
|
169
|
+
p(`${at} : éléments de liste mal formés`);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
checkRange(f.min, f.max, LIMITS.listItems, at, p);
|
|
173
|
+
if (f.item.length === 0)
|
|
174
|
+
p(`${at} : la liste n'a aucun champ`);
|
|
175
|
+
if (f.item.length > LIMITS.fieldsPerListItem)
|
|
176
|
+
p(`${at} : trop de champs par élément (> ${LIMITS.fieldsPerListItem})`);
|
|
177
|
+
checkFields(f.item, at, p, true);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (f.type === 'gallery')
|
|
181
|
+
checkRange(f.min, f.max, LIMITS.galleryImages, at, p);
|
|
182
|
+
if (f.type === 'number') {
|
|
183
|
+
if (f.min !== undefined && f.max !== undefined && f.min > f.max)
|
|
184
|
+
p(`${at} : min > max`);
|
|
185
|
+
if (f.step !== undefined && !(f.step > 0))
|
|
186
|
+
p(`${at} : step doit être positif`);
|
|
187
|
+
}
|
|
188
|
+
if ((f.type === 'text' || f.type === 'textarea') && f.maxLength !== undefined) {
|
|
189
|
+
if (!Number.isInteger(f.maxLength) || f.maxLength < 1 || f.maxLength > LIMITS.textMaxLength)
|
|
190
|
+
p(`${at} : maxLength invalide`);
|
|
191
|
+
}
|
|
192
|
+
if (f.type === 'image' || f.type === 'gallery') {
|
|
193
|
+
if (f.ratio !== undefined && !(f.ratio > 0 && f.ratio < 20))
|
|
194
|
+
p(`${at} : ratio invalide (ex. 16 / 9)`);
|
|
195
|
+
}
|
|
196
|
+
if (f.type === 'select') {
|
|
197
|
+
if (f.options.length === 0)
|
|
198
|
+
p(`${at} : aucune option`);
|
|
199
|
+
if (f.options.length > LIMITS.selectOptions)
|
|
200
|
+
p(`${at} : trop d'options (> ${LIMITS.selectOptions})`);
|
|
201
|
+
const values = f.options.map((o) => o.value);
|
|
202
|
+
if (new Set(values).size !== values.length)
|
|
203
|
+
p(`${at} : une option est déclarée deux fois`);
|
|
204
|
+
for (const o of f.options) {
|
|
205
|
+
if (o.value === '' || o.value.length > LIMITS.label)
|
|
206
|
+
p(`${at} : valeur d'option « ${o.value} » invalide`);
|
|
207
|
+
checkLabel(o.label, `${at}, option « ${o.value} »`, p);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (f.type === 'date') {
|
|
211
|
+
for (const d of [f.min, f.max])
|
|
212
|
+
if (d !== undefined && !isIsoDate(d))
|
|
213
|
+
p(`${at} : date limite « ${printable(d)} » invalide (AAAA-MM-JJ)`);
|
|
214
|
+
if (f.min !== undefined && f.max !== undefined && f.min > f.max)
|
|
215
|
+
p(`${at} : min > max`);
|
|
216
|
+
}
|
|
217
|
+
if (f.translatable && !['text', 'textarea', 'richText', 'image', 'gallery', 'link', 'url', 'video'].includes(f.type)) {
|
|
218
|
+
p(`${at} : le type « ${f.type} » ne se traduit pas`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function checkKey(key, re, where, p) {
|
|
223
|
+
if (key.length > LIMITS.key || !re.test(key))
|
|
224
|
+
p(`${printable(where)} : clé invalide`);
|
|
225
|
+
if (isReservedKey(key))
|
|
226
|
+
p(`${printable(where)} : clé réservée`);
|
|
227
|
+
}
|
|
228
|
+
function isIsoDate(v) {
|
|
229
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(v))
|
|
230
|
+
return false;
|
|
231
|
+
const d = new Date(v + 'T00:00:00Z');
|
|
232
|
+
return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === v;
|
|
233
|
+
}
|
|
234
|
+
function checkLabel(label, where, p) {
|
|
235
|
+
if (typeof label !== 'string' || label.trim() === '')
|
|
236
|
+
p(`${where} : libellé manquant`);
|
|
237
|
+
else if (label.length > LIMITS.label)
|
|
238
|
+
p(`${where} : libellé trop long (> ${LIMITS.label})`);
|
|
239
|
+
}
|
|
240
|
+
function checkRange(min, max, cap, where, p) {
|
|
241
|
+
for (const [n, v] of [['min', min], ['max', max]]) {
|
|
242
|
+
if (v !== undefined && (!Number.isInteger(v) || v < 0 || v > cap))
|
|
243
|
+
p(`${where} : ${n} invalide (entier entre 0 et ${cap})`);
|
|
244
|
+
}
|
|
245
|
+
if (min !== undefined && max !== undefined && min > max)
|
|
246
|
+
p(`${where} : min > max`);
|
|
247
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types de champ du schéma d'un site Overspace (format 1) — docs/integration/design.md §2.3.
|
|
3
|
+
*
|
|
4
|
+
* Règle d'évolution (§4.3) : on peut AJOUTER un type ou une option ; on ne retire ni ne change
|
|
5
|
+
* jamais le sens d'un type existant (des sites déjà branchés en dépendent).
|
|
6
|
+
*/
|
|
7
|
+
/** Document TipTap (JSON). Nettoyé côté serveur avant stockage ; rendu par `<RichText>`. */
|
|
8
|
+
export type RichTextDoc = {
|
|
9
|
+
type: 'doc';
|
|
10
|
+
content?: unknown[];
|
|
11
|
+
};
|
|
12
|
+
/** Image : référence média (`overspace-media:<chemin>`) ou chemin du site (`/photo.webp`), avec ou sans texte alternatif. */
|
|
13
|
+
export type ImageValue = string | {
|
|
14
|
+
src: string;
|
|
15
|
+
alt?: string;
|
|
16
|
+
};
|
|
17
|
+
/** Lien avec libellé. `href` : https, chemin interne (`/…`), `mailto:` ou `tel:`. */
|
|
18
|
+
export type LinkValue = {
|
|
19
|
+
label: string;
|
|
20
|
+
href: string;
|
|
21
|
+
};
|
|
22
|
+
export type RichTextMark = 'bold' | 'italic' | 'underline' | 'strike' | 'link' | 'heading' | 'bulletList' | 'orderedList' | 'blockquote';
|
|
23
|
+
export type SelectOption = {
|
|
24
|
+
readonly value: string;
|
|
25
|
+
readonly label: string;
|
|
26
|
+
};
|
|
27
|
+
/** Options communes à tous les champs simples. */
|
|
28
|
+
type Common<V> = {
|
|
29
|
+
/** Libellé affiché dans le dashboard. */
|
|
30
|
+
label: string;
|
|
31
|
+
/** Aide affichée sous le champ. */
|
|
32
|
+
hint?: string;
|
|
33
|
+
/** Valeur vide refusée à l'enregistrement. */
|
|
34
|
+
required?: boolean;
|
|
35
|
+
/** Une valeur par langue (sites multilingues, §2.4). Sans effet sur un site monolingue. */
|
|
36
|
+
translatable?: boolean;
|
|
37
|
+
/** Valeur d'origine : pré-remplie dans le dashboard, renvoyée par le SDK si Overspace est injoignable. */
|
|
38
|
+
default?: V;
|
|
39
|
+
/** Ancien nom du champ (renommage sans perte de contenu, §3.2). */
|
|
40
|
+
renamedFrom?: string;
|
|
41
|
+
};
|
|
42
|
+
export type TextField = Common<string> & {
|
|
43
|
+
type: 'text';
|
|
44
|
+
maxLength?: number;
|
|
45
|
+
placeholder?: string;
|
|
46
|
+
};
|
|
47
|
+
export type TextareaField = Common<string> & {
|
|
48
|
+
type: 'textarea';
|
|
49
|
+
maxLength?: number;
|
|
50
|
+
rows?: number;
|
|
51
|
+
};
|
|
52
|
+
export type RichTextField = Common<RichTextDoc> & {
|
|
53
|
+
type: 'richText';
|
|
54
|
+
marks?: readonly RichTextMark[];
|
|
55
|
+
};
|
|
56
|
+
export type ImageField = Common<ImageValue> & {
|
|
57
|
+
type: 'image';
|
|
58
|
+
ratio?: number;
|
|
59
|
+
altRequired?: boolean;
|
|
60
|
+
};
|
|
61
|
+
export type GalleryField = Common<ImageValue[]> & {
|
|
62
|
+
type: 'gallery';
|
|
63
|
+
min?: number;
|
|
64
|
+
max?: number;
|
|
65
|
+
ratio?: number;
|
|
66
|
+
};
|
|
67
|
+
export type VideoField = Common<string> & {
|
|
68
|
+
type: 'video';
|
|
69
|
+
};
|
|
70
|
+
/** Lien seul (sans libellé) — type historique, conservé pour ne déplacer aucune donnée existante. */
|
|
71
|
+
export type UrlField = Common<string> & {
|
|
72
|
+
type: 'url';
|
|
73
|
+
};
|
|
74
|
+
export type LinkField = Common<LinkValue> & {
|
|
75
|
+
type: 'link';
|
|
76
|
+
internalOnly?: boolean;
|
|
77
|
+
};
|
|
78
|
+
export type NumberField = Common<number> & {
|
|
79
|
+
type: 'number';
|
|
80
|
+
min?: number;
|
|
81
|
+
max?: number;
|
|
82
|
+
step?: number;
|
|
83
|
+
unit?: string;
|
|
84
|
+
};
|
|
85
|
+
export type DateField = Common<string> & {
|
|
86
|
+
type: 'date';
|
|
87
|
+
min?: string;
|
|
88
|
+
max?: string;
|
|
89
|
+
};
|
|
90
|
+
export type BooleanField = Common<boolean> & {
|
|
91
|
+
type: 'boolean';
|
|
92
|
+
};
|
|
93
|
+
export type SelectField<O extends string = string> = Common<O | O[]> & {
|
|
94
|
+
type: 'select';
|
|
95
|
+
options: readonly {
|
|
96
|
+
readonly value: O;
|
|
97
|
+
readonly label: string;
|
|
98
|
+
}[];
|
|
99
|
+
multiple?: boolean;
|
|
100
|
+
};
|
|
101
|
+
export type ReferenceField = Common<string> & {
|
|
102
|
+
type: 'reference';
|
|
103
|
+
category?: string;
|
|
104
|
+
};
|
|
105
|
+
/** Champ simple : tout sauf la liste. Seuls ces champs peuvent composer une liste. */
|
|
106
|
+
export type ScalarField = TextField | TextareaField | RichTextField | ImageField | GalleryField | VideoField | UrlField | LinkField | NumberField | DateField | BooleanField | SelectField | ReferenceField;
|
|
107
|
+
/** Liste répétable d'éléments composés de champs simples (un seul niveau d'imbrication). */
|
|
108
|
+
export type ListField<I extends Record<string, ScalarField> = Record<string, ScalarField>> = {
|
|
109
|
+
type: 'list';
|
|
110
|
+
label: string;
|
|
111
|
+
hint?: string;
|
|
112
|
+
item: I;
|
|
113
|
+
itemLabel?: string;
|
|
114
|
+
min?: number;
|
|
115
|
+
max?: number;
|
|
116
|
+
/** `false` : nombre d'éléments figé (pas de bouton « Ajouter »). */
|
|
117
|
+
addable?: boolean;
|
|
118
|
+
renamedFrom?: string;
|
|
119
|
+
default?: Array<{
|
|
120
|
+
[K in keyof I]?: FieldValue<I[K]>;
|
|
121
|
+
}>;
|
|
122
|
+
};
|
|
123
|
+
export type AnyField = ScalarField | ListField;
|
|
124
|
+
export type FieldType = AnyField['type'];
|
|
125
|
+
export declare const FIELD_TYPES: readonly ["text", "textarea", "richText", "image", "gallery", "video", "url", "link", "number", "date", "boolean", "select", "reference", "list"];
|
|
126
|
+
/** Valeur stockée pour un champ (ce que le dashboard enregistre et ce que l'API renvoie). */
|
|
127
|
+
export type FieldValue<F> = F extends ListField<infer I> ? Array<{
|
|
128
|
+
-readonly [K in keyof I]: FieldValue<I[K]>;
|
|
129
|
+
}> : F extends SelectField<infer O> ? F extends {
|
|
130
|
+
multiple: true;
|
|
131
|
+
} ? O[] : O : F extends RichTextField ? RichTextDoc : F extends ImageField ? ImageValue : F extends GalleryField ? ImageValue[] : F extends LinkField ? LinkValue : F extends NumberField ? number : F extends BooleanField ? boolean : string;
|
|
132
|
+
type Opts<F> = Omit<F, 'type'>;
|
|
133
|
+
export declare const field: {
|
|
134
|
+
text: (o: Opts<TextField>) => TextField;
|
|
135
|
+
textarea: (o: Opts<TextareaField>) => TextareaField;
|
|
136
|
+
richText: (o: Opts<RichTextField>) => RichTextField;
|
|
137
|
+
image: (o: Opts<ImageField>) => ImageField;
|
|
138
|
+
gallery: (o: Opts<GalleryField>) => GalleryField;
|
|
139
|
+
video: (o: Opts<VideoField>) => VideoField;
|
|
140
|
+
url: (o: Opts<UrlField>) => UrlField;
|
|
141
|
+
link: (o: Opts<LinkField>) => LinkField;
|
|
142
|
+
number: (o: Opts<NumberField>) => NumberField;
|
|
143
|
+
date: (o: Opts<DateField>) => DateField;
|
|
144
|
+
boolean: (o: Opts<BooleanField>) => BooleanField;
|
|
145
|
+
select: <const O extends string, const M extends boolean = false>(o: Opts<SelectField<O>> & {
|
|
146
|
+
options: readonly {
|
|
147
|
+
readonly value: O;
|
|
148
|
+
readonly label: string;
|
|
149
|
+
}[];
|
|
150
|
+
multiple?: M;
|
|
151
|
+
}) => SelectField<O> & {
|
|
152
|
+
multiple: M;
|
|
153
|
+
};
|
|
154
|
+
reference: (o: Opts<ReferenceField>) => ReferenceField;
|
|
155
|
+
list: <const I extends Record<string, ScalarField>>(o: Opts<ListField<I>>) => ListField<I>;
|
|
156
|
+
};
|
|
157
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const FIELD_TYPES = [
|
|
2
|
+
'text',
|
|
3
|
+
'textarea',
|
|
4
|
+
'richText',
|
|
5
|
+
'image',
|
|
6
|
+
'gallery',
|
|
7
|
+
'video',
|
|
8
|
+
'url',
|
|
9
|
+
'link',
|
|
10
|
+
'number',
|
|
11
|
+
'date',
|
|
12
|
+
'boolean',
|
|
13
|
+
'select',
|
|
14
|
+
'reference',
|
|
15
|
+
'list',
|
|
16
|
+
];
|
|
17
|
+
export const field = {
|
|
18
|
+
text: (o) => ({ type: 'text', ...o }),
|
|
19
|
+
textarea: (o) => ({ type: 'textarea', ...o }),
|
|
20
|
+
richText: (o) => ({ type: 'richText', ...o }),
|
|
21
|
+
image: (o) => ({ type: 'image', ...o }),
|
|
22
|
+
gallery: (o) => ({ type: 'gallery', ...o }),
|
|
23
|
+
video: (o) => ({ type: 'video', ...o }),
|
|
24
|
+
url: (o) => ({ type: 'url', ...o }),
|
|
25
|
+
link: (o) => ({ type: 'link', ...o }),
|
|
26
|
+
number: (o) => ({ type: 'number', ...o }),
|
|
27
|
+
date: (o) => ({ type: 'date', ...o }),
|
|
28
|
+
boolean: (o) => ({ type: 'boolean', ...o }),
|
|
29
|
+
select: (o) => ({ type: 'select', ...o }),
|
|
30
|
+
reference: (o) => ({ type: 'reference', ...o }),
|
|
31
|
+
list: (o) => ({ type: 'list', ...o }),
|
|
32
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CompiledSchema } from './compile.js';
|
|
2
|
+
/**
|
|
3
|
+
* Empreinte d'un schéma compilé — la même partout (serveur, CLI, SDK) : stockée dans
|
|
4
|
+
* `site_schemas.hash`, renvoyée par l'API (`schemaHash`), envoyée par le SDK (`Overspace-Schema-Hash`,
|
|
5
|
+
* revue Fable R-21) et comparée par `doctor` / `schema diff`.
|
|
6
|
+
*
|
|
7
|
+
* Forme canonique : clés d'objet triées (ordre du code de caractère), aucun espace ; l'ordre des
|
|
8
|
+
* tableaux compte (c'est l'ordre d'affichage). Indépendante de la façon dont le fichier a été écrit.
|
|
9
|
+
*/
|
|
10
|
+
export declare function canonicalJson(value: unknown): string;
|
|
11
|
+
/** SHA-256 (hexadécimal, 64 caractères) de la forme canonique. Web Crypto : Node ≥ 20, navigateurs, edge. */
|
|
12
|
+
export declare function schemaHash(schema: CompiledSchema): Promise<string>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function canonicalJson(value) {
|
|
2
|
+
if (value === null || typeof value !== 'object') {
|
|
3
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
4
|
+
throw new Error('canonicalJson : nombre non fini');
|
|
5
|
+
if (value === undefined)
|
|
6
|
+
throw new Error('canonicalJson : valeur indéfinie');
|
|
7
|
+
return JSON.stringify(value);
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(value))
|
|
10
|
+
return '[' + value.map((v) => canonicalJson(v)).join(',') + ']';
|
|
11
|
+
const entries = Object.entries(value)
|
|
12
|
+
.filter(([, v]) => v !== undefined)
|
|
13
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
14
|
+
return '{' + entries.map(([k, v]) => JSON.stringify(k) + ':' + canonicalJson(v)).join(',') + '}';
|
|
15
|
+
}
|
|
16
|
+
export async function schemaHash(schema) {
|
|
17
|
+
const bytes = new TextEncoder().encode(canonicalJson(schema));
|
|
18
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', bytes);
|
|
19
|
+
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join('');
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LA règle « lien sûr » d'Overspace — une seule définition, utilisée par la validation serveur du
|
|
3
|
+
* contenu, le nettoyage du texte riche et les composants du SDK (revue Fable R-29, R-34).
|
|
4
|
+
*
|
|
5
|
+
* Accepté : `https://` / `http://` avec hôte (sans identifiants), chemin interne (`/…`, ni `//` ni `/\`),
|
|
6
|
+
* ancre (`#…`), `mailto:adresse`, `tel:numéro`.
|
|
7
|
+
* Refusé : tout autre schéma (`javascript:`, `data:`, `vbscript:`…), espaces en bord, caractères de
|
|
8
|
+
* contrôle, `https:hôte` sans `//` (lu comme un chemin relatif par le navigateur), `https://a@b`
|
|
9
|
+
* (identifiants dans l'URL, trompeurs à la lecture).
|
|
10
|
+
*/
|
|
11
|
+
export declare const HREF_MAX = 2000;
|
|
12
|
+
export declare function isSafeHref(href: string, internalOnly?: boolean): boolean;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const HREF_MAX = 2_000;
|
|
2
|
+
export function isSafeHref(href, internalOnly = false) {
|
|
3
|
+
const v = href;
|
|
4
|
+
if (typeof v !== 'string' || v === '' || v !== v.trim() || v.length > HREF_MAX)
|
|
5
|
+
return false;
|
|
6
|
+
if (/[\u0000-\u001f\u007f]/.test(v))
|
|
7
|
+
return false;
|
|
8
|
+
if (/^tel:\+?[0-9 ().-]{3,30}$/i.test(v))
|
|
9
|
+
return !internalOnly;
|
|
10
|
+
if (/\s/.test(v))
|
|
11
|
+
return false;
|
|
12
|
+
if (v.startsWith('/'))
|
|
13
|
+
return !v.startsWith('//') && !v.startsWith('/\\');
|
|
14
|
+
if (v.startsWith('#'))
|
|
15
|
+
return true;
|
|
16
|
+
if (internalOnly)
|
|
17
|
+
return false;
|
|
18
|
+
if (/^mailto:[^@\s]+@[^@\s]+$/i.test(v))
|
|
19
|
+
return true;
|
|
20
|
+
if (!/^https?:\/\//i.test(v))
|
|
21
|
+
return false;
|
|
22
|
+
try {
|
|
23
|
+
const u = new URL(v);
|
|
24
|
+
return (u.protocol === 'https:' || u.protocol === 'http:') && u.hostname !== '' && u.username === '' && u.password === '';
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CompiledField, CompiledSchema, CompiledSection } from './compile.js';
|
|
2
|
+
/**
|
|
3
|
+
* Lecture NORMALISÉE d'une section (ADR 0007 §3, revue Fable R-25) — partagée par l'API v1 (serveur) et le SDK
|
|
4
|
+
* (valeurs par défaut sans réseau, mode autonome, contrôle des réponses reçues).
|
|
5
|
+
*
|
|
6
|
+
* Garanties pour le site client (et pour les types du SDK) :
|
|
7
|
+
* - chaque champ déclaré est présent et du bon type ;
|
|
8
|
+
* - champ ABSENT en base → valeur par défaut du schéma, sinon valeur vide du type ;
|
|
9
|
+
* - valeur de MAUVAIS type (ancienne donnée) → valeur vide du type (jamais une donnée inattendue) ;
|
|
10
|
+
* - traductions fusionnées champ par champ, repli sur la langue par défaut si vide ;
|
|
11
|
+
* - clés hors schéma (ancien nom d'un champ renommé, champ archivé) servies telles quelles pendant leur
|
|
12
|
+
* grâce de 90 jours (R-21).
|
|
13
|
+
* Fonctions pures : aucun accès à la base (les références et médias sont résolus par l'appelant).
|
|
14
|
+
*/
|
|
15
|
+
type Obj = Record<string, unknown>;
|
|
16
|
+
/** Valeur vide d'un type de champ. */
|
|
17
|
+
export declare function emptyValue(f: CompiledField): unknown;
|
|
18
|
+
export declare function normalizeSection(section: CompiledSection, content: unknown, translations: unknown, locale: string | null, defaultLocale: string): Obj;
|
|
19
|
+
/**
|
|
20
|
+
* Section demandée → section du schéma. Une ancienne clé (section renommée, `renamedFrom`) reste lisible pendant
|
|
21
|
+
* la grâce : elle désigne la section sous son nouveau nom (le contenu a été déplacé par l'activation).
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveSection(schema: CompiledSchema, key: string): CompiledSection | null;
|
|
24
|
+
/** Langue demandée (`?locale=`) si le site la déclare, sinon la langue par défaut. */
|
|
25
|
+
export declare function pickLocale(schema: CompiledSchema, requested: string | null): string;
|
|
26
|
+
/** Identifiants d'articles cités par les champs `reference` d'une section normalisée. */
|
|
27
|
+
export declare function referenceIds(section: CompiledSection, values: Obj): string[];
|
|
28
|
+
/** Remplace les identifiants par l'article résolu (même site, publié) ou `null` — R-31. */
|
|
29
|
+
export declare function applyReferences(section: CompiledSection, values: Obj, articles: Map<string, unknown>): Obj;
|
|
30
|
+
export {};
|