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