includio-cms 0.0.21 → 0.0.23

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.
@@ -4,9 +4,9 @@ export declare const getEntries: import("@sveltejs/kit").RemoteQueryFunction<{
4
4
  slug?: string | undefined;
5
5
  status?: "draft" | "published" | "archived" | undefined;
6
6
  type?: "collection" | "singleton" | undefined;
7
+ ids?: string[] | undefined;
7
8
  dataValues?: Record<string, unknown> | undefined;
8
9
  dataLike?: Record<string, unknown> | undefined;
9
- ids?: string[] | undefined;
10
10
  } | undefined;
11
11
  }, import("../../types/entries.js").Entry[]>;
12
12
  export declare const createNewEntry: import("@sveltejs/kit").RemoteQueryFunction<{
@@ -1,7 +1,6 @@
1
- import { resolveMediaFields } from '../../fields/resolveImageFields.js';
2
1
  import z from 'zod';
3
2
  import { getCMS } from '../../../cms.js';
4
- import { getEntryTranslation } from '../utils/getEntryTranslation.js';
3
+ import { populateEntryData } from '../../fields/populateEntry.js';
5
4
  // Helper function to transform string values to localized objects
6
5
  function transformDataValuesToLocalized(dataValues, defaultLanguage) {
7
6
  const result = {};
@@ -83,10 +82,9 @@ export const getEntry = async (data, options = {}) => {
83
82
  return null;
84
83
  }
85
84
  const config = getCMS().getBySlug(entry.slug);
86
- const translatedEntry = getEntryTranslation(entry, language);
87
85
  return {
88
86
  ...entry,
89
- data: await resolveMediaFields(translatedEntry.data, config.fields),
87
+ data: await populateEntryData(entry.data, config.fields, language),
90
88
  populated: true
91
89
  };
92
90
  };
@@ -118,10 +116,9 @@ export const getEntries = async (data, options = {}) => {
118
116
  const entries = await getRawEntries(transformedData);
119
117
  return Promise.all(entries.map(async (entry) => {
120
118
  const config = getCMS().getBySlug(entry.slug);
121
- const translatedEntry = getEntryTranslation(entry, language);
122
119
  return {
123
120
  ...entry,
124
- data: await resolveMediaFields(translatedEntry.data, config.fields),
121
+ data: await populateEntryData(entry.data, config.fields, language),
125
122
  populated: true
126
123
  };
127
124
  }));
@@ -141,10 +138,9 @@ export const getCollectionEntries = async (slug, options = {}) => {
141
138
  }
142
139
  const entries = await getRawCollectionEntries(slug);
143
140
  const language = options.language || getCMS().languages[0];
144
- const translatedEntries = entries.map((entry) => getEntryTranslation(entry, language));
145
- return Promise.all(translatedEntries.map(async (row) => ({
141
+ return Promise.all(entries.map(async (row) => ({
146
142
  ...row,
147
- data: await resolveMediaFields(row.data, config.fields),
143
+ data: await populateEntryData(row.data, config.fields, language),
148
144
  populated: true
149
145
  })));
150
146
  };
@@ -1,2 +1 @@
1
- import type { RawEntry } from '../../../../types/entries.js';
2
- export declare function getEntryTranslation(entry: RawEntry, language: string): RawEntry;
1
+ export declare function translateObject(obj: unknown, language: string): unknown;
@@ -1,24 +1,18 @@
1
- export function getEntryTranslation(entry, language) {
2
- function translate(obj) {
3
- if (Array.isArray(obj)) {
4
- return obj.map(translate);
1
+ export function translateObject(obj, language) {
2
+ if (Array.isArray(obj)) {
3
+ return obj.map((item) => translateObject(item, language));
4
+ }
5
+ if (obj && typeof obj === 'object') {
6
+ // Jeśli obiekt zawiera klucz języka, zwracamy tłumaczenie
7
+ if (language in obj) {
8
+ return obj[language];
5
9
  }
6
- if (obj && typeof obj === 'object') {
7
- // Jeśli obiekt zawiera klucz języka, zwracamy tłumaczenie
8
- if (language in obj) {
9
- return obj[language];
10
- }
11
- // W przeciwnym razie rekurencyjnie przechodzimy przez wszystkie pola
12
- const result = {};
13
- for (const key in obj) {
14
- result[key] = translate(obj[key]);
15
- }
16
- return result;
10
+ // W przeciwnym razie rekurencyjnie przechodzimy przez wszystkie pola
11
+ const result = {};
12
+ for (const key in obj) {
13
+ result[key] = translateObject(obj[key], language);
17
14
  }
18
- return obj;
15
+ return result;
19
16
  }
20
- return {
21
- ...entry,
22
- data: translate(entry.data)
23
- };
17
+ return obj;
24
18
  }
@@ -0,0 +1,3 @@
1
+ import type { EntryData, PopulatedEntryData } from '../../../types/entries.js';
2
+ import type { Field } from '../../../types/fields.js';
3
+ export declare function populateEntryData(data: EntryData, fields: Field[], language: string): Promise<PopulatedEntryData>;
@@ -0,0 +1,9 @@
1
+ import { translateObject } from '../entries/utils/getEntryTranslation.js';
2
+ import { resolveMediaFields } from './resolveImageFields.js';
3
+ import { resolveUrlFields } from './resolveUrlFields.js';
4
+ export async function populateEntryData(data, fields, language) {
5
+ let populatedData = translateObject(data, language);
6
+ populatedData = await resolveMediaFields(populatedData, fields);
7
+ populatedData = await resolveUrlFields(populatedData, fields, language);
8
+ return populatedData;
9
+ }
@@ -0,0 +1,3 @@
1
+ import type { EntryData, PopulatedEntryData } from '../../../types/entries.js';
2
+ import type { Field } from '../../../types/fields.js';
3
+ export declare function resolveUrlFields(data: EntryData, fields: Field[], language: string): Promise<PopulatedEntryData>;
@@ -0,0 +1,103 @@
1
+ import { getCMS } from '../../cms.js';
2
+ import { translateObject } from '../entries/utils/getEntryTranslation.js';
3
+ export async function resolveUrlFields(data, fields, language) {
4
+ const entriesIds = [];
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ const collectIds = (value, fields) => {
7
+ for (const field of fields) {
8
+ const val = value?.[field.slug];
9
+ if (val == null)
10
+ continue;
11
+ switch (field.type) {
12
+ case 'url': {
13
+ if (typeof val === 'object' &&
14
+ val !== null &&
15
+ 'id' in val &&
16
+ typeof val.id === 'string') {
17
+ entriesIds.push(val.id);
18
+ }
19
+ break;
20
+ }
21
+ case 'object':
22
+ collectIds(val.data, field.fields);
23
+ break;
24
+ case 'array':
25
+ if (Array.isArray(val)) {
26
+ val.forEach((item) => {
27
+ // Find the object definition for this item based on its slug
28
+ const objectDef = field.of.find((objDef) => objDef.slug === item.slug);
29
+ if (objectDef) {
30
+ collectIds(item.data, objectDef.fields);
31
+ }
32
+ });
33
+ }
34
+ break;
35
+ }
36
+ }
37
+ };
38
+ collectIds(data, fields);
39
+ if (entriesIds.length === 0)
40
+ return data;
41
+ const entries = (await getCMS().databaseAdapter.getEntries({
42
+ data: {
43
+ ids: entriesIds
44
+ }
45
+ }));
46
+ const entriesMap = Object.fromEntries(entries.map((m) => [m.id, m]));
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ const resolveValues = (value, fields) => {
49
+ // Start with a copy of all original data to preserve non-field properties
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ const result = { ...value };
52
+ for (const field of fields) {
53
+ const val = value?.[field.slug];
54
+ if (val == null) {
55
+ result[field.slug] = val;
56
+ continue;
57
+ }
58
+ switch (field.type) {
59
+ case 'url': {
60
+ if (typeof val === 'object' &&
61
+ val !== null &&
62
+ 'id' in val &&
63
+ typeof val.id === 'string') {
64
+ result[field.slug] = entriesMap[val.id].data.seo?.slug ?? val;
65
+ }
66
+ if (typeof val === 'object' &&
67
+ val !== null &&
68
+ 'url' in val &&
69
+ typeof val.url === 'object' &&
70
+ val.url !== null) {
71
+ result[field.slug] = translateObject(val.url, language);
72
+ }
73
+ break;
74
+ }
75
+ case 'object':
76
+ result[field.slug] = {
77
+ ...val,
78
+ data: resolveValues(val.data, field.fields)
79
+ };
80
+ break;
81
+ case 'array':
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ result[field.slug] = val.map((item) => {
84
+ // Find the object definition for this item based on its slug
85
+ const objectDef = field.of.find((objDef) => objDef.slug === item.slug);
86
+ if (objectDef) {
87
+ return {
88
+ ...item,
89
+ data: resolveValues(item.data, objectDef.fields)
90
+ };
91
+ }
92
+ // If no matching object definition found, return item unchanged
93
+ return item;
94
+ });
95
+ break;
96
+ default:
97
+ result[field.slug] = val;
98
+ }
99
+ }
100
+ return result;
101
+ };
102
+ return resolveValues(data, fields);
103
+ }
@@ -40,9 +40,9 @@ function getFieldTypeAsString(field) {
40
40
  return 'string';
41
41
  case 'seo':
42
42
  return `{
43
- slug: string;
44
- title: string;
45
- description: string;
43
+ slug?: string;
44
+ title?: string;
45
+ description?: string;
46
46
  }`;
47
47
  case 'url':
48
48
  return 'string';
@@ -0,0 +1,18 @@
1
+ <script lang="ts">
2
+ type Props = {
3
+ title?: string;
4
+ description?: string;
5
+ };
6
+
7
+ let { title, description }: Props = $props();
8
+ </script>
9
+
10
+ <svelte:head>
11
+ {#if title}
12
+ <title>{title}</title>
13
+ <meta name="title" content={title} />
14
+ {/if}
15
+ {#if description}
16
+ <meta name="description" content={description} />
17
+ {/if}
18
+ </svelte:head>
@@ -0,0 +1,7 @@
1
+ type Props = {
2
+ title?: string;
3
+ description?: string;
4
+ };
5
+ declare const Seo: import("svelte").Component<Props, {}, "">;
6
+ type Seo = ReturnType<typeof Seo>;
7
+ export default Seo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "includio-cms",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run prepack",