@overspace-app/client 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,143 @@
1
+ const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
2
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3
+ export function emptyValue(f) {
4
+ switch (f.type) {
5
+ case 'text':
6
+ case 'textarea':
7
+ case 'video':
8
+ case 'url':
9
+ case 'date':
10
+ return '';
11
+ case 'boolean':
12
+ return false;
13
+ case 'gallery':
14
+ case 'list':
15
+ return [];
16
+ case 'select':
17
+ return f.multiple ? [] : null;
18
+ default:
19
+ return null;
20
+ }
21
+ }
22
+ function isImageValue(v) {
23
+ return (typeof v === 'string' && v !== '') || (isObj(v) && typeof v.src === 'string' && v.src !== '');
24
+ }
25
+ function coerceScalar(f, v) {
26
+ switch (f.type) {
27
+ case 'text':
28
+ case 'textarea':
29
+ case 'video':
30
+ case 'url':
31
+ case 'date':
32
+ return typeof v === 'string' ? v : '';
33
+ case 'richText':
34
+ return isObj(v) && v.type === 'doc' ? v : null;
35
+ case 'image':
36
+ return isImageValue(v) ? v : null;
37
+ case 'gallery':
38
+ return Array.isArray(v) ? v.filter(isImageValue) : [];
39
+ case 'link':
40
+ return isObj(v) && typeof v.href === 'string' && v.href !== '' ? { label: typeof v.label === 'string' ? v.label : '', href: v.href } : null;
41
+ case 'number':
42
+ return typeof v === 'number' && Number.isFinite(v) ? v : null;
43
+ case 'boolean':
44
+ return typeof v === 'boolean' ? v : false;
45
+ case 'select': {
46
+ const allowed = new Set(f.options.map((o) => o.value));
47
+ if (f.multiple)
48
+ return Array.isArray(v) ? v.filter((x) => typeof x === 'string' && allowed.has(x)) : [];
49
+ return typeof v === 'string' && allowed.has(v) ? v : null;
50
+ }
51
+ case 'reference':
52
+ return typeof v === 'string' && UUID.test(v) ? v : null;
53
+ }
54
+ }
55
+ function normalizeField(f, v) {
56
+ if (v === undefined)
57
+ return f.default !== undefined ? normalizeField(f, f.default) : emptyValue(f);
58
+ if (f.type !== 'list')
59
+ return coerceScalar(f, v);
60
+ if (!Array.isArray(v))
61
+ return [];
62
+ return v.filter(isObj).map((item) => {
63
+ const out = {};
64
+ for (const sub of f.item)
65
+ out[sub.key] = item[sub.key] === undefined ? emptyValue(sub) : coerceScalar(sub, item[sub.key]);
66
+ return out;
67
+ });
68
+ }
69
+ function isBlank(v) {
70
+ return v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0);
71
+ }
72
+ export function normalizeSection(section, content, translations, locale, defaultLocale) {
73
+ const base = isObj(content) ? content : {};
74
+ const tr = locale && locale !== defaultLocale && isObj(translations) && isObj(translations[locale]) ? translations[locale] : null;
75
+ const declared = new Set(section.fields.map((f) => f.key));
76
+ const out = {};
77
+ for (const [k, v] of Object.entries(base))
78
+ if (!declared.has(k))
79
+ out[k] = v;
80
+ for (const f of section.fields) {
81
+ let value = normalizeField(f, base[f.key]);
82
+ if (tr && f.type === 'list' && Array.isArray(value) && Array.isArray(tr[f.key])) {
83
+ const trItems = tr[f.key];
84
+ value = value.map((item, i) => {
85
+ const t = trItems[i];
86
+ if (!isObj(t))
87
+ return item;
88
+ const merged = { ...item };
89
+ for (const sub of f.item) {
90
+ if (sub.translatable && !isBlank(t[sub.key]))
91
+ merged[sub.key] = coerceScalar(sub, t[sub.key]);
92
+ }
93
+ return merged;
94
+ });
95
+ }
96
+ else if (tr && f.type !== 'list' && f.translatable && !isBlank(tr[f.key])) {
97
+ value = coerceScalar(f, tr[f.key]);
98
+ }
99
+ out[f.key] = value;
100
+ }
101
+ return out;
102
+ }
103
+ export function resolveSection(schema, key) {
104
+ return schema.sections.find((s) => s.key === key) ?? schema.sections.find((s) => s.renamedFrom === key) ?? null;
105
+ }
106
+ export function pickLocale(schema, requested) {
107
+ if (requested && (requested === schema.locales.default || schema.locales.others.includes(requested)))
108
+ return requested;
109
+ return schema.locales.default;
110
+ }
111
+ export function referenceIds(section, values) {
112
+ const ids = new Set();
113
+ for (const f of section.fields) {
114
+ if (f.type === 'reference' && typeof values[f.key] === 'string')
115
+ ids.add(values[f.key]);
116
+ if (f.type === 'list' && Array.isArray(values[f.key])) {
117
+ for (const item of values[f.key]) {
118
+ for (const sub of f.item)
119
+ if (sub.type === 'reference' && typeof item[sub.key] === 'string')
120
+ ids.add(item[sub.key]);
121
+ }
122
+ }
123
+ }
124
+ return [...ids];
125
+ }
126
+ export function applyReferences(section, values, articles) {
127
+ const pick = (v) => (typeof v === 'string' ? (articles.get(v) ?? null) : null);
128
+ const out = { ...values };
129
+ for (const f of section.fields) {
130
+ if (f.type === 'reference')
131
+ out[f.key] = pick(values[f.key]);
132
+ if (f.type === 'list' && Array.isArray(values[f.key]) && f.item.some((s) => s.type === 'reference')) {
133
+ out[f.key] = values[f.key].map((item) => {
134
+ const copy = { ...item };
135
+ for (const sub of f.item)
136
+ if (sub.type === 'reference')
137
+ copy[sub.key] = pick(item[sub.key]);
138
+ return copy;
139
+ });
140
+ }
141
+ }
142
+ return out;
143
+ }
@@ -0,0 +1,49 @@
1
+ import type { AnyField, FieldValue } from './fields.js';
2
+ /**
3
+ * Définition d'un site (`overspace.config.ts`) — docs/integration/design.md §2.
4
+ * Objets indexés par clé : l'ordre d'écriture est l'ordre d'affichage, et TypeScript déduit les
5
+ * types du contenu directement depuis le fichier (aucune génération de code).
6
+ */
7
+ export type PageDefinition = {
8
+ label: string;
9
+ /** Chemin de la page sur le site (`/`, `/offre`) : sert à l'aperçu. */
10
+ path: string;
11
+ };
12
+ export type SectionDefinition = {
13
+ label: string;
14
+ /** Clé d'une page déclarée dans `pages`. */
15
+ page: string;
16
+ fields: Record<string, AnyField>;
17
+ /** Ancien nom de la section (renommage sans perte de contenu, §3.2). */
18
+ renamedFrom?: string;
19
+ };
20
+ export type SiteDefinition = {
21
+ locales: {
22
+ default: string;
23
+ others?: readonly string[];
24
+ };
25
+ pages: Record<string, PageDefinition>;
26
+ sections: Record<string, SectionDefinition>;
27
+ articles?: {
28
+ enabled: boolean;
29
+ categories?: readonly string[];
30
+ };
31
+ };
32
+ /** Identité : ne sert qu'à conserver les types littéraux du fichier de configuration. */
33
+ export declare function defineSite<const D extends SiteDefinition>(definition: D): D;
34
+ /** Contenu d'une section tel que l'API le renvoie (langue déjà résolue). */
35
+ export type SectionValues<S extends SectionDefinition> = {
36
+ -readonly [K in keyof S['fields']]: FieldValue<S['fields'][K]>;
37
+ };
38
+ /** Clés de section d'un site. */
39
+ export type SectionKey<D extends SiteDefinition> = Extract<keyof D['sections'], string>;
40
+ /** Contenu typé d'une section donnée d'un site. */
41
+ export type SectionContent<D extends SiteDefinition, K extends SectionKey<D>> = SectionValues<D['sections'][K]>;
42
+ /** Clés de page d'un site. */
43
+ export type PageKey<D extends SiteDefinition> = Extract<keyof D['pages'], string>;
44
+ /** Contenu d'une page : ses sections, chacune typée. */
45
+ export type PageContent<D extends SiteDefinition, P extends PageKey<D>> = {
46
+ -readonly [K in SectionKey<D> as D['sections'][K]['page'] extends P ? K : never]: SectionContent<D, K>;
47
+ };
48
+ /** Langues d'un site. */
49
+ export type Locale<D extends SiteDefinition> = D['locales']['default'] | (D['locales']['others'] extends readonly (infer L)[] ? L : never);
@@ -0,0 +1,3 @@
1
+ export function defineSite(definition) {
2
+ return definition;
3
+ }
@@ -0,0 +1,11 @@
1
+ export type VideoProvider = 'youtube' | 'vimeo';
2
+ export type ParsedVideo = {
3
+ provider: VideoProvider;
4
+ id: string;
5
+ };
6
+ /** Extrait provider + id d'un lien YouTube/Vimeo collé par l'utilisateur, ou null. */
7
+ export declare function parseVideoUrl(input: string): ParsedVideo | null;
8
+ /** Lien canonique d'une vidéo reconnue : c'est lui qui est stocké, jamais le lien collé. */
9
+ export declare function canonicalVideoUrl(v: ParsedVideo): string;
10
+ /** Valide un couple provider/id (utilisé par le sanitizer — défense en profondeur). */
11
+ export declare function isValidVideo(provider: unknown, id: unknown): id is string;
package/dist/video.js ADDED
@@ -0,0 +1,44 @@
1
+ const YT_ID = /^[A-Za-z0-9_-]{11}$/;
2
+ const VIMEO_ID = /^\d{6,12}$/;
3
+ const YOUTUBE_HOSTS = new Set(['youtube.com', 'www.youtube.com', 'm.youtube.com', 'youtube-nocookie.com', 'www.youtube-nocookie.com']);
4
+ const VIMEO_HOSTS = new Set(['vimeo.com', 'www.vimeo.com', 'player.vimeo.com']);
5
+ export function parseVideoUrl(input) {
6
+ const raw = (input || '').trim();
7
+ if (!raw || raw.length > 500)
8
+ return null;
9
+ let u;
10
+ try {
11
+ u = new URL(raw);
12
+ }
13
+ catch {
14
+ return null;
15
+ }
16
+ if (u.protocol !== 'https:' || u.username !== '' || u.password !== '')
17
+ return null;
18
+ const host = u.hostname.toLowerCase();
19
+ if (host === 'youtu.be') {
20
+ const id = u.pathname.split('/')[1] ?? '';
21
+ return YT_ID.test(id) ? { provider: 'youtube', id } : null;
22
+ }
23
+ if (YOUTUBE_HOSTS.has(host)) {
24
+ const id = u.pathname === '/watch' ? (u.searchParams.get('v') ?? '') : (u.pathname.match(/^\/(?:embed|shorts|live)\/([^/]+)\/?$/)?.[1] ?? '');
25
+ return YT_ID.test(id) ? { provider: 'youtube', id } : null;
26
+ }
27
+ if (VIMEO_HOSTS.has(host)) {
28
+ const id = u.pathname.match(/^\/(?:video\/)?(\d{6,12})\/?$/)?.[1] ?? '';
29
+ return VIMEO_ID.test(id) ? { provider: 'vimeo', id } : null;
30
+ }
31
+ return null;
32
+ }
33
+ export function canonicalVideoUrl(v) {
34
+ return v.provider === 'youtube' ? `https://www.youtube.com/watch?v=${v.id}` : `https://vimeo.com/${v.id}`;
35
+ }
36
+ export function isValidVideo(provider, id) {
37
+ if (typeof id !== 'string')
38
+ return false;
39
+ if (provider === 'youtube')
40
+ return YT_ID.test(id);
41
+ if (provider === 'vimeo')
42
+ return VIMEO_ID.test(id);
43
+ return false;
44
+ }
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@overspace-app/client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Overspace — schéma de site et client de lecture typé (sans dépendance).",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./schema": {
14
+ "types": "./dist/schema/index.d.ts",
15
+ "import": "./dist/schema/index.js"
16
+ },
17
+ "./revalidate": {
18
+ "types": "./dist/revalidate/index.d.ts",
19
+ "import": "./dist/revalidate/index.js"
20
+ },
21
+ "./video": {
22
+ "types": "./dist/video.d.ts",
23
+ "import": "./dist/video.js"
24
+ },
25
+ "./preview": {
26
+ "types": "./dist/preview.d.ts",
27
+ "import": "./dist/preview.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "README.md",
33
+ "LICENSE"
34
+ ],
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "build": "node ../../scripts/build-packages.mjs"
40
+ },
41
+ "engines": {
42
+ "node": ">=20"
43
+ }
44
+ }