@n4it/crud-policy
Version:
NestJs CRUD for RESTful APIs - policy
27 lines • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRequestEntityIdGetter = exports.isSameIdInBodyAndParams = exports.Error = void 0;
const common_1 = require("@nestjs/common");
var Error;
(function (Error) {
Error["ID_MISMATCH"] = "Requested id does not match";
})(Error || (exports.Error = Error = {}));
const isSameIdInBodyAndParams = (bodyId, paramsId) => {
if (bodyId && paramsId && bodyId !== paramsId) {
return false;
}
return true;
};
exports.isSameIdInBodyAndParams = isSameIdInBodyAndParams;
const createRequestEntityIdGetter = (getIdFromBody, getIdFromParams) => {
return (params, body) => {
const idFromBody = getIdFromBody(body);
const idFromParams = getIdFromParams(params);
if (!(0, exports.isSameIdInBodyAndParams)(idFromBody, idFromParams)) {
throw new common_1.UnauthorizedException(Error.ID_MISMATCH);
}
return idFromBody ?? idFromParams;
};
};
exports.createRequestEntityIdGetter = createRequestEntityIdGetter;
//# sourceMappingURL=get-relevant-id.js.map