This commit is contained in:
64
src/util.ts
64
src/util.ts
@@ -1,64 +0,0 @@
|
|||||||
import { Effect, pipe } from "effect"
|
|
||||||
import { AbstractClass, Class as ConcreteClass } from "type-fest"
|
|
||||||
import { z } from "zod"
|
|
||||||
|
|
||||||
|
|
||||||
export function identity<T>(value: T) {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type ClassType = "AbstractClass" | "Class"
|
|
||||||
|
|
||||||
export type Class<
|
|
||||||
Type extends ClassType,
|
|
||||||
T,
|
|
||||||
Arguments extends unknown[] = any[],
|
|
||||||
> = (
|
|
||||||
Type extends "AbstractClass"
|
|
||||||
? AbstractClass<T, Arguments>
|
|
||||||
: Type extends "Class"
|
|
||||||
? ConcreteClass<T, Arguments>
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
|
|
||||||
export type GetClassType<C> = (
|
|
||||||
C extends ConcreteClass<any>
|
|
||||||
? "Class"
|
|
||||||
: C extends AbstractClass<any>
|
|
||||||
? "AbstractClass"
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the static members of a class.
|
|
||||||
* @template C - The class type.
|
|
||||||
*/
|
|
||||||
export type StaticMembers<C> = {
|
|
||||||
[Key in keyof C as Key extends "prototype" ? never : Key]: C[Key]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
|
||||||
*
|
|
||||||
* @param schema - The ZodType schema to use for parsing.
|
|
||||||
* @param args - The arguments to pass to the `safeParseAsync` method of the schema.
|
|
||||||
* @returns An Effect monad representing the parsing result.
|
|
||||||
*/
|
|
||||||
export const parseZodTypeEffect = <
|
|
||||||
Output,
|
|
||||||
Input,
|
|
||||||
>(
|
|
||||||
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
|
||||||
...args: Parameters<typeof schema.safeParseAsync>
|
|
||||||
) => pipe(
|
|
||||||
Effect.promise(() => schema.safeParseAsync(...args)),
|
|
||||||
|
|
||||||
Effect.flatMap(response =>
|
|
||||||
response.success
|
|
||||||
? Effect.succeed(response.data)
|
|
||||||
: Effect.fail(response.error)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
101
src/util/class.ts
Normal file
101
src/util/class.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import { AbstractClass, Class as ConcreteClass } from "type-fest"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the possible types of a class.
|
||||||
|
*/
|
||||||
|
export type ClassType = "AbstractClass" | "Class"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a class based on the specified type.
|
||||||
|
* @template Type - The type of the class ("AbstractClass" or "Class").
|
||||||
|
* @template T - The type parameter of the class.
|
||||||
|
* @template Arguments - The type of arguments the class constructor takes.
|
||||||
|
*/
|
||||||
|
export type Class<
|
||||||
|
Type extends ClassType,
|
||||||
|
T,
|
||||||
|
Arguments extends unknown[] = any[],
|
||||||
|
> = (
|
||||||
|
Type extends "AbstractClass"
|
||||||
|
? AbstractClass<T, Arguments>
|
||||||
|
: Type extends "Class"
|
||||||
|
? ConcreteClass<T, Arguments>
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the type of a class (either "Class" or "AbstractClass").
|
||||||
|
* @template C - The class type to determine.
|
||||||
|
*/
|
||||||
|
export type GetClassType<C> = (
|
||||||
|
C extends ConcreteClass<any>
|
||||||
|
? "Class"
|
||||||
|
: C extends AbstractClass<any>
|
||||||
|
? "AbstractClass"
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an array of instances corresponding to the provided classes.
|
||||||
|
* @template Classes - An array of classes extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type ClassesInstances<Classes extends readonly AbstractClass<any>[]> = (
|
||||||
|
Classes extends [infer Class, ...infer Rest]
|
||||||
|
? Class extends AbstractClass<any>
|
||||||
|
? Rest extends AbstractClass<any>[]
|
||||||
|
? [InstanceType<Class>, ...ClassesInstances<Rest>]
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
: []
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an intersection of instances of the provided classes.
|
||||||
|
* @template Classes - An array of classes extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type ClassesInstancesIntersection<Classes extends readonly AbstractClass<any>[]> = (
|
||||||
|
Classes extends [infer Class, ...infer Rest]
|
||||||
|
? Class extends AbstractClass<any>
|
||||||
|
? Rest extends AbstractClass<any>[]
|
||||||
|
? InstanceType<Class> & ClassesInstancesIntersection<Rest>
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
: {}
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the static members of a class.
|
||||||
|
* @template Class - A class extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type StaticMembers<Class extends AbstractClass<any>> = (
|
||||||
|
Omit<Class, "prototype">
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an array of static members corresponding to the provided classes.
|
||||||
|
* @template Classes - An array of classes extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type ClassesStaticMembers<Classes extends readonly AbstractClass<any>[]> = (
|
||||||
|
Classes extends [infer Class, ...infer Rest]
|
||||||
|
? Class extends AbstractClass<any>
|
||||||
|
? Rest extends AbstractClass<any>[]
|
||||||
|
? [StaticMembers<Class>, ...ClassesStaticMembers<Rest>]
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
: []
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an intersection of static members of the provided classes.
|
||||||
|
* @template Classes - An array of classes extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type ClassesStaticMembersIntersection<Classes extends readonly AbstractClass<any>[]> = (
|
||||||
|
Classes extends [infer Class, ...infer Rest]
|
||||||
|
? Class extends AbstractClass<any>
|
||||||
|
? Rest extends AbstractClass<any>[]
|
||||||
|
? StaticMembers<Class> & ClassesStaticMembersIntersection<Rest>
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
: {}
|
||||||
|
)
|
||||||
26
src/util/effect.ts
Normal file
26
src/util/effect.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { Effect, pipe } from "effect"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
||||||
|
*
|
||||||
|
* @param schema - The ZodType schema to use for parsing.
|
||||||
|
* @param args - The arguments to pass to the `safeParseAsync` method of the schema.
|
||||||
|
* @returns An Effect monad representing the parsing result.
|
||||||
|
*/
|
||||||
|
export const parseZodTypeEffect = <
|
||||||
|
Output,
|
||||||
|
Input,
|
||||||
|
>(
|
||||||
|
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
||||||
|
...args: Parameters<typeof schema.safeParseAsync>
|
||||||
|
) => pipe(
|
||||||
|
Effect.promise(() => schema.safeParseAsync(...args)),
|
||||||
|
|
||||||
|
Effect.flatMap(response =>
|
||||||
|
response.success
|
||||||
|
? Effect.succeed(response.data)
|
||||||
|
: Effect.fail(response.error)
|
||||||
|
),
|
||||||
|
)
|
||||||
4
src/util/index.ts
Normal file
4
src/util/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export * from "./class"
|
||||||
|
export * from "./effect"
|
||||||
|
export * from "./inheritance"
|
||||||
|
export * from "./misc"
|
||||||
40
src/util/inheritance.ts
Normal file
40
src/util/inheritance.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Represents the common keys between two types.
|
||||||
|
* @template A - The first type.
|
||||||
|
* @template B - The second type.
|
||||||
|
*/
|
||||||
|
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges an inheritance tree defined by an array of types, considering overrides.
|
||||||
|
* @template T - An array of types representing the inheritance tree.
|
||||||
|
*/
|
||||||
|
export type MergeInheritanceTree<T extends readonly any[]> = (
|
||||||
|
T extends [infer Super, infer Self, ...infer Rest]
|
||||||
|
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
|
? MergeInheritanceTree<[
|
||||||
|
Omit<Super, CommonKeys<Self, Super>> & Self,
|
||||||
|
...Rest,
|
||||||
|
]>
|
||||||
|
: never
|
||||||
|
: T extends [infer Self]
|
||||||
|
? Self
|
||||||
|
: void
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges an inheritance tree defined by an array of types without allowing overrides.
|
||||||
|
* @template T - An array of types representing the inheritance tree.
|
||||||
|
*/
|
||||||
|
export type MergeInheritanceTreeWithoutOverriding<T extends readonly any[]> = (
|
||||||
|
T extends [infer Super, infer Self, ...infer Rest]
|
||||||
|
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
|
? MergeInheritanceTreeWithoutOverriding<[
|
||||||
|
Super & Self,
|
||||||
|
...Rest,
|
||||||
|
]>
|
||||||
|
: never
|
||||||
|
: T extends [infer Self]
|
||||||
|
? Self
|
||||||
|
: void
|
||||||
|
)
|
||||||
3
src/util/misc.ts
Normal file
3
src/util/misc.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export function identity<T>(value: T) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user