Repository navigation
Expand file tree
/
Copy pathhttpclient.mjs
More file actions
145 lines (131 loc) · 3.77 KB
/
Copy pathhttpclient.mjs
File metadata and controls
145 lines (131 loc) · 3.77 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class HTTPStatusError extends Error {
constructor(response, ...arg) {
super(...arg)
this.status = response.status
this.headers = response.headers
}
}
class HTTPRedirectError extends HTTPStatusError {
constructor(response, ...arg) {
super(response, ...arg)
}
}
class HTTPClient {
constructor(url, body, options={}) {
if (!options.headers) { options.headers = {} }
this.url = this.create_request_url(url, options.query)
this.body = this.create_request_body(body, options)
this.options = options
this.params = this.create_http_params(this.body, options)
}
create_request_url(url, query) {
const qparam = (typeof query === "string") ? query : new URLSearchParams(query)
return `${url}?${qparam}`
}
create_request_body(body, options) {
if (!body || options.method === "GET" || options.method === "DELETE") { return }
if (typeof body === 'string' || options.style === "blob") { return body }
if (options.style === "form") {
options.headers["Content-Type"] = "application/x-www-form-urlencoded"
return new URLSearchParams(body)
}
// JSON
options.headers["Content-Type"] = "application/json"
return JSON.stringify(body)
}
create_http_params(body, options) {
return {
method: options.method,
body,
headers: options.headers
}
}
/**
* Do HTTP request.
* @returns {Promise<string>} response body.
*/
async request() {
const result = await fetch(this.url, this.params)
if (result.status >= 300 && result.status < 400) {
throw new HTTPRedirectError(result, "HTTP returns redirect.")
} else if (result.status >= 400) {
throw new HTTPStatusError(result, "HTTP returns error status.")
}
if (this.options.blob) {
const blob = await result.blob()
return blob
} else {
const text = await result.text()
if (result.status === 204 || !text) {
// No content
return null
} else {
if (this.options.disable_parse_json) {
return text
} else {
try {
return JSON.parse(text)
} catch(e) {
return text
}
}
}
}
}
}
/**
* @typedef {Object} requestOptions
* @property {string|object} [query] Query object
* @property {object} [headers] HTTP request headers
* @property {string} [style] Request body translation style
* @property {boolean} [blob] Is response body a blob?
*/
const http = {
/**
* GET Method request
* @param {string} url Request resource URL
* @param {requestOptions} options
* @returns {Promise<string>} Response data
*/
get: (url, options={}) => {
options.method = "GET"
const client = new HTTPClient(url, null, options)
return client.request()
},
/**
* POST method request
* @param {string} url Request resource URL
* @param {*} body Request body
* @param {requestOptions} options
* @returns {Promise<string>} Response data
*/
post: (url, body, options={}) => {
options.method = "POST"
const client = new HTTPClient(url, body, options)
return client.request()
},
/**
* PUT Method request
* @param {string} url Request resource URL
* @param {*} body Request body
* @param {requestOptions} options
* @returns {Promise<string>} Response data
*/
put: (url, body, options={}) => {
options.method = "PUT"
const client = new HTTPClient(url, body, options)
return client.request()
},
/**
* DELETE method request
* @param {string} url Request resource URL
* @param {requestOptions} options
* @returns {Promise<string>} Response data
*/
delete: (url, options={}) => {
options.method = "DELETE"
const client = new HTTPClient(url, null, options)
return client.request()
}
}
export {http, HTTPClient}