135 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			2.7 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) {
 | 
						|
    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.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;
 | 
						|
};
 |