-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
38 lines (33 loc) · 822 Bytes
/
client.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
import {
Headers,
Request,
Response,
default as fetch
} from "node-fetch"
import methods from "methods"
export default class Client {
#router
constructor(router) {
this.#router = router
}
async request(url, options) {
try { // check for URL
return await fetch(url)
} catch(e) {
return await this.#route(...arguments)
}
}
async #route(url, request = { method, url }) {
console.log('URL', url)
console.log('Request', request)
return this.#router
.handle( request, new Object, new Function )
}
}
methods.reduce((_, method) =>
Client.prototype[method] // http verb (head, get, post, put, patch, etc.)
= async function (
url
, options = { url, method: method.toUpperCase() })
{ return await this.request(url, options) }
, null);