123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*!
- * TSRPC Proto v1.4.3
- * -----------------------------------------
- * Copyright (c) Kingworks Corporation.
- * MIT License
- * https://github.com/k8w/tsrpc-proto
- */
- 'use strict';
- Object.defineProperty(exports, '__esModule', { value: true });
- var tsbufferSchema = require('tsbuffer-schema');
- var tslib = require('tslib');
- var empty = function () { };
- function setLogLevel(logger, logLevel) {
- switch (logLevel) {
- case 'none':
- return { debug: empty, log: empty, warn: empty, error: empty };
- case 'error':
- return { debug: empty, log: empty, warn: empty, error: logger.error.bind(logger) };
- case 'warn':
- return { debug: empty, log: empty, warn: logger.warn.bind(logger), error: logger.error.bind(logger) };
- case 'info':
- return { debug: empty, log: logger.log.bind(logger), warn: logger.warn.bind(logger), error: logger.error.bind(logger) };
- case 'debug':
- return logger;
- default:
- throw new Error("Invalid logLevel: '".concat(logLevel, "'"));
- }
- }
- exports.TsrpcErrorType = void 0;
- (function (TsrpcErrorType) {
- /** Network error, like connection broken, network timeout, etc. */
- TsrpcErrorType["NetworkError"] = "NetworkError";
- /**
- * Server exception, for example "request format error", "database exception", etc.
- *
- * @remarks
- * This error message may be not suitable to show to user,
- * but the error info is useful for engineer to find some bug.
- * So you can show a user-friendly message to user (like "System error, please contact XXX"),
- * and report some debug info at the same time.
- */
- TsrpcErrorType["ServerError"] = "ServerError";
- /** Client exception, for example parse server output error.
- * (May because of the proto file is not the same between server and client)
- */
- TsrpcErrorType["ClientError"] = "ClientError";
- /**
- * The business error returned by `call.error`.
- * It is always business-relatived, for example `call.error('Password is incorrect')`, `call.error('Not enough credit')`, etc.
- */
- TsrpcErrorType["ApiError"] = "ApiError";
- })(exports.TsrpcErrorType || (exports.TsrpcErrorType = {}));
- var TransportDataProto = {
- "ServerInputData": {
- "type": tsbufferSchema.SchemaType.Interface,
- "properties": [
- {
- "id": 0,
- "name": "serviceId",
- "type": {
- "type": tsbufferSchema.SchemaType.Number,
- "scalarType": "uint"
- }
- },
- {
- "id": 1,
- "name": "buffer",
- "type": {
- "type": tsbufferSchema.SchemaType.Buffer,
- "arrayType": "Uint8Array"
- }
- },
- {
- "id": 2,
- "name": "sn",
- "type": {
- "type": tsbufferSchema.SchemaType.Number,
- "scalarType": "uint"
- },
- "optional": true
- }
- ]
- },
- "ServerOutputData": {
- "type": tsbufferSchema.SchemaType.Interface,
- "properties": [
- {
- "id": 0,
- "name": "buffer",
- "type": {
- "type": tsbufferSchema.SchemaType.Buffer,
- "arrayType": "Uint8Array"
- },
- "optional": true
- },
- {
- "id": 1,
- "name": "error",
- "type": {
- "type": tsbufferSchema.SchemaType.Reference,
- "target": "TsrpcErrorData"
- },
- "optional": true
- },
- {
- "id": 2,
- "name": "serviceId",
- "type": {
- "type": tsbufferSchema.SchemaType.Number,
- "scalarType": "uint"
- },
- "optional": true
- },
- {
- "id": 3,
- "name": "sn",
- "type": {
- "type": tsbufferSchema.SchemaType.Number,
- "scalarType": "uint"
- },
- "optional": true
- }
- ]
- },
- "TsrpcErrorData": {
- "type": tsbufferSchema.SchemaType.Interface,
- "properties": [
- {
- "id": 0,
- "name": "message",
- "type": {
- "type": tsbufferSchema.SchemaType.String
- }
- },
- {
- "id": 1,
- "name": "type",
- "type": {
- "type": tsbufferSchema.SchemaType.Reference,
- "target": "TsrpcErrorType"
- }
- },
- {
- "id": 2,
- "name": "code",
- "type": {
- "type": tsbufferSchema.SchemaType.Union,
- "members": [
- {
- "id": 0,
- "type": {
- "type": tsbufferSchema.SchemaType.String
- }
- },
- {
- "id": 1,
- "type": {
- "type": tsbufferSchema.SchemaType.Number,
- "scalarType": "int"
- }
- }
- ]
- },
- "optional": true
- }
- ],
- "indexSignature": {
- "keyType": "String",
- "type": {
- "type": tsbufferSchema.SchemaType.Any
- }
- }
- },
- "TsrpcErrorType": {
- "type": tsbufferSchema.SchemaType.Enum,
- "members": [
- {
- "id": 0,
- "value": "NetworkError"
- },
- {
- "id": 1,
- "value": "ServerError"
- },
- {
- "id": 2,
- "value": "ClientError"
- },
- {
- "id": 3,
- "value": "ApiError"
- }
- ]
- }
- };
- /**
- * A unified Error that returned by TSRPC server or client
- *
- * @remarks
- * It has many uses, for example:
- *
- * 1. You can handle business errors and network errors uniformly.
- * 2. In API handle process, `throw new TsrpcError('xxx')` would return the same error to client directly (like `call.error()`),
- * while `throw new Error('XXX')` would return a unified "Server Internal Error".
- */
- var TsrpcError = /** @class */ (function () {
- function TsrpcError(dataOrMessage, data) {
- var _a;
- if (typeof dataOrMessage === 'string') {
- this.message = dataOrMessage;
- this.type = (_a = data === null || data === void 0 ? void 0 : data.type) !== null && _a !== void 0 ? _a : exports.TsrpcErrorType.ApiError;
- tslib.__assign(this, data);
- }
- else {
- tslib.__assign(this, dataOrMessage);
- }
- }
- TsrpcError.prototype.toString = function () {
- return "[TSRPC ".concat(this.type, "]: ").concat(this.message);
- };
- TsrpcError.Type = exports.TsrpcErrorType;
- return TsrpcError;
- }());
- exports.TransportDataProto = TransportDataProto;
- exports.TsrpcError = TsrpcError;
- exports.setLogLevel = setLogLevel;
|