@contentstorage/core 0.4.1 → 0.6.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.
@@ -23,10 +23,10 @@ export declare function initContentStorage(config: Pick<AppConfig, 'contentKey'
23
23
  * If not provided, and path is not found/value not string, undefined is returned.
24
24
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
25
25
  */
26
- export declare function getText<Path extends keyof ContentStructure>(contentKey: Path, variables?: ContentStructure[Path] extends {
26
+ export declare function getText<Path extends keyof ContentStructure>(contentId: Path, variables?: ContentStructure[Path] extends {
27
27
  variables: infer Vars;
28
28
  } ? keyof Vars : Record<string, any>): GetTextReturn;
29
- export declare function getImage(contentKey: keyof ContentStructure): GetImageReturn | undefined;
30
- export declare function getVariation<Path extends keyof ContentStructure>(contentKey: Path, variationKey?: ContentStructure[Path] extends {
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 {
31
31
  data: infer D;
32
32
  } ? keyof D : string, variables?: Record<string, any>): GetVariationReturn;
@@ -55,24 +55,24 @@ export function initContentStorage(config) {
55
55
  * If not provided, and path is not found/value not string, undefined is returned.
56
56
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
57
57
  */
58
- export function getText(contentKey, variables) {
58
+ export function getText(contentId, variables) {
59
59
  const defaultVal = {
60
- contentKey,
60
+ contentId,
61
61
  text: '',
62
62
  };
63
63
  if (!activeContent) {
64
- const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(contentKey)}"). Ensure setContentLanguage() was called and completed successfully.`;
64
+ const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(contentId)}"). Ensure setContentLanguage() was called and completed successfully.`;
65
65
  console.warn(msg);
66
66
  return defaultVal;
67
67
  }
68
- const keys = contentKey.split('.');
68
+ const keys = contentId.split('.');
69
69
  let current = activeContent;
70
70
  for (const key of keys) {
71
71
  if (current && typeof current === 'object' && key in current) {
72
72
  current = current[key];
73
73
  }
74
74
  else {
75
- const msg = `[Contentstorage] getText: Path "${String(contentKey)}" not found in loaded content.`;
75
+ const msg = `[Contentstorage] getText: Path "${String(contentId)}" not found in loaded content.`;
76
76
  console.warn(msg);
77
77
  return defaultVal;
78
78
  }
@@ -82,7 +82,7 @@ export function getText(contentKey, variables) {
82
82
  const key = current;
83
83
  const existingEntry = window.memoryMap.get(key);
84
84
  const idSet = existingEntry ? existingEntry.ids : new Set();
85
- idSet.add(contentKey); // Add the current ID to the set.
85
+ idSet.add(contentId); // Add the current ID to the set.
86
86
  window.memoryMap.set(key, {
87
87
  ids: idSet,
88
88
  type: 'text',
@@ -90,42 +90,42 @@ export function getText(contentKey, variables) {
90
90
  }
91
91
  if (!variables || Object.keys(variables).length === 0) {
92
92
  return {
93
- contentKey,
93
+ contentId,
94
94
  text: current,
95
95
  };
96
96
  }
97
97
  return {
98
- contentKey,
99
- text: populateTextWithVariables(current, variables, contentKey),
98
+ contentId,
99
+ text: populateTextWithVariables(current, variables, contentId),
100
100
  };
101
101
  }
102
102
  else {
103
- const msg = `[Contentstorage] getText: Value at path "${String(contentKey)}" is not a string (actual type: ${typeof current}).`;
103
+ const msg = `[Contentstorage] getText: Value at path "${String(contentId)}" is not a string (actual type: ${typeof current}).`;
104
104
  console.warn(msg);
105
105
  return defaultVal;
106
106
  }
107
107
  }
108
- export function getImage(contentKey) {
108
+ export function getImage(contentId) {
109
109
  const defaultVal = {
110
- contentKey,
110
+ contentId,
111
111
  data: { url: '', altText: '', contentstorage_type: 'image' },
112
112
  };
113
113
  if (!activeContent) {
114
- const msg = `[Contentstorage] getImage: Content not loaded (Key: "${contentKey}"). Ensure setContentLanguage() was called and completed successfully.`;
114
+ const msg = `[Contentstorage] getImage: Content not loaded (Content Id: "${contentId}"). Ensure setContentLanguage() was called and completed successfully.`;
115
115
  console.warn(msg);
116
116
  return {
117
- contentKey,
117
+ contentId,
118
118
  data: { url: '', altText: '', contentstorage_type: 'image' },
119
119
  };
120
120
  }
121
- const keys = contentKey.split('.');
121
+ const keys = contentId.split('.');
122
122
  let current = activeContent;
123
123
  for (const key of keys) {
124
124
  if (current && typeof current === 'object' && key in current) {
125
125
  current = current[key];
126
126
  }
127
127
  else {
128
- const msg = `[Contentstorage] getImage: Path "${contentKey}" not found in loaded content.`;
128
+ const msg = `[Contentstorage] getImage: Path "${contentId}" not found in loaded content.`;
129
129
  console.warn(msg);
130
130
  return defaultVal;
131
131
  }
@@ -139,7 +139,7 @@ export function getImage(contentKey) {
139
139
  if (window.parent && window.parent !== window) {
140
140
  const existingEntry = window.memoryMap.get(key);
141
141
  const idSet = existingEntry ? existingEntry.ids : new Set();
142
- idSet.add(contentKey); // Add the current ID to the set.
142
+ idSet.add(contentId); // Add the current ID to the set.
143
143
  window.memoryMap.set(key, {
144
144
  ids: idSet,
145
145
  type: 'image',
@@ -147,7 +147,7 @@ export function getImage(contentKey) {
147
147
  }
148
148
  console.log('currentData.url', currentData.url);
149
149
  return {
150
- contentKey,
150
+ contentId,
151
151
  data: {
152
152
  ...currentData,
153
153
  url: key,
@@ -155,29 +155,29 @@ export function getImage(contentKey) {
155
155
  };
156
156
  }
157
157
  else {
158
- const msg = `[Contentstorage] getImage: Value at path "${contentKey}" is not a valid image object (actual value: ${JSON.stringify(current)}).`;
158
+ const msg = `[Contentstorage] getImage: Value at path "${contentId}" is not a valid image object (actual value: ${JSON.stringify(current)}).`;
159
159
  console.warn(msg);
160
160
  return defaultVal;
161
161
  }
162
162
  }
163
- export function getVariation(contentKey, variationKey, variables) {
163
+ export function getVariation(contentId, variationKey, variables) {
164
164
  const defaultVal = {
165
- contentKey,
165
+ contentId,
166
166
  text: '',
167
167
  };
168
168
  if (!activeContent) {
169
- const msg = `[Contentstorage] getVariation: Content not loaded (Key: "${contentKey}", Variation: "${variationKey?.toString()}"). Ensure setContentLanguage() was called and completed successfully.`;
169
+ const msg = `[Contentstorage] getVariation: Content not loaded (Content Id: "${contentId}", Variation: "${variationKey?.toString()}"). Ensure setContentLanguage() was called and completed successfully.`;
170
170
  console.warn(msg);
171
171
  return defaultVal;
172
172
  }
173
- const keys = contentKey.split('.');
173
+ const keys = contentId.split('.');
174
174
  let current = activeContent;
175
175
  for (const key of keys) {
176
176
  if (current && typeof current === 'object' && key in current) {
177
177
  current = current[key];
178
178
  }
179
179
  else {
180
- const msg = `[Contentstorage] getVariation: Path "${contentKey}" for variation object not found in loaded content.`;
180
+ const msg = `[Contentstorage] getVariation: Path "${contentId}" for variation object not found in loaded content.`;
181
181
  console.warn(msg);
182
182
  return defaultVal;
183
183
  }
@@ -197,7 +197,7 @@ export function getVariation(contentKey, variationKey, variables) {
197
197
  const key = current;
198
198
  const existingEntry = window.memoryMap.get(key);
199
199
  const idSet = existingEntry ? existingEntry.ids : new Set();
200
- idSet.add(contentKey); // Add the current ID to the set.
200
+ idSet.add(contentId); // Add the current ID to the set.
201
201
  window.memoryMap.set(key, {
202
202
  ids: idSet,
203
203
  type: 'variation',
@@ -206,17 +206,17 @@ export function getVariation(contentKey, variationKey, variables) {
206
206
  }
207
207
  if (!variables || Object.keys(variables).length === 0) {
208
208
  return {
209
- contentKey,
209
+ contentId,
210
210
  text: current,
211
211
  };
212
212
  }
213
213
  return {
214
- contentKey,
215
- text: populateTextWithVariables(current, variables, contentKey),
214
+ contentId,
215
+ text: populateTextWithVariables(current, variables, contentId),
216
216
  };
217
217
  }
218
218
  else {
219
- const msg = `[Contentstorage] getVariation: Variation value for key "${variationKey}" at path "${contentKey}" is not a string (actual type: ${typeof variationObject.data[variationKey]}).`;
219
+ const msg = `[Contentstorage] getVariation: Variation value for key "${variationKey}" at path "${contentId}" is not a string (actual type: ${typeof variationObject.data[variationKey]}).`;
220
220
  console.warn(msg);
221
221
  }
222
222
  }
@@ -224,13 +224,13 @@ export function getVariation(contentKey, variationKey, variables) {
224
224
  if ('default' in variationObject.data && typeof variationKey === 'string') {
225
225
  if (typeof variationObject.data.default === 'string') {
226
226
  if (variationKey && variationKey !== 'default') {
227
- console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${contentKey}". Returning 'default' variation.`);
227
+ console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${contentId}". Returning 'default' variation.`);
228
228
  }
229
229
  if (window.parent && window.parent !== window) {
230
230
  const key = current;
231
231
  const existingEntry = window.memoryMap.get(key);
232
232
  const idSet = existingEntry ? existingEntry.ids : new Set();
233
- idSet.add(contentKey); // Add the current ID to the set.
233
+ idSet.add(contentId); // Add the current ID to the set.
234
234
  window.memoryMap.set(key, {
235
235
  ids: idSet,
236
236
  type: 'variation',
@@ -238,20 +238,20 @@ export function getVariation(contentKey, variationKey, variables) {
238
238
  });
239
239
  }
240
240
  return {
241
- contentKey,
241
+ contentId,
242
242
  text: variationObject.data.default,
243
243
  };
244
244
  }
245
245
  else {
246
- console.warn(`[Contentstorage] getVariation: 'default' variation value at path "${contentKey}" is not a string (actual type: ${typeof variationObject.data.default}).`);
246
+ console.warn(`[Contentstorage] getVariation: 'default' variation value at path "${contentId}" is not a string (actual type: ${typeof variationObject.data.default}).`);
247
247
  }
248
248
  }
249
249
  // If neither specific key nor 'default' is found or valid
250
- console.warn(`[Contentstorage] getVariation: Neither variation key "${variationKey?.toString()}" nor 'default' variation found or valid at path "${contentKey}".`);
250
+ console.warn(`[Contentstorage] getVariation: Neither variation key "${variationKey?.toString()}" nor 'default' variation found or valid at path "${contentId}".`);
251
251
  return defaultVal;
252
252
  }
253
253
  else {
254
- console.warn(`[Contentstorage] getVariation: Value at path "${contentKey}" is not a valid variation object (actual value: ${JSON.stringify(current)}).`);
254
+ console.warn(`[Contentstorage] getVariation: Value at path "${contentId}" is not a valid variation object (actual value: ${JSON.stringify(current)}).`);
255
255
  return defaultVal;
256
256
  }
257
257
  }
@@ -10,7 +10,28 @@ import { CONTENTSTORAGE_CONFIG } from '../contentstorage-config.js';
10
10
  import { jsonToTS } from '../type-generation/index.js';
11
11
  export async function generateTypes() {
12
12
  console.log(chalk.blue('Starting type generation...'));
13
- const config = await loadConfig();
13
+ const args = process.argv.slice(2);
14
+ const cliConfig = {};
15
+ for (let i = 0; i < args.length; i++) {
16
+ if (args[i].startsWith('--')) {
17
+ const key = args[i].substring(2);
18
+ const value = args[i + 1];
19
+ if (value && !value.startsWith('--')) {
20
+ if (key === 'lang') {
21
+ cliConfig.languageCodes = [value];
22
+ }
23
+ else if (key === 'content-key') {
24
+ cliConfig.contentKey = value;
25
+ }
26
+ else if (key === 'output') {
27
+ cliConfig.typesOutputFile = value;
28
+ }
29
+ i++; // Move to the next argument
30
+ }
31
+ }
32
+ }
33
+ const fileConfig = await loadConfig();
34
+ const config = { ...fileConfig, ...cliConfig };
14
35
  if (!config.typesOutputFile) {
15
36
  console.error(chalk.red.bold("Configuration error: 'typesOutputFile' is missing."));
16
37
  process.exit(1);
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
- contentKey: string;
26
+ contentId: string;
27
27
  text: string;
28
28
  };
29
29
  export type GetImageReturn = {
30
- contentKey: string;
30
+ contentId: string;
31
31
  data: ImageObject;
32
32
  };
33
33
  export type GetVariationReturn = {
34
- contentKey: string;
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.4.1",
5
+ "version": "0.6.0",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",