index.d.mts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
  2. type ReturnType<T> = T extends (...args: any) => infer R ? R : never;
  3. type PromisifyFn<T> = ReturnType<T> extends Promise<any> ? T : (...args: ArgumentsType<T>) => Promise<Awaited<ReturnType<T>>>;
  4. type BirpcResolver = (name: string, resolved: (...args: unknown[]) => unknown) => ((...args: unknown[]) => unknown) | undefined;
  5. interface ChannelOptions {
  6. /**
  7. * Function to post raw message
  8. */
  9. post: (data: any, ...extras: any[]) => any | Promise<any>;
  10. /**
  11. * Listener to receive raw message
  12. */
  13. on: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  14. /**
  15. * Clear the listener when `$close` is called
  16. */
  17. off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  18. /**
  19. * Custom function to serialize data
  20. *
  21. * by default it passes the data as-is
  22. */
  23. serialize?: (data: any) => any;
  24. /**
  25. * Custom function to deserialize data
  26. *
  27. * by default it passes the data as-is
  28. */
  29. deserialize?: (data: any) => any;
  30. /**
  31. * Call the methods with the RPC context or the original functions object
  32. */
  33. bind?: 'rpc' | 'functions';
  34. }
  35. interface EventOptions<Remote> {
  36. /**
  37. * Names of remote functions that do not need response.
  38. */
  39. eventNames?: (keyof Remote)[];
  40. /**
  41. * Maximum timeout for waiting for response, in milliseconds.
  42. *
  43. * @default 60_000
  44. */
  45. timeout?: number;
  46. /**
  47. * Custom resolver to resolve function to be called
  48. *
  49. * For advanced use cases only
  50. */
  51. resolver?: BirpcResolver;
  52. /**
  53. * Custom error handler
  54. *
  55. * @deprecated use `onFunctionError` and `onGeneralError` instead
  56. */
  57. onError?: (error: Error, functionName: string, args: any[]) => boolean | void;
  58. /**
  59. * Custom error handler for errors occurred in local functions being called
  60. *
  61. * @returns `true` to prevent the error from being thrown
  62. */
  63. onFunctionError?: (error: Error, functionName: string, args: any[]) => boolean | void;
  64. /**
  65. * Custom error handler for errors occurred during serialization or messsaging
  66. *
  67. * @returns `true` to prevent the error from being thrown
  68. */
  69. onGeneralError?: (error: Error, functionName?: string, args?: any[]) => boolean | void;
  70. /**
  71. * Custom error handler for timeouts
  72. *
  73. * @returns `true` to prevent the error from being thrown
  74. */
  75. onTimeoutError?: (functionName: string, args: any[]) => boolean | void;
  76. }
  77. type BirpcOptions<Remote> = EventOptions<Remote> & ChannelOptions;
  78. type BirpcFn<T> = PromisifyFn<T> & {
  79. /**
  80. * Send event without asking for response
  81. */
  82. asEvent: (...args: ArgumentsType<T>) => void;
  83. };
  84. interface BirpcGroupFn<T> {
  85. /**
  86. * Call the remote function and wait for the result.
  87. */
  88. (...args: ArgumentsType<T>): Promise<Awaited<ReturnType<T>>[]>;
  89. /**
  90. * Send event without asking for response
  91. */
  92. asEvent: (...args: ArgumentsType<T>) => void;
  93. }
  94. type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {
  95. [K in keyof RemoteFunctions]: BirpcFn<RemoteFunctions[K]>;
  96. } & {
  97. $functions: LocalFunctions;
  98. $close: (error?: Error) => void;
  99. };
  100. type BirpcGroupReturn<RemoteFunctions> = {
  101. [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>;
  102. };
  103. interface BirpcGroup<RemoteFunctions, LocalFunctions = Record<string, never>> {
  104. readonly clients: BirpcReturn<RemoteFunctions, LocalFunctions>[];
  105. readonly functions: LocalFunctions;
  106. readonly broadcast: BirpcGroupReturn<RemoteFunctions>;
  107. updateChannels: (fn?: ((channels: ChannelOptions[]) => void)) => BirpcReturn<RemoteFunctions, LocalFunctions>[];
  108. }
  109. declare const DEFAULT_TIMEOUT = 60000;
  110. declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
  111. declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
  112. declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
  113. export { type ArgumentsType, type BirpcFn, type BirpcGroup, type BirpcGroupFn, type BirpcGroupReturn, type BirpcOptions, type BirpcResolver, type BirpcReturn, type ChannelOptions, DEFAULT_TIMEOUT, type EventOptions, type PromisifyFn, type ReturnType, cachedMap, createBirpc, createBirpcGroup };