generated from Seekra/repository-template
Compare commits
5 Commits
5d2064134b
...
da76d14d15
| Author | SHA1 | Date | |
|---|---|---|---|
|
da76d14d15
|
|||
|
547bc241da
|
|||
|
4ef6008976
|
|||
|
035aa1aa77
|
|||
|
462ae00506
|
@@ -1,55 +0,0 @@
|
||||
{
|
||||
"contents": [
|
||||
{
|
||||
"name": "general",
|
||||
"type": "section",
|
||||
"i18n": "preferences.settings.sections.general.name",
|
||||
"description": "preferences.settings.sections.general.description",
|
||||
"content": [
|
||||
{
|
||||
"type": "bool",
|
||||
"name": "darkMode",
|
||||
"i18n": "preferences.darkMode",
|
||||
"default": true
|
||||
},
|
||||
{
|
||||
"type": "selection",
|
||||
"name": "language",
|
||||
"i18n": "preferences.settings.language",
|
||||
"description": "preferences.settings.language.description",
|
||||
"default": "en",
|
||||
"allowMultiple": false,
|
||||
"options": [
|
||||
{
|
||||
"name": "en",
|
||||
"i18n": "preferences.locale.languages.en"
|
||||
},
|
||||
{
|
||||
"name": "de",
|
||||
"i18n": "preferences.locale.languages.de"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"name": "exampleSection",
|
||||
"description": "preferences.settings.sections.exampleSection.description",
|
||||
"content": [
|
||||
{
|
||||
"type": "number",
|
||||
"name": "aNumber",
|
||||
"i18n": "preferences.settings.sections.exampleSection.content.aNumber",
|
||||
"default": 42
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "exampleStandaloneConfiguration",
|
||||
"type": "bool",
|
||||
"i18n": "preferences.settings.exampleStandalone.name",
|
||||
"default": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"contents": []
|
||||
}
|
||||
@@ -69,3 +69,12 @@ limitations under the License.
|
||||
* @typedef {Object} SettingsConfig
|
||||
* @property {SettingConfigEntry[]} contents
|
||||
*/
|
||||
/**
|
||||
* @typedef {{ type: 'bool', name: string, i18n: string, description?: string, default: boolean }} BoolSettingConfig
|
||||
* @typedef {{ type: 'number', name: string, i18n: string, description?: string, default: number }} NumberSettingConfig
|
||||
* @typedef {{ type: 'string', name: string, i18n: string, description?: string, default: string }} StringSettingConfig
|
||||
* @typedef {{ type: 'selection', name: string, i18n: string, description?: string, default: string | string[], allowMultiple?: boolean, options: SelectionOption[] }} SelectionSettingConfig
|
||||
* @typedef {{ type: 'section', name: string, i18n?: string, description?: string, content: SettingConfigEntry[] }} SectionSettingConfig
|
||||
* @typedef {BoolSettingConfig | NumberSettingConfig | StringSettingConfig | SelectionSettingConfig | SectionSettingConfig} SettingConfigEntry
|
||||
* @typedef {{ contents: SettingConfigEntry[] }} SettingsConfig
|
||||
*/
|
||||
@@ -17,21 +17,16 @@ limitations under the License.
|
||||
import { validateSettingsConfig } from './settingsValidator.js';
|
||||
|
||||
/**
|
||||
* Loads and parses the settings configuration from a JSON file.
|
||||
* @param {string} [url='/settings.json']
|
||||
* Loads and parses the settings configuration via dynamic import.
|
||||
* @returns {Promise<import('../types/settingsConfig').SettingsConfig>}
|
||||
*/
|
||||
export async function loadSettingsConfig(url = '/settings.json') {
|
||||
export async function loadSettingsConfig() {
|
||||
let raw;
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
raw = await response.json();
|
||||
raw = (await import('../settings.json')).default;
|
||||
} catch (e) {
|
||||
throw new Error(`[settings] Failed to load config from "${url}": ${e.message}`);
|
||||
throw new Error(`[settings] Failed to load settings.json: ${e.message}`);
|
||||
}
|
||||
|
||||
const result = validateSettingsConfig(raw);
|
||||
|
||||
@@ -57,21 +57,23 @@ function validateEntry(entry, path) {
|
||||
assertString(entry.name, `${path}.name`);
|
||||
assertString(entry.i18n, `${path}.i18n`);
|
||||
|
||||
if (entry.default !== undefined) {
|
||||
if (entry.type === 'bool' && typeof entry.default !== 'boolean') {
|
||||
throw new Error(`[settings] "${path}.default" must be a boolean`);
|
||||
}
|
||||
if (entry.type === 'number' && typeof entry.default !== 'number') {
|
||||
throw new Error(`[settings] "${path}.default" must be a number`);
|
||||
}
|
||||
if (entry.type === 'string' && typeof entry.default !== 'string') {
|
||||
throw new Error(`[settings] "${path}.default" must be a string`);
|
||||
}
|
||||
if (entry.type === 'selection') {
|
||||
validateSelectionOptions(entry.options, path);
|
||||
if (typeof entry.allowMultiple !== 'boolean') {
|
||||
throw new Error(`[settings] "${path}.allowMultiple" must be a boolean`);
|
||||
}
|
||||
if (entry.default === undefined) {
|
||||
throw new Error(`[settings] "${path}.default" is required`);
|
||||
}
|
||||
|
||||
if (entry.type === 'bool' && typeof entry.default !== 'boolean') {
|
||||
throw new Error(`[settings] "${path}.default" must be a boolean`);
|
||||
}
|
||||
if (entry.type === 'number' && typeof entry.default !== 'number') {
|
||||
throw new Error(`[settings] "${path}.default" must be a number`);
|
||||
}
|
||||
if (entry.type === 'string' && typeof entry.default !== 'string') {
|
||||
throw new Error(`[settings] "${path}.default" must be a string`);
|
||||
}
|
||||
if (entry.type === 'selection') {
|
||||
validateSelectionOptions(entry.options, path);
|
||||
if (typeof entry.allowMultiple !== 'boolean') {
|
||||
throw new Error(`[settings] "${path}.allowMultiple" must be a boolean`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user