index.js 7.5 KB

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