|
7 | 7 | const InvalidArgumentError = require('./errors/invalid-argument-error'); |
8 | 8 | const typeis = require('type-is'); |
9 | 9 |
|
10 | | -/** |
11 | | - * Constructor. |
12 | | - */ |
13 | | - |
14 | | -function Request(options) { |
15 | | - options = options || {}; |
| 10 | +class Request { |
| 11 | + constructor({ headers, method, query, body, ...otherOptions } = {}) { |
| 12 | + if (!headers) { |
| 13 | + throw new InvalidArgumentError('Missing parameter: `headers`'); |
| 14 | + } |
16 | 15 |
|
17 | | - if (!options.headers) { |
18 | | - throw new InvalidArgumentError('Missing parameter: `headers`'); |
19 | | - } |
| 16 | + if (!method) { |
| 17 | + throw new InvalidArgumentError('Missing parameter: `method`'); |
| 18 | + } |
20 | 19 |
|
21 | | - if (!options.method) { |
22 | | - throw new InvalidArgumentError('Missing parameter: `method`'); |
23 | | - } |
| 20 | + if (!query) { |
| 21 | + throw new InvalidArgumentError('Missing parameter: `query`'); |
| 22 | + } |
24 | 23 |
|
25 | | - if (!options.query) { |
26 | | - throw new InvalidArgumentError('Missing parameter: `query`'); |
| 24 | + this.body = body || {}; |
| 25 | + this.headers = {}; |
| 26 | + this.method = method; |
| 27 | + this.query = query; |
| 28 | + |
| 29 | + // Store the headers in lower case. |
| 30 | + Object.entries(headers).forEach(([header, value]) => { |
| 31 | + this.headers[header.toLowerCase()] = value; |
| 32 | + }); |
| 33 | + |
| 34 | + // Store additional properties of the request object passed in |
| 35 | + Object.entries(otherOptions) |
| 36 | + .filter(([property]) => !this[property]) |
| 37 | + .forEach(([property, value]) => { |
| 38 | + this[property] = value; |
| 39 | + }); |
27 | 40 | } |
28 | 41 |
|
29 | | - this.body = options.body || {}; |
30 | | - this.headers = {}; |
31 | | - this.method = options.method; |
32 | | - this.query = options.query; |
33 | | - |
34 | | - // Store the headers in lower case. |
35 | | - for (const field in options.headers) { |
36 | | - if (Object.prototype.hasOwnProperty.call(options.headers, field)) { |
37 | | - this.headers[field.toLowerCase()] = options.headers[field]; |
38 | | - } |
| 42 | + /** |
| 43 | + * Get a request header. |
| 44 | + * @param {String} field |
| 45 | + */ |
| 46 | + get(field) { |
| 47 | + return this.headers[field.toLowerCase()]; |
39 | 48 | } |
40 | 49 |
|
41 | | - // Store additional properties of the request object passed in |
42 | | - for (const property in options) { |
43 | | - if (Object.prototype.hasOwnProperty.call(options, property) && !this[property]) { |
44 | | - this[property] = options[property]; |
45 | | - } |
| 50 | + /** |
| 51 | + * Check if the content-type matches any of the given mime types. |
| 52 | + * @param {...String|Array} types |
| 53 | + */ |
| 54 | + is(...types) { |
| 55 | + return typeis(this, types.flat()) || false; |
46 | 56 | } |
47 | 57 | } |
48 | 58 |
|
49 | | -/** |
50 | | - * Get a request header. |
51 | | - */ |
52 | | - |
53 | | -Request.prototype.get = function(field) { |
54 | | - return this.headers[field.toLowerCase()]; |
55 | | -}; |
56 | | - |
57 | | -/** |
58 | | - * Check if the content-type matches any of the given mime type. |
59 | | - */ |
60 | | - |
61 | | -Request.prototype.is = function(types) { |
62 | | - if (!Array.isArray(types)) { |
63 | | - types = [].slice.call(arguments); |
64 | | - } |
65 | | - |
66 | | - return typeis(this, types) || false; |
67 | | -}; |
68 | | - |
69 | | -/** |
70 | | - * Export constructor. |
71 | | - */ |
72 | | - |
73 | 59 | module.exports = Request; |
0 commit comments