api-core
Version:
Model-based dynamic multi-level APIs for any provider, plus multiple consumption channels
139 lines • 6.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiEdgeQuery = void 0;
const ApiEdgeQueryType_1 = require("./ApiEdgeQueryType");
const ApiEdgeQueryContext_1 = require("./ApiEdgeQueryContext");
const ApiEdgeError_1 = require("../query/ApiEdgeError");
class ApiEdgeQuery {
constructor(edge, type = ApiEdgeQueryType_1.ApiEdgeQueryType.Get, context = new ApiEdgeQueryContext_1.ApiEdgeQueryContext(), body = null) {
this.originalFields = [];
this.applySchemaOnInputItem = (item) => {
let output = {};
this.edge.schema.transformations.forEach((transformation) => {
if (transformation.parsedField(item) !== undefined)
transformation.applyToInput(item, output);
});
return output;
};
this.applySchemaOnItem = (item) => {
let output = {};
if (this.originalFields.length) {
this.edge.schema.transformations.forEach((transformation) => {
if (this.originalFields.indexOf(transformation.affectedSchemaField) != -1)
transformation.applyToOutput(item, output);
});
}
else {
this.edge.schema.transformations.forEach((transformation) => {
transformation.applyToOutput(item, output);
});
}
return output;
};
this.applyListSchema = (value) => {
if (!this.edge.schema)
return value;
value.data = value.data.map((item) => this.applySchemaOnItem(item));
return value;
};
this.applyInputSchema = (value) => {
if (!this.edge.schema)
return value;
return this.applySchemaOnInputItem(value);
};
this.applySchema = (value) => {
if (!this.edge.schema)
return value;
value.data = this.applySchemaOnItem(value.data);
return value;
};
this.execute = () => {
if (this.context.fields.length) {
this.originalFields = this.context.fields;
this.context.fields = this.edge.schema.transformFields(this.context.fields);
this.context.filters.forEach(filter => filter.field = this.edge.schema.transformField(filter.field));
}
if (this.body
&& this.type !== ApiEdgeQueryType_1.ApiEdgeQueryType.Get
&& this.type !== ApiEdgeQueryType_1.ApiEdgeQueryType.Exists
&& this.type !== ApiEdgeQueryType_1.ApiEdgeQueryType.Delete
&& this.type !== ApiEdgeQueryType_1.ApiEdgeQueryType.List) {
if (!this.context.id)
this.context.id = this.body.id;
if (this.edge.schema) {
const result = this.edge.schema.cleanAndValidateModel(this.body, this.type === ApiEdgeQueryType_1.ApiEdgeQueryType.Patch);
if (!result.valid) {
const errors = result.errors
? result.errors.join(', ')
: '';
throw new ApiEdgeError_1.ApiEdgeError(422, "Schema Validation Failed: " + errors);
}
}
this.body = this.applyInputSchema(this.body);
}
switch (this.type) {
case ApiEdgeQueryType_1.ApiEdgeQueryType.Get:
return this.edge.getEntry(this.context).then(this.applySchema);
case ApiEdgeQueryType_1.ApiEdgeQueryType.Exists:
return this.edge.exists(this.context);
case ApiEdgeQueryType_1.ApiEdgeQueryType.Create:
return this.edge.createEntry(this.context, this.body).then(this.applySchema);
case ApiEdgeQueryType_1.ApiEdgeQueryType.Delete:
return this.edge.removeEntry(this.context, this.body).then(this.applySchema);
case ApiEdgeQueryType_1.ApiEdgeQueryType.Update:
return this.edge.updateEntry(this.context, this.body).then(this.applySchema);
case ApiEdgeQueryType_1.ApiEdgeQueryType.Patch:
return this.edge.patchEntry(this.context, this.body).then(this.applySchema);
case ApiEdgeQueryType_1.ApiEdgeQueryType.List:
return this.edge.listEntries(this.context).then(this.applyListSchema);
default:
throw new ApiEdgeError_1.ApiEdgeError(500, "Unsupported Query Type");
}
};
this.edge = edge;
this.type = type;
this.context = context;
this.body = body;
switch (this.type) {
case ApiEdgeQueryType_1.ApiEdgeQueryType.Get:
if (!this.edge.allowGet) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Get queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.Exists:
if (!this.edge.allowExists) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Exists queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.Create:
if (!this.edge.allowCreate) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Create queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.Delete:
if (!this.edge.allowRemove) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Delete queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.Update:
if (!this.edge.allowUpdate) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Update queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.Patch:
if (!this.edge.allowPatch) {
throw new ApiEdgeError_1.ApiEdgeError(405, `Patch queries not allowed on edge: ${this.edge.name}`);
}
break;
case ApiEdgeQueryType_1.ApiEdgeQueryType.List:
if (!this.edge.allowList) {
throw new ApiEdgeError_1.ApiEdgeError(405, `List queries not allowed on edge: ${this.edge.name}`);
}
break;
default:
throw new ApiEdgeError_1.ApiEdgeError(405, "Unsupported Query Type");
}
}
}
exports.ApiEdgeQuery = ApiEdgeQuery;
//# sourceMappingURL=ApiEdgeQuery.js.map