UNPKG

@venly/venly-core-sdk

Version:

Javascrip/Typescript SDK for Venly's Web3 Services

164 lines 6.65 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var VyApiResponseError_1, VyApiResponse_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.VyApiResponse = exports.VyApiResponseBase = exports.VyPagination = exports.VyApiResponseError = exports.VyApiResponseErrorConverter = void 0; const json_1 = require("../helpers/json"); let VyApiResponseErrorConverter = class VyApiResponseErrorConverter extends json_1.JsonConverterBase { constructor() { super(false, true); } deserialize(json, expectedType, settings) { var result = json_1.JsonConvert.deserializeObject(json, VyApiResponseError, settings, true); if (json.errorCode != null) result.code = json.errorCode; if (json.errorMessage != null) result.message = json.errorMessage; return result; } }; exports.VyApiResponseErrorConverter = VyApiResponseErrorConverter; exports.VyApiResponseErrorConverter = VyApiResponseErrorConverter = __decorate([ json_1.JsonConverter, __metadata("design:paramtypes", []) ], VyApiResponseErrorConverter); let VyApiResponseError = VyApiResponseError_1 = class VyApiResponseError { // public toString = () : string => { // return JSON.stringify(this); // } static fromError(reason) { if (reason instanceof Error) { return new VyApiResponseError_1({ code: reason.name, message: reason.message }); } return new VyApiResponseError_1({ code: 'error', message: reason }); } constructor(initializer) { if (initializer != null) Object.assign(this, initializer); } }; exports.VyApiResponseError = VyApiResponseError; __decorate([ (0, json_1.JsonProperty)("code", String), __metadata("design:type", String) ], VyApiResponseError.prototype, "code", void 0); __decorate([ (0, json_1.JsonProperty)("traceCode", String), __metadata("design:type", String) ], VyApiResponseError.prototype, "traceCode", void 0); __decorate([ (0, json_1.JsonProperty)("message", String), __metadata("design:type", String) ], VyApiResponseError.prototype, "message", void 0); __decorate([ (0, json_1.JsonProperty)("id", String), __metadata("design:type", String) ], VyApiResponseError.prototype, "id", void 0); exports.VyApiResponseError = VyApiResponseError = VyApiResponseError_1 = __decorate([ (0, json_1.JsonObject)(VyApiResponseErrorConverter), __metadata("design:paramtypes", [Object]) ], VyApiResponseError); let VyPagination = class VyPagination { }; exports.VyPagination = VyPagination; __decorate([ (0, json_1.JsonProperty)("pageNumber", Number), __metadata("design:type", Number) ], VyPagination.prototype, "pageNumber", void 0); __decorate([ (0, json_1.JsonProperty)("pageSize", Number), __metadata("design:type", Number) ], VyPagination.prototype, "pageSize", void 0); __decorate([ (0, json_1.JsonProperty)("numberOfPages", Number), __metadata("design:type", Number) ], VyPagination.prototype, "numberOfPages", void 0); __decorate([ (0, json_1.JsonProperty)("numberOfElements", Number), __metadata("design:type", Number) ], VyPagination.prototype, "numberOfElements", void 0); __decorate([ (0, json_1.JsonProperty)("hasNextPage", Boolean), __metadata("design:type", Boolean) ], VyPagination.prototype, "hasNextPage", void 0); __decorate([ (0, json_1.JsonProperty)("hasPreviousPage", Boolean), __metadata("design:type", Boolean) ], VyPagination.prototype, "hasPreviousPage", void 0); exports.VyPagination = VyPagination = __decorate([ (0, json_1.JsonObject)() ], VyPagination); let VyApiResponseBase = class VyApiResponseBase { constructor() { this.getError = () => new Error(this.errorsToStr()); this.errorsToStr = () => { var _a; if (this.success) return null; var errMsg = `Response contains ${(_a = this.errors) === null || _a === void 0 ? void 0 : _a.length} error(s) :`; if (this.errors == null) return errMsg; //return new Error(errMsg); for (var i = 0; i < this.errors.length; ++i) { var currErr = this.errors[i]; errMsg += `\n*[${i}] ${JSON.stringify(currErr)}`; } return errMsg; //return new Error(errMsg); }; } }; exports.VyApiResponseBase = VyApiResponseBase; __decorate([ (0, json_1.JsonProperty)('success', Boolean), __metadata("design:type", Boolean) ], VyApiResponseBase.prototype, "success", void 0); __decorate([ (0, json_1.JsonProperty)('pagination'), __metadata("design:type", VyPagination) ], VyApiResponseBase.prototype, "pagination", void 0); __decorate([ (0, json_1.JsonProperty)('errors'), __metadata("design:type", Array) ], VyApiResponseBase.prototype, "errors", void 0); exports.VyApiResponseBase = VyApiResponseBase = __decorate([ (0, json_1.JsonObject)() ], VyApiResponseBase); let VyApiResponse = VyApiResponse_1 = class VyApiResponse extends VyApiResponseBase { static Success(data) { var response = new VyApiResponse_1(); response.success = true; response.statusCode = 200; response.data = data; return response; } static Fail(reason, statusCode = 400) { var response = new VyApiResponse_1(); response.success = false; response.statusCode = statusCode; response.errors = [VyApiResponseError.fromError(reason)]; return response; } }; exports.VyApiResponse = VyApiResponse; __decorate([ (0, json_1.JsonProperty)('data'), __metadata("design:type", Object) ], VyApiResponse.prototype, "data", void 0); exports.VyApiResponse = VyApiResponse = VyApiResponse_1 = __decorate([ (0, json_1.JsonObject)() ], VyApiResponse); //# sourceMappingURL=VyApiResponse.js.map