@avonjs/avonjs
Version:
A fluent Node.js API generator.
77 lines (76 loc) • 2.53 kB
JavaScript
"use strict";
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 AvonRequest_1 = __importDefault(require("./AvonRequest"));
const QueryParser_1 = __importDefault(require("./QueryParser"));
class ResourceIndexRequest extends AvonRequest_1.default {
/**
* Indicates type of the request instance.
*/
type() {
return Contracts_1.RequestTypes.ResourceIndexRequest;
}
/**
* Get the paginator instance for the index request.
*/
async searchIndex() {
const repository = await this.resource().search(this, this.filters(), this.orderings(), this.trashed());
const { items, count } = await repository.search(this.string('search', ''), this.currentPage(), this.perPage());
await Promise.all(this.resource()
.indexFields(this, this.model())
.onlyLoadedLazyFields()
.map((field) => field.resolveForResources(this, items)));
return {
count,
resources: await Promise.all(items.map((item) => this.newResource(item).serializeForIndex(this))),
};
}
/**
* Get the page number.
*/
currentPage() {
return this.number('page', 1);
}
/**
* Get per page.
*/
perPage() {
const perPageOptions = this.resource().perPageOptions();
const perPage = this.number('perPage');
return perPageOptions.includes(perPage) ? perPage : perPageOptions[0];
}
/**
* Get the filters for the request.
*/
filters() {
return new QueryParser_1.default(this.query('filters', []), this.availableFilters()).matches();
}
/**
* Get all of the possibly available filters for the request.
*/
availableFilters() {
return this.resource().availableFilters(this);
}
/**
* Check if filter found in request.
*/
hasFilter(key) {
return this.exists(`filters.${key}`);
}
/**
* Get the orderings for the request.
*/
orderings() {
return new QueryParser_1.default(this.query('orders', []), this.availableOrderings()).matches();
}
/**
* Get all of the possibly available orderings for the request.
*/
availableOrderings() {
return this.resource().availableOrderings(this);
}
}
exports.default = ResourceIndexRequest;