feat: Update with the new side effects, types, and orginization.

This commit is contained in:
BluJ
2023-03-06 12:42:40 -07:00
parent 19504ef559
commit f74be4ec7d
10 changed files with 191 additions and 155 deletions

View File

@@ -501,10 +501,8 @@ export class Config<A extends ConfigSpec> extends IBuilder<A> {
} }
static of<B extends { [key: string]: Value<ValueSpec> }>(spec: B) { static of<B extends { [key: string]: Value<ValueSpec> }>(spec: B) {
// deno-lint-ignore no-explicit-any
const answer: { [K in keyof B]: BuilderExtract<B[K]> } = {} as any; const answer: { [K in keyof B]: BuilderExtract<B[K]> } = {} as any;
for (const key in spec) { for (const key in spec) {
// deno-lint-ignore no-explicit-any
answer[key] = spec[key].build() as any; answer[key] = spec[key].build() as any;
} }
return new Config(answer); return new Config(answer);

View File

@@ -39,17 +39,15 @@ import { Config } from ".";
``` ```
*/ */
export class Variants< export class Variants<
A extends { [key: string]: ConfigSpec } A extends { [key: string]: ConfigSpec },
> extends IBuilder<A> { > extends IBuilder<A> {
static of< static of<
A extends { A extends {
[key: string]: Config<ConfigSpec>; [key: string]: Config<ConfigSpec>;
} },
>(a: A) { >(a: A) {
// deno-lint-ignore no-explicit-any
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any; const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
for (const key in a) { for (const key in a) {
// deno-lint-ignore no-explicit-any
variants[key] = a[key].build() as any; variants[key] = a[key].build() as any;
} }
return new Variants(variants); return new Variants(variants);
@@ -60,14 +58,14 @@ export class Variants<
} }
static withVariant<K extends string, B extends ConfigSpec>( static withVariant<K extends string, B extends ConfigSpec>(
key: K, key: K,
value: Config<B> value: Config<B>,
) { ) {
return Variants.empty().withVariant(key, value); return Variants.empty().withVariant(key, value);
} }
withVariant<K extends string, B extends ConfigSpec>( withVariant<K extends string, B extends ConfigSpec>(
key: K, key: K,
value: Config<B> value: Config<B>,
) { ) {
return new Variants({ return new Variants({
...this.a, ...this.a,

View File

@@ -4,6 +4,13 @@ import { ConfigSpec } from "../types/config-types";
import { nullIfEmpty, okOf } from "../util"; import { nullIfEmpty, okOf } from "../util";
import { TypeFromProps } from "../util/propertiesMatcher"; import { TypeFromProps } from "../util/propertiesMatcher";
/**
* We want to setup a config export with a get and set, this
* is going to be the default helper to setup config, because it will help
* enforce that we have a spec, write, and reading.
* @param options
* @returns
*/
export function setupConfigExports<A extends ConfigSpec>(options: { export function setupConfigExports<A extends ConfigSpec>(options: {
spec: Config<A>; spec: Config<A>;
dependsOn: DependsOn; dependsOn: DependsOn;

View File

@@ -3,7 +3,7 @@ import { object, string } from "ts-matches";
export type HealthCheck = ( export type HealthCheck = (
effects: Types.Effects, effects: Types.Effects,
dateMs: number dateMs: number,
) => Promise<HealthResult>; ) => Promise<HealthResult>;
export type HealthResult = export type HealthResult =
| { success: string } | { success: string }
@@ -23,16 +23,23 @@ function safelyStringify(e: unknown) {
} }
async function timeoutHealth( async function timeoutHealth(
effects: Types.Effects, effects: Types.Effects,
timeMs: number timeMs: number,
): Promise<HealthResult> { ): Promise<HealthResult> {
await effects.sleep(timeMs); await effects.sleep(timeMs);
return { failure: "Timed out " }; return { failure: "Timed out " };
} }
/**
* Health runner is usually used during the main function, and will be running in a loop.
* This health check then will be run every intervalS seconds.
* The return needs a create()
* then from there we need a start().
* The stop function is used to stop the health check.
*/
export default function healthRunner( export default function healthRunner(
name: string, name: string,
fn: HealthCheck, fn: HealthCheck,
{ defaultIntervalS = 60 } = {} { defaultIntervalS = 60 } = {},
) { ) {
return { return {
create(effects: Types.Effects, defaultIntervalCreatedS = defaultIntervalS) { create(effects: Types.Effects, defaultIntervalCreatedS = defaultIntervalS) {

View File

@@ -1,15 +1,23 @@
import { Effects, ResultType } from "../types"; import { Effects, ResultType } from "../types";
import { error, errorCode, isKnownError, ok } from "../util"; import { error, errorCode, isKnownError, ok, okOf } from "../util";
import { HealthResult } from "./healthRunner";
/**
* This is a helper function to check if a web url is reachable.
* @param url
* @param createSuccess
* @returns
*/
export const checkWebUrl: ( export const checkWebUrl: (
url: string url: string,
createSuccess?: null | ((response?: string | null) => string),
) => ( ) => (
effects: Effects, effects: Effects,
duration: number duration: number,
) => Promise<ResultType<null | void>> = (url) => { ) => Promise<ResultType<HealthResult>> = (url, createSuccess = null) => {
return async (effects, duration) => { return async (effects, duration) => {
let errorValue; let errorValue;
if ( if (
// deno-lint-ignore no-cond-assign
(errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })) (errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 }))
) { ) {
return errorValue; return errorValue;
@@ -17,7 +25,12 @@ export const checkWebUrl: (
return await effects return await effects
.fetch(url) .fetch(url)
.then((_) => ok) .then((x) =>
okOf({
success:
createSuccess?.(x.body) ?? `Successfully fetched URL: ${url}`,
}),
)
.catch((e) => { .catch((e) => {
effects.warn(`Error while fetching URL: ${url}`); effects.warn(`Error while fetching URL: ${url}`);
effects.error(JSON.stringify(e)); effects.error(JSON.stringify(e));
@@ -27,17 +40,38 @@ export const checkWebUrl: (
}; };
}; };
/**
* Running a health script, is used when we want to have a simple
* script in bash or something like that. It should return something that is useful
* in {result: string} else it is considered an error
* @param param0
* @returns
*/
export const runHealthScript = export const runHealthScript =
({ command, args }: { command: string; args: string[] }) => ({
command,
args,
message,
}: {
command: string;
args: string[];
message: ((result: unknown) => string) | null;
}) =>
async ( async (
effects: Effects, effects: Effects,
_duration: number _duration: number,
): Promise<ResultType<null | void>> => { ): Promise<ResultType<HealthResult>> => {
const res = await effects.runCommand({ command, args }); const res = await effects.runCommand({ command, args });
if ("result" in res) { if ("result" in res) {
return { result: null }; return {
result: {
success:
message?.(res) ??
`Have ran script ${command} and the result: ${res.result}`,
},
};
} else { } else {
return res; throw res;
} }
}; };

View File

@@ -1,9 +1,7 @@
export * as configTypes from "./types/config-types"; export * as configTypes from "./types/config-types";
import { ConfigSpec } from "./types/config-types"; import { ConfigSpec } from "./types/config-types";
// deno-lint-ignore no-namespace
export namespace ExpectedExports { export namespace ExpectedExports {
// deno-lint-ignore no-unused-labels
version: 1; version: 1;
/** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */ /** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */
export type setConfig = (options: { export type setConfig = (options: {
@@ -40,16 +38,6 @@ export namespace ExpectedExports {
}) => Promise<ResultType<unknown>>; }) => Promise<ResultType<unknown>>;
}; };
/**
* Migrations are used when we are changing versions when updating/ downgrading.
* There are times that we need to move files around, and do other operations during a migration.
*/
export type migration = (options: {
effects: Effects;
input: VersionString;
args: unknown[];
}) => Promise<ResultType<MigrationRes>>;
/** /**
* Actions are used so we can effect the service, like deleting a directory. * Actions are used so we can effect the service, like deleting a directory.
* One old use case is to add a action where we add a file, that will then be run during the * One old use case is to add a action where we add a file, that will then be run during the
@@ -70,73 +58,26 @@ export namespace ExpectedExports {
effects: Effects; effects: Effects;
started(): null; started(): null;
}) => Promise<ResultType<unknown>>; }) => Promise<ResultType<unknown>>;
/**
* Every time a package completes an install, this function is called before the main.
* Can be used to do migration like things.
*/
export type init = (options: {
effects: Effects;
previousVersion: null | string;
}) => Promise<ResultType<unknown>>;
/** This will be ran during any time a package is uninstalled, for example during a update
* this will be called.
*/
export type uninit = (options: {
effects: Effects;
nextVersion: null | string;
}) => Promise<ResultType<unknown>>;
} }
export type TimeMs = number; export type TimeMs = number;
export type VersionString = string; export type VersionString = string;
/** @deprecated */
// deno-lint-ignore no-namespace
export namespace LegacyExpectedExports {
/** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */
export type setConfig = (
effects: Effects,
input: Record<string, unknown>
) => Promise<ResultType<SetResult>>;
/** Get configuration returns a shape that describes the format that the embassy ui will generate, and later send to the set config */
export type getConfig = (effects: Effects) => Promise<ResultType<ConfigRes>>;
/** These are how we make sure the our dependency configurations are valid and if not how to fix them. */
export type dependencies = Dependencies;
/** For backing up service data though the embassyOS UI */
export type createBackup = (effects: Effects) => Promise<ResultType<unknown>>;
/** For restoring service data that was previously backed up using the embassyOS UI create backup flow. Backup restores are also triggered via the embassyOS UI, or doing a system restore flow during setup. */
export type restoreBackup = (
effects: Effects
) => Promise<ResultType<unknown>>;
/** Properties are used to get values from the docker, like a username + password, what ports we are hosting from */
export type properties = (
effects: Effects
) => Promise<ResultType<Properties>>;
/** Health checks are used to determine if the service is working properly after starting
* A good use case is if we are using a web server, seeing if we can get to the web server.
*/
export type health = {
/** Should be the health check id */
[id: string]: (
effects: Effects,
dateMs: number
) => Promise<ResultType<unknown>>;
};
/**
* Migrations are used when we are changing versions when updating/ downgrading.
* There are times that we need to move files around, and do other operations during a migration.
*/
export type migration = (
effects: Effects,
version: string,
...args: unknown[]
) => Promise<ResultType<MigrationRes>>;
/**
* Actions are used so we can effect the service, like deleting a directory.
* One old use case is to add a action where we add a file, that will then be run during the
* service starting, and that file would indicate that it would rescan all the data.
*/
export type action = {
[id: string]: (
effects: Effects,
config?: ConfigSpec
) => Promise<ResultType<ActionResult>>;
};
/**
* This is the entrypoint for the main container. Used to start up something like the service that the
* package represents, like running a bitcoind in a bitcoind-wrapper.
*/
export type main = (effects: Effects) => Promise<ResultType<unknown>>;
}
export type ConfigRes = { export type ConfigRes = {
/** This should be the previous config, that way during set config we start with the previous */ /** This should be the previous config, that way during set config we start with the previous */
config?: null | Record<string, unknown>; config?: null | Record<string, unknown>;
@@ -184,7 +125,9 @@ export type Effects = {
term(): Promise<void>; term(): Promise<void>;
}; };
/** Uses the chown on the system */
chown(input: { volumeId: string; path: string; uid: string }): Promise<null>; chown(input: { volumeId: string; path: string; uid: string }): Promise<null>;
/** Uses the chmod on the system */
chmod(input: { volumeId: string; path: string; mode: string }): Promise<null>; chmod(input: { volumeId: string; path: string; mode: string }): Promise<null>;
sleep(timeMs: TimeMs): Promise<null>; sleep(timeMs: TimeMs): Promise<null>;
@@ -203,25 +146,31 @@ export type Effects = {
/** Sandbox mode lets us read but not write */ /** Sandbox mode lets us read but not write */
is_sandboxed(): boolean; is_sandboxed(): boolean;
/** Check that a file exists or not */
exists(input: { volumeId: string; path: string }): Promise<boolean>; exists(input: { volumeId: string; path: string }): Promise<boolean>;
/** Declaring that we are opening a interface on some protocal for local network */
bindLocal(options: { bindLocal(options: {
internalPort: number; internalPort: number;
name: string; name: string;
externalPort: number; externalPort: number;
}): Promise<string>; }): Promise<string>;
/** Declaring that we are opening a interface on some protocal for tor network */
bindTor(options: { bindTor(options: {
internalPort: number; internalPort: number;
name: string; name: string;
externalPort: number; externalPort: number;
}): Promise<string>; }): Promise<string>;
/** Similar to the fetch api via the mdn, this is simplified but the point is
* to get something from some website, and return the response.
*/
fetch( fetch(
url: string, url: string,
options?: { options?: {
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH"; method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH";
headers?: Record<string, string>; headers?: Record<string, string>;
body?: string; body?: string;
} },
): Promise<{ ): Promise<{
method: string; method: string;
ok: boolean; ok: boolean;
@@ -234,6 +183,10 @@ export type Effects = {
json(): Promise<unknown>; json(): Promise<unknown>;
}>; }>;
/**
* Run rsync between two volumes. This is used to backup data between volumes.
* This is a long running process, and a structure that we can either wait for, or get the progress of.
*/
runRsync(options: { runRsync(options: {
srcVolume: string; srcVolume: string;
dstVolume: string; dstVolume: string;
@@ -246,15 +199,82 @@ export type Effects = {
wait: () => Promise<null>; wait: () => Promise<null>;
progress: () => Promise<number>; progress: () => Promise<number>;
}; };
/** Get the address for another service for local internet*/
getServiceLocalAddress(options: {
packageId: string;
interfaceName: string;
}): Promise<string>;
/** Get the address for another service for tor interfaces */
getServiceTorAddress(options: {
packageId: string;
interfaceName: string;
}): Promise<string>;
/**
* Get the port address for another service
*/
getServicePortForward(options: {
packageId: string;
internalPort: number;
}): Promise<string>;
/** When we want to create a link in the front end interfaces, and example is
* exposing a url to view a web service
*/
exportAddress(options: {
/** The title of this field to be dsimplayed */
name: string;
/** Human readable description, used as tooltip usually */
description: string;
/** URI location */
address: string;
id: string;
/** Defaults to false, but describes if this address can be opened in a browser as an
* ui interface
*/
ui?: boolean;
}): Promise<string>;
/**
*Remove an address that was exported. Used problably during main or during setConfig.
* @param options
*/
removeAddress(options: { id: string }): Promise<void>;
/**
*
* @param options
*/
exportAction(options: {
name: string;
description: string;
id: string;
input: null | ConfigSpec;
}): Promise<void>;
/**
* Remove an action that was exported. Used problably during main or during setConfig.
*/
removeAction(options: { id: string }): Promise<void>;
getConfigured(): Promise<boolean>;
/**
* This called after a valid set config as well as during init.
* @param configured
*/
setConfigured(configured: boolean): Promise<void>;
}; };
// rsync options: https://linux.die.net/man/1/rsync /* rsync options: https://linux.die.net/man/1/rsync
*/
export type BackupOptions = { export type BackupOptions = {
delete: boolean; delete: boolean;
force: boolean; force: boolean;
ignoreExisting: boolean; ignoreExisting: boolean;
exclude: string[]; exclude: string[];
}; };
/**
* This is the metadata that is returned from the metadata call.
*/
export type Metadata = { export type Metadata = {
fileType: string; fileType: string;
isDir: boolean; isDir: boolean;
@@ -362,12 +382,12 @@ export type Dependencies = {
/** Checks are called to make sure that our dependency is in the correct shape. If a known error is returned we know that the dependency needs modification */ /** Checks are called to make sure that our dependency is in the correct shape. If a known error is returned we know that the dependency needs modification */
check( check(
effects: Effects, effects: Effects,
input: ConfigSpec input: ConfigSpec,
): Promise<ResultType<void | null>>; ): Promise<ResultType<void | null>>;
/** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */ /** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */
autoConfigure( autoConfigure(
effects: Effects, effects: Effects,
input: ConfigSpec input: ConfigSpec,
): Promise<ResultType<ConfigSpec>>; ): Promise<ResultType<ConfigSpec>>;
}; };
}; };

View File

@@ -1,4 +1,3 @@
// deno-lint-ignore-file ban-types
export type ConfigSpec = Record<string, ValueSpec>; export type ConfigSpec = Record<string, ValueSpec>;
export type ValueType = export type ValueType =

View File

@@ -11,7 +11,6 @@ type TypePointer = "pointer";
type TypeUnion = "union"; type TypeUnion = "union";
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardDefaultNullable<A, Type> = type GuardDefaultNullable<A, Type> =
A extends { readonly default: unknown} ? Type : A extends { readonly default: unknown} ? Type :
A extends { readonly nullable: true} ? Type : A extends { readonly nullable: true} ? Type :
@@ -19,24 +18,20 @@ type GuardDefaultNullable<A, Type> =
Type Type
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardNumber<A> = type GuardNumber<A> =
A extends {readonly type:TypeNumber} ? GuardDefaultNullable<A, number> : A extends {readonly type:TypeNumber} ? GuardDefaultNullable<A, number> :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardString<A> = type GuardString<A> =
A extends {readonly type:TypeString} ? GuardDefaultNullable<A, string> : A extends {readonly type:TypeString} ? GuardDefaultNullable<A, string> :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardBoolean<A> = type GuardBoolean<A> =
A extends {readonly type:TypeBoolean} ? GuardDefaultNullable<A, boolean> : A extends {readonly type:TypeBoolean} ? GuardDefaultNullable<A, boolean> :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardObject<A> = type GuardObject<A> =
A extends {readonly type: TypeObject, readonly spec: infer B} ? ( A extends {readonly type: TypeObject, readonly spec: infer B} ? (
B extends Record<string, unknown> ? {readonly [K in keyof B & string]: _<GuardAll<B[K]>>} : B extends Record<string, unknown> ? {readonly [K in keyof B & string]: _<GuardAll<B[K]>>} :
@@ -45,24 +40,19 @@ type GuardObject<A> =
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
export type GuardList<A> = export type GuardList<A> =
A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {spec?: infer C }} ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({type: B, spec: C})>> : A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {spec?: infer C }} ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({type: B, spec: C})>> :
// deno-lint-ignore ban-types
A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {}} ? ReadonlyArray<GuardAll<Omit<A, "type" > & ({type: B})>> : A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {}} ? ReadonlyArray<GuardAll<Omit<A, "type" > & ({type: B})>> :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardPointer<A> = type GuardPointer<A> =
A extends {readonly type:TypePointer} ? (string | null) : A extends {readonly type:TypePointer} ? (string | null) :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardEnum<A> = type GuardEnum<A> =
A extends {readonly type:TypeEnum, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> : A extends {readonly type:TypeEnum, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
unknown unknown
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
type GuardUnion<A> = type GuardUnion<A> =
A extends {readonly type:TypeUnion, readonly tag: {id: infer Id & string}, variants: infer Variants & Record<string, unknown>} ? {[K in keyof Variants]: {[keyType in Id & string]: K}&TypeFromProps<Variants[K]>}[keyof Variants] : A extends {readonly type:TypeUnion, readonly tag: {id: infer Id & string}, variants: infer Variants & Record<string, unknown>} ? {[K in keyof Variants]: {[keyType in Id & string]: K}&TypeFromProps<Variants[K]>}[keyof Variants] :
unknown unknown
@@ -77,7 +67,6 @@ export type GuardAll<A> = GuardNumber<A> &
GuardUnion<A> & GuardUnion<A> &
GuardEnum<A>; GuardEnum<A>;
// prettier-ignore // prettier-ignore
// deno-fmt-ignore
export type TypeFromProps<A> = export type TypeFromProps<A> =
A extends Record<string, unknown> ? {readonly [K in keyof A & string]: _<GuardAll<A[K]>>} : A extends Record<string, unknown> ? {readonly [K in keyof A & string]: _<GuardAll<A[K]>>} :
unknown; unknown;
@@ -118,7 +107,7 @@ function charRange(value = "") {
*/ */
export function generateDefault( export function generateDefault(
generate: { charset: string; len: number }, generate: { charset: string; len: number },
{ random = () => Math.random() } = {} { random = () => Math.random() } = {},
) { ) {
const validCharSets: number[][] = generate.charset const validCharSets: number[][] = generate.charset
.split(",") .split(",")
@@ -129,7 +118,7 @@ export function generateDefault(
} }
const max = validCharSets.reduce( const max = validCharSets.reduce(
(acc, x) => x.reduce((x, y) => Math.max(x, y), acc), (acc, x) => x.reduce((x, y) => Math.max(x, y), acc),
0 0,
); );
let i = 0; let i = 0;
const answer: string[] = Array(generate.len); const answer: string[] = Array(generate.len);
@@ -138,7 +127,7 @@ export function generateDefault(
const inRange = validCharSets.reduce( const inRange = validCharSets.reduce(
(acc, [lower, upper]) => (acc, [lower, upper]) =>
acc || (nextValue >= lower && nextValue <= upper), acc || (nextValue >= lower && nextValue <= upper),
false false,
); );
if (!inRange) continue; if (!inRange) continue;
answer[i] = String.fromCharCode(nextValue); answer[i] = String.fromCharCode(nextValue);
@@ -166,19 +155,17 @@ export function matchNumberWithRange(range: string) {
? "any" ? "any"
: left === "[" : left === "["
? `greaterThanOrEqualTo${leftValue}` ? `greaterThanOrEqualTo${leftValue}`
: `greaterThan${leftValue}` : `greaterThan${leftValue}`,
) )
.validate( .validate(
rightValue === "*" // prettier-ignore
? (_) => true rightValue === "*" ? (_) => true :
: right === "]" right === "]"? (x) => x <= Number(rightValue) :
? (x) => x <= Number(rightValue) (x) => x < Number(rightValue),
: (x) => x < Number(rightValue), // prettier-ignore
rightValue === "*" rightValue === "*" ? "any" :
? "any" right === "]" ? `lessThanOrEqualTo${rightValue}` :
: right === "]" `lessThan${rightValue}`,
? `lessThanOrEqualTo${rightValue}`
: `lessThan${rightValue}`
); );
} }
function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) { function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) {
@@ -199,12 +186,12 @@ const isGenerator = matches.shape({
}).test; }).test;
function defaultNullable<A>( function defaultNullable<A>(
parser: matches.Parser<unknown, A>, parser: matches.Parser<unknown, A>,
value: unknown value: unknown,
) { ) {
if (matchDefault.test(value)) { if (matchDefault.test(value)) {
if (isGenerator(value.default)) { if (isGenerator(value.default)) {
return parser.defaultTo( return parser.defaultTo(
parser.unsafeCast(generateDefault(value.default)) parser.unsafeCast(generateDefault(value.default)),
); );
} }
return parser.defaultTo(value.default); return parser.defaultTo(value.default);
@@ -222,34 +209,28 @@ function defaultNullable<A>(
* @returns * @returns
*/ */
export function guardAll<A extends ValueSpecAny>( export function guardAll<A extends ValueSpecAny>(
value: A value: A,
): matches.Parser<unknown, GuardAll<A>> { ): matches.Parser<unknown, GuardAll<A>> {
if (!isType.test(value)) { if (!isType.test(value)) {
// deno-lint-ignore no-explicit-any
return matches.unknown as any; return matches.unknown as any;
} }
switch (value.type) { switch (value.type) {
case "boolean": case "boolean":
// deno-lint-ignore no-explicit-any
return defaultNullable(matches.boolean, value) as any; return defaultNullable(matches.boolean, value) as any;
case "string": case "string":
// deno-lint-ignore no-explicit-any
return defaultNullable(withPattern(value), value) as any; return defaultNullable(withPattern(value), value) as any;
case "number": case "number":
return defaultNullable( return defaultNullable(
withIntegral(withRange(value), value), withIntegral(withRange(value), value),
value value,
// deno-lint-ignore no-explicit-any
) as any; ) as any;
case "object": case "object":
if (matchSpec.test(value)) { if (matchSpec.test(value)) {
// deno-lint-ignore no-explicit-any
return defaultNullable(typeFromProps(value.spec), value) as any; return defaultNullable(typeFromProps(value.spec), value) as any;
} }
// deno-lint-ignore no-explicit-any
return matches.unknown as any; return matches.unknown as any;
case "list": { case "list": {
@@ -261,22 +242,18 @@ export function guardAll<A extends ValueSpecAny>(
const subtype = matchSubType.unsafeCast(value).subtype; const subtype = matchSubType.unsafeCast(value).subtype;
return defaultNullable( return defaultNullable(
matches matches
// deno-lint-ignore no-explicit-any
.arrayOf(guardAll({ type: subtype, ...spec } as any)) .arrayOf(guardAll({ type: subtype, ...spec } as any))
.validate((x) => rangeValidate(x.length), "valid length"), .validate((x) => rangeValidate(x.length), "valid length"),
value value,
// deno-lint-ignore no-explicit-any
) as any; ) as any;
} }
case "enum": case "enum":
if (matchValues.test(value)) { if (matchValues.test(value)) {
return defaultNullable( return defaultNullable(
matches.literals(value.values[0], ...value.values), matches.literals(value.values[0], ...value.values),
value value,
// deno-lint-ignore no-explicit-any
) as any; ) as any;
} }
// deno-lint-ignore no-explicit-any
return matches.unknown as any; return matches.unknown as any;
case "union": case "union":
if (matchUnion.test(value)) { if (matchUnion.test(value)) {
@@ -284,15 +261,13 @@ export function guardAll<A extends ValueSpecAny>(
...Object.entries(value.variants).map(([variant, spec]) => ...Object.entries(value.variants).map(([variant, spec]) =>
matches matches
.shape({ [value.tag.id]: matches.literal(variant) }) .shape({ [value.tag.id]: matches.literal(variant) })
.concat(typeFromProps(spec)) .concat(typeFromProps(spec)),
) // deno-lint-ignore no-explicit-any ),
) as any; ) as any;
} }
// deno-lint-ignore no-explicit-any
return matches.unknown as any; return matches.unknown as any;
} }
// deno-lint-ignore no-explicit-any
return matches.unknown as any; return matches.unknown as any;
} }
/** /**
@@ -304,17 +279,15 @@ export function guardAll<A extends ValueSpecAny>(
* @returns * @returns
*/ */
export function typeFromProps<A extends ConfigSpec>( export function typeFromProps<A extends ConfigSpec>(
valueDictionary: A valueDictionary: A,
): matches.Parser<unknown, TypeFromProps<A>> { ): matches.Parser<unknown, TypeFromProps<A>> {
// deno-lint-ignore no-explicit-any
if (!recordString.test(valueDictionary)) return matches.unknown as any; if (!recordString.test(valueDictionary)) return matches.unknown as any;
return matches.shape( return matches.shape(
Object.fromEntries( Object.fromEntries(
Object.entries(valueDictionary).map(([key, value]) => [ Object.entries(valueDictionary).map(([key, value]) => [
key, key,
guardAll(value), guardAll(value),
]) ]),
) ),
// deno-lint-ignore no-explicit-any
) as any; ) as any;
} }

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-lib0.alpha2", "version": "0.4.0-lib0.alpha3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-lib0.alpha2", "version": "0.4.0-lib0.alpha3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@iarna/toml": "^2.2.5", "@iarna/toml": "^2.2.5",

View File

@@ -1,6 +1,6 @@
{ {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-lib0.alpha3", "version": "0.4.0-lib0.alpha4",
"description": "For making the patterns that are wanted in making services for the startOS.", "description": "For making the patterns that are wanted in making services for the startOS.",
"main": "./index.cjs", "main": "./index.cjs",
"types": "./index.d.ts", "types": "./index.d.ts",