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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9yZXNwb25zZS1iYXNlLmpzIl0sIm5hbWVzIjpbInV0aWxzIiwicmVxdWlyZSIsIm1vZHVsZSIsImV4cG9ydHMiLCJSZXNwb25zZUJhc2UiLCJvYmoiLCJtaXhpbiIsImtleSIsInByb3RvdHlwZSIsIk9iamVjdCIsImhhc093blByb3BlcnR5IiwiY2FsbCIsImdldCIsImZpZWxkIiwiaGVhZGVyIiwidG9Mb3dlckNhc2UiLCJfc2V0SGVhZGVyUHJvcGVydGllcyIsImN0IiwidHlwZSIsInBhcmFtcyIsImxpbmtzI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