response-base.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var utils = require('./utils');
  6. /**
  7. * Expose `ResponseBase`.
  8. */
  9. module.exports = ResponseBase;
  10. /**
  11. * Initialize a new `ResponseBase`.
  12. *
  13. * @api public
  14. */
  15. function ResponseBase(obj) {
  16. if (obj) return mixin(obj);
  17. }
  18. /**
  19. * Mixin the prototype properties.
  20. *
  21. * @param {Object} obj
  22. * @return {Object}
  23. * @api private
  24. */
  25. function mixin(obj) {
  26. for (var key in ResponseBase.prototype) {
  27. obj[key] = ResponseBase.prototype[key];
  28. }
  29. return obj;
  30. }
  31. /**
  32. * Get case-insensitive `field` value.
  33. *
  34. * @param {String} field
  35. * @return {String}
  36. * @api public
  37. */
  38. ResponseBase.prototype.get = function(field) {
  39. return this.header[field.toLowerCase()];
  40. };
  41. /**
  42. * Set header related properties:
  43. *
  44. * - `.type` the content type without params
  45. *
  46. * A response of "Content-Type: text/plain; charset=utf-8"
  47. * will provide you with a `.type` of "text/plain".
  48. *
  49. * @param {Object} header
  50. * @api private
  51. */
  52. ResponseBase.prototype._setHeaderProperties = function(header){
  53. // TODO: moar!
  54. // TODO: make this a util
  55. // content-type
  56. var ct = header['content-type'] || '';
  57. this.type = utils.type(ct);
  58. // params
  59. var params = utils.params(ct);
  60. for (var key in params) this[key] = params[key];
  61. this.links = {};
  62. // links
  63. try {
  64. if (header.link) {
  65. this.links = utils.parseLinks(header.link);
  66. }
  67. } catch (err) {
  68. // ignore
  69. }
  70. };
  71. /**
  72. * Set flags such as `.ok` based on `status`.
  73. *
  74. * For example a 2xx response will give you a `.ok` of __true__
  75. * whereas 5xx will be __false__ and `.error` will be __true__. The
  76. * `.clientError` and `.serverError` are also available to be more
  77. * specific, and `.statusType` is the class of error ranging from 1..5
  78. * sometimes useful for mapping respond colors etc.
  79. *
  80. * "sugar" properties are also defined for common cases. Currently providing:
  81. *
  82. * - .noContent
  83. * - .badRequest
  84. * - .unauthorized
  85. * - .notAcceptable
  86. * - .notFound
  87. *
  88. * @param {Number} status
  89. * @api private
  90. */
  91. ResponseBase.prototype._setStatusProperties = function(status){
  92. var type = status / 100 | 0;
  93. // status / class
  94. this.status = this.statusCode = status;
  95. this.statusType = type;
  96. // basics
  97. this.info = 1 == type;
  98. this.ok = 2 == type;
  99. this.redirect = 3 == type;
  100. this.clientError = 4 == type;
  101. this.serverError = 5 == type;
  102. this.error = (4 == type || 5 == type)
  103. ? this.toError()
  104. : false;
  105. // sugar
  106. this.created = 201 == status;
  107. this.accepted = 202 == status;
  108. this.noContent = 204 == status;
  109. this.badRequest = 400 == status;
  110. this.unauthorized = 401 == status;
  111. this.notAcceptable = 406 == status;
  112. this.forbidden = 403 == status;
  113. this.notFound = 404 == status;
  114. this.unprocessableEntity = 422 == status;
  115. };