@backmatter/core 0.0.29 → 0.0.31
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/package.json +1 -1
- package/src/model.ts +38 -0
package/package.json
CHANGED
package/src/model.ts
CHANGED
|
@@ -257,7 +257,45 @@ export function serializeSchema(
|
|
|
257
257
|
return snapshot;
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
+
/** Remove `{locale}` path/route segments from rows stored before locale removal. */
|
|
261
|
+
export function stripLocaleTemplateSegments(template: string): string {
|
|
262
|
+
return template.split("/").filter((part) => part !== "{locale}").join("/");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function migrateLegacySchemaInput(input: unknown): unknown {
|
|
266
|
+
if (!input || typeof input !== "object") return input;
|
|
267
|
+
const value = input as Record<string, unknown>;
|
|
268
|
+
if (!Array.isArray(value.models)) return input;
|
|
269
|
+
return {
|
|
270
|
+
...value,
|
|
271
|
+
models: value.models.map((raw) => {
|
|
272
|
+
if (!raw || typeof raw !== "object") return raw;
|
|
273
|
+
const model = raw as Record<string, unknown>;
|
|
274
|
+
const next: Record<string, unknown> = { ...model };
|
|
275
|
+
if (typeof model.path === "string") {
|
|
276
|
+
next.path = stripLocaleTemplateSegments(model.path);
|
|
277
|
+
}
|
|
278
|
+
if (typeof model.route === "string") {
|
|
279
|
+
next.route = stripLocaleTemplateSegments(model.route);
|
|
280
|
+
}
|
|
281
|
+
if (model.fields && typeof model.fields === "object" && !Array.isArray(model.fields)) {
|
|
282
|
+
const fields = { ...(model.fields as Record<string, unknown>) };
|
|
283
|
+
for (const [key, snap] of Object.entries(fields)) {
|
|
284
|
+
const kind =
|
|
285
|
+
snap && typeof snap === "object"
|
|
286
|
+
? (snap as { kind?: string }).kind
|
|
287
|
+
: undefined;
|
|
288
|
+
if (kind === "locale") delete fields[key];
|
|
289
|
+
}
|
|
290
|
+
next.fields = fields;
|
|
291
|
+
}
|
|
292
|
+
return next;
|
|
293
|
+
}),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
260
297
|
export function parseSchemaSnapshot(input: unknown): SchemaSnapshot {
|
|
298
|
+
input = migrateLegacySchemaInput(input);
|
|
261
299
|
if (!input || typeof input !== "object") {
|
|
262
300
|
throw new Error("schema must be an object");
|
|
263
301
|
}
|