123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- '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) {
- 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) this[key] = params[key];
- this.links = {};
- // links
- try {
- if (header.link) {
- this.links = utils.parseLinks(header.link);
- }
- } catch (err) {
- // 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.status = this.statusCode = status;
- this.statusType = type;
- // basics
- this.info = 1 == type;
- this.ok = 2 == type;
- this.redirect = 3 == type;
- this.clientError = 4 == type;
- this.serverError = 5 == type;
- this.error = (4 == type || 5 == type)
- ? this.toError()
- : false;
- // sugar
- this.created = 201 == status;
- this.accepted = 202 == status;
- this.noContent = 204 == status;
- this.badRequest = 400 == status;
- this.unauthorized = 401 == status;
- this.notAcceptable = 406 == status;
- this.forbidden = 403 == status;
- this.notFound = 404 == status;
- this.unprocessableEntity = 422 == status;
- };
|