-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
90 lines (81 loc) · 2.79 KB
/
index.mjs
File metadata and controls
90 lines (81 loc) · 2.79 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
class HTTPError extends Error {
constructor(response) {
super(response.statusText);
this.name = 'HTTPStatus';
this.response = response;
this.code = response.status;
}
}
async function noutaa(url, options) {
let urlWithParams;
let body = options && options.body;
const headers = {};
const accept = options && options.accept;
if (
options
&& options.constructor === Object
&& Object.keys(options).length
) {
const { json, urlencoded, params } = options;
// update body with "json", attach headers
if (json) {
if (json.constructor === Object || Array.isArray(json)) {
body = JSON.stringify(json);
headers['Content-Type'] = 'application/json; charset=utf-8';
}
}
// update body with "urlencoded" as URLSearchParams
if (urlencoded) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
if (urlencoded.constructor === Object) {
body = new URLSearchParams();
Object.entries(urlencoded).forEach(([key, value]) => {
body.append(key, value);
});
} else { // else if constructor === URLSearchParams
body = urlencoded;
}
}
// update body with "form" as URLSearchParams
// headers['Content-Type'] = 'multipart/form-data';
// body = new FormData();
// attach url query string a.k.a. "params"
if (params) {
const [urlWithoutParams, paramsOriginal] = url.split('?');
const urlParams = new URLSearchParams(paramsOriginal);
Object.entries(params).forEach(([key, value]) => {
urlParams.append(key, value);
});
urlWithParams = (paramsOriginal)
? `${urlWithoutParams}?${urlParams}`
: `${url}?${urlParams}`;
}
}
// merge old options with new config
const config = {
...options,
headers: { // input headers overwrite generated headers
...headers,
...(options && options.headers),
},
body, // generated body overwrites input
};
const res = await fetch(urlWithParams || url, config);
if (res.ok) {
switch (accept) {
case 'json':
case 'application/json':
case 'application/json; charset=utf-8':
return res.json();
default: return res;
}
}
throw new HTTPError(res);
}
export default noutaa;
export const GET = async (url, options = {}) => noutaa(url, options);
export const POST = async (url, options = {}) => noutaa(url, { ...options, method: 'POST' });
export const PUT = async (url, options = {}) => noutaa(url, { ...options, method: 'PUT' });
export const PATCH = async (url, options = {}) => noutaa(url, { ...options, method: 'PATCH' });
export const HEAD = async (url, options = {}) => noutaa(url, { ...options, method: 'HEAD' });
export const DELETE = async (url, options = {}) => noutaa(url, { ...options, method: 'DELETE' });