@contentstorage/core 0.3.16 → 0.3.17

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.
@@ -1,4 +1,4 @@
1
- import { ContentStructure, DotNotationPaths } from '../types.js';
1
+ import { ContentStructure } 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.
@@ -15,4 +15,4 @@ export declare function setContentLanguage(contentJson: ContentStructure | null)
15
15
  * If not provided, and path is not found/value not string, undefined is returned.
16
16
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
17
17
  */
18
- export declare function getText(pathString: DotNotationPaths, fallbackValue?: string): string | undefined;
18
+ export declare function getText(pathString: keyof ContentStructure, fallbackValue?: string): string | undefined;
@@ -27,29 +27,37 @@ export function setContentLanguage(contentJson) {
27
27
  * If not provided, and path is not found/value not string, undefined is returned.
28
28
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
29
29
  */
30
- export function getText(pathString, fallbackValue) {
30
+ export function getText(
31
+ // The pathString is now a direct key of your (flattened) content structure.
32
+ // TypeScript will provide autocompletion for these keys.
33
+ pathString, fallbackValue) {
31
34
  if (!activeContent) {
32
- const msg = `[Contentstorage] getText: Content not loaded (Path: "${String(pathString)}"). Ensure setContentLanguage() was called and completed successfully.`;
35
+ const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(pathString)}"). Ensure setContentLanguage() was called and completed successfully.`;
33
36
  console.warn(msg);
34
37
  return fallbackValue;
35
38
  }
36
- const keys = pathString.split('.');
37
- let current = activeContent;
38
- for (const key of keys) {
39
- if (current && typeof current === 'object' && key in current) {
40
- current = current[key];
39
+ // Direct lookup since activeContent is expected to be flat and pathString is a key.
40
+ // We use Object.prototype.hasOwnProperty.call for safer property checking.
41
+ if (Object.prototype.hasOwnProperty.call(activeContent, pathString)) {
42
+ // Since pathString is `keyof CustomContentStructure`, activeContent[pathString] is type-safe.
43
+ // However, CustomContentStructure is initially `[key: string]: any` in the library,
44
+ // so we might need a cast here if strict typing is desired for `value`.
45
+ // But the consumer's augmentation makes CustomContentStructure specific.
46
+ const value = activeContent[pathString]; // Using `as string` because keys are strings with dots.
47
+ if (typeof value === 'string') {
48
+ return value;
41
49
  }
42
50
  else {
43
- const msg = `[Contentstorage] getText: Path "${String(pathString)}" not found in loaded content.`;
51
+ const msg = `[Contentstorage] getText: Value at key "${String(pathString)}" is not a string (actual type: ${typeof value}).'}'.`;
44
52
  console.warn(msg);
45
53
  return fallbackValue;
46
54
  }
47
55
  }
48
- if (typeof current === 'string') {
49
- return current;
50
- }
51
56
  else {
52
- const msg = `[Contentstorage] getText: Value at path "${String(pathString)}" is not a string (actual type: ${typeof current}).`;
57
+ // This case should ideally be caught by TypeScript if pathString is truly `keyof CustomContentStructure`
58
+ // and the key doesn't exist, unless `activeContent` is out of sync with the types.
59
+ // However, it's good for runtime robustness.
60
+ const msg = `[Contentstorage] getText: Key "${String(pathString)}" not found in loaded content'.`;
53
61
  console.warn(msg);
54
62
  return fallbackValue;
55
63
  }
package/dist/types.d.ts CHANGED
@@ -23,17 +23,3 @@ 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];
27
- /**
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.
31
- *
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.
35
- */
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.16",
5
+ "version": "0.3.17",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",