service-model
Version:
An object oriented web service framework inspired by Windows Communication Foundation.
80 lines (79 loc) • 2.5 kB
JavaScript
/**
* Represents a collection of headers for a [[Message]].
*/
var MessageHeaders = (function () {
/**
* Constructs a MessagesHeaders object.
* @param headers Object map of headers. *Header keys must be lowercase.*
*/
function MessageHeaders(headers) {
this._headers = headers;
}
/**
* Gets a header.
* @param name The case insensitive name of the header.
*/
MessageHeaders.prototype.get = function (name) {
if (!this._headers)
return;
return this._headers[name.toLowerCase()];
};
/**
* Sets a header.
* @param name The case insensitive name of the header.
* @param value The value of the header.
*/
MessageHeaders.prototype.set = function (name, value) {
if (!this._headers) {
this._headers = {};
}
this._headers[name.toLowerCase()] = value;
};
/**
* Returns true if the collection contains the header; otherwise, returns false.
* @param name The case insensitive name of the header.
*/
MessageHeaders.prototype.has = function (name) {
if (!this._headers)
return false;
return !!this._headers[name.toLowerCase()];
};
/**
* Deletes a header.
* @param name The case insensitive name of the header.
*/
MessageHeaders.prototype.delete = function (name) {
if (!this._headers)
return;
delete this._headers[name.toLowerCase()];
};
/**
* Merges in the messages headers, overwriting any existing headers.
* @param headers The headers to merge
*/
MessageHeaders.prototype.merge = function (headers) {
var obj = headers.toObject();
if (obj) {
if (!this._headers) {
this._headers = {};
}
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
// skip using `this.set`, since headers are coming from a MessageHeaders object we know that all
// the keys will be lowercase
this._headers[name] = obj[name];
}
}
}
};
/**
* Returns the headers as an object map. Will return `undefined` if no headers have been set.
*/
MessageHeaders.prototype.toObject = function () {
if (this._headers) {
return this._headers;
}
};
return MessageHeaders;
})();
exports.MessageHeaders = MessageHeaders;