api-core
Version:
Model-based dynamic multi-level APIs for any provider, plus multiple consumption channels
103 lines • 4.19 kB
JavaScript
"use strict";
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());
});
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiEdgeQueryContext = void 0;
const ApiEdgeQueryFilter_1 = require("./ApiEdgeQueryFilter");
const ApiEdgeQueryParameter_1 = require("./ApiEdgeQueryParameter");
class ApiEdgeQueryContext {
constructor(id = null, fields = []) {
this.fields = [];
this.populatedRelations = [];
this.sortBy = [];
this.filters = [];
this.parameters = [];
this.clone = () => {
let temp = new ApiEdgeQueryContext();
temp.id = this.id;
temp.identity = this.identity;
this.fields.forEach(f => temp.fields.push(f));
this.populatedRelations.forEach(f => temp.populatedRelations.push(f));
this.filters.forEach(f => temp.filters.push(f.clone()));
this.parameters.forEach(f => temp.parameters.push(f.clone()));
this.sortBy.forEach(f => temp.sortBy.push([f[0], f[1]]));
if (this.pagination) {
temp.pagination = {
skip: this.pagination.skip,
limit: this.pagination.limit
};
}
return temp;
};
this.toJSON = () => {
return {
id: this.id,
fields: this.fields,
populatedRelations: this.populatedRelations.map(relation => relation.name),
pagination: this.pagination,
sortBy: this.sortBy,
filters: this.filters,
parameters: this.parameters
};
};
this.paginate = (skip, limit) => {
this.pagination = {
skip, limit
};
return this;
};
this.sort = (fieldName, ascending = true) => {
this.sortBy.push([fieldName, (ascending ? 1 : -1)]);
return this;
};
this.id = id;
this.fields = fields;
}
populate(relation) {
this.populatedRelations.push(relation);
return this;
}
field(field) {
this.fields.push(field);
return this;
}
filter(field, type, value) {
this.filters.push(new ApiEdgeQueryFilter_1.ApiEdgeQueryFilter(field, type, value));
return this;
}
parameter(key, value) {
if (typeof value === "undefined") {
const param = this.parameters.find(p => p.key === key);
return param ? param.value : null;
}
else {
this.parameters.push(new ApiEdgeQueryParameter_1.ApiEdgeQueryParameter(key, value));
return this;
}
}
}
_a = ApiEdgeQueryContext;
ApiEdgeQueryContext.fromJSON = (obj, api) => __awaiter(void 0, void 0, void 0, function* () {
const context = new ApiEdgeQueryContext();
context.id = obj.id;
context.fields = obj.fields;
context.populatedRelations = [];
for (let name of obj.populatedRelations) {
context.populatedRelations.push(yield api.findRelation(name));
}
context.pagination = obj.pagination;
context.sortBy = obj.sortBy;
context.filters = obj.filters.map(({ field, type, value }) => new ApiEdgeQueryFilter_1.ApiEdgeQueryFilter(field, type, value));
context.parameters = obj.parameters.map(({ key, value }) => new ApiEdgeQueryParameter_1.ApiEdgeQueryParameter(key, value));
return context;
});
exports.ApiEdgeQueryContext = ApiEdgeQueryContext;
//# sourceMappingURL=ApiEdgeQueryContext.js.map