-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (73 loc) · 2.17 KB
/
index.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
import request from 'request'
const { isURL } = require('validator')
class Http {
constructor({
method,
url,
formData,
config = {
headers: {},
},
}) {
this.method = method
this.url = url
this.formData = formData
this.config = config
}
sendResponse() {
return new Promise((resolve, reject) => {
if (!isURL(this.url))
return reject(`"${this.url}" is not a valid endpoint url`)
request(
{
method: this.method,
uri: this.url,
json: true,
body: this.formData,
headers: this.config.headers,
preambleCRLF: true,
postambleCRLF: true,
},
function(error, response, body) {
const res = {
data: body,
headers: response.headers,
statusCode: response.statusCode,
}
resolve(res)
reject(error)
}
)
})
}
}
export default {
get(url, formData = {}) {
const http = new Http({ method: 'get', url: url, formData })
return http.sendResponse()
},
post(url, formData = {}) {
const http = new Http({ method: 'post', url: url, formData })
return http.sendResponse()
},
put(url, formData = {}) {
const http = new Http({ method: 'put', url, formData })
return http.sendResponse()
},
patch(url, formData = {}) {
const http = new Http({ method: 'patch', url, formData })
return http.sendResponse()
},
head(url, formData = {}) {
const http = new Http({ method: 'head', url, formData })
return http.sendResponse()
},
delete(url, formData = {}) {
const http = new Http({ method: 'delete', url, formData })
return http.sendResponse()
},
options(url, formData = {}) {
const http = new Http({ method: 'options', url, formData })
return http.sendResponse()
},
}