@contentstorage/core 0.4.0 → 0.5.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.
|
@@ -5,6 +5,8 @@ export declare let appConfig: Pick<AppConfig, 'contentKey' | 'languageCodes'> |
|
|
|
5
5
|
* Loads and sets the content for a specific language.
|
|
6
6
|
* It will internally ensure the application configuration (for contentDir) is loaded.
|
|
7
7
|
* @param contentJson
|
|
8
|
+
* Language code which is used for live editor to manage pending changes
|
|
9
|
+
* @param languageCode
|
|
8
10
|
*/
|
|
9
11
|
export declare function setContentLanguage({ languageCode, contentJson, }: {
|
|
10
12
|
languageCode: LanguageCode;
|
|
@@ -21,10 +23,10 @@ export declare function initContentStorage(config: Pick<AppConfig, 'contentKey'
|
|
|
21
23
|
* If not provided, and path is not found/value not string, undefined is returned.
|
|
22
24
|
* @returns The text string from the JSON, or the fallbackValue, or undefined.
|
|
23
25
|
*/
|
|
24
|
-
export declare function getText<Path extends keyof ContentStructure>(
|
|
26
|
+
export declare function getText<Path extends keyof ContentStructure>(contentId: Path, variables?: ContentStructure[Path] extends {
|
|
25
27
|
variables: infer Vars;
|
|
26
28
|
} ? keyof Vars : Record<string, any>): GetTextReturn;
|
|
27
|
-
export declare function getImage(
|
|
28
|
-
export declare function getVariation<Path extends keyof ContentStructure>(
|
|
29
|
+
export declare function getImage(contentId: keyof ContentStructure): GetImageReturn | undefined;
|
|
30
|
+
export declare function getVariation<Path extends keyof ContentStructure>(contentId: Path, variationKey?: ContentStructure[Path] extends {
|
|
29
31
|
data: infer D;
|
|
30
32
|
} ? keyof D : string, variables?: Record<string, any>): GetVariationReturn;
|
|
@@ -10,6 +10,8 @@ window.currentLanguageCode = null;
|
|
|
10
10
|
* Loads and sets the content for a specific language.
|
|
11
11
|
* It will internally ensure the application configuration (for contentDir) is loaded.
|
|
12
12
|
* @param contentJson
|
|
13
|
+
* Language code which is used for live editor to manage pending changes
|
|
14
|
+
* @param languageCode
|
|
13
15
|
*/
|
|
14
16
|
export function setContentLanguage({ languageCode, contentJson, }) {
|
|
15
17
|
if (!contentJson || typeof contentJson !== 'object') {
|
|
@@ -53,24 +55,24 @@ export function initContentStorage(config) {
|
|
|
53
55
|
* If not provided, and path is not found/value not string, undefined is returned.
|
|
54
56
|
* @returns The text string from the JSON, or the fallbackValue, or undefined.
|
|
55
57
|
*/
|
|
56
|
-
export function getText(
|
|
58
|
+
export function getText(contentId, variables) {
|
|
57
59
|
const defaultVal = {
|
|
58
|
-
|
|
60
|
+
contentId,
|
|
59
61
|
text: '',
|
|
60
62
|
};
|
|
61
63
|
if (!activeContent) {
|
|
62
|
-
const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(
|
|
64
|
+
const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(contentId)}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
63
65
|
console.warn(msg);
|
|
64
66
|
return defaultVal;
|
|
65
67
|
}
|
|
66
|
-
const keys =
|
|
68
|
+
const keys = contentId.split('.');
|
|
67
69
|
let current = activeContent;
|
|
68
70
|
for (const key of keys) {
|
|
69
71
|
if (current && typeof current === 'object' && key in current) {
|
|
70
72
|
current = current[key];
|
|
71
73
|
}
|
|
72
74
|
else {
|
|
73
|
-
const msg = `[Contentstorage] getText: Path "${String(
|
|
75
|
+
const msg = `[Contentstorage] getText: Path "${String(contentId)}" not found in loaded content.`;
|
|
74
76
|
console.warn(msg);
|
|
75
77
|
return defaultVal;
|
|
76
78
|
}
|
|
@@ -80,7 +82,7 @@ export function getText(contentKey, variables) {
|
|
|
80
82
|
const key = current;
|
|
81
83
|
const existingEntry = window.memoryMap.get(key);
|
|
82
84
|
const idSet = existingEntry ? existingEntry.ids : new Set();
|
|
83
|
-
idSet.add(
|
|
85
|
+
idSet.add(contentId); // Add the current ID to the set.
|
|
84
86
|
window.memoryMap.set(key, {
|
|
85
87
|
ids: idSet,
|
|
86
88
|
type: 'text',
|
|
@@ -88,42 +90,42 @@ export function getText(contentKey, variables) {
|
|
|
88
90
|
}
|
|
89
91
|
if (!variables || Object.keys(variables).length === 0) {
|
|
90
92
|
return {
|
|
91
|
-
|
|
93
|
+
contentId,
|
|
92
94
|
text: current,
|
|
93
95
|
};
|
|
94
96
|
}
|
|
95
97
|
return {
|
|
96
|
-
|
|
97
|
-
text: populateTextWithVariables(current, variables,
|
|
98
|
+
contentId,
|
|
99
|
+
text: populateTextWithVariables(current, variables, contentId),
|
|
98
100
|
};
|
|
99
101
|
}
|
|
100
102
|
else {
|
|
101
|
-
const msg = `[Contentstorage] getText: Value at path "${String(
|
|
103
|
+
const msg = `[Contentstorage] getText: Value at path "${String(contentId)}" is not a string (actual type: ${typeof current}).`;
|
|
102
104
|
console.warn(msg);
|
|
103
105
|
return defaultVal;
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
|
-
export function getImage(
|
|
108
|
+
export function getImage(contentId) {
|
|
107
109
|
const defaultVal = {
|
|
108
|
-
|
|
110
|
+
contentId,
|
|
109
111
|
data: { url: '', altText: '', contentstorage_type: 'image' },
|
|
110
112
|
};
|
|
111
113
|
if (!activeContent) {
|
|
112
|
-
const msg = `[Contentstorage] getImage: Content not loaded (
|
|
114
|
+
const msg = `[Contentstorage] getImage: Content not loaded (Content Id: "${contentId}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
113
115
|
console.warn(msg);
|
|
114
116
|
return {
|
|
115
|
-
|
|
117
|
+
contentId,
|
|
116
118
|
data: { url: '', altText: '', contentstorage_type: 'image' },
|
|
117
119
|
};
|
|
118
120
|
}
|
|
119
|
-
const keys =
|
|
121
|
+
const keys = contentId.split('.');
|
|
120
122
|
let current = activeContent;
|
|
121
123
|
for (const key of keys) {
|
|
122
124
|
if (current && typeof current === 'object' && key in current) {
|
|
123
125
|
current = current[key];
|
|
124
126
|
}
|
|
125
127
|
else {
|
|
126
|
-
const msg = `[Contentstorage] getImage: Path "${
|
|
128
|
+
const msg = `[Contentstorage] getImage: Path "${contentId}" not found in loaded content.`;
|
|
127
129
|
console.warn(msg);
|
|
128
130
|
return defaultVal;
|
|
129
131
|
}
|
|
@@ -137,7 +139,7 @@ export function getImage(contentKey) {
|
|
|
137
139
|
if (window.parent && window.parent !== window) {
|
|
138
140
|
const existingEntry = window.memoryMap.get(key);
|
|
139
141
|
const idSet = existingEntry ? existingEntry.ids : new Set();
|
|
140
|
-
idSet.add(
|
|
142
|
+
idSet.add(contentId); // Add the current ID to the set.
|
|
141
143
|
window.memoryMap.set(key, {
|
|
142
144
|
ids: idSet,
|
|
143
145
|
type: 'image',
|
|
@@ -145,7 +147,7 @@ export function getImage(contentKey) {
|
|
|
145
147
|
}
|
|
146
148
|
console.log('currentData.url', currentData.url);
|
|
147
149
|
return {
|
|
148
|
-
|
|
150
|
+
contentId,
|
|
149
151
|
data: {
|
|
150
152
|
...currentData,
|
|
151
153
|
url: key,
|
|
@@ -153,29 +155,29 @@ export function getImage(contentKey) {
|
|
|
153
155
|
};
|
|
154
156
|
}
|
|
155
157
|
else {
|
|
156
|
-
const msg = `[Contentstorage] getImage: Value at path "${
|
|
158
|
+
const msg = `[Contentstorage] getImage: Value at path "${contentId}" is not a valid image object (actual value: ${JSON.stringify(current)}).`;
|
|
157
159
|
console.warn(msg);
|
|
158
160
|
return defaultVal;
|
|
159
161
|
}
|
|
160
162
|
}
|
|
161
|
-
export function getVariation(
|
|
163
|
+
export function getVariation(contentId, variationKey, variables) {
|
|
162
164
|
const defaultVal = {
|
|
163
|
-
|
|
165
|
+
contentId,
|
|
164
166
|
text: '',
|
|
165
167
|
};
|
|
166
168
|
if (!activeContent) {
|
|
167
|
-
const msg = `[Contentstorage] getVariation: Content not loaded (
|
|
169
|
+
const msg = `[Contentstorage] getVariation: Content not loaded (Content Id: "${contentId}", Variation: "${variationKey?.toString()}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
168
170
|
console.warn(msg);
|
|
169
171
|
return defaultVal;
|
|
170
172
|
}
|
|
171
|
-
const keys =
|
|
173
|
+
const keys = contentId.split('.');
|
|
172
174
|
let current = activeContent;
|
|
173
175
|
for (const key of keys) {
|
|
174
176
|
if (current && typeof current === 'object' && key in current) {
|
|
175
177
|
current = current[key];
|
|
176
178
|
}
|
|
177
179
|
else {
|
|
178
|
-
const msg = `[Contentstorage] getVariation: Path "${
|
|
180
|
+
const msg = `[Contentstorage] getVariation: Path "${contentId}" for variation object not found in loaded content.`;
|
|
179
181
|
console.warn(msg);
|
|
180
182
|
return defaultVal;
|
|
181
183
|
}
|
|
@@ -195,7 +197,7 @@ export function getVariation(contentKey, variationKey, variables) {
|
|
|
195
197
|
const key = current;
|
|
196
198
|
const existingEntry = window.memoryMap.get(key);
|
|
197
199
|
const idSet = existingEntry ? existingEntry.ids : new Set();
|
|
198
|
-
idSet.add(
|
|
200
|
+
idSet.add(contentId); // Add the current ID to the set.
|
|
199
201
|
window.memoryMap.set(key, {
|
|
200
202
|
ids: idSet,
|
|
201
203
|
type: 'variation',
|
|
@@ -204,17 +206,17 @@ export function getVariation(contentKey, variationKey, variables) {
|
|
|
204
206
|
}
|
|
205
207
|
if (!variables || Object.keys(variables).length === 0) {
|
|
206
208
|
return {
|
|
207
|
-
|
|
209
|
+
contentId,
|
|
208
210
|
text: current,
|
|
209
211
|
};
|
|
210
212
|
}
|
|
211
213
|
return {
|
|
212
|
-
|
|
213
|
-
text: populateTextWithVariables(current, variables,
|
|
214
|
+
contentId,
|
|
215
|
+
text: populateTextWithVariables(current, variables, contentId),
|
|
214
216
|
};
|
|
215
217
|
}
|
|
216
218
|
else {
|
|
217
|
-
const msg = `[Contentstorage] getVariation: Variation value for key "${variationKey}" at path "${
|
|
219
|
+
const msg = `[Contentstorage] getVariation: Variation value for key "${variationKey}" at path "${contentId}" is not a string (actual type: ${typeof variationObject.data[variationKey]}).`;
|
|
218
220
|
console.warn(msg);
|
|
219
221
|
}
|
|
220
222
|
}
|
|
@@ -222,13 +224,13 @@ export function getVariation(contentKey, variationKey, variables) {
|
|
|
222
224
|
if ('default' in variationObject.data && typeof variationKey === 'string') {
|
|
223
225
|
if (typeof variationObject.data.default === 'string') {
|
|
224
226
|
if (variationKey && variationKey !== 'default') {
|
|
225
|
-
console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${
|
|
227
|
+
console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${contentId}". Returning 'default' variation.`);
|
|
226
228
|
}
|
|
227
229
|
if (window.parent && window.parent !== window) {
|
|
228
230
|
const key = current;
|
|
229
231
|
const existingEntry = window.memoryMap.get(key);
|
|
230
232
|
const idSet = existingEntry ? existingEntry.ids : new Set();
|
|
231
|
-
idSet.add(
|
|
233
|
+
idSet.add(contentId); // Add the current ID to the set.
|
|
232
234
|
window.memoryMap.set(key, {
|
|
233
235
|
ids: idSet,
|
|
234
236
|
type: 'variation',
|
|
@@ -236,20 +238,20 @@ export function getVariation(contentKey, variationKey, variables) {
|
|
|
236
238
|
});
|
|
237
239
|
}
|
|
238
240
|
return {
|
|
239
|
-
|
|
241
|
+
contentId,
|
|
240
242
|
text: variationObject.data.default,
|
|
241
243
|
};
|
|
242
244
|
}
|
|
243
245
|
else {
|
|
244
|
-
console.warn(`[Contentstorage] getVariation: 'default' variation value at path "${
|
|
246
|
+
console.warn(`[Contentstorage] getVariation: 'default' variation value at path "${contentId}" is not a string (actual type: ${typeof variationObject.data.default}).`);
|
|
245
247
|
}
|
|
246
248
|
}
|
|
247
249
|
// If neither specific key nor 'default' is found or valid
|
|
248
|
-
console.warn(`[Contentstorage] getVariation: Neither variation key "${variationKey?.toString()}" nor 'default' variation found or valid at path "${
|
|
250
|
+
console.warn(`[Contentstorage] getVariation: Neither variation key "${variationKey?.toString()}" nor 'default' variation found or valid at path "${contentId}".`);
|
|
249
251
|
return defaultVal;
|
|
250
252
|
}
|
|
251
253
|
else {
|
|
252
|
-
console.warn(`[Contentstorage] getVariation: Value at path "${
|
|
254
|
+
console.warn(`[Contentstorage] getVariation: Value at path "${contentId}" is not a valid variation object (actual value: ${JSON.stringify(current)}).`);
|
|
253
255
|
return defaultVal;
|
|
254
256
|
}
|
|
255
257
|
}
|
|
@@ -24,7 +24,10 @@ export async function fetchContent(language) {
|
|
|
24
24
|
throw new Error(`Expected a single JSON object from ${fileUrl} for language ${languageToFetch}, but received type ${typeof jsonData}. Cannot proceed.`);
|
|
25
25
|
}
|
|
26
26
|
console.log(`Received JSON for ${languageToFetch}`);
|
|
27
|
-
setContentLanguage(
|
|
27
|
+
setContentLanguage({
|
|
28
|
+
languageCode: languageToFetch,
|
|
29
|
+
contentJson: jsonData,
|
|
30
|
+
});
|
|
28
31
|
}
|
|
29
32
|
catch (error) {
|
|
30
33
|
// Catch errors related to fetching or saving a single language file
|
package/dist/types.d.ts
CHANGED
|
@@ -23,15 +23,15 @@ export type LanguageCode = 'SQ' | 'BE' | 'BS' | 'BG' | 'HR' | 'CS' | 'DA' | 'NL'
|
|
|
23
23
|
export interface ContentStructure {
|
|
24
24
|
}
|
|
25
25
|
export type GetTextReturn = {
|
|
26
|
-
|
|
26
|
+
contentId: string;
|
|
27
27
|
text: string;
|
|
28
28
|
};
|
|
29
29
|
export type GetImageReturn = {
|
|
30
|
-
|
|
30
|
+
contentId: string;
|
|
31
31
|
data: ImageObject;
|
|
32
32
|
};
|
|
33
33
|
export type GetVariationReturn = {
|
|
34
|
-
|
|
34
|
+
contentId: string;
|
|
35
35
|
text: string;
|
|
36
36
|
};
|
|
37
37
|
export interface ImageObject {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@contentstorage/core",
|
|
3
3
|
"author": "Kaido Hussar <kaidohus@gmail.com>",
|
|
4
4
|
"homepage": "https://contentstorage.app",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.5.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
8
8
|
"module": "dist/index.js",
|