@nestia/core
Version:
Super-fast validation decorators of NestJS
51 lines • 2.41 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlainBody = PlainBody;
const common_1 = require("@nestjs/common");
const get_text_body_1 = require("./internal/get_text_body");
const is_request_body_undefined_1 = require("./internal/is_request_body_undefined");
const validate_request_body_1 = require("./internal/validate_request_body");
/** @internal */
function PlainBody(assert) {
const checker = assert
? (0, validate_request_body_1.validate_request_body)("PlainBody")({
type: "assert",
assert,
})
: null;
return (0, common_1.createParamDecorator)(function PlainBody(_data, context) {
return __awaiter(this, void 0, void 0, function* () {
const request = context
.switchToHttp()
.getRequest();
if ((0, is_request_body_undefined_1.is_request_body_undefined)(request) &&
(checker !== null && checker !== void 0 ? checker : (() => null))(undefined) === null)
return undefined;
else if (!isTextPlain(request.headers["content-type"]))
throw new common_1.BadRequestException(`Request body type is not "text/plain".`);
const value = yield (0, get_text_body_1.get_text_body)(request);
if (checker) {
const error = checker(value);
if (error !== null)
throw error;
}
return value;
});
})();
}
/** @internal */
const isTextPlain = (text) => text !== undefined &&
text
.split(";")
.map((str) => str.trim())
.some((str) => str === "text/plain");
//# sourceMappingURL=PlainBody.js.map