index.js 8.7 KB

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