import { JRPCNotification, JRPCParams, JRPCRequest, Json } from "../interfaces.js"; import { JRPCEngineV2 } from "./jrpcEngineV2.js"; import { MiddlewareContext } from "./MiddlewareContext.js"; export type NonEmptyArray = [Type, ...Type[]]; export type JsonRpcCall = JRPCNotification | JRPCRequest; /** * An unholy incantation that converts a union of object types into an * intersection of object types. * * @example * type A = { a: string } | { b: number }; * type B = UnionToIntersection; // { a: string } & { b: number } */ export type UnionToIntersection = (Union extends never ? never : (k: Union) => void) extends ((k: infer Args) => void) ? Args : never; /** * Infer the KeyValues type from a {@link MiddlewareContext}. */ export type InferKeyValues = Type extends MiddlewareContext ? KeyValues : never; /** * Simplifies an object type by "merging" its properties. * * - Expands intersections into a single object type. * - Forces mapped/conditional results to resolve into a readable shape. * - No runtime effect; purely a type-level normalization. * * @example * type A = { a: string } & { b: number }; * type B = Simplify; // { a: string; b: number } */ type Simplify = Type extends (infer Object) ? { [Key in keyof Object]: Object[Key]; } : never; /** * Rejects record types that contain any `never`-valued property. * * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged. * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`. * * @example * type A = ExcludeNever<{ a: string; b: never }>; // never * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number } */ type ExcludeNever> = { [Key in keyof Type]-?: [Type[Key]] extends [never] ? Key : never; }[keyof Type] extends never ? Type : never; /** * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext} * supertype. * * @param Contexts - The union of {@link MiddlewareContext}s to merge. * @returns The merged {@link MiddlewareContext} supertype. * @example * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>; * type B = MergeContexts; // MiddlewareContext<{ a: string, b: number }> */ export type MergeContexts = ExcludeNever>>> extends never ? never : MiddlewareContext>>>>; /** * A constraint for {@link MiddlewareContext} generic parameters. */ export type ContextConstraint = MiddlewareContext; /** * The empty context type, i.e. `MiddlewareContext<{}>`. */ export type EmptyContext = MiddlewareContext<{}>; export type WithoutId = Request & { id?: never; }; export type MixedParam = [Extract] extends [never] ? never : [Extract] extends [never] ? never : Extract | WithoutId>; export type ResultConstraint = Request extends JRPCRequest ? Json : void; export type Next = (request?: Readonly) => Promise> | undefined>; export type MiddlewareParams = { request: Readonly; context: Context; next: Next; }; export type JRPCMiddlewareV2 = ResultConstraint, Context extends ContextConstraint = MiddlewareContext> = (params: MiddlewareParams) => Readonly | undefined | Promise | undefined>; export type RequestState = { request: Request; result: Readonly> | undefined; }; /** * The options for the JSON-RPC request/notification handling operation. */ export type HandleOptions = { context?: Context | InferKeyValues; }; /** * The request type of a middleware. */ export type RequestOf = Middleware extends JRPCMiddlewareV2, any> ? Request : never; export type ContextOf = Middleware extends JRPCMiddlewareV2, infer C> ? C : never; /** * A constraint for {@link JRPCMiddlewareV2} generic parameters. */ export type MiddlewareConstraint = JRPCMiddlewareV2, MiddlewareContext>; /** * The context supertype of a middleware type. */ export type MergedContextOf = MergeContexts>; declare const INVALID_ENGINE: unique symbol; /** * An internal type for invalid engines that explains why the engine is invalid. * * @template Message - The message explaining why the engine is invalid. */ export type InvalidEngine = { [INVALID_ENGINE]: Message; }; type JsonPrimitive = string | number | boolean | null; /** * A record of RPC method handler functions or hard-coded results, keyed to particular method names. * Only primitive JSON values are permitted as hard-coded results. * * Uses `never` as the function parameter type so that handlers with * arbitrarily-narrowed request params remain assignable to the record value type * (every function type is a subtype of `(params: never) => R`). */ export type MiddlewareScaffold = Record Readonly | undefined | Promise | undefined>) | JsonPrimitive>; export type DeepCloned = Type extends readonly (infer ArrayType)[] ? DeepCloned[] : Type extends object ? { -readonly [Key in keyof Type & (string | number)]: DeepCloned; } : Type; export type OnError = (error: unknown) => void; export type Options = { onError?: OnError; } & ({ engine: ReturnType>; } | { middleware: NonEmptyArray; }); export {};