You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Types.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as I from "./Interfaces";
  2. export type AnyFunction = (...any:any)=>any
  3. export type AsyncFunction<Fn extends AnyFunction = AnyFunction> = ReturnType<Fn> extends Promise<any> ? Fn : (...any:Parameters<Fn>) => Promise<ReturnType<Fn>>
  4. export type RPCGroup = { [name in string] : AsyncFunction }
  5. export type RPCInterface<Impl extends RPCInterface = {}> = { [groupName in string]: RPCGroup } & Impl
  6. export type Visibility = "127.0.0.1" | "0.0.0.0"
  7. export type ConnectionHandler = (socket:I.Socket) => void
  8. export type ErrorHandler = (socket:I.Socket, error:any) => void
  9. export type CloseHandler = (socket:I.Socket) => void
  10. export type SocketConf = {
  11. connectionHandler?: ConnectionHandler
  12. errorHandler?: ErrorHandler
  13. closeHandler?: CloseHandler
  14. visibility?: Visibility
  15. }
  16. export type ResponseType = "Subscribe" | "Success" | "Error"
  17. export type Outcome = "Success" | "Error"
  18. export type Respose<T> = T & { result: Outcome }
  19. export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
  20. export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
  21. export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uuid: string }
  22. export type RPCType = 'Hook' | 'Unhook' | 'Call'
  23. export type HookT<G extends RPCGroup, K extends keyof G, SubresT> = AsyncFunction<HookFunction<G[K], SubresT>>
  24. export type CallT<G extends RPCGroup, K extends keyof G> = AsyncFunction<G[K]>
  25. export type HookRPC<G extends RPCGroup, K extends keyof G, SubresT = {}> = {
  26. name: K
  27. hook: HookT<G, K, SubresT>
  28. onCallback?: CallbackFunction,
  29. onClose?: HookCloseFunction<SubresT>
  30. }
  31. export type CallRPC<G extends RPCGroup, K extends keyof G> = {
  32. name: K
  33. call: CallT<G,K>
  34. }
  35. export type RPC<G extends RPCGroup, Kg extends keyof G, SubresT> = CallRPC<G, Kg> | HookRPC<G, Kg, SubresT>
  36. export type BaseInfo = {
  37. name: string,
  38. owner: string,
  39. argNames: string[],
  40. }
  41. export type HookInfo<T = {}> = BaseInfo & {
  42. type: 'Hook',
  43. generator: (socket?:I.Socket) => HookFunction<AnyFunction, T>
  44. }
  45. export type CallInfo = BaseInfo & {
  46. type: 'Call',
  47. call: AsyncFunction
  48. }
  49. export type RpcInfo = HookInfo | CallInfo
  50. export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
  51. export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
  52. export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any
  53. export type HookFunction<F extends AnyFunction = AnyFunction, SubResT = {}> = AsyncFunction<(...args:Parameters<F>) => SubscriptionResponse<SubResT> | ErrorResponse>
  54. export type CallbackFunction = (callback:AnyFunction, ...args:any) => any