-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathrequest.js
59 lines (48 loc) · 1.34 KB
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
/**
* Module dependencies.
*/
const InvalidArgumentError = require('./errors/invalid-argument-error');
const typeis = require('type-is');
class Request {
constructor({ headers, method, query, body, ...otherOptions } = {}) {
if (!headers) {
throw new InvalidArgumentError('Missing parameter: `headers`');
}
if (!method) {
throw new InvalidArgumentError('Missing parameter: `method`');
}
if (!query) {
throw new InvalidArgumentError('Missing parameter: `query`');
}
this.body = body || {};
this.headers = {};
this.method = method;
this.query = query;
// Store the headers in lower case.
Object.entries(headers).forEach(([header, value]) => {
this.headers[header.toLowerCase()] = value;
});
// Store additional properties of the request object passed in
Object.entries(otherOptions)
.filter(([property]) => !this[property])
.forEach(([property, value]) => {
this[property] = value;
});
}
/**
* Get a request header.
* @param {String} field
*/
get(field) {
return this.headers[field.toLowerCase()];
}
/**
* Check if the content-type matches any of the given mime types.
* @param {...String|Array} types
*/
is(...types) {
return typeis(this, types.flat()) || false;
}
}
module.exports = Request;