@lupinum/ginko-content 0.3.2 → 0.3.4

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.
@@ -84,20 +84,20 @@ function fieldFromSchema(key, schema, localized) {
84
84
  const required = !["ZodOptional", "ZodNullable", "ZodDefault"].includes(getSchemaTypeName(schema) ?? "");
85
85
  if (metadata) return fieldFromMetadata(key, metadata, schema, unwrapped, localized, required);
86
86
  const reference = getReferenceDescriptor(unwrapped);
87
- if (reference) return field({ key, type: "relation", required, default: defaultFromSchema(schema), relation: { collection: reference.collection ?? "", multiple: false } });
87
+ if (reference) return field({ key, type: "relation", required, default: defaultFromSchema(schema, key), relation: { collection: reference.collection ?? "", multiple: false } });
88
88
  const typeName = getSchemaTypeName(unwrapped);
89
- if (typeName === "ZodObject") return field({ key, type: "object", required, localized, default: defaultFromSchema(schema), fields: nestedFields(unwrapped), validation: validationFromSchema(schema) });
89
+ if (typeName === "ZodObject") return field({ key, type: "object", required, localized, default: defaultFromSchema(schema, key), fields: nestedFields(unwrapped), validation: validationFromSchema(schema) });
90
90
  if (typeName === "ZodArray") {
91
91
  const element = getSchemaDef(unwrapped)?.element;
92
92
  const relation = getReferenceDescriptor(unwrapSchema(element));
93
- if (relation) return field({ key, type: "relations", required, default: defaultFromSchema(schema), relation: { collection: relation.collection ?? "", multiple: true } });
93
+ if (relation) return field({ key, type: "relations", required, default: defaultFromSchema(schema, key), relation: { collection: relation.collection ?? "", multiple: true } });
94
94
  const children = getSchemaTypeName(unwrapSchema(element)) === "ZodObject" ? nestedFields(element) : null;
95
- return field({ key, type: children ? "array" : "json", required, localized, default: defaultFromSchema(schema), fields: children, validation: validationFromSchema(schema) });
95
+ return field({ key, type: children ? "array" : "json", required, localized, default: defaultFromSchema(schema, key), fields: children, validation: validationFromSchema(schema) });
96
96
  }
97
- if (typeName === "ZodNumber") return field({ key, type: "number", required, default: defaultFromSchema(schema), validation: validationFromSchema(schema) });
98
- if (typeName === "ZodBoolean") return field({ key, type: "toggle", required, default: defaultFromSchema(schema), validation: validationFromSchema(schema) });
99
- if (typeName === "ZodDate") return field({ key, type: "date", required, default: defaultFromSchema(schema), validation: validationFromSchema(schema) });
100
- return field({ key, type: typeName === "ZodString" ? "text" : "json", required, localized, default: defaultFromSchema(schema), validation: validationFromSchema(schema) });
97
+ if (typeName === "ZodNumber") return field({ key, type: "number", required, default: defaultFromSchema(schema, key), validation: validationFromSchema(schema) });
98
+ if (typeName === "ZodBoolean") return field({ key, type: "toggle", required, default: defaultFromSchema(schema, key), validation: validationFromSchema(schema) });
99
+ if (typeName === "ZodDate") return field({ key, type: "date", required, default: defaultFromSchema(schema, key), validation: validationFromSchema(schema) });
100
+ return field({ key, type: typeName === "ZodString" ? "text" : "json", required, localized, default: defaultFromSchema(schema, key), validation: validationFromSchema(schema) });
101
101
  }
102
102
  function fieldFromMetadata(key, metadata, sourceSchema, schema, localized, required) {
103
103
  const type = metadata.type === "boolean" ? "toggle" : metadata.type === "asset" ? "file" : metadata.type;
@@ -107,7 +107,7 @@ function fieldFromMetadata(key, metadata, sourceSchema, schema, localized, requi
107
107
  type,
108
108
  required: metadata.required ?? required,
109
109
  localized: metadata.localized ?? localized,
110
- default: defaultFromSchema(sourceSchema),
110
+ default: defaultFromSchema(sourceSchema, key),
111
111
  options: metadata.options ?? null,
112
112
  relation: metadata.relation ? { collection: metadata.relation.collectionId, multiple: metadata.relation.multiple ?? type === "relations" } : null,
113
113
  media: metadata.image || metadata.asset ? { mediaTypes: portableMediaTypes(metadata.image?.accept ?? metadata.asset?.accept), aspectRatio: metadata.image?.aspectRatio ?? null } : null,
@@ -132,7 +132,7 @@ function mergeField(existing, key, override, localized) {
132
132
  localized: override.localized ?? base.localized,
133
133
  searchable: override.searchable ?? base.searchable,
134
134
  sortable: override.sortable ?? base.sortable,
135
- default: Object.prototype.hasOwnProperty.call(override, "defaultValue") ? { present: true, value: override.defaultValue } : base.default,
135
+ default: Object.prototype.hasOwnProperty.call(override, "defaultValue") ? { present: true, value: jsonDefault(override.defaultValue, key) } : base.default,
136
136
  options: override.options ?? base.options,
137
137
  relation: override.relation ? { collection: override.relation.collectionId, multiple: override.relation.multiple ?? type === "relations" } : base.relation,
138
138
  fields: override.fields ? configuredFields(override.fields, false) : base.fields,
@@ -143,17 +143,45 @@ function mergeField(existing, key, override, localized) {
143
143
  language: override.language ?? base.language
144
144
  });
145
145
  }
146
- function defaultFromSchema(schema) {
146
+ function defaultFromSchema(schema, key) {
147
147
  let current = schema;
148
148
  while (current) {
149
149
  const typeName = getSchemaTypeName(current);
150
150
  const definition = getSchemaDef(current);
151
- if (typeName === "ZodDefault") return { present: true, value: definition?.defaultValue };
151
+ if (typeName === "ZodDefault") return { present: true, value: jsonDefault(definition?.defaultValue, key) };
152
152
  if (!["ZodOptional", "ZodNullable"].includes(typeName ?? "")) break;
153
153
  current = definition?.innerType;
154
154
  }
155
155
  return { present: false };
156
156
  }
157
+ function jsonDefault(value, key, ancestors = /* @__PURE__ */ new Set()) {
158
+ if (value === null || typeof value === "string" || typeof value === "boolean") return value;
159
+ if (typeof value === "number") {
160
+ if (!Number.isFinite(value)) throw new TypeError(`Field "${key}" has a non-finite default value.`);
161
+ return value;
162
+ }
163
+ if (value instanceof Date) {
164
+ if (Number.isNaN(value.getTime())) throw new TypeError(`Field "${key}" has an invalid date default.`);
165
+ return value.toISOString();
166
+ }
167
+ if (typeof value !== "object" || value === null) throw new TypeError(`Field "${key}" has a non-JSON-serializable default value.`);
168
+ if (ancestors.has(value)) throw new TypeError(`Field "${key}" has a cyclic default value.`);
169
+ ancestors.add(value);
170
+ try {
171
+ if (Array.isArray(value)) return value.map((entry, index) => jsonDefault(entry, `${key}[${index}]`, ancestors));
172
+ const prototype = Object.getPrototypeOf(value);
173
+ if (prototype !== Object.prototype && prototype !== null) throw new TypeError(`Field "${key}" has a non-JSON-serializable default value.`);
174
+ if (Object.getOwnPropertySymbols(value).length > 0) throw new TypeError(`Field "${key}" has a non-JSON-serializable default value.`);
175
+ const result = {};
176
+ for (const [property, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) {
177
+ if (!descriptor.enumerable || !("value" in descriptor)) throw new TypeError(`Field "${key}" has a non-JSON-serializable default value.`);
178
+ result[property] = jsonDefault(descriptor.value, `${key}.${property}`, ancestors);
179
+ }
180
+ return result;
181
+ } finally {
182
+ ancestors.delete(value);
183
+ }
184
+ }
157
185
  function validationFromSchema(schema, semanticType) {
158
186
  const typeName = getSchemaTypeName(schema);
159
187
  const definition = getSchemaDef(schema);
@@ -170,16 +198,16 @@ function validationFromSchema(schema, semanticType) {
170
198
  const format = ["email", "url", "date", "datetime", "time"].includes(String(value.format)) ? value.format : null;
171
199
  return {
172
200
  kind: "string",
173
- minLength: typeof value.minLength === "number" ? value.minLength : null,
174
- maxLength: typeof value.maxLength === "number" ? value.maxLength : null,
201
+ minLength: finiteNumber(value.minLength),
202
+ maxLength: finiteNumber(value.maxLength),
175
203
  format
176
204
  };
177
205
  }
178
206
  if (typeName === "ZodNumber") {
179
207
  return {
180
208
  kind: "number",
181
- min: typeof value.minValue === "number" ? value.minValue : null,
182
- max: typeof value.maxValue === "number" ? value.maxValue : null,
209
+ min: finiteNumber(value.minValue),
210
+ max: finiteNumber(value.maxValue),
183
211
  integer: value.isInt === true
184
212
  };
185
213
  }
@@ -210,12 +238,17 @@ function arrayLimits(checks) {
210
238
  if (Array.isArray(checks)) {
211
239
  for (const check of checks) {
212
240
  const definition = check._zod?.def ?? check.def;
213
- if (definition?.check === "min_length" && typeof definition.minimum === "number") minItems = definition.minimum;
214
- if (definition?.check === "max_length" && typeof definition.maximum === "number") maxItems = definition.maximum;
241
+ const minimum = finiteNumber(definition?.minimum);
242
+ const maximum = finiteNumber(definition?.maximum);
243
+ if (definition?.check === "min_length" && minimum !== null) minItems = minimum;
244
+ if (definition?.check === "max_length" && maximum !== null) maxItems = maximum;
215
245
  }
216
246
  }
217
247
  return { minItems, maxItems };
218
248
  }
249
+ function finiteNumber(value) {
250
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
251
+ }
219
252
  function configuredFields(input, localized) {
220
253
  const entries = Array.isArray(input) ? input.map((value, index) => [String(index), value]) : Object.entries(input);
221
254
  return entries.flatMap(([key, value]) => value.type === "divider" || value.type === "section" ? [] : [mergeField(void 0, key, value, localized)]);
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lupinum/ginko-content",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "configKey": "content",
5
5
  "compatibility": {
6
6
  "nuxt": ">=4.4.7 <5"
package/dist/module.mjs CHANGED
@@ -4,7 +4,8 @@ import { readFile, readdir, rm } from 'node:fs/promises';
4
4
  import { join as join$1, basename, resolve as resolve$1, dirname, isAbsolute as isAbsolute$1 } from 'node:path';
5
5
  import { buildResolvedContentContract } from '../dist/cms-contract/index.js';
6
6
  import fs, { existsSync } from 'fs';
7
- import { join, relative, resolve, isAbsolute } from 'pathe';
7
+ import { createRequire } from 'node:module';
8
+ import { join, relative, isAbsolute, resolve } from 'pathe';
8
9
  import jiti from 'jiti';
9
10
  import { genSafeVariableName, genImport } from 'knitwork';
10
11
  import { hash } from 'ohash';
@@ -29,7 +30,7 @@ import { createRouterMatcher } from 'vue-router';
29
30
  import { globby } from 'globby';
30
31
 
31
32
  const name = "@lupinum/ginko-content";
32
- const version = "0.3.2";
33
+ const version = "0.3.4";
33
34
  const peerDependencies = {
34
35
  nuxt: ">=4.4.7 <5"};
35
36
 
@@ -40,6 +41,17 @@ const CONFIG_FILES = [
40
41
  "content.config.mjs",
41
42
  "content.config.cjs"
42
43
  ];
44
+ const isLocalModule = (filename, rootDir) => {
45
+ const localPath = relative(rootDir, filename);
46
+ return Boolean(localPath) && !localPath.startsWith("../") && !isAbsolute(localPath) && !localPath.split("/").includes("node_modules");
47
+ };
48
+ const clearLocalNativeModules = (cache, rootDir) => {
49
+ for (const [id, entry] of Object.entries(cache)) {
50
+ if (entry && isLocalModule(entry.filename, rootDir)) {
51
+ Reflect.deleteProperty(cache, id);
52
+ }
53
+ }
54
+ };
43
55
  function resolveContentConfigPath(nuxt) {
44
56
  return CONFIG_FILES.map((name) => join(nuxt.options.rootDir, name)).find((path) => existsSync(path));
45
57
  }
@@ -48,8 +60,21 @@ async function loadContentConfig(nuxt) {
48
60
  if (!configPath) {
49
61
  return {};
50
62
  }
51
- const importer = jiti(nuxt.options.rootDir, { interopDefault: true });
52
- const loaded = await importer.import(configPath);
63
+ const nativeRequire = createRequire(configPath);
64
+ clearLocalNativeModules(nativeRequire.cache, nuxt.options.rootDir);
65
+ const importer = jiti(nuxt.options.rootDir, {
66
+ interopDefault: true,
67
+ // Nuxt reloads module setup in the same process for `options.watch`
68
+ // changes. The authored config and its imports must not come from the
69
+ // previous setup's module cache.
70
+ moduleCache: false
71
+ });
72
+ let loaded;
73
+ try {
74
+ loaded = importer(configPath);
75
+ } finally {
76
+ clearLocalNativeModules(nativeRequire.cache, nuxt.options.rootDir);
77
+ }
53
78
  return loaded?.default || loaded || {};
54
79
  }
55
80
 
@@ -193,11 +218,8 @@ const registerContentDevRuntime = (nuxt, options, contentContext) => {
193
218
  const isIgnored = makeIgnored(contentContext.ignores);
194
219
  let viteServer;
195
220
  if (options.watch !== false) {
196
- nuxt.options.vite ||= {};
197
- nuxt.options.vite.plugins ||= [];
198
- nuxt.options.vite.plugins.push({
199
- name: "ginko-content-hmr",
200
- configureServer(server) {
221
+ nuxt.hook("vite:serverCreated", (server, environment) => {
222
+ if (environment.isClient) {
201
223
  viteServer = server;
202
224
  }
203
225
  });
@@ -1868,6 +1890,12 @@ const module$1 = defineNuxtModule({
1868
1890
  noExternal: runtimeInlineDependencies
1869
1891
  });
1870
1892
  const contentConfigPath = resolveContentConfigPath(nuxt);
1893
+ if (nuxt.options.dev && options.watch !== false && contentConfigPath) {
1894
+ nuxt.options.watch ||= [];
1895
+ if (!nuxt.options.watch.includes(contentConfigPath)) {
1896
+ nuxt.options.watch.push(contentConfigPath);
1897
+ }
1898
+ }
1871
1899
  const appContentConfig = await loadContentConfig(nuxt);
1872
1900
  if (!contentConfigPath || !appContentConfig.collections || !Object.keys(appContentConfig.collections).length) {
1873
1901
  throw new Error("@lupinum/ginko-content requires a content.config.ts with at least one collection. Define collections with defineContentConfig({ collections: { ... } }).");
@@ -1935,7 +1963,12 @@ const module$1 = defineNuxtModule({
1935
1963
  search: resolvedSearch,
1936
1964
  validation: options.validation || "report"
1937
1965
  };
1938
- await rm(resolve$1(nuxt.options.buildDir, "content-cache/validation.json"), { force: true });
1966
+ const contentCacheDir = resolve$1(nuxt.options.buildDir, "content-cache");
1967
+ if (nuxt.options.dev) {
1968
+ await rm(contentCacheDir, { recursive: true, force: true });
1969
+ } else {
1970
+ await rm(resolve$1(contentCacheDir, "validation.json"), { force: true });
1971
+ }
1939
1972
  const layers = nuxt.options._layers || [{ cwd: nuxt.options.rootDir, config: {} }];
1940
1973
  const nitroPublicAssets = nuxt.options.nitro?.publicAssets || [];
1941
1974
  contentContext.validationPublicAssets = await collectContentValidationPublicAssets({
@@ -1,2 +1,6 @@
1
+ type ContentHot = {
2
+ on: (event: 'ginko-content:update', callback: (data: unknown) => void) => void;
3
+ };
4
+ export declare function registerContentHotReload(hot: ContentHot | undefined, isClient: boolean, refresh: () => unknown): void;
1
5
  declare const _default: any;
2
6
  export default _default;
@@ -1,6 +1,14 @@
1
- import { defineNuxtPlugin } from "#imports";
2
- export default defineNuxtPlugin(() => {
3
- if (import.meta.client && import.meta.hot) {
4
- import("../composables/hot-reload.js").then(({ registerContentHotReload }) => registerContentHotReload());
1
+ import { defineNuxtPlugin, refreshNuxtData } from "#imports";
2
+ export function registerContentHotReload(hot, isClient, refresh) {
3
+ if (!hot || !isClient) {
4
+ return;
5
5
  }
6
+ hot.on("ginko-content:update", (data) => {
7
+ if (data && typeof data === "object") {
8
+ refresh();
9
+ }
10
+ });
11
+ }
12
+ export default defineNuxtPlugin(() => {
13
+ registerContentHotReload(import.meta.hot, import.meta.client, refreshNuxtData);
6
14
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "framework": "vue",
3
3
  "name": "@lupinum/ginko-content",
4
- "version": "0.3.2",
4
+ "version": "0.3.4",
5
5
  "contributions": {
6
6
  "html": {
7
7
  "description-markup": "markdown",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lupinum/ginko-content",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Collection-first content engine for Nuxt",
5
5
  "homepage": "https://github.com/lupinum-dev/ginko-content",
6
6
  "bugs": {
@@ -1,5 +0,0 @@
1
- type ContentHot = {
2
- on: (event: 'ginko-content:update', callback: (data: unknown) => void) => void;
3
- };
4
- export declare function registerContentHotReload(hot?: ContentHot | undefined, isClient?: boolean | undefined): void;
5
- export {};
@@ -1,15 +0,0 @@
1
- import { refreshNuxtData } from "#imports";
2
- let registered = false;
3
- const onContentUpdate = (data) => {
4
- if (!data || typeof data !== "object") {
5
- return;
6
- }
7
- refreshNuxtData();
8
- };
9
- export function registerContentHotReload(hot = import.meta.hot, isClient = import.meta.client) {
10
- if (!hot || !isClient || registered) {
11
- return;
12
- }
13
- registered = true;
14
- hot.on("ginko-content:update", onContentUpdate);
15
- }