@contentstorage/core 0.3.13 → 0.3.15

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(contentDir: string, languageCode: string): Promise<void>;
7
+ export declare function setContentLanguage(contentJson: ContentStructure | null): 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(contentDir: string, languageCode: str
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,36 +1,20 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
1
  let activeContent = null;
4
- let activeLanguage = null;
5
2
  /**
6
3
  * Loads and sets the content for a specific language.
7
4
  * It will internally ensure the application configuration (for contentDir) is loaded.
8
5
  * @param languageCode The language code (e.g., 'EN', 'FR') for the JSON file to load.
9
6
  */
10
- export async function setContentLanguage(contentDir, languageCode) {
11
- if (!contentDir ||
12
- typeof contentDir !== 'string' ||
13
- contentDir.trim() === '') {
7
+ export function setContentLanguage(contentJson) {
8
+ if (!contentJson || typeof contentJson !== 'object') {
14
9
  throw new Error('[Contentstorage] Invalid contentUrl provided to setContentLanguage.');
15
10
  }
16
- if (!languageCode ||
17
- typeof languageCode !== 'string' ||
18
- languageCode.trim() === '') {
19
- throw new Error('[Contentstorage] Invalid language code provided to setContentLanguage.');
20
- }
21
- const targetFilename = `${languageCode}.json`;
22
- const jsonFilePath = path.join(contentDir, targetFilename);
23
- console.log(`[Contentstorage] Attempting to load content for language '${languageCode}' from ${jsonFilePath}...`);
24
11
  try {
25
- const jsonContentString = await fs.readFile(jsonFilePath, 'utf-8');
26
- activeContent = JSON.parse(jsonContentString); // Relies on augmentation
27
- activeLanguage = languageCode;
28
- console.log(`[Contentstorage] Content for language '${languageCode}' loaded successfully.`);
12
+ activeContent = contentJson; // Relies on augmentation
13
+ console.log(`[Contentstorage] Content loaded.`);
29
14
  }
30
15
  catch (error) {
31
16
  activeContent = null; // Reset on failure
32
- console.error(`[Contentstorage] Failed to load content for language '${languageCode}' from ${jsonFilePath}. Error: ${error.message}`);
33
- 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}`);
34
18
  }
35
19
  }
36
20
  /**
@@ -43,9 +27,7 @@ export async function setContentLanguage(contentDir, languageCode) {
43
27
  * If not provided, and path is not found/value not string, undefined is returned.
44
28
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
45
29
  */
46
- export function getText(
47
- // @ts-expect-error Is fine
48
- pathString, fallbackValue) {
30
+ export function getText(pathString, fallbackValue) {
49
31
  if (!activeContent) {
50
32
  const msg = `[Contentstorage] getText: Content not loaded (Path: "${String(pathString)}"). Ensure setContentLanguage() was called and completed successfully.`;
51
33
  console.warn(msg);
@@ -58,7 +40,7 @@ pathString, fallbackValue) {
58
40
  current = current[key];
59
41
  }
60
42
  else {
61
- const msg = `[Contentstorage] getText: Path "${String(pathString)}" not found in loaded content for language '${activeLanguage}'.`;
43
+ const msg = `[Contentstorage] getText: Path "${String(pathString)}" not found in loaded content.`;
62
44
  console.warn(msg);
63
45
  return fallbackValue;
64
46
  }
@@ -72,10 +54,3 @@ pathString, fallbackValue) {
72
54
  return fallbackValue;
73
55
  }
74
56
  }
75
- /**
76
- * Gets the currently active language code.
77
- * @returns The active language code or null if no language is set.
78
- */
79
- export function getCurrentLanguage() {
80
- return activeLanguage;
81
- }
package/dist/types.d.ts CHANGED
@@ -23,13 +23,17 @@ export type LanguageCode = 'SQ' | 'BE' | 'BS' | 'BG' | 'HR' | 'CS' | 'DA' | 'NL'
23
23
  export interface ContentStructure {
24
24
  [key: string]: any;
25
25
  }
26
+ type MaxPathDepth = [any, any, any, any, any, any];
26
27
  /**
27
- * Generates a union of all possible dot-notation path strings for a given object type `T`.
28
- * Defaults to using the `ContentStructure` interface, which consumers augment.
28
+ * Generates a union of all possible dot-notation path strings for a given object type `T`,
29
+ * with a limit on recursion depth to prevent TypeScript errors.
30
+ * Defaults to using the `CustomContentStructure` interface, which consumers augment.
29
31
  *
30
- * @template T The object type to generate paths from. Defaults to `ContentStructure`.
31
- * @template Prefix Internal accumulator for the current path prefix during recursion.
32
+ * @template T The object type to generate paths from.
33
+ * @template Prefix Internal accumulator for the current path prefix.
34
+ * @template CurrentDepth Internal tuple to track recursion depth.
32
35
  */
33
- export type DotNotationPaths<T = ContentStructure, Prefix extends string = ''> = T extends object ? {
34
- [K in keyof T]-?: K extends string | number ? T[K] extends object ? `${Prefix}${K}` | DotNotationPaths<T[K], `${Prefix}${K}.`> : `${Prefix}${K}` : never;
35
- }[keyof T] : '';
36
+ export type DotNotationPaths<T = ContentStructure, Prefix extends string = '', CurrentDepth extends any[] = []> = T extends object ? {
37
+ [K in keyof T & (string | number)]-?: CurrentDepth['length'] extends MaxPathDepth['length'] ? `${Prefix}${K}` : T[K] extends object ? `${Prefix}${K}` | DotNotationPaths<T[K], `${Prefix}${K}.`, [...CurrentDepth, any]> : `${Prefix}${K}`;
38
+ }[keyof T & (string | number)] : never;
39
+ export {};
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.13",
5
+ "version": "0.3.15",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",