mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 07:24:52 +09:00
* Add max level select to logs Closes: #745 * Add log filter for server actions Closes: #744 * Respond to comments Change name of component, clean up onChange anon function
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { Static, TSchema, Type } from "@sinclair/typebox";
|
|
|
|
export function StringEnum<T extends string[]>(values: [...T]) {
|
|
return Type.Unsafe<T[number]>({ type: "string", enum: values });
|
|
}
|
|
|
|
export function StringKey<T extends string>(object: Record<T, any>) {
|
|
return Type.Unsafe<keyof typeof object>({ type: "string", enum: Object.keys(object) });
|
|
}
|
|
|
|
export interface JSONDeserialisable<T> {
|
|
fromJSON(json: unknown): T;
|
|
jsonSchema: object;
|
|
}
|
|
|
|
export function jsonArray<T>(ItemClass: JSONDeserialisable<T>) {
|
|
return {
|
|
jsonSchema: {
|
|
type: "array",
|
|
items: ItemClass.jsonSchema,
|
|
},
|
|
fromJSON(json: Array<T>) {
|
|
return json.map(i => ItemClass.fromJSON(i));
|
|
},
|
|
};
|
|
}
|
|
|
|
export function jsonPrimitive<T, U>(type: U) {
|
|
return {
|
|
jsonSchema: { type },
|
|
fromJSON(json: T) {
|
|
return json;
|
|
},
|
|
};
|
|
}
|
|
export const JsonBoolean = jsonPrimitive<boolean, "boolean">("boolean");
|
|
export const JsonNumber = jsonPrimitive<number, "number">("number");
|
|
export const JsonString = jsonPrimitive<string, "string">("string");
|
|
|
|
export function plainJson<T extends TSchema>(schema: T) {
|
|
return {
|
|
jsonSchema: schema,
|
|
fromJSON(json: Static<T>) {
|
|
return json;
|
|
},
|
|
};
|
|
}
|