@avonjs/avonjs
Version:
A fluent Node.js API generator.
50 lines (49 loc) • 1.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Contracts_1 = require("../../Contracts");
const ActionNotFoundException_1 = __importDefault(require("../../Exceptions/ActionNotFoundException"));
const MethodNotAllowedException_1 = __importDefault(require("../../Exceptions/MethodNotAllowedException"));
const ModelNotFoundException_1 = __importDefault(require("../../Exceptions/ModelNotFoundException"));
const AvonRequest_1 = __importDefault(require("./AvonRequest"));
class ActionRequest extends AvonRequest_1.default {
/**
* Indicates type of the request instance.
*/
type() {
return Contracts_1.RequestTypes.ActionRequest;
}
/**
* Get the action instance for the request or abort.
*/
action() {
const action = this.resource()
.availableActions(this)
.find((action) => action.uriKey() === this.route('actionName'));
ActionNotFoundException_1.default.unless(action);
MethodNotAllowedException_1.default.when(action.isDestructive() && !this.isMethod('delete'));
MethodNotAllowedException_1.default.when(!action.isDestructive() && this.isMethod('delete'));
return action;
}
/**
* Get the selected models for the action.
*/
async models() {
const models = await this.repository().whereKeys(this.resourceIds()).all();
ModelNotFoundException_1.default.when(models.length === 0 && this.action().isInline());
return models;
}
/**
* Get resource IDs from query.
*/
resourceIds() {
if (this.action().isInline()) {
return [this.resourceId()];
}
const resourceIds = this.get('resources', []);
return Array.isArray(resourceIds) ? resourceIds : [resourceIds];
}
}
exports.default = ActionRequest;