UNPKG

service-model

Version:

An object oriented web service framework inspired by Windows Communication Foundation.

55 lines (54 loc) 2.79 kB
var restMessageFormatter_1 = require("../dispatcher/restMessageFormatter"); var restFaultFormatter_1 = require("../dispatcher/restFaultFormatter"); var restOperationSelector_1 = require("../dispatcher/restOperationSelector"); var baseAddressMessageFilter_1 = require("../dispatcher/baseAddressMessageFilter"); var annotations_1 = require("../annotations"); /** * Endpoint behavior that enables REST communication on an endpoint. * * <uml> * hide members * hide circle * EndpointBehavior <|.. RestBehavior * </uml> */ var RestBehavior = (function () { function RestBehavior() { } RestBehavior.prototype.applyEndpointBehavior = function (description, endpoint) { endpoint.faultFormatter = this.createFaultFormatter(); endpoint.filter = this.createMessageFilter(endpoint); endpoint.operationSelector = this.createOperationSelector(description, endpoint); // Note that we assume the operations in the dispatcher line up with the operations in the description. This is // true if the dispatcher is created through the DispatcherFactory but could be incorrect otherwise. We'll at // least check that the names are the same. var operations = description.contract.operations; if (operations.length != endpoint.operations.length) { throw new Error("DispatchEndpoint and EndpointDescription do not have the same number of operations."); } for (var i = 0; i < operations.length; i++) { // check if method is annotated as callback through the REST api var annotation = operations[i].method.getAnnotations(annotations_1.WebInvokeAnnotation)[0]; if (annotation) { if (endpoint.operations[i].name != operations[i].name) { throw new Error("Mismatch between operations in DispatchEndpoint and EndpointDescription"); } endpoint.operations[i].formatter = this.createMessageFormatter(endpoint, operations[i]); } } }; RestBehavior.prototype.createFaultFormatter = function () { return new restFaultFormatter_1.RestFaultFormatter(); }; RestBehavior.prototype.createMessageFilter = function (endpoint) { return new baseAddressMessageFilter_1.BaseAddressMessageFilter(endpoint.address).and(endpoint.filter); }; RestBehavior.prototype.createOperationSelector = function (description, endpoint) { return new restOperationSelector_1.RestOperationSelector(description, endpoint); }; RestBehavior.prototype.createMessageFormatter = function (endpoint, operation) { return new restMessageFormatter_1.RestMessageFormatter(endpoint, operation); }; return RestBehavior; })(); exports.RestBehavior = RestBehavior;