131 lines
10 KiB
JavaScript
131 lines
10 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
/**
|
||
|
* Module dependencies.
|
||
|
*/
|
||
|
var utils = require('./utils');
|
||
|
/**
|
||
|
* Expose `ResponseBase`.
|
||
|
*/
|
||
|
|
||
|
|
||
|
module.exports = ResponseBase;
|
||
|
/**
|
||
|
* Initialize a new `ResponseBase`.
|
||
|
*
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function ResponseBase(obj) {
|
||
|
if (obj) return mixin(obj);
|
||
|
}
|
||
|
/**
|
||
|
* Mixin the prototype properties.
|
||
|
*
|
||
|
* @param {Object} obj
|
||
|
* @return {Object}
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
|
||
|
function mixin(obj) {
|
||
|
for (var key in ResponseBase.prototype) {
|
||
|
if (Object.prototype.hasOwnProperty.call(ResponseBase.prototype, key)) obj[key] = ResponseBase.prototype[key];
|
||
|
}
|
||
|
|
||
|
return obj;
|
||
|
}
|
||
|
/**
|
||
|
* Get case-insensitive `field` value.
|
||
|
*
|
||
|
* @param {String} field
|
||
|
* @return {String}
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
|
||
|
ResponseBase.prototype.get = function (field) {
|
||
|
return this.header[field.toLowerCase()];
|
||
|
};
|
||
|
/**
|
||
|
* Set header related properties:
|
||
|
*
|
||
|
* - `.type` the content type without params
|
||
|
*
|
||
|
* A response of "Content-Type: text/plain; charset=utf-8"
|
||
|
* will provide you with a `.type` of "text/plain".
|
||
|
*
|
||
|
* @param {Object} header
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
|
||
|
ResponseBase.prototype._setHeaderProperties = function (header) {
|
||
|
// TODO: moar!
|
||
|
// TODO: make this a util
|
||
|
// content-type
|
||
|
var ct = header['content-type'] || '';
|
||
|
this.type = utils.type(ct); // params
|
||
|
|
||
|
var params = utils.params(ct);
|
||
|
|
||
|
for (var key in params) {
|
||
|
if (Object.prototype.hasOwnProperty.call(params, key)) this[key] = params[key];
|
||
|
}
|
||
|
|
||
|
this.links = {}; // links
|
||
|
|
||
|
try {
|
||
|
if (header.link) {
|
||
|
this.links = utils.parseLinks(header.link);
|
||
|
}
|
||
|
} catch (_unused) {// ignore
|
||
|
}
|
||
|
};
|
||
|
/**
|
||
|
* Set flags such as `.ok` based on `status`.
|
||
|
*
|
||
|
* For example a 2xx response will give you a `.ok` of __true__
|
||
|
* whereas 5xx will be __false__ and `.error` will be __true__. The
|
||
|
* `.clientError` and `.serverError` are also available to be more
|
||
|
* specific, and `.statusType` is the class of error ranging from 1..5
|
||
|
* sometimes useful for mapping respond colors etc.
|
||
|
*
|
||
|
* "sugar" properties are also defined for common cases. Currently providing:
|
||
|
*
|
||
|
* - .noContent
|
||
|
* - .badRequest
|
||
|
* - .unauthorized
|
||
|
* - .notAcceptable
|
||
|
* - .notFound
|
||
|
*
|
||
|
* @param {Number} status
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
|
||
|
ResponseBase.prototype._setStatusProperties = function (status) {
|
||
|
var type = status / 100 | 0; // status / class
|
||
|
|
||
|
this.statusCode = status;
|
||
|
this.status = this.statusCode;
|
||
|
this.statusType = type; // basics
|
||
|
|
||
|
this.info = type === 1;
|
||
|
this.ok = type === 2;
|
||
|
this.redirect = type === 3;
|
||
|
this.clientError = type === 4;
|
||
|
this.serverError = type === 5;
|
||
|
this.error = type === 4 || type === 5 ? this.toError() : false; // sugar
|
||
|
|
||
|
this.created = status === 201;
|
||
|
this.accepted = status === 202;
|
||
|
this.noContent = status === 204;
|
||
|
this.badRequest = status === 400;
|
||
|
this.unauthorized = status === 401;
|
||
|
this.notAcceptable = status === 406;
|
||
|
this.forbidden = status === 403;
|
||
|
this.notFound = status === 404;
|
||
|
this.unprocessableEntity = status === 422;
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9yZXNwb25zZS1iYXNlLmpzIl0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