123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import * as I from "./Interfaces";
-
- export type AnyFunction = (...any:any)=>any
- export type AsyncFunction<Fn extends AnyFunction = AnyFunction> = ReturnType<Fn> extends Promise<any> ? Fn : (...any:Parameters<Fn>) => Promise<ReturnType<Fn>>
- export type RPCGroup = { [name in string] : AsyncFunction }
- export type RPCInterface<Impl extends RPCInterface = {}> = { [groupName in string]: RPCGroup } & Impl
-
- export type Visibility = "127.0.0.1" | "0.0.0.0"
- export type ConnectionHandler = (socket:I.Socket) => void
- export type ErrorHandler = (socket:I.Socket, error:any) => void
- export type CloseHandler = (socket:I.Socket) => void
- export type SocketConf = {
- connectionHandler?: ConnectionHandler
- errorHandler?: ErrorHandler
- closeHandler?: CloseHandler
- visibility?: Visibility
- }
-
- export type ResponseType = "Subscribe" | "Success" | "Error"
- export type Outcome = "Success" | "Error"
-
-
- export type Respose<T> = T & { result: Outcome }
- export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
- export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
- export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uuid: string }
-
- export type RPCType = 'Hook' | 'Unhook' | 'Call'
-
- export type HookT<G extends RPCGroup, K extends keyof G, SubresT> = AsyncFunction<HookFunction<G[K], SubresT>>
- export type CallT<G extends RPCGroup, K extends keyof G> = AsyncFunction<G[K]>
-
- export type HookRPC<G extends RPCGroup, K extends keyof G, SubresT = {}> = {
- name: K
- hook: HookT<G, K, SubresT>
- onCallback?: CallbackFunction,
- onClose?: HookCloseFunction<SubresT>
- }
-
- export type CallRPC<G extends RPCGroup, K extends keyof G> = {
- name: K
- call: CallT<G,K>
- }
-
- export type RPC<G extends RPCGroup, Kg extends keyof G, SubresT> = CallRPC<G, Kg> | HookRPC<G, Kg, SubresT>
-
- export type BaseInfo = {
- name: string,
- owner: string,
- argNames: string[],
- }
-
- export type HookInfo<T = {}> = BaseInfo & {
- type: 'Hook',
- generator: (socket?:I.Socket) => HookFunction<AnyFunction, T>
- }
-
- export type CallInfo = BaseInfo & {
- type: 'Call',
- call: AsyncFunction
- }
-
- export type RpcInfo = HookInfo | CallInfo
- export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
-
- export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
- export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any
- export type HookFunction<F extends AnyFunction = AnyFunction, SubResT = {}> = AsyncFunction<(...args:Parameters<F>) => SubscriptionResponse<SubResT> | ErrorResponse>
- export type CallbackFunction = (callback:AnyFunction, ...args:any) => any
|