mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 16:33:12 +09:00
Improve config field definition type
This commit is contained in:
3 files changed
+26
-10
No files matched your search
@@ -387,7 +387,7 @@ async function initialize(): Promise<InitializeParameters> {
|
|||||||
|
|
||||||
controllerConfig.set("controller.version", version); // Allows tracking last loaded version
|
controllerConfig.set("controller.version", version); // Allows tracking last loaded version
|
||||||
|
|
||||||
if (!controllerConfig.get("controller.auth_secret")) {
|
if (controllerConfig.get("controller.auth_secret") === "") {
|
||||||
logger.info("Generating new controller authentication secret");
|
logger.info("Generating new controller authentication secret");
|
||||||
let asyncRandomBytes = util.promisify(crypto.randomBytes);
|
let asyncRandomBytes = util.promisify(crypto.randomBytes);
|
||||||
let bytes = await asyncRandomBytes(256);
|
let bytes = await asyncRandomBytes(256);
|
||||||
|
|||||||
@@ -63,14 +63,24 @@ export const ConfigSchema = Type.Record(Type.String(), Type.Union([
|
|||||||
]));
|
]));
|
||||||
export type ConfigSchema = Static<typeof ConfigSchema>;
|
export type ConfigSchema = Static<typeof ConfigSchema>;
|
||||||
|
|
||||||
type FieldType = "boolean" | "string" | "number" | "object";
|
|
||||||
type FieldValue = null | boolean | string | number | object;
|
type FieldValue = null | boolean | string | number | object;
|
||||||
export interface FieldDefinition {
|
|
||||||
|
type FieldType<T extends FieldValue> =
|
||||||
|
T extends boolean ? "boolean" :
|
||||||
|
T extends string ? "string" :
|
||||||
|
T extends number ? "number" :
|
||||||
|
T extends object ? "object" :
|
||||||
|
never;
|
||||||
|
|
||||||
|
export interface FieldDefinition<
|
||||||
|
Type extends FieldValue = any,
|
||||||
|
Fields extends Record<keyof Fields, FieldValue> = any,
|
||||||
|
> {
|
||||||
/**
|
/**
|
||||||
* string declaring the type of the config value, supports boolean,
|
* string declaring the type of the config value, supports boolean,
|
||||||
* string, number and object.
|
* string, number and object.
|
||||||
*/
|
*/
|
||||||
type: FieldType;
|
type: FieldType<Type>;
|
||||||
/**
|
/**
|
||||||
* Text used to identify the config field in user interfaces.
|
* Text used to identify the config field in user interfaces.
|
||||||
*/
|
*/
|
||||||
@@ -79,6 +89,11 @@ export interface FieldDefinition {
|
|||||||
* Text used to describe the config field in user interfaces.
|
* Text used to describe the config field in user interfaces.
|
||||||
*/
|
*/
|
||||||
description?: string;
|
description?: string;
|
||||||
|
/**
|
||||||
|
* Validation function ran to confirm the value is consistent with other config values.
|
||||||
|
* Should throw an error if the value in invalid / inconsistent
|
||||||
|
*/
|
||||||
|
extendedValidation?: (value: Type, config: Config<Fields>) => void;
|
||||||
/**
|
/**
|
||||||
* Value to use for the autocomplete attribute in the Web UI's input element.
|
* Value to use for the autocomplete attribute in the Web UI's input element.
|
||||||
* Useful for making browsers behave properly with credential inputs.
|
* Useful for making browsers behave properly with credential inputs.
|
||||||
@@ -93,7 +108,7 @@ export interface FieldDefinition {
|
|||||||
/**
|
/**
|
||||||
* True if this field can be set to null. Defaults to false.
|
* True if this field can be set to null. Defaults to false.
|
||||||
*/
|
*/
|
||||||
optional?: boolean;
|
optional?: Type extends null ? true : false;
|
||||||
/**
|
/**
|
||||||
* Value this config field should take in a newly initialized config.
|
* Value this config field should take in a newly initialized config.
|
||||||
* This can also be an function returning the value to use.
|
* This can also be an function returning the value to use.
|
||||||
@@ -137,8 +152,10 @@ export interface FieldDefinition {
|
|||||||
hidden?: boolean;
|
hidden?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ConfigDefs<Fields> = {
|
export type ConfigDefs<
|
||||||
[Prop in keyof Fields]: FieldDefinition;
|
Fields extends Record<keyof Fields, FieldValue>
|
||||||
|
> = {
|
||||||
|
[Prop in keyof Fields]: FieldDefinition<Fields[Prop], Fields>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function defaultValue(def: FieldDefinition) {
|
function defaultValue(def: FieldDefinition) {
|
||||||
@@ -177,7 +194,7 @@ export enum ConfigAccess {
|
|||||||
* @extends events.EventEmitter
|
* @extends events.EventEmitter
|
||||||
*/
|
*/
|
||||||
export class Config<
|
export class Config<
|
||||||
Fields extends { [Field in keyof Fields]: FieldValue },
|
Fields extends Record<keyof Fields, FieldValue>
|
||||||
> extends events.EventEmitter<ConfigEvents> {
|
> extends events.EventEmitter<ConfigEvents> {
|
||||||
/**
|
/**
|
||||||
* Mapping of config name to field meta data
|
* Mapping of config name to field meta data
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
|||||||
"Do not share this.",
|
"Do not share this.",
|
||||||
restartRequired: true,
|
restartRequired: true,
|
||||||
type: "string",
|
type: "string",
|
||||||
optional: true,
|
initialValue: "",
|
||||||
},
|
},
|
||||||
"controller.heartbeat_interval": {
|
"controller.heartbeat_interval": {
|
||||||
title: "Heartbeat Interval",
|
title: "Heartbeat Interval",
|
||||||
@@ -495,7 +495,6 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
|
|||||||
"the process. Set to 0 to disable.",
|
"the process. Set to 0 to disable.",
|
||||||
type: "number",
|
type: "number",
|
||||||
initialValue: 300,
|
initialValue: 300,
|
||||||
optional: true,
|
|
||||||
},
|
},
|
||||||
"factorio.game_port": {
|
"factorio.game_port": {
|
||||||
description: "UDP port to run game on, uses a port in host.factorio_port_range if null",
|
description: "UDP port to run game on, uses a port in host.factorio_port_range if null",
|
||||||
|
|||||||
Reference in new issue
Block a user