@cms0/cms0 0.2.7 → 0.2.9

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
@@ -11,11 +11,15 @@ pnpm add @cms0/cms0
11
11
  ## Usage
12
12
 
13
13
  ```ts
14
- import { cms0 } from "@cms0/cms0";
15
- import type { Image } from "@cms0/cms0/custom-types";
14
+ import { cms0, toNextMetadata } from "@cms0/cms0";
15
+ import type { Metadata } from "next";
16
+ import type { Image, Seo } from "@cms0/cms0/custom-types";
16
17
 
17
18
  type RootSchema = {
18
- images: Image[];
19
+ homePage: {
20
+ heroImage: Image;
21
+ seo: Seo;
22
+ };
19
23
  };
20
24
 
21
25
  export const data = cms0<RootSchema>({
@@ -24,8 +28,29 @@ export const data = cms0<RootSchema>({
24
28
  key: import.meta.env.VITE_CMS0_API_KEY,
25
29
  },
26
30
  });
31
+
32
+ const page = await data.homePage();
33
+ const metadata = toNextMetadata<Metadata>(page.seo, { locale: "en" });
34
+ ```
35
+
36
+ ## Accessor Options
37
+
38
+ All root/model accessors accept options:
39
+
40
+ ```ts
41
+ await data.homePage({
42
+ includeId: true,
43
+ locale: "en",
44
+ fields: ["heroImage", "seo.openGraph.images"],
45
+ exclude: ["seo.jsonLd"],
46
+ query: { raw: 1 },
47
+ });
27
48
  ```
28
49
 
50
+ - `fields`: recursive include projection (supports nested paths like `a.b.c`).
51
+ - `exclude`: recursive remove projection (also supports nested paths).
52
+ - `query`: passthrough query params to API.
53
+
29
54
  ## Custom Types
30
55
 
31
56
  See `CUSTOM_TYPES.md` for the built-in custom types you can reference.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.customInlineTypeNames = exports.customModelTypeNames = exports.customTypeNames = exports.customTypeDescriptors = exports.customInlineTypeMetadata = exports.customInlineDescriptors = exports.customModelDescriptors = void 0;
4
4
  exports.getCustomInlineTypeMetadata = getCustomInlineTypeMetadata;
5
5
  exports.resolveCustomTypeDependencies = resolveCustomTypeDependencies;
6
+ const shared_1 = require("@cms0/shared");
6
7
  const primitive = (type) => ({
7
8
  kind: "primitive",
8
9
  type,
@@ -54,6 +55,237 @@ const localizedRichTextDescriptor = {
54
55
  type: "json",
55
56
  customType: "LocalizedRichText",
56
57
  };
58
+ function createUnionDescriptor(branches) {
59
+ return {
60
+ kind: "union",
61
+ anyOf: branches,
62
+ branchKeys: (0, shared_1.computeUnionBranchKeys)(branches),
63
+ };
64
+ }
65
+ const localizedOrStringDescriptor = createUnionDescriptor([
66
+ localizedStringDescriptor,
67
+ {
68
+ kind: "primitive",
69
+ type: "string",
70
+ },
71
+ ]);
72
+ const keywordsDescriptor = createUnionDescriptor([
73
+ localizedStringDescriptor,
74
+ {
75
+ type: "array",
76
+ items: {
77
+ kind: "primitive",
78
+ type: "string",
79
+ },
80
+ },
81
+ ]);
82
+ const robotsDescriptor = createUnionDescriptor([
83
+ {
84
+ kind: "primitive",
85
+ type: "boolean",
86
+ },
87
+ {
88
+ type: "object",
89
+ properties: {
90
+ index: {
91
+ kind: "primitive",
92
+ type: "boolean",
93
+ optional: true,
94
+ nullable: false,
95
+ },
96
+ follow: {
97
+ kind: "primitive",
98
+ type: "boolean",
99
+ optional: true,
100
+ nullable: false,
101
+ },
102
+ nocache: {
103
+ kind: "primitive",
104
+ type: "boolean",
105
+ optional: true,
106
+ nullable: false,
107
+ },
108
+ },
109
+ },
110
+ ]);
111
+ const alternatesDescriptor = {
112
+ type: "object",
113
+ properties: {
114
+ canonical: {
115
+ kind: "primitive",
116
+ type: "string",
117
+ optional: true,
118
+ nullable: false,
119
+ },
120
+ languages: {
121
+ kind: "primitive",
122
+ type: "json",
123
+ optional: true,
124
+ nullable: false,
125
+ },
126
+ },
127
+ };
128
+ const imageOrStringDescriptor = createUnionDescriptor([
129
+ {
130
+ kind: "modelRef",
131
+ model: "Image",
132
+ },
133
+ {
134
+ kind: "primitive",
135
+ type: "string",
136
+ },
137
+ ]);
138
+ const openGraphDescriptor = {
139
+ type: "object",
140
+ properties: {
141
+ type: {
142
+ kind: "enum",
143
+ valueType: "string",
144
+ values: ["website", "article", "profile"],
145
+ optional: true,
146
+ nullable: false,
147
+ },
148
+ url: {
149
+ kind: "primitive",
150
+ type: "string",
151
+ optional: true,
152
+ nullable: false,
153
+ },
154
+ siteName: {
155
+ kind: "primitive",
156
+ type: "string",
157
+ optional: true,
158
+ nullable: false,
159
+ },
160
+ title: {
161
+ ...localizedOrStringDescriptor,
162
+ optional: true,
163
+ nullable: false,
164
+ },
165
+ description: {
166
+ ...localizedOrStringDescriptor,
167
+ optional: true,
168
+ nullable: false,
169
+ },
170
+ images: {
171
+ type: "array",
172
+ items: imageOrStringDescriptor,
173
+ optional: true,
174
+ nullable: false,
175
+ },
176
+ locale: {
177
+ kind: "primitive",
178
+ type: "string",
179
+ optional: true,
180
+ nullable: false,
181
+ },
182
+ alternateLocale: {
183
+ type: "array",
184
+ items: {
185
+ kind: "primitive",
186
+ type: "string",
187
+ },
188
+ optional: true,
189
+ nullable: false,
190
+ },
191
+ },
192
+ };
193
+ const twitterDescriptor = {
194
+ type: "object",
195
+ properties: {
196
+ card: {
197
+ kind: "enum",
198
+ valueType: "string",
199
+ values: ["summary", "summary_large_image", "app", "player"],
200
+ optional: true,
201
+ nullable: false,
202
+ },
203
+ site: {
204
+ kind: "primitive",
205
+ type: "string",
206
+ optional: true,
207
+ nullable: false,
208
+ },
209
+ creator: {
210
+ kind: "primitive",
211
+ type: "string",
212
+ optional: true,
213
+ nullable: false,
214
+ },
215
+ title: {
216
+ ...localizedOrStringDescriptor,
217
+ optional: true,
218
+ nullable: false,
219
+ },
220
+ description: {
221
+ ...localizedOrStringDescriptor,
222
+ optional: true,
223
+ nullable: false,
224
+ },
225
+ images: {
226
+ type: "array",
227
+ items: imageOrStringDescriptor,
228
+ optional: true,
229
+ nullable: false,
230
+ },
231
+ },
232
+ };
233
+ const seoDescriptor = {
234
+ type: "object",
235
+ customType: "Seo",
236
+ properties: {
237
+ title: {
238
+ ...localizedOrStringDescriptor,
239
+ optional: true,
240
+ nullable: false,
241
+ },
242
+ description: {
243
+ ...localizedOrStringDescriptor,
244
+ optional: true,
245
+ nullable: false,
246
+ },
247
+ keywords: {
248
+ ...keywordsDescriptor,
249
+ optional: true,
250
+ nullable: false,
251
+ },
252
+ canonical: {
253
+ kind: "primitive",
254
+ type: "string",
255
+ optional: true,
256
+ nullable: false,
257
+ },
258
+ robots: {
259
+ ...robotsDescriptor,
260
+ optional: true,
261
+ nullable: false,
262
+ },
263
+ alternates: {
264
+ ...alternatesDescriptor,
265
+ optional: true,
266
+ nullable: false,
267
+ },
268
+ openGraph: {
269
+ ...openGraphDescriptor,
270
+ optional: true,
271
+ nullable: false,
272
+ },
273
+ twitter: {
274
+ ...twitterDescriptor,
275
+ optional: true,
276
+ nullable: false,
277
+ },
278
+ jsonLd: {
279
+ type: "array",
280
+ items: {
281
+ kind: "primitive",
282
+ type: "json",
283
+ },
284
+ optional: true,
285
+ nullable: false,
286
+ },
287
+ },
288
+ };
57
289
  exports.customModelDescriptors = {
58
290
  File: fileDescriptor,
59
291
  Image: imageDescriptor,
@@ -63,6 +295,7 @@ exports.customInlineDescriptors = {
63
295
  RichText: richTextDescriptor,
64
296
  LocalizedString: localizedStringDescriptor,
65
297
  LocalizedRichText: localizedRichTextDescriptor,
298
+ Seo: seoDescriptor,
66
299
  };
67
300
  exports.customInlineTypeMetadata = {
68
301
  RichText: {},
@@ -72,6 +305,7 @@ exports.customInlineTypeMetadata = {
72
305
  LocalizedRichText: {
73
306
  includeId: { mapLikeFields: ["locales"] },
74
307
  },
308
+ Seo: {},
75
309
  };
76
310
  exports.customTypeDescriptors = {
77
311
  ...exports.customModelDescriptors,
@@ -90,13 +324,20 @@ function resolveCustomTypeDependencies(names) {
90
324
  const name = queue.shift();
91
325
  if (!name || resolved.has(name))
92
326
  continue;
93
- const descriptor = exports.customModelDescriptors[name];
327
+ const modelDescriptor = exports.customModelDescriptors[name];
328
+ const inlineDescriptor = exports.customInlineDescriptors[name];
329
+ const descriptor = modelDescriptor ?? inlineDescriptor;
94
330
  if (!descriptor)
95
331
  continue;
96
332
  resolved.add(name);
97
333
  const refs = new Set();
98
- for (const prop of Object.values(descriptor.properties)) {
99
- collectModelRefs(prop, refs);
334
+ if (modelDescriptor) {
335
+ for (const prop of Object.values(modelDescriptor.properties)) {
336
+ collectModelRefs(prop, refs);
337
+ }
338
+ }
339
+ else {
340
+ collectModelRefs(inlineDescriptor, refs);
100
341
  }
101
342
  refs.forEach((ref) => {
102
343
  if (exports.customTypeDescriptors[ref] && !resolved.has(ref)) {
@@ -111,11 +352,15 @@ function collectModelRefs(desc, out) {
111
352
  out.add(desc.model);
112
353
  return;
113
354
  }
114
- if (desc.type === "array") {
355
+ if ("type" in desc && desc.type === "array") {
115
356
  collectModelRefs(desc.items, out);
116
357
  return;
117
358
  }
118
- if (desc.type === "object") {
359
+ if ("type" in desc && desc.type === "object") {
119
360
  Object.values(desc.properties).forEach((child) => collectModelRefs(child, out));
361
+ return;
362
+ }
363
+ if (desc.kind === "union") {
364
+ desc.anyOf.forEach((branch) => collectModelRefs(branch, out));
120
365
  }
121
366
  }