@contentstorage/core 0.3.12 → 0.3.14

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AppConfig, LanguageCode } from './types.js';
2
- import { setContentLanguage, getCurrentLanguage, getText } from './lib/contentManagement.js';
2
+ import { setContentLanguage, getText } from './lib/contentManagement.js';
3
3
  export { AppConfig, LanguageCode };
4
- export { setContentLanguage, getText, getCurrentLanguage };
4
+ export { setContentLanguage, getText };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { setContentLanguage, getCurrentLanguage, getText, } from './lib/contentManagement.js';
2
- export { setContentLanguage, getText, getCurrentLanguage };
1
+ import { setContentLanguage, getText } from './lib/contentManagement.js';
2
+ export { setContentLanguage, getText };
@@ -1,10 +1,10 @@
1
- import { DotNotationPaths } from '../types.js';
1
+ import { ContentStructure, DotNotationPaths } from '../types.js';
2
2
  /**
3
3
  * Loads and sets the content for a specific language.
4
4
  * It will internally ensure the application configuration (for contentDir) is loaded.
5
5
  * @param languageCode The language code (e.g., 'EN', 'FR') for the JSON file to load.
6
6
  */
7
- export declare function setContentLanguage(languageCode: string): Promise<void>;
7
+ export declare function setContentLanguage(contentJson: ContentStructure | null): Promise<void>;
8
8
  /**
9
9
  * Retrieves the text string from the loaded JSON content for the given path.
10
10
  * Autocompletion for pathString is enabled via module augmentation of CustomContentStructure.
@@ -16,8 +16,3 @@ export declare function setContentLanguage(languageCode: string): Promise<void>;
16
16
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
17
17
  */
18
18
  export declare function getText(pathString: DotNotationPaths, fallbackValue?: string): string | undefined;
19
- /**
20
- * Gets the currently active language code.
21
- * @returns The active language code or null if no language is set.
22
- */
23
- export declare function getCurrentLanguage(): string | null;
@@ -1,56 +1,20 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { loadConfig } from './configLoader.js';
4
1
  let activeContent = null;
5
- let activeLanguage = null;
6
- let loadedAppConfig = null; // Cache for the loaded config
7
- /**
8
- * Ensures the application configuration (especially contentDir) is loaded.
9
- * Calls your library's loadConfig function.
10
- */
11
- async function ensureConfigInitialized() {
12
- if (loadedAppConfig) {
13
- return loadedAppConfig;
14
- }
15
- try {
16
- const config = await loadConfig(); // This is your library's function
17
- if (!config || !config.contentDir) {
18
- throw new Error('contentDir not found in the loaded configuration (contentstorage.config.js).');
19
- }
20
- loadedAppConfig = config; // Ensure it matches the expected structure
21
- console.log(`[LocalizationLibrary] Configuration loaded. Content directory set to: ${loadedAppConfig.contentDir}`);
22
- return loadedAppConfig;
23
- }
24
- catch (error) {
25
- console.error('[Contentstorage] Failed to initialize configuration:', error);
26
- throw new Error(`[Contentstorage] Critical error: Could not load or validate app configuration. ${error.message}`);
27
- }
28
- }
29
2
  /**
30
3
  * Loads and sets the content for a specific language.
31
4
  * It will internally ensure the application configuration (for contentDir) is loaded.
32
5
  * @param languageCode The language code (e.g., 'EN', 'FR') for the JSON file to load.
33
6
  */
34
- export async function setContentLanguage(languageCode) {
35
- if (!languageCode ||
36
- typeof languageCode !== 'string' ||
37
- languageCode.trim() === '') {
38
- throw new Error('[Contentstorage] Invalid language code provided to setContentLanguage.');
7
+ export async function setContentLanguage(contentJson) {
8
+ if (!contentJson || typeof contentJson !== 'object') {
9
+ throw new Error('[Contentstorage] Invalid contentUrl provided to setContentLanguage.');
39
10
  }
40
- const config = await ensureConfigInitialized(); // Gets contentDir from loaded config
41
- const targetFilename = `${languageCode}.json`;
42
- const jsonFilePath = path.join(config.contentDir, targetFilename);
43
- console.log(`[Contentstorage] Attempting to load content for language '${languageCode}' from ${jsonFilePath}...`);
44
11
  try {
45
- const jsonContentString = await fs.readFile(jsonFilePath, 'utf-8');
46
- activeContent = JSON.parse(jsonContentString); // Relies on augmentation
47
- activeLanguage = languageCode;
48
- console.log(`[Contentstorage] Content for language '${languageCode}' loaded successfully.`);
12
+ activeContent = contentJson; // Relies on augmentation
13
+ console.log(`[Contentstorage] Content loaded.`);
49
14
  }
50
15
  catch (error) {
51
16
  activeContent = null; // Reset on failure
52
- console.error(`[Contentstorage] Failed to load content for language '${languageCode}' from ${jsonFilePath}. Error: ${error.message}`);
53
- throw new Error(`[Contentstorage] Could not load content for language: ${languageCode}. Ensure file exists at '${jsonFilePath}' and is valid JSON.`);
17
+ console.error(`[Contentstorage] Failed to load content. Error: ${error.message}`);
54
18
  }
55
19
  }
56
20
  /**
@@ -78,7 +42,7 @@ pathString, fallbackValue) {
78
42
  current = current[key];
79
43
  }
80
44
  else {
81
- const msg = `[Contentstorage] getText: Path "${String(pathString)}" not found in loaded content for language '${activeLanguage}'.`;
45
+ const msg = `[Contentstorage] getText: Path "${String(pathString)}" not found in loaded content.`;
82
46
  console.warn(msg);
83
47
  return fallbackValue;
84
48
  }
@@ -92,10 +56,3 @@ pathString, fallbackValue) {
92
56
  return fallbackValue;
93
57
  }
94
58
  }
95
- /**
96
- * Gets the currently active language code.
97
- * @returns The active language code or null if no language is set.
98
- */
99
- export function getCurrentLanguage() {
100
- return activeLanguage;
101
- }
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.3.12",
5
+ "version": "0.3.14",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",