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