|
|
|
|
@@ -1,11 +1,13 @@
|
|
|
|
|
import { Effect, pipe } from "effect"
|
|
|
|
|
import { HasRequiredKeys } from "type-fest"
|
|
|
|
|
import { z } from "zod"
|
|
|
|
|
import { SchemableClass, SchemableConfig } from "."
|
|
|
|
|
import { parseZodTypeEffect } from "./util"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
|
|
|
|
|
|
|
|
|
type NewEntityArgs<Input extends object> =
|
|
|
|
|
type NewSchemableArgs<Input extends object> =
|
|
|
|
|
HasRequiredKeys<Input> extends true
|
|
|
|
|
? [Input, ...ParamsArgs]
|
|
|
|
|
: [] | [Input, ...ParamsArgs]
|
|
|
|
|
@@ -16,6 +18,34 @@ export const newSchemable = <
|
|
|
|
|
$Config extends SchemableConfig,
|
|
|
|
|
>(
|
|
|
|
|
class_: C | SchemableClass<$Config>,
|
|
|
|
|
...[values, params]: NewEntityArgs<$Config["input"]>
|
|
|
|
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
|
|
|
|
) =>
|
|
|
|
|
new class_(class_.schemaWithDefaultValues.parse(values || {}, params))
|
|
|
|
|
new class_(class_.schemaWithDefaultValues.parse(values || {}, params)) as InstanceType<C>
|
|
|
|
|
|
|
|
|
|
export const newSchemablePromise = async <
|
|
|
|
|
C extends SchemableClass<$Config>,
|
|
|
|
|
$Config extends SchemableConfig,
|
|
|
|
|
>(
|
|
|
|
|
class_: C | SchemableClass<$Config>,
|
|
|
|
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
|
|
|
|
) =>
|
|
|
|
|
new class_(await class_.schemaWithDefaultValues.parseAsync(values || {}, params)) as InstanceType<C>
|
|
|
|
|
|
|
|
|
|
export const newSchemableEffect = <
|
|
|
|
|
C extends SchemableClass<$Config>,
|
|
|
|
|
$Config extends SchemableConfig,
|
|
|
|
|
>(
|
|
|
|
|
class_: C | SchemableClass<$Config>,
|
|
|
|
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
|
|
|
|
) => pipe(
|
|
|
|
|
parseZodTypeEffect<
|
|
|
|
|
z.output<typeof class_.schemaWithDefaultValues>,
|
|
|
|
|
z.input<typeof class_.schemaWithDefaultValues>
|
|
|
|
|
>(
|
|
|
|
|
class_.schemaWithDefaultValues,
|
|
|
|
|
values || {},
|
|
|
|
|
params,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
Effect.map(values => new class_(values) as InstanceType<C>),
|
|
|
|
|
)
|
|
|
|
|
|