@usirin/spellbook
Version:
Type-safe API surfaces that work across process boundaries
29 lines • 2.19 kB
TypeScript
import type { StandardSchemaV1 as StandardV1 } from "@standard-schema/spec";
export interface SpellSpec<TParamsSchema extends StandardV1, TResultSchema extends StandardV1, TContextSchema extends StandardV1> {
description: string;
parameters: TParamsSchema;
result: TResultSchema;
context: TContextSchema;
execute: (parameters: StandardV1.InferOutput<TParamsSchema>, context: StandardV1.InferOutput<TContextSchema>) => Promise<StandardV1.InferOutput<TResultSchema>>;
}
export declare function createSpell<TParams extends StandardV1, TResult extends StandardV1, TContext extends StandardV1>(spec: SpellSpec<TParams, TResult, TContext>): {
(parameters: StandardV1.InferInput<TParams>, context: StandardV1.InferInput<TContext>): Promise<StandardV1.InferOutput<TResult>>;
_spec: SpellSpec<TParams, TResult, TContext>;
_tag: string;
};
export type Spell<TParamsSchema extends StandardV1 = StandardV1<any, any>, TResultSchema extends StandardV1 = StandardV1<any, any>, TContextSchema extends StandardV1 = StandardV1<any, any>> = {
(parameters: StandardV1.InferInput<TParamsSchema>, context: StandardV1.InferOutput<TContextSchema>): Promise<StandardV1.InferOutput<TResultSchema>>;
_spec: SpellSpec<TParamsSchema, TResultSchema, TContextSchema>;
_tag: string;
};
type ContextTypeOf<S> = S extends Spell<any, any, infer C> ? StandardV1.InferInput<C> : never;
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
export type DerivedContextType<TSpells extends Record<string, Spell<any, any, any>>> = UnionToIntersection<ContextTypeOf<TSpells[keyof TSpells]>>;
export declare function createSpellbook<TSpells extends Record<string, Spell<any, any, any>>, TContext extends DerivedContextType<TSpells>>(spells: TSpells, context: TContext): Spellbook<TSpells, TContext>;
export type Spellbook<TSpells extends Record<string, Spell<any, any, any>> = Record<string, Spell<any, any, any>>, TContext = DerivedContextType<TSpells>> = {
[K in keyof TSpells]: (parameters: Parameters<TSpells[K]>[0]) => ReturnType<TSpells[K]>;
} & {
_context: TContext;
};
export {};
//# sourceMappingURL=spellbook.d.ts.map