@agence-zef/sdk 0.0.0-stage → 0.2.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 +4 -2
- package/dist/index.d.ts +61 -0
- package/dist/index.js +89 -0
- package/dist/types.d.ts +110 -0
- package/dist/types.js +1 -0
- package/package.json +24 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agence ZEF
|
|
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,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agence-zef/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Client typé de l'API `/api/v1` du CMS de l'agence ZEF.
|
|
4
|
+
|
|
5
|
+
Paquet du CMS de l'agence ZEF (dépôt privé). Licence MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ConfigCollections, Registry, ZefConfig } from "@agence-zef/schema";
|
|
2
|
+
import type { ApiItem, ApiLayout, ApiLink, ApiList, ApiPage, ApiPageEntry, ApiPreviewDocument, ApiRedirect, ApiSection, ApiSite, ApiSyncResult } from "./types.js";
|
|
3
|
+
export type * from "./types.js";
|
|
4
|
+
/** Erreur renvoyée par l'API (clé invalide, schéma refusé…). */
|
|
5
|
+
export declare class ZefApiError extends Error {
|
|
6
|
+
readonly status: number;
|
|
7
|
+
readonly code: string;
|
|
8
|
+
readonly details: string[];
|
|
9
|
+
constructor(status: number, code: string, message: string, details?: string[]);
|
|
10
|
+
}
|
|
11
|
+
export type ClientOptions = {
|
|
12
|
+
/** Adresse du CMS du client : `https://cave-vivante.agence-zef.fr`. */
|
|
13
|
+
url: string;
|
|
14
|
+
apiKey: string;
|
|
15
|
+
fetch?: typeof fetch;
|
|
16
|
+
};
|
|
17
|
+
type Collections<C extends ZefConfig> = ZefConfig extends C ? Record<string, ApiItem> : ConfigCollections<C>;
|
|
18
|
+
type LocaleOption = {
|
|
19
|
+
locale?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Client typé de l'API d'un site. Le type du registre (`typeof config`) donne
|
|
23
|
+
* les noms de collections et la forme de leurs données.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createClient<C extends ZefConfig = ZefConfig>(options: ClientOptions): {
|
|
26
|
+
/** Nom du site, langues, version du registre. */
|
|
27
|
+
site: () => Promise<ApiSite>;
|
|
28
|
+
/** Toutes les pages publiées et leurs adresses par langue. */
|
|
29
|
+
pages: () => Promise<ApiPageEntry[]>;
|
|
30
|
+
/** Page par adresse (`/la-cave`, `/en/the-cellar`) ; `null` si elle n'existe pas. */
|
|
31
|
+
page: (url: string) => Promise<ApiPage | null>;
|
|
32
|
+
collection: <K extends keyof Collections<C> & string>(name: K, opts?: LocaleOption & {
|
|
33
|
+
limit?: number;
|
|
34
|
+
offset?: number;
|
|
35
|
+
}) => Promise<ApiList<Collections<C>[K]>>;
|
|
36
|
+
item: <K extends keyof Collections<C> & string>(name: K, slug: string, opts?: LocaleOption) => Promise<Collections<C>[K] | null>;
|
|
37
|
+
/** Header et footer du site (sections partagées par toutes les pages). */
|
|
38
|
+
layout: (opts?: LocaleOption) => Promise<ApiLayout>;
|
|
39
|
+
/** Aperçu : header et footer en brouillon, champs marqués pour l'overlay. */
|
|
40
|
+
previewLayout: (opts?: LocaleOption) => Promise<ApiLayout>;
|
|
41
|
+
/** Redirections à appliquer au build (anciennes adresses, liens externes). */
|
|
42
|
+
redirects: () => Promise<ApiRedirect[]>;
|
|
43
|
+
/** Aperçu (jeton de l'éditeur) : document par identifiant, champs marqués pour l'overlay. */
|
|
44
|
+
/** `version` : une version de l'historique à la place du brouillon (E-05). */
|
|
45
|
+
previewDocument: (id: string, opts?: LocaleOption & {
|
|
46
|
+
version?: string;
|
|
47
|
+
}) => Promise<ApiPreviewDocument | null>;
|
|
48
|
+
/** Aperçu : une section du brouillon, résolue pour être rendue seule. */
|
|
49
|
+
previewSection: (section: {
|
|
50
|
+
id: string;
|
|
51
|
+
type: string;
|
|
52
|
+
}, opts?: LocaleOption & {
|
|
53
|
+
/** Page d'un élément de collection : ses variables (`{{domaine}}`) sont remplacées. */
|
|
54
|
+
item?: string;
|
|
55
|
+
}) => Promise<ApiSection>;
|
|
56
|
+
/** Vérifie la clé et enregistre le lien du dépôt (`zef link`). */
|
|
57
|
+
link: () => Promise<ApiLink>;
|
|
58
|
+
/** Envoie le registre du site (`zef sync`). */
|
|
59
|
+
sync: (registry: Registry) => Promise<ApiSyncResult>;
|
|
60
|
+
};
|
|
61
|
+
export type ZefClient<C extends ZefConfig = ZefConfig> = ReturnType<typeof createClient<C>>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** Erreur renvoyée par l'API (clé invalide, schéma refusé…). */
|
|
2
|
+
export class ZefApiError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
details;
|
|
6
|
+
constructor(status, code, message, details = []) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.details = details;
|
|
11
|
+
this.name = "ZefApiError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Client typé de l'API d'un site. Le type du registre (`typeof config`) donne
|
|
16
|
+
* les noms de collections et la forme de leurs données.
|
|
17
|
+
*/
|
|
18
|
+
export function createClient(options) {
|
|
19
|
+
const base = `${options.url.replace(/\/$/, "")}/api/v1`;
|
|
20
|
+
const doFetch = options.fetch ?? fetch;
|
|
21
|
+
async function request(path, init = {}) {
|
|
22
|
+
const query = new URLSearchParams();
|
|
23
|
+
for (const [k, v] of Object.entries(init.query ?? {})) {
|
|
24
|
+
if (v !== undefined && v !== null)
|
|
25
|
+
query.set(k, String(v));
|
|
26
|
+
}
|
|
27
|
+
const qs = query.size ? `?${query}` : "";
|
|
28
|
+
const res = await doFetch(`${base}${path}${qs}`, {
|
|
29
|
+
method: init.method ?? "GET",
|
|
30
|
+
headers: {
|
|
31
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
32
|
+
...(init.body === undefined ? {} : { "content-type": "application/json" }),
|
|
33
|
+
},
|
|
34
|
+
body: init.body === undefined ? undefined : JSON.stringify(init.body),
|
|
35
|
+
});
|
|
36
|
+
if (res.status === 404 && (init.method ?? "GET") === "GET") {
|
|
37
|
+
const body = (await res.json().catch(() => null));
|
|
38
|
+
if (body?.error.code === "not_found")
|
|
39
|
+
return null;
|
|
40
|
+
throw new ZefApiError(404, body?.error.code ?? "not_found", body?.error.message ?? "");
|
|
41
|
+
}
|
|
42
|
+
if (!res.ok) {
|
|
43
|
+
const body = (await res.json().catch(() => null));
|
|
44
|
+
throw new ZefApiError(res.status, body?.error.code ?? "http_error", body?.error.message ?? `Réponse ${res.status} du CMS`, body?.error.details);
|
|
45
|
+
}
|
|
46
|
+
return (await res.json());
|
|
47
|
+
}
|
|
48
|
+
const required = (value, what) => {
|
|
49
|
+
if (value === null)
|
|
50
|
+
throw new ZefApiError(404, "not_found", `${what} introuvable`);
|
|
51
|
+
return value;
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
/** Nom du site, langues, version du registre. */
|
|
55
|
+
site: async () => required(await request("/site"), "Site"),
|
|
56
|
+
/** Toutes les pages publiées et leurs adresses par langue. */
|
|
57
|
+
pages: async () => required(await request("/pages"), "Pages").pages,
|
|
58
|
+
/** Page par adresse (`/la-cave`, `/en/the-cellar`) ; `null` si elle n'existe pas. */
|
|
59
|
+
page: (url) => request("/pages", { query: { url } }),
|
|
60
|
+
collection: async (name, opts = {}) => required(await request(`/collections/${encodeURIComponent(name)}`, {
|
|
61
|
+
query: opts,
|
|
62
|
+
}), `Collection « ${name} »`),
|
|
63
|
+
item: (name, slug, opts = {}) => request(`/collections/${encodeURIComponent(name)}/${encodeURIComponent(slug)}`, { query: opts }),
|
|
64
|
+
/** Header et footer du site (sections partagées par toutes les pages). */
|
|
65
|
+
layout: async (opts = {}) => required(await request("/layout", { query: opts }), "Header et footer"),
|
|
66
|
+
/** Aperçu : header et footer en brouillon, champs marqués pour l'overlay. */
|
|
67
|
+
previewLayout: async (opts = {}) => required(await request("/layout", { query: { ...opts, stega: 1 } }), "Header et footer"),
|
|
68
|
+
/** Redirections à appliquer au build (anciennes adresses, liens externes). */
|
|
69
|
+
redirects: async () => required(await request("/redirects"), "Redirections").redirects,
|
|
70
|
+
/** Aperçu (jeton de l'éditeur) : document par identifiant, champs marqués pour l'overlay. */
|
|
71
|
+
/** `version` : une version de l'historique à la place du brouillon (E-05). */
|
|
72
|
+
previewDocument: (id, opts = {}) => request(`/preview/document/${encodeURIComponent(id)}`, {
|
|
73
|
+
query: { ...opts, stega: 1 },
|
|
74
|
+
}),
|
|
75
|
+
/** Aperçu : une section du brouillon, résolue pour être rendue seule. */
|
|
76
|
+
previewSection: async (section, opts = {}) => {
|
|
77
|
+
const { item, ...query } = opts;
|
|
78
|
+
return required(await request("/preview/section", {
|
|
79
|
+
method: "POST",
|
|
80
|
+
body: item ? { section, item } : { section },
|
|
81
|
+
query: { ...query, stega: 1 },
|
|
82
|
+
}), "Section");
|
|
83
|
+
},
|
|
84
|
+
/** Vérifie la clé et enregistre le lien du dépôt (`zef link`). */
|
|
85
|
+
link: async () => required(await request("/link", { method: "POST" }), "Lien"),
|
|
86
|
+
/** Envoie le registre du site (`zef sync`). */
|
|
87
|
+
sync: async (registry) => required(await request("/schema", { method: "POST", body: registry }), "Registre"),
|
|
88
|
+
};
|
|
89
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Media } from "@agence-zef/schema";
|
|
2
|
+
export type ApiKeyType = "publiee" | "apercu";
|
|
3
|
+
export type ApiSite = {
|
|
4
|
+
name: string;
|
|
5
|
+
locales: string[];
|
|
6
|
+
defaultLocale: string;
|
|
7
|
+
/** Version du registre reçu par `zef sync`, `null` avant le premier envoi. */
|
|
8
|
+
schemaVersion: number | null;
|
|
9
|
+
/**
|
|
10
|
+
* Le site peut être indexé : faux tant qu'il est en développement (§ 5.11) ; `@agence-zef/astro`
|
|
11
|
+
* pose alors `noindex` sur toutes les pages et un `robots.txt` bloquant.
|
|
12
|
+
*/
|
|
13
|
+
indexable: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Identifiant de mesure Google Analytics 4 (`G-…`) choisi dans C-21, seulement une fois le
|
|
16
|
+
* site en ligne : à passer à `<CookieBanner gaId>`, qui le charge après accord.
|
|
17
|
+
*/
|
|
18
|
+
ga4: string | null;
|
|
19
|
+
};
|
|
20
|
+
export type ApiSection = {
|
|
21
|
+
id: string;
|
|
22
|
+
/** Nom de la section dans le registre (`hero`). */
|
|
23
|
+
type: string;
|
|
24
|
+
variant: string | null;
|
|
25
|
+
anchor: string | null;
|
|
26
|
+
/** CSS personnalisé de l'agence (`&` désigne la section), sinon `null`. */
|
|
27
|
+
css: string | null;
|
|
28
|
+
/**
|
|
29
|
+
* Champs résolus dans la langue demandée. Bloc HTML (`type: "zef-html"`) :
|
|
30
|
+
* `{ name, html, css, js }`.
|
|
31
|
+
*/
|
|
32
|
+
data: Record<string, unknown>;
|
|
33
|
+
};
|
|
34
|
+
export type ApiSeo = {
|
|
35
|
+
title: string | null;
|
|
36
|
+
description: string | null;
|
|
37
|
+
noindex: boolean;
|
|
38
|
+
/** URL canonique choisie par l'agence, sinon `null` (l'adresse de la page). */
|
|
39
|
+
canonical: string | null;
|
|
40
|
+
/** Image de partage (1200 × 630 conseillé), sinon `null` : celle du gabarit du site. */
|
|
41
|
+
image: Media | null;
|
|
42
|
+
};
|
|
43
|
+
export type ApiPage = {
|
|
44
|
+
id: string;
|
|
45
|
+
locale: string;
|
|
46
|
+
url: string;
|
|
47
|
+
title: string;
|
|
48
|
+
seo: ApiSeo;
|
|
49
|
+
sections: ApiSection[];
|
|
50
|
+
/** Adresse de la page dans chaque langue. */
|
|
51
|
+
alternates: Record<string, string>;
|
|
52
|
+
};
|
|
53
|
+
/** Une page et ses adresses, pour générer les routes du site. */
|
|
54
|
+
export type ApiPageEntry = {
|
|
55
|
+
id: string;
|
|
56
|
+
urls: Record<string, string>;
|
|
57
|
+
};
|
|
58
|
+
/** Élément de collection : ses champs, puis les sections ajoutées sous le gabarit (E-01). */
|
|
59
|
+
export type ApiItem = Record<string, unknown> & {
|
|
60
|
+
id: string;
|
|
61
|
+
slug: string;
|
|
62
|
+
url: string | null;
|
|
63
|
+
sections: ApiSection[];
|
|
64
|
+
};
|
|
65
|
+
/** Aperçu de l'éditeur : une page ou un élément de collection, brouillon compris. */
|
|
66
|
+
export type ApiPreviewDocument = {
|
|
67
|
+
kind: "page";
|
|
68
|
+
page: ApiPage;
|
|
69
|
+
} | {
|
|
70
|
+
kind: "item";
|
|
71
|
+
collection: string;
|
|
72
|
+
titleField: string;
|
|
73
|
+
item: ApiItem;
|
|
74
|
+
};
|
|
75
|
+
/** Header et footer du site : sections partagées par toutes les pages. */
|
|
76
|
+
export type ApiLayout = {
|
|
77
|
+
header: ApiSection | null;
|
|
78
|
+
footer: ApiSection | null;
|
|
79
|
+
};
|
|
80
|
+
export type ApiList<T> = {
|
|
81
|
+
items: T[];
|
|
82
|
+
total: number;
|
|
83
|
+
};
|
|
84
|
+
export type ApiLink = {
|
|
85
|
+
site: string;
|
|
86
|
+
keyType: ApiKeyType;
|
|
87
|
+
};
|
|
88
|
+
/** Redirection du site (C-17), destination déjà traduite en adresse. */
|
|
89
|
+
export type ApiRedirect = {
|
|
90
|
+
from: string;
|
|
91
|
+
to: string;
|
|
92
|
+
status: 301 | 302;
|
|
93
|
+
};
|
|
94
|
+
export type ApiSyncResult = {
|
|
95
|
+
version: number;
|
|
96
|
+
/** `false` si le registre est identique à la version précédente. */
|
|
97
|
+
changed: boolean;
|
|
98
|
+
/** Champs retirés du registre qui contiennent encore des données (§ 3.3 du plan technique). */
|
|
99
|
+
removed: {
|
|
100
|
+
path: string;
|
|
101
|
+
documents: number;
|
|
102
|
+
}[];
|
|
103
|
+
};
|
|
104
|
+
export type ApiErrorBody = {
|
|
105
|
+
error: {
|
|
106
|
+
code: string;
|
|
107
|
+
message: string;
|
|
108
|
+
details?: string[];
|
|
109
|
+
};
|
|
110
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agence-zef/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Client typé de l'API d'un CMS ZEF (client.agence-zef.fr/api/v1).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@agence-zef/schema": "0.2.0"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://agence-zef.fr",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"typecheck": "tsc --noEmit"
|
|
26
|
+
}
|
|
6
27
|
}
|