@jsonjoy.com/reactive-rpc
Version:
Reactive-RPC is a library for building reactive APIs over WebSocket, HTTP, and other RPCs.
36 lines (35 loc) • 2.94 kB
TypeScript
import { RpcCaller, type RpcApiCallerOptions } from './RpcCaller';
import { FunctionStreamingType, FunctionType } from '@jsonjoy.com/json-type/lib/type/classes';
import { StaticRpcMethod, type StaticRpcMethodOptions } from '../methods/StaticRpcMethod';
import { StreamingRpcMethod, type StreamingRpcMethodOptions } from '../methods/StreamingRpcMethod';
import type { ObjectType, TypeSystem, ObjectFieldType, TypeOf, SchemaOf } from '@jsonjoy.com/json-type';
import type { Printable } from 'sonic-forest/lib/print/types';
import type { ObjectValue, UnObjectType, UnObjectValue } from '@jsonjoy.com/json-type/lib/value/ObjectValue';
import type { Observable } from 'rxjs';
import type { RpcValue } from '../../messages/Value';
type ObjectFieldToTuple<F> = F extends ObjectFieldType<infer K, infer V> ? [K, V] : never;
type ToObject<T> = T extends [string, unknown][] ? {
[K in T[number] as K[0]]: K[1];
} : never;
type ObjectFieldsToMap<F> = ToObject<{
[K in keyof F]: ObjectFieldToTuple<F[K]>;
}>;
type ObjectValueToTypeMap<V> = ObjectFieldsToMap<UnObjectType<UnObjectValue<V>>>;
type MethodReq<F> = F extends FunctionType<infer Req, any> ? TypeOf<SchemaOf<Req>> : F extends FunctionStreamingType<infer Req, any> ? TypeOf<SchemaOf<Req>> : never;
type MethodRes<F> = F extends FunctionType<any, infer Res> ? TypeOf<SchemaOf<Res>> : F extends FunctionStreamingType<any, infer Res> ? TypeOf<SchemaOf<Res>> : never;
type MethodDefinition<Ctx, F> = F extends FunctionType<any, any> ? StaticRpcMethodOptions<Ctx, MethodReq<F>, MethodRes<F>> : F extends FunctionStreamingType<any, any> ? StreamingRpcMethodOptions<Ctx, MethodReq<F>, MethodRes<F>> : never;
export interface ObjectValueCallerOptions<V extends ObjectValue<ObjectType<any>>, Ctx = unknown> extends Omit<RpcApiCallerOptions<Ctx>, 'getMethod'> {
router: V;
}
export declare class ObjectValueCaller<V extends ObjectValue<ObjectType<any>>, Ctx = unknown> extends RpcCaller<Ctx> implements Printable {
readonly router: V;
protected readonly system: TypeSystem;
protected readonly methods: Map<string, StaticRpcMethod<Ctx, unknown, unknown> | StreamingRpcMethod<Ctx, unknown, unknown>>;
constructor({ router: value, ...rest }: ObjectValueCallerOptions<V, Ctx>);
get<K extends keyof ObjectValueToTypeMap<V>>(id: K): MethodDefinition<Ctx, ObjectValueToTypeMap<V>[K]> | undefined;
call<K extends keyof ObjectValueToTypeMap<V>>(id: K, request: MethodReq<ObjectValueToTypeMap<V>[K]>, ctx: Ctx): Promise<RpcValue<MethodRes<ObjectValueToTypeMap<V>[K]>>>;
callSimple<K extends keyof ObjectValueToTypeMap<V>>(id: K, request: MethodReq<ObjectValueToTypeMap<V>[K]>, ctx?: Ctx): Promise<MethodRes<ObjectValueToTypeMap<V>[K]>>;
call$<K extends keyof ObjectValueToTypeMap<V>>(id: K, request: Observable<MethodReq<ObjectValueToTypeMap<V>[K]>>, ctx: Ctx): Observable<RpcValue<MethodRes<ObjectValueToTypeMap<V>[K]>>>;
toString(tab?: string): string;
}
export {};