index.d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. * TSRPC Proto v1.4.3
  3. * -----------------------------------------
  4. * Copyright (c) Kingworks Corporation.
  5. * MIT License
  6. * https://github.com/k8w/tsrpc-proto
  7. */
  8. import { bigint64 } from 'tsbuffer-schema';
  9. import { biguint64 } from 'tsbuffer-schema';
  10. import { int } from 'tsbuffer-schema';
  11. import { Overwrite } from 'tsbuffer-schema';
  12. import { TSBufferProto } from 'tsbuffer-schema';
  13. import { TSBufferSchema } from 'tsbuffer-schema';
  14. import { uint } from 'tsbuffer-schema';
  15. /**
  16. * The return of `client.callApi()`
  17. */
  18. export declare type ApiReturn<Res> = ApiReturnSucc<Res> | ApiReturnError;
  19. /**
  20. * Unified error, include network error, business error, code exception etc.
  21. */
  22. export declare interface ApiReturnError {
  23. isSucc: false;
  24. res?: undefined;
  25. err: TsrpcError;
  26. }
  27. /**
  28. * Successful by `call.succ()`
  29. */
  30. export declare interface ApiReturnSucc<Res> {
  31. isSucc: true;
  32. res: Res;
  33. err?: undefined;
  34. }
  35. /**
  36. * Send request and wait for response
  37. * @remarks
  38. * SchemaId of request and ressponse is generated by client, named with the prefix `Req` or `Res`.
  39. */
  40. export declare interface ApiServiceDef extends BaseServiceDef {
  41. type: 'api';
  42. /**
  43. * Auto generated by `tsrpc-cli`
  44. * @example
  45. * ```ts title="PtlAddComment.ts"
  46. * export interface ReqAddComment {
  47. * articleId: string;
  48. * comment: string;
  49. * }
  50. *
  51. * export interface ResAddComment {
  52. * commentId: string;
  53. * }
  54. *
  55. * // This would be auto generated to `service.conf`
  56. * export const conf = {
  57. * needLogin: true,
  58. * needRoles: ['SuperAdmin', 'ArticleAdmin', 'CommentAdmin']
  59. * };
  60. * ```
  61. */
  62. conf?: {
  63. [key: string]: any;
  64. };
  65. }
  66. export declare interface BaseServiceDef {
  67. id: number;
  68. name: string;
  69. }
  70. /**
  71. * API service request and response type, and Msg service type.
  72. * For coding auto hint.
  73. */
  74. export declare interface BaseServiceType {
  75. /** Send a request, and wait for a response */
  76. api: {
  77. [apiName: string]: {
  78. /** Request type */
  79. req: any;
  80. /** Response type */
  81. res: any;
  82. };
  83. };
  84. /** Msg service, listen or send one-way msg without response */
  85. msg: {
  86. /** Msg type */
  87. [msgName: string]: any;
  88. };
  89. }
  90. export { bigint64 }
  91. export { biguint64 }
  92. export { int }
  93. /**
  94. * An abstract logger interface, which can be used to customize log behaviour.
  95. * Usually, you can pass `console` for convinience.
  96. * Or you can write your own implementation, for example, to report to a log system, or hide some log output.
  97. */
  98. export declare interface Logger {
  99. debug(...args: any[]): void;
  100. log(...args: any[]): void;
  101. warn(...args: any[]): void;
  102. error(...args: any[]): void;
  103. }
  104. export declare type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';
  105. /**
  106. * Send or listen specific data, without waiting for response.
  107. */
  108. export declare interface MsgServiceDef extends BaseServiceDef {
  109. type: 'msg';
  110. conf?: {
  111. [key: string]: any;
  112. };
  113. }
  114. export { Overwrite }
  115. /**
  116. * Basic transport data unit,
  117. * which represents data that server received, which should be sent by `client.callApi` or `client.sendMsg`.
  118. */
  119. export declare interface ServerInputData {
  120. serviceId: uint;
  121. buffer: Uint8Array;
  122. /** Short link don't need this */
  123. sn?: uint;
  124. }
  125. /**
  126. * Basic transport data unit,
  127. * which represents data that server sent by `call.succ` or `call.error` or `conn.sendMsg`.
  128. */
  129. export declare interface ServerOutputData {
  130. /** ApiResponse or Msg */
  131. buffer?: Uint8Array;
  132. /** Api Error, cannot exists at the same time with `buffer` */
  133. error?: TsrpcErrorData;
  134. /** Short link apiRes don't need this */
  135. serviceId?: uint;
  136. /** Short link don't need this */
  137. sn?: uint;
  138. }
  139. export declare type ServiceDef = ApiServiceDef | MsgServiceDef;
  140. /**
  141. * TSRPC Server Protocol Definitions
  142. * @typeParam ServiceType - API request and response types, and Msg types.
  143. */
  144. export declare interface ServiceProto<ServiceType extends BaseServiceType = any> {
  145. version?: number;
  146. /**
  147. * Service is the basic interactive unit for server and client.
  148. * Include {@link ApiServiceDef} and {@link MsgServiceDef}.
  149. */
  150. services: ServiceDef[];
  151. /**
  152. * `TSBufferProto` that includes all types used by the services.
  153. * @see
  154. * {@link tsbuffer-schema#TSBufferProto | TSBufferProto}
  155. * {@link tsbuffer-schema#TSBufferSchema | TSBufferSchema}
  156. */
  157. types: TSBufferProto;
  158. /** For IntelliSense in VSCode */
  159. __SERVICE_TYPE__?: ServiceType;
  160. }
  161. export declare function setLogLevel(logger: Logger, logLevel: LogLevel): Logger;
  162. export declare const TransportDataProto: {
  163. ServerInputData: TSBufferSchema;
  164. ServerOutputData: TSBufferSchema;
  165. [key: string]: TSBufferSchema;
  166. };
  167. /**
  168. * A unified Error that returned by TSRPC server or client
  169. *
  170. * @remarks
  171. * It has many uses, for example:
  172. *
  173. * 1. You can handle business errors and network errors uniformly.
  174. * 2. In API handle process, `throw new TsrpcError('xxx')` would return the same error to client directly (like `call.error()`),
  175. * while `throw new Error('XXX')` would return a unified "Server Internal Error".
  176. */
  177. export declare class TsrpcError implements TsrpcErrorData {
  178. static Type: typeof TsrpcErrorType;
  179. message: string;
  180. type: TsrpcErrorType;
  181. code?: string | int;
  182. [key: string]: any;
  183. constructor(data: TsrpcErrorData);
  184. /**
  185. * The `type` is `ApiError` by default
  186. */
  187. constructor(message: string, data?: Partial<TsrpcErrorData>);
  188. toString(): string;
  189. }
  190. export declare interface TsrpcErrorData {
  191. message: string;
  192. /**
  193. * @defaultValue ApiError
  194. */
  195. type: TsrpcErrorType;
  196. code?: string | int;
  197. [key: string]: any;
  198. }
  199. export declare enum TsrpcErrorType {
  200. /** Network error, like connection broken, network timeout, etc. */
  201. NetworkError = "NetworkError",
  202. /**
  203. * Server exception, for example "request format error", "database exception", etc.
  204. *
  205. * @remarks
  206. * This error message may be not suitable to show to user,
  207. * but the error info is useful for engineer to find some bug.
  208. * So you can show a user-friendly message to user (like "System error, please contact XXX"),
  209. * and report some debug info at the same time.
  210. */
  211. ServerError = "ServerError",
  212. /** Client exception, for example parse server output error.
  213. * (May because of the proto file is not the same between server and client)
  214. */
  215. ClientError = "ClientError",
  216. /**
  217. * The business error returned by `call.error`.
  218. * It is always business-relatived, for example `call.error('Password is incorrect')`, `call.error('Not enough credit')`, etc.
  219. */
  220. ApiError = "ApiError"
  221. }
  222. export { uint }
  223. export { }