index.mjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 { SchemaType } from 'tsbuffer-schema';
  9. import { __assign } from 'tslib';
  10. var empty = function () { };
  11. function setLogLevel(logger, logLevel) {
  12. switch (logLevel) {
  13. case 'none':
  14. return { debug: empty, log: empty, warn: empty, error: empty };
  15. case 'error':
  16. return { debug: empty, log: empty, warn: empty, error: logger.error.bind(logger) };
  17. case 'warn':
  18. return { debug: empty, log: empty, warn: logger.warn.bind(logger), error: logger.error.bind(logger) };
  19. case 'info':
  20. return { debug: empty, log: logger.log.bind(logger), warn: logger.warn.bind(logger), error: logger.error.bind(logger) };
  21. case 'debug':
  22. return logger;
  23. default:
  24. throw new Error("Invalid logLevel: '".concat(logLevel, "'"));
  25. }
  26. }
  27. var TsrpcErrorType;
  28. (function (TsrpcErrorType) {
  29. /** Network error, like connection broken, network timeout, etc. */
  30. TsrpcErrorType["NetworkError"] = "NetworkError";
  31. /**
  32. * Server exception, for example "request format error", "database exception", etc.
  33. *
  34. * @remarks
  35. * This error message may be not suitable to show to user,
  36. * but the error info is useful for engineer to find some bug.
  37. * So you can show a user-friendly message to user (like "System error, please contact XXX"),
  38. * and report some debug info at the same time.
  39. */
  40. TsrpcErrorType["ServerError"] = "ServerError";
  41. /** Client exception, for example parse server output error.
  42. * (May because of the proto file is not the same between server and client)
  43. */
  44. TsrpcErrorType["ClientError"] = "ClientError";
  45. /**
  46. * The business error returned by `call.error`.
  47. * It is always business-relatived, for example `call.error('Password is incorrect')`, `call.error('Not enough credit')`, etc.
  48. */
  49. TsrpcErrorType["ApiError"] = "ApiError";
  50. })(TsrpcErrorType || (TsrpcErrorType = {}));
  51. var TransportDataProto = {
  52. "ServerInputData": {
  53. "type": SchemaType.Interface,
  54. "properties": [
  55. {
  56. "id": 0,
  57. "name": "serviceId",
  58. "type": {
  59. "type": SchemaType.Number,
  60. "scalarType": "uint"
  61. }
  62. },
  63. {
  64. "id": 1,
  65. "name": "buffer",
  66. "type": {
  67. "type": SchemaType.Buffer,
  68. "arrayType": "Uint8Array"
  69. }
  70. },
  71. {
  72. "id": 2,
  73. "name": "sn",
  74. "type": {
  75. "type": SchemaType.Number,
  76. "scalarType": "uint"
  77. },
  78. "optional": true
  79. }
  80. ]
  81. },
  82. "ServerOutputData": {
  83. "type": SchemaType.Interface,
  84. "properties": [
  85. {
  86. "id": 0,
  87. "name": "buffer",
  88. "type": {
  89. "type": SchemaType.Buffer,
  90. "arrayType": "Uint8Array"
  91. },
  92. "optional": true
  93. },
  94. {
  95. "id": 1,
  96. "name": "error",
  97. "type": {
  98. "type": SchemaType.Reference,
  99. "target": "TsrpcErrorData"
  100. },
  101. "optional": true
  102. },
  103. {
  104. "id": 2,
  105. "name": "serviceId",
  106. "type": {
  107. "type": SchemaType.Number,
  108. "scalarType": "uint"
  109. },
  110. "optional": true
  111. },
  112. {
  113. "id": 3,
  114. "name": "sn",
  115. "type": {
  116. "type": SchemaType.Number,
  117. "scalarType": "uint"
  118. },
  119. "optional": true
  120. }
  121. ]
  122. },
  123. "TsrpcErrorData": {
  124. "type": SchemaType.Interface,
  125. "properties": [
  126. {
  127. "id": 0,
  128. "name": "message",
  129. "type": {
  130. "type": SchemaType.String
  131. }
  132. },
  133. {
  134. "id": 1,
  135. "name": "type",
  136. "type": {
  137. "type": SchemaType.Reference,
  138. "target": "TsrpcErrorType"
  139. }
  140. },
  141. {
  142. "id": 2,
  143. "name": "code",
  144. "type": {
  145. "type": SchemaType.Union,
  146. "members": [
  147. {
  148. "id": 0,
  149. "type": {
  150. "type": SchemaType.String
  151. }
  152. },
  153. {
  154. "id": 1,
  155. "type": {
  156. "type": SchemaType.Number,
  157. "scalarType": "int"
  158. }
  159. }
  160. ]
  161. },
  162. "optional": true
  163. }
  164. ],
  165. "indexSignature": {
  166. "keyType": "String",
  167. "type": {
  168. "type": SchemaType.Any
  169. }
  170. }
  171. },
  172. "TsrpcErrorType": {
  173. "type": SchemaType.Enum,
  174. "members": [
  175. {
  176. "id": 0,
  177. "value": "NetworkError"
  178. },
  179. {
  180. "id": 1,
  181. "value": "ServerError"
  182. },
  183. {
  184. "id": 2,
  185. "value": "ClientError"
  186. },
  187. {
  188. "id": 3,
  189. "value": "ApiError"
  190. }
  191. ]
  192. }
  193. };
  194. /**
  195. * A unified Error that returned by TSRPC server or client
  196. *
  197. * @remarks
  198. * It has many uses, for example:
  199. *
  200. * 1. You can handle business errors and network errors uniformly.
  201. * 2. In API handle process, `throw new TsrpcError('xxx')` would return the same error to client directly (like `call.error()`),
  202. * while `throw new Error('XXX')` would return a unified "Server Internal Error".
  203. */
  204. var TsrpcError = /** @class */ (function () {
  205. function TsrpcError(dataOrMessage, data) {
  206. var _a;
  207. if (typeof dataOrMessage === 'string') {
  208. this.message = dataOrMessage;
  209. this.type = (_a = data === null || data === void 0 ? void 0 : data.type) !== null && _a !== void 0 ? _a : TsrpcErrorType.ApiError;
  210. __assign(this, data);
  211. }
  212. else {
  213. __assign(this, dataOrMessage);
  214. }
  215. }
  216. TsrpcError.prototype.toString = function () {
  217. return "[TSRPC ".concat(this.type, "]: ").concat(this.message);
  218. };
  219. TsrpcError.Type = TsrpcErrorType;
  220. return TsrpcError;
  221. }());
  222. export { TransportDataProto, TsrpcError, TsrpcErrorType, setLogLevel };