@contentstorage/core 0.3.27 → 0.3.29

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.
@@ -0,0 +1,3 @@
1
+ export declare const CONTENTSTORAGE_CONFIG: {
2
+ BASE_URL: string;
3
+ };
@@ -0,0 +1,3 @@
1
+ export const CONTENTSTORAGE_CONFIG = {
2
+ BASE_URL: 'https://di0fmnnsdfsl2.cloudfront.net',
3
+ };
@@ -0,0 +1,6 @@
1
+ import { AppConfig } from '../types.js';
2
+ /**
3
+ * Helper function to define your application configuration.
4
+ * Provides autocompletion and type-checking for contentstorage.config.js files.
5
+ */
6
+ export declare function defineConfig(config: AppConfig): AppConfig;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Helper function to define your application configuration.
3
+ * Provides autocompletion and type-checking for contentstorage.config.js files.
4
+ */
5
+ export function defineConfig(config) {
6
+ // You can add basic runtime validation here if desired,
7
+ // e.g., check if contentUrl is a valid URL format,
8
+ // or if languageCodes is not empty.
9
+ if (!config.languageCodes || config.languageCodes.length === 0) {
10
+ console.warn('Warning: languageCodes array is empty or missing in the configuration.');
11
+ }
12
+ if (!config.contentDir) {
13
+ // This would typically be a hard error, but defineConfig is more for type safety at edit time.
14
+ // Runtime validation (see point 3) is better for hard errors.
15
+ console.warn('Warning: contentDir is missing in the configuration.');
16
+ }
17
+ // ... other checks
18
+ return config;
19
+ }
@@ -31,7 +31,6 @@ export function flattenJson(data, prefix = '', result = {}) {
31
31
  });
32
32
  }
33
33
  else {
34
- // It's an object (and not a special one that stops flattening, due to the preceding 'if' block)
35
34
  let isEmptyObject = true;
36
35
  for (const key in data) {
37
36
  if (Object.prototype.hasOwnProperty.call(data, key)) {
@@ -48,7 +47,6 @@ export function flattenJson(data, prefix = '', result = {}) {
48
47
  }
49
48
  }
50
49
  else if (prefix) {
51
- // Primitive value (string, number, boolean, null) and has a prefix
52
50
  result[prefix] = data;
53
51
  }
54
52
  // If the initial data is a primitive and prefix is empty, result remains empty.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AppConfig, LanguageCode, ContentStructure } from './types.js';
2
- import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
2
+ import { setContentLanguage, getText, getImage, getVariation } from './lib/contentManagement.js';
3
3
  export { AppConfig, LanguageCode, ContentStructure };
4
- export { setContentLanguage, getText, getImage };
4
+ export { setContentLanguage, getText, getImage, getVariation };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
2
- export { setContentLanguage, getText, getImage };
1
+ import { setContentLanguage, getText, getImage, getVariation, } from './lib/contentManagement.js';
2
+ export { setContentLanguage, getText, getImage, getVariation };
@@ -32,14 +32,14 @@ export async function loadConfig() {
32
32
  ...userConfig,
33
33
  };
34
34
  // Validate required fields
35
- if (!mergedConfig.contentUrl) {
35
+ if (!mergedConfig.contentKey) {
36
36
  console.error('Error: Configuration is missing the required "contentUrl" property.');
37
37
  process.exit(1); // Exit if required URL is missing
38
38
  }
39
39
  // Resolve paths relative to the user's project root (process.cwd())
40
40
  const finalConfig = {
41
41
  languageCodes: mergedConfig.languageCodes || [],
42
- contentUrl: mergedConfig.contentUrl,
42
+ contentKey: mergedConfig.contentKey,
43
43
  contentDir: path.resolve(process.cwd(), mergedConfig.contentDir),
44
44
  typesOutputFile: path.resolve(process.cwd(), mergedConfig.typesOutputFile),
45
45
  };
@@ -2,7 +2,7 @@ import { ContentStructure, ImageObject } 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
- * @param languageCode The language code (e.g., 'EN', 'FR') for the JSON file to load.
5
+ * @param contentJson
6
6
  */
7
7
  export declare function setContentLanguage(contentJson: object): void;
8
8
  /**
@@ -16,8 +16,7 @@ export declare function setContentLanguage(contentJson: object): void;
16
16
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
17
17
  */
18
18
  export declare function getText(pathString: keyof ContentStructure, fallbackValue?: string): string | undefined;
19
- export declare function getImage(pathString: string, // Using string for dynamic paths
20
- fallbackValue?: ImageObject): ImageObject | undefined;
19
+ export declare function getImage(pathString: keyof ContentStructure, fallbackValue?: ImageObject): ImageObject | undefined;
21
20
  export declare function getVariation<Path extends keyof ContentStructure>(pathString: Path, variationKey?: ContentStructure[Path] extends {
22
21
  data: infer D;
23
22
  } ? keyof D : string, fallbackString?: string): string | undefined;
@@ -2,7 +2,7 @@ let activeContent = null;
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
- * @param languageCode The language code (e.g., 'EN', 'FR') for the JSON file to load.
5
+ * @param contentJson
6
6
  */
7
7
  export function setContentLanguage(contentJson) {
8
8
  if (!contentJson || typeof contentJson !== 'object') {
@@ -54,8 +54,7 @@ export function getText(pathString, fallbackValue) {
54
54
  return fallbackValue;
55
55
  }
56
56
  }
57
- export function getImage(pathString, // Using string for dynamic paths
58
- fallbackValue) {
57
+ export function getImage(pathString, fallbackValue) {
59
58
  if (!activeContent) {
60
59
  const msg = `[Contentstorage] getImage: Content not loaded (Key: "${pathString}"). Ensure setContentLanguage() was called and completed successfully.`;
61
60
  console.warn(msg);
@@ -110,7 +109,9 @@ export function getVariation(pathString, variationKey, fallbackString) {
110
109
  typeof current.data === 'object' &&
111
110
  current.data !== null) {
112
111
  const variationObject = current;
113
- if (variationKey && typeof variationKey === 'string' && variationKey in variationObject.data) {
112
+ if (variationKey &&
113
+ typeof variationKey === 'string' &&
114
+ variationKey in variationObject.data) {
114
115
  if (typeof variationObject.data[variationKey] === 'string') {
115
116
  return variationObject.data[variationKey];
116
117
  }
@@ -122,7 +123,8 @@ export function getVariation(pathString, variationKey, fallbackString) {
122
123
  // If specific variationKey is not found or not provided, try to return the 'default' variation
123
124
  if ('default' in variationObject.data && typeof variationKey === 'string') {
124
125
  if (typeof variationObject.data.default === 'string') {
125
- if (variationKey && variationKey !== 'default') { // Warn if specific key was requested but default is being returned
126
+ if (variationKey && variationKey !== 'default') {
127
+ // Warn if specific key was requested but default is being returned
126
128
  const msg = `[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${pathString}". Returning 'default' variation.`;
127
129
  console.warn(msg);
128
130
  }
@@ -7,6 +7,7 @@ import jsonToTS from 'json-to-ts'; // Import the library
7
7
  import chalk from 'chalk'; // Optional: for colored output
8
8
  import { loadConfig } from '../lib/configLoader.js';
9
9
  import { flattenJson } from '../helpers/flattenJson.js';
10
+ import { CONTENTSTORAGE_CONFIG } from '../contentstorage-config.js';
10
11
  export async function generateTypes() {
11
12
  console.log(chalk.blue('Starting type generation...'));
12
13
  const config = await loadConfig();
@@ -70,11 +71,10 @@ export async function generateTypes() {
70
71
  }
71
72
  }
72
73
  else {
73
- // --- Fetch from URL ---
74
- if (!config.contentUrl) {
75
- throw new Error("Cannot generate types: 'contentDir' is not accessible or not specified, and 'contentUrl' is also missing in configuration.");
74
+ if (!config.contentKey) {
75
+ throw new Error('Cannot generate types: contentKey is missing');
76
76
  }
77
- const fileUrl = `${config.contentUrl}/${firstLanguageCode}.json`; // Adjust URL construction if necessary
77
+ const fileUrl = `${CONTENTSTORAGE_CONFIG.BASE_URL}/${config.contentKey}/content/${firstLanguageCode}.json`; // Adjust URL construction if necessary
78
78
  dataSourceDescription = `remote URL (${fileUrl})`;
79
79
  console.log(chalk.gray(`Attempting to fetch JSON from: ${fileUrl}`));
80
80
  try {
@@ -123,10 +123,6 @@ export async function generateTypes() {
123
123
  catch (error) {
124
124
  console.error(chalk.red.bold('\nError generating TypeScript types:'));
125
125
  console.error(chalk.red(error.message));
126
- // Optionally log stack for more details during development
127
- // if (error.stack && process.env.NODE_ENV === 'development') {
128
- // console.error(chalk.gray(error.stack));
129
- // }
130
126
  process.exit(1);
131
127
  }
132
128
  }
@@ -4,11 +4,12 @@ import fs from 'fs/promises';
4
4
  import path from 'path';
5
5
  import chalk from 'chalk';
6
6
  import { loadConfig } from '../lib/configLoader.js';
7
+ import { CONTENTSTORAGE_CONFIG } from '../contentstorage-config.js';
7
8
  export async function pullContent() {
8
9
  console.log(chalk.blue('Starting content pull...'));
9
10
  // Load configuration (assuming this function is defined elsewhere and works)
10
11
  const config = await loadConfig();
11
- console.log(chalk.gray(`Content base URL: ${config.contentUrl}`));
12
+ console.log(chalk.gray(`Content key: ${config.contentKey}`));
12
13
  console.log(chalk.gray(`Saving content to: ${config.contentDir}`));
13
14
  try {
14
15
  // Validate languageCodes array
@@ -24,7 +25,7 @@ export async function pullContent() {
24
25
  await fs.mkdir(config.contentDir, { recursive: true });
25
26
  // Process each language code
26
27
  for (const languageCode of config.languageCodes) {
27
- const fileUrl = `${config.contentUrl}/${languageCode}.json`;
28
+ const fileUrl = `${CONTENTSTORAGE_CONFIG.BASE_URL}/${config.contentKey}/content/${languageCode}.json`;
28
29
  const filename = `${languageCode}.json`;
29
30
  const outputPath = path.join(config.contentDir, filename);
30
31
  console.log(chalk.blue(`\nProcessing language: ${languageCode}`));
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type AppConfig = {
2
2
  languageCodes: LanguageCode[];
3
- contentUrl: string;
3
+ contentKey: string;
4
4
  contentDir: string;
5
5
  typesOutputFile: string;
6
6
  };
@@ -23,7 +23,7 @@ export type LanguageCode = 'SQ' | 'BE' | 'BS' | 'BG' | 'HR' | 'CS' | 'DA' | 'NL'
23
23
  export interface ContentStructure {
24
24
  }
25
25
  export interface ImageObject {
26
- contentstorage_type: "image";
26
+ contentstorage_type: 'image';
27
27
  url: string;
28
28
  altText: string;
29
29
  }
@@ -31,6 +31,6 @@ export interface VariationData {
31
31
  [key: string]: string;
32
32
  }
33
33
  export interface VariationObject {
34
- contentstorage_type: "variation";
34
+ contentstorage_type: 'variation';
35
35
  data: VariationData;
36
36
  }
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.27",
5
+ "version": "0.3.29",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",