-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttp.js
104 lines (92 loc) · 2.85 KB
/
Http.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
const fetch = require('node-fetch')
const nodePath = require('path')
const get = require('rubico/get')
const curry = require('rubico/curry')
const pathJoin = nodePath.join
/**
* @name Http
*
* @synopsis
* ```coffeescript [specscript]
* Http(url string|URL) -> http
* ```
*
* @description
* The Hypertext Transfer Protocol. [wikipedia](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol)
*
* @todo
* CONNECT method Http.prototype.connect
* https://stackoverflow.com/questions/11697943/when-should-one-use-connect-and-get-http-methods-at-http-proxy-server/40329026
*/
const Http = function (url, httpOptions) {
if (this == null || this.constructor != Http) {
return new Http(url, httpOptions)
}
this.url = new URL(url)
this.httpOptions = httpOptions
return this
}
/**
* @name Http.prototype.get
*
* @synopsis
* ```coffeescript [specscript]
* Http(url).get(path string, options {
* body: string,
* headers: Object<string>,
* }) -> Promise<Response>
* ```
*
* @description
* Makes an http GET request. [All options](https://github.com/node-fetch/node-fetch#options) from node-fetch.
*
* ```javascript
* Http('https://google.com/').get('/')
* .then(res => res.text())
* .then(console.log)
* ```
*
* [http method reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
*
* [Response reference](https://developer.mozilla.org/en-US/docs/Web/API/Response).
*/
Http.prototype.get = function httpGet(path, options) {
const url = new URL(this.url),
pathUrl = new URL(`http://throwaway/${path}`)
url.pathname = pathJoin(url.pathname, pathUrl.pathname)
url.search = pathUrl.search
return fetch(url, {
...this.httpOptions,
...options,
})
}
// (method string, path string, options object) => Promise<HttpResponse>
const httpRequest = function httpRequest(method, path, options) {
const url = new URL(this.url),
pathUrl = new URL(`http://throwaway/${path}`)
url.pathname = pathJoin(url.pathname, pathUrl.pathname)
url.search = pathUrl.search
return fetch(url, { ...this.httpOptions, ...options, method })
}
Http.prototype.head = function head(path, options) {
return httpRequest.call(this, 'HEAD', path, options)
}
Http.prototype.post = function post(path, options) {
return httpRequest.call(this, 'POST', path, options)
}
Http.prototype.put = function put(path, options) {
return httpRequest.call(this, 'PUT', path, options)
}
Http.prototype.delete = function del(path, options) {
return httpRequest.call(this, 'DELETE', path, options)
}
Http.prototype.options = function opts(path, options) {
return httpRequest.call(this, 'OPTIONS', path, options)
}
Http.prototype.trace = function trace(path, options) {
return httpRequest.call(this, 'TRACE', path, options)
}
Http.prototype.patch = function patch(path, options) {
return httpRequest.call(this, 'PATCH', path, options)
}
module.exports = Http