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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, rpcName: string, args: any[]) => void
  7. export type CloseHandler = (socket:I.Socket) => void
  8. export type SesameFunction = (sesame : string) => boolean
  9. export type ExceptionHandling = 'local' | 'remote'
  10. export type SesameConf = {
  11. sesame?: string | SesameFunction
  12. }
  13. export type HandlerType = {
  14. 'error' : ErrorHandler
  15. 'close' : CloseHandler
  16. }
  17. export type ServerConf = {
  18. connectionHandler?: ConnectionHandler
  19. errorHandler?: ErrorHandler
  20. closeHandler?: CloseHandler
  21. visibility?: Visibility
  22. exceptionHandling?: ExceptionHandling
  23. } & SesameConf
  24. export type SocketConf = {
  25. tls:boolean
  26. }
  27. export type ResponseType = "Subscribe" | "Success" | "Error"
  28. export type Outcome = "Success" | "Error"
  29. export type Respose<T> = T & { result: Outcome }
  30. export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
  31. export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
  32. export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uuid: string }
  33. export type RPCType = 'Hook' | 'Unhook' | 'Call'
  34. export type CallRPC<N, F> = {
  35. name: N
  36. call: F
  37. }
  38. export type HookRPC<N, F extends AnyFunction, SubresT = {}> = {
  39. name: N
  40. hook: HookFunction<F, SubresT>
  41. onCallback?: AnyFunction
  42. onClose?: HookCloseFunction<SubresT>
  43. }
  44. export type RPC<N, F extends AnyFunction, SubresT = {}> = HookRPC<N, F, SubresT> | CallRPC<N,F> | F
  45. export type RPCInterface<Impl extends RPCInterface = {}> = {
  46. [grp in string] : {
  47. [rpc in string] : AnyFunction
  48. }
  49. } & Impl
  50. export type RPCInterfaceArray<Itfc extends RPCInterface, SubresT = {}> = {
  51. [grp in keyof Itfc]: Array<
  52. { [rpc in keyof Itfc[grp]]: RPC<rpc, Itfc[grp][rpc], SubresT> }[keyof Itfc[grp]]
  53. >
  54. }
  55. export type BaseInfo = {
  56. name: string,
  57. owner: string,
  58. argNames: string[],
  59. }
  60. export type HookInfo<SubresT = {}> = BaseInfo & {
  61. type: 'Hook',
  62. generator: (socket?:I.Socket) => (...args:any[]) => SubresT
  63. }
  64. export type CallInfo = BaseInfo & {
  65. type: 'Call',
  66. call: AnyFunction
  67. }
  68. export type RpcInfo = HookInfo | CallInfo
  69. export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
  70. export type OnFunction = <T extends "error" | "close">(type: T, f: HandlerType[T]) => void
  71. export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any