index.d.mts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Thenable<T> = T | PromiseLike<T>;
  5. type BirpcResolver<This> = (this: This, name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: any[]) => any) | undefined>;
  6. interface ChannelOptions {
  7. /**
  8. * Function to post raw message
  9. */
  10. post: (data: any, ...extras: any[]) => any | Promise<any>;
  11. /**
  12. * Listener to receive raw message
  13. */
  14. on: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  15. /**
  16. * Clear the listener when `$close` is called
  17. */
  18. off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  19. /**
  20. * Custom function to serialize data
  21. *
  22. * by default it passes the data as-is
  23. */
  24. serialize?: (data: any) => any;
  25. /**
  26. * Custom function to deserialize data
  27. *
  28. * by default it passes the data as-is
  29. */
  30. deserialize?: (data: any) => any;
  31. /**
  32. * Call the methods with the RPC context or the original functions object
  33. */
  34. bind?: 'rpc' | 'functions';
  35. /**
  36. * Custom meta data to attached to the RPC instance's `$meta` property
  37. */
  38. meta?: any;
  39. }
  40. interface EventOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> {
  41. /**
  42. * Names of remote functions that do not need response.
  43. */
  44. eventNames?: (keyof RemoteFunctions)[];
  45. /**
  46. * Maximum timeout for waiting for response, in milliseconds.
  47. *
  48. * @default 60_000
  49. */
  50. timeout?: number;
  51. /**
  52. * Custom resolver to resolve function to be called
  53. *
  54. * For advanced use cases only
  55. */
  56. resolver?: BirpcResolver<BirpcReturn<RemoteFunctions, LocalFunctions>>;
  57. /**
  58. * Hook triggered before an event is sent to the remote
  59. *
  60. * @param req - Request parameters
  61. * @param next - Function to continue the request
  62. * @param resolve - Function to resolve the response directly
  63. */
  64. onRequest?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
  65. /**
  66. * Custom error handler
  67. *
  68. * @deprecated use `onFunctionError` and `onGeneralError` instead
  69. */
  70. onError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
  71. /**
  72. * Custom error handler for errors occurred in local functions being called
  73. *
  74. * @returns `true` to prevent the error from being thrown
  75. */
  76. onFunctionError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
  77. /**
  78. * Custom error handler for errors occurred during serialization or messsaging
  79. *
  80. * @returns `true` to prevent the error from being thrown
  81. */
  82. onGeneralError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName?: string, args?: any[]) => boolean | void;
  83. /**
  84. * Custom error handler for timeouts
  85. *
  86. * @returns `true` to prevent the error from being thrown
  87. */
  88. onTimeoutError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, functionName: string, args: any[]) => boolean | void;
  89. }
  90. type BirpcOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> = EventOptions<RemoteFunctions, LocalFunctions> & ChannelOptions;
  91. type BirpcFn<T> = PromisifyFn<T> & {
  92. /**
  93. * Send event without asking for response
  94. */
  95. asEvent: (...args: ArgumentsType<T>) => Promise<void>;
  96. };
  97. interface BirpcGroupFn<T> {
  98. /**
  99. * Call the remote function and wait for the result.
  100. */
  101. (...args: ArgumentsType<T>): Promise<Awaited<ReturnType<T>>[]>;
  102. /**
  103. * Send event without asking for response
  104. */
  105. asEvent: (...args: ArgumentsType<T>) => Promise<void>;
  106. }
  107. interface BirpcReturnBuiltin<RemoteFunctions, LocalFunctions = Record<string, never>> {
  108. /**
  109. * Raw functions object
  110. */
  111. $functions: LocalFunctions;
  112. /**
  113. * Whether the RPC is closed
  114. */
  115. readonly $closed: boolean;
  116. /**
  117. * Custom meta data attached to the RPC instance
  118. */
  119. readonly $meta: any;
  120. /**
  121. * Close the RPC connection
  122. */
  123. $close: (error?: Error) => void;
  124. /**
  125. * Reject pending calls
  126. */
  127. $rejectPendingCalls: (handler?: PendingCallHandler) => Promise<void>[];
  128. /**
  129. * Call the remote function and wait for the result.
  130. * An alternative to directly calling the function
  131. */
  132. $call: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<Awaited<ReturnType<RemoteFunctions[K]>>>;
  133. /**
  134. * Same as `$call`, but returns `undefined` if the function is not defined on the remote side.
  135. */
  136. $callOptional: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<Awaited<ReturnType<RemoteFunctions[K]> | undefined>>;
  137. /**
  138. * Send event without asking for response
  139. */
  140. $callEvent: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<void>;
  141. /**
  142. * Call the remote function with the raw options.
  143. */
  144. $callRaw: (options: {
  145. method: string;
  146. args: unknown[];
  147. event?: boolean;
  148. optional?: boolean;
  149. }) => Promise<Awaited<ReturnType<any>>[]>;
  150. }
  151. type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {
  152. [K in keyof RemoteFunctions]: BirpcFn<RemoteFunctions[K]>;
  153. } & BirpcReturnBuiltin<RemoteFunctions, LocalFunctions>;
  154. type PendingCallHandler = (options: Pick<PromiseEntry, 'method' | 'reject'>) => void | Promise<void>;
  155. interface BirpcGroupReturnBuiltin<RemoteFunctions> {
  156. /**
  157. * Call the remote function and wait for the result.
  158. * An alternative to directly calling the function
  159. */
  160. $call: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<Awaited<ReturnType<RemoteFunctions[K]>>>;
  161. /**
  162. * Same as `$call`, but returns `undefined` if the function is not defined on the remote side.
  163. */
  164. $callOptional: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<Awaited<ReturnType<RemoteFunctions[K]> | undefined>>;
  165. /**
  166. * Send event without asking for response
  167. */
  168. $callEvent: <K extends keyof RemoteFunctions>(method: K, ...args: ArgumentsType<RemoteFunctions[K]>) => Promise<void>;
  169. }
  170. type BirpcGroupReturn<RemoteFunctions> = {
  171. [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>;
  172. } & BirpcGroupReturnBuiltin<RemoteFunctions>;
  173. interface BirpcGroup<RemoteFunctions, LocalFunctions = Record<string, never>> {
  174. readonly clients: BirpcReturn<RemoteFunctions, LocalFunctions>[];
  175. readonly functions: LocalFunctions;
  176. readonly broadcast: BirpcGroupReturn<RemoteFunctions>;
  177. updateChannels: (fn?: ((channels: ChannelOptions[]) => void)) => BirpcReturn<RemoteFunctions, LocalFunctions>[];
  178. }
  179. interface PromiseEntry {
  180. resolve: (arg: any) => void;
  181. reject: (error: any) => void;
  182. method: string;
  183. timeoutId?: ReturnType<typeof setTimeout>;
  184. }
  185. declare const TYPE_REQUEST: "q";
  186. interface Request {
  187. /**
  188. * Type
  189. */
  190. t: typeof TYPE_REQUEST;
  191. /**
  192. * ID
  193. */
  194. i?: string;
  195. /**
  196. * Method
  197. */
  198. m: string;
  199. /**
  200. * Arguments
  201. */
  202. a: any[];
  203. /**
  204. * Optional
  205. */
  206. o?: boolean;
  207. }
  208. declare const DEFAULT_TIMEOUT = 60000;
  209. declare const setTimeout: typeof globalThis.setTimeout;
  210. declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions, LocalFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
  211. declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
  212. declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions, LocalFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
  213. export { DEFAULT_TIMEOUT, cachedMap, createBirpc, createBirpcGroup };
  214. export type { ArgumentsType, BirpcFn, BirpcGroup, BirpcGroupFn, BirpcGroupReturn, BirpcGroupReturnBuiltin, BirpcOptions, BirpcResolver, BirpcReturn, BirpcReturnBuiltin, ChannelOptions, EventOptions, PromisifyFn, ReturnType, Thenable };