@tunnel-cast/nestjs
Version:
## About This is a Tunnel-Cast - NestJS integration package, wrap the `cast` application into the `NestJS` building blocks.
54 lines • 2.67 kB
JavaScript
;
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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CastInterceptorFactory = exports.CastInterceptor = void 0;
const common_1 = require("@nestjs/common");
const constants_1 = require("../constants");
const errors_1 = require("../errors");
const cast_1 = require("@tunnel-cast/common/cast");
const storage_1 = require("../storage");
function CastInterceptor(model, fieldKey) {
let MixinInterceptor = class MixinInterceptor {
constructor(storage, options) {
this.storage = storage;
this.options = options;
}
async intercept(context, next) {
const ctx = context.switchToHttp();
const req = ctx.getRequest();
const res = ctx.getResponse();
const castResult = cast_1.cast(model, req[fieldKey]);
if (castResult.errors) {
throw new errors_1.InvalidCastException(this.options.transformError(castResult.errors));
}
else {
req[fieldKey] = castResult.value;
}
return next.handle();
}
};
MixinInterceptor = __decorate([
__param(0, common_1.Inject(constants_1.CAST_METADATA_STORAGE)),
__param(1, common_1.Optional()), __param(1, common_1.Inject(constants_1.CAST_MODULE_OPTIONS)),
__metadata("design:paramtypes", [storage_1.MetadataStorage, Object])
], MixinInterceptor);
const Interceptor = common_1.mixin(MixinInterceptor);
return Interceptor;
}
exports.CastInterceptor = CastInterceptor;
function CastInterceptorFactory(fieldKey) {
return (model) => CastInterceptor(model, fieldKey);
}
exports.CastInterceptorFactory = CastInterceptorFactory;
//# sourceMappingURL=cast-interceptor-factory.js.map