UNPKG

breeze2-odata4

Version:

Library to allow OData 4 support for breezejs 2

97 lines 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ts_odatajs_1 = require("ts-odatajs"); var class_registry_1 = require("../class-registry"); var AnnotationAdapter = (function () { function AnnotationAdapter() { this.decorators = []; this.edmTypes = { action: ['parameter'], complexType: ['property', 'navigationProperty'], entityContainer: ['actionImport', 'entitySet', 'functionImport', 'singleton'], entityType: ['property', 'navigationProperty'], enumType: ['member'], function: ['parameter'], term: [], typeDefinition: [] }; this.decorators = class_registry_1.ClassRegistry.AnnotationDecorators.get(); } AnnotationAdapter.prototype.adapt = function (metadata) { this.metadata = metadata; ts_odatajs_1.oData.utils.forEachSchema(this.metadata.schema, this.adaptSchema.bind(this)); }; AnnotationAdapter.prototype.adaptSchema = function (schema) { var _this = this; var annotations = schema.annotations || []; annotations.forEach(function (itemAnnotation) { var annotatableType = _this.getAnnotatableType(schema, itemAnnotation.target); if (annotatableType === null) { throw new Error("Could not find element with type name " + itemAnnotation.target); } itemAnnotation.annotation .forEach(function (annotation) { var decorator = _this.decorators .find(function (d) { return annotation.term.indexOf("." + d.annotation) > -1; }); if (!decorator) { return; } decorator.decorate(annotatableType, annotation); }); }); }; AnnotationAdapter.prototype.getAnnotatableType = function (schema, target) { var targetSplit = target.split('/'); var typeName = targetSplit[0]; var propertyName = targetSplit[1]; var result; var kind; for (var edmType in this.edmTypes) { kind = edmType; var schemaProperty = schema[kind]; if (!schemaProperty) { continue; } else if (schemaProperty instanceof Array) { result = ts_odatajs_1.oData.utils.lookupInMetadata(typeName, this.metadata.schema, kind); } else if (schema.namespace + "." + schemaProperty.name === typeName) { result = schemaProperty; } if (result) { break; } } if (!propertyName) { return result; } var baseType = result; result = null; var properties = this.edmTypes[kind]; properties.forEach(function (p) { if (result) { return; } var item; var typeProperty = baseType[p]; if (!typeProperty) { return; } if (typeProperty instanceof Array) { item = typeProperty.find(function (e) { return e.name === propertyName; }); } else if (item.name === propertyName) { item = typeProperty; } if (item) { result = item; } }); return result; }; return AnnotationAdapter; }()); exports.AnnotationAdapter = AnnotationAdapter; //# sourceMappingURL=annotation-adapter.js.map