UNPKG

nestjs-chainlist

Version:

chainlist module for Nest framework (node.js)

59 lines 2.89 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChainsPipe = void 0; const common_1 = require("@nestjs/common"); const chains_service_1 = require("./chains.service"); let ChainsPipe = class ChainsPipe { toChainValue(val) { return isNaN(+val) ? val : +val; } transform(value, metadata) { var _a, _b, _c; const options = metadata.data; const supportChains = (_a = options.supportChains) !== null && _a !== void 0 ? _a : this.chainsService.globalChainList.getChainIds(); const list = this.chainsService.createChainList(supportChains); if (options.supportChainList) { const _list = this.chainsService.getChainList(options.supportChainList); _list.forEach((chain) => list.add(chain)); } if ((_b = options.unsupportChains) === null || _b === void 0 ? void 0 : _b.length) { options.unsupportChains.forEach((chain) => list.del(chain)); } const required = (_c = options.required) !== null && _c !== void 0 ? _c : true; if (Array.isArray(value)) { if (required) { const invalidValues = value.filter((val) => !list.include(this.toChainValue(val))); if (invalidValues.length > 0) { throw new common_1.BadRequestException(`Invalid chain values: ${invalidValues.join(', ')}`); } } return value.map(val => this.chainsService.getChain(val)); } else { if (required) { if (!list.include(this.toChainValue(value))) { throw new common_1.BadRequestException(`Invalid chain value: ${value}`); } } return this.chainsService.getChain(value); } } }; exports.ChainsPipe = ChainsPipe; __decorate([ (0, common_1.Inject)(), __metadata("design:type", chains_service_1.ChainsService) ], ChainsPipe.prototype, "chainsService", void 0); exports.ChainsPipe = ChainsPipe = __decorate([ (0, common_1.Injectable)() ], ChainsPipe); //# sourceMappingURL=chains.pipe.js.map