@avonjs/avonjs
Version:
A fluent Node.js API generator.
163 lines (162 loc) • 5.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const collect_js_1 = require("collect.js");
const Fields_1 = require("../Fields");
const Relation_1 = __importDefault(require("../Fields/Relation"));
class FieldCollection extends collect_js_1.Collection {
/**
* Find a given field by its attribute.
*/
findFieldByAttribute(attribute, defaultValue) {
return this.first((field) => field.attribute === attribute, defaultValue);
}
/**
* Resolve value of fields.
*/
resolve(resource) {
return new FieldCollection(this.each((field) => field.resolve(resource)));
}
/**
* Resolve value of fields for display.
*/
resolveForDisplay(resource) {
return new FieldCollection(this.each((field) => field.resolveForDisplay(resource)));
}
/**
* Remove non-creation fields from the collection.
*/
onlyCreationFields(request) {
return new FieldCollection(this.filter((field) => field.isShownOnCreation(request)));
}
/**
* Remove non-update fields from the collection.
*/
onlyUpdateFields(request, resource) {
return new FieldCollection(this.filter((field) => field.isShownOnUpdate(request, resource)));
}
/**
* Filter fields for showing on index.
*/
filterForIndex(request, resource) {
return new FieldCollection(this.filter((field) => field.isShownOnIndex(request, resource)).values());
}
/**
* Filter fields for showing on detail.
*/
filterForDetail(request, resource) {
return new FieldCollection(this.filter((field) => field.isShownOnDetail(request, resource)).values());
}
/**
* Filter fields for showing on review.
*/
filterForReview(request, resource) {
return new FieldCollection(this.filter((field) => field.isShownOnReview(request, resource)).values());
}
/**
* Filter fields for showing on review.
*/
filterForAssociation(request) {
return new FieldCollection(this.filter((field) => field.isShownOnAssociation(request)).values());
}
/**
* Reject if the field supports Filterable Field.
*/
withOnlyFilterableFields() {
return new FieldCollection(this.filter((field) => field.isFilterable()).values());
}
/**
* Reject if the field supports Orderable Field.
*/
withOnlyOrderableFields() {
return new FieldCollection(this.filter((field) => field.isOrderable()).values());
}
/**
* Reject if the field not supports Relatable Field.
*/
withOnlyRelatableFields() {
return new FieldCollection(this.filter((field) => field instanceof Relation_1.default).values());
}
/**
* Reject if the field not supports Relatable Field and not eager loaded.
*/
onlyLoadedRelatableFields() {
return new FieldCollection(this.withOnlyRelatableFields()
.filter((field) => field.isLoaded())
.values());
}
/**
* Reject if the field is relatable Field.
*/
withoutRelatableFields() {
return new FieldCollection(this.reject((field) => field instanceof Relation_1.default).values());
}
/**
* Reject if the field not supports Lazy Field.
*/
withOnlyLazyFields() {
return new FieldCollection(this.filter((field) => field instanceof Fields_1.Lazy).values());
}
/**
* Reject if the field not supports Lazy Field and not eager loaded.
*/
onlyLoadedLazyFields() {
return new FieldCollection(this.withOnlyLazyFields()
.filter((field) => field.isLoaded())
.values());
}
/**
* Reject if the field is Lazy Field.
*/
withoutLazyFields() {
return new FieldCollection(this.reject((field) => field instanceof Fields_1.Lazy).values());
}
/**
* Reject if the field is relatable Field.
*/
withoutUnfillableFields() {
return new FieldCollection(this.filter((field) => field.fillable()).values());
}
/**
* Reject if the field is relatable Field.
*/
withoutUnresolvableFields() {
return new FieldCollection(this.filter((field) => field.resolvable()).values());
}
/**
* Filter elements should be displayed for the given request.
*/
authorized(request) {
return new FieldCollection(this.filter((field) => field.authorize(request)).values());
}
/**
* Transform fields to open api valid schema.
*/
payloadSchemas(request) {
return this.mapWithKeys((field) => [
field.attribute,
field.schema(request).payload,
]).all();
}
/**
* Transform fields to open api valid schema.
*/
responseSchemas(request) {
return this.mapWithKeys((field) => [
field.attribute,
field.schema(request).response,
]).all();
}
/**
* Transform fields to object by values.
*/
fieldValues(request) {
return this.mapWithKeys((field) => [
field.attribute,
field.getValue(request),
]).all();
}
}
exports.default = FieldCollection;