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.3KB

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