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

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