-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.js
115 lines (106 loc) · 2.9 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @constructor
* @private
* @param url the base url to utilize
* @param authToken authorization token to make requests
* @param options Optional set of options to utilize over the default headers
*/
var TeslaRequest_ = function (url, authToken, options) {
const this_ = this
var queryString = ''
// Set default header options if none are passed in
options = options || {
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + authToken
}
}
options.muteHttpExceptions = true
/**
* Sets the payload option
*
* @param obj Object payload to stringify
* @returns {TeslaRequest_} this request to be chained
*/
const payload = function (obj) {
options.payload = JSON.stringify(obj)
return this_
}
/**
* Sets the type of REST method to send
*
* @param type String value equal to one of the REST method types
* @param path the path to send the request to
* @returns {TeslaRequest_} this request to be chained
*/
const method = function (type, path) {
options.method = type;
return fetchObject_(url + (path || '') + queryString, options)
}
/**
* Adds a parameter to the URL query string.
* Can be repeated for additional key-value mappings
*
* @param key the key to add
* @param value the value to set
* @returns {TeslaRequest_} this request to be chained
*/
this.addParam = function (key, value) {
key = encodeURI(key)
value = encodeURI(value)
queryString += (queryString.indexOf('?') === -1 ? '?' : '&') + key + '=' + value
return this_
}
/**
* Set request as a GET method
*
* @param path the path to send the request to
* @returns {TeslaRequest_} this request to be chained
*/
this.get = function (path) {
return method('get', path)
}
/**
* Set request as a POST method
*
* @param path the path to send the request to
* @param obj Optional object to send as payload
* @returns {TeslaRequest_} this request to be chained
*/
this.post = function (path, obj) {
if (obj) {
payload(obj)
}
return method('post', path)
}
/**
* Set request as a PATCH method.
*
* @param path the path to send the request to
* @param obj Optional object to send as payload
* @returns {TeslaRequest_} this request to be chained
*/
this.patch = function (path, obj) {
if (obj) {
payload(obj)
}
return method('patch', path)
}
/**
* Set request as a DELETE method (delete is a keyword)
*
* @param path the path to send the request to
* @returns {TeslaRequest_} this request to be chained
*/
this.remove = function (path) {
return method('delete', path)
}
/**
* Used to clone the request instance. Useful for firing multiple requests.
*
* @returns {TeslaRequest_} A copy of this object
*/
this.clone = function () {
return new TeslaRequest_(url, authToken, options)
}
}