-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (155 loc) · 3.63 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** @typedef {import('./index').HTTPMethods} HTTPMethods */
if (!globalThis.fetch || typeof globalThis.fetch !== 'function') {
throw new Error("Couldn't find fetch!");
}
/**
* Validate URL function.
* @param {string} url URL want to validate.
* @return {boolean}
*/
function validateUrl(url) {
try {
return !!new URL(url);
} catch {
return false;
}
}
/**
* Resolve URL w/ (baseURL)
* @param {string | URL} url Children url
* @param {string | URL} baseUrl Parent url
* @param {boolean} forceHost Force change the baseUrl
* @return {URL}
*/
function resolveUrl(url, baseUrl, forceHost) {
if (url instanceof URL) {
if (
baseUrl instanceof URL &&
url.hostname !== baseUrl.hostname &&
forceHost
) {
return new URL(url.pathname, baseUrl);
}
}
if (
baseUrl &&
((typeof baseUrl === 'string' && baseUrl.length) ||
baseUrl instanceof URL) &&
validateUrl(baseUrl)
) {
baseUrl = new URL(baseUrl);
if (baseUrl.hostname === 'localhost') baseUrl.hostname = '127.0.0.1';
return new URL(url, baseUrl.href);
} else {
if (!validateUrl(url)) throw new TypeError('Invalid URL');
else return new URL(url);
}
}
/**
* @class FetchPrefixUrl
*/
class FetchPrefixUrl {
/**
* @param {string} baseUrl Request Base URL.
*/
constructor(baseUrl) {
/** @private */
this.baseUrl = resolveUrl(baseUrl);
}
/**
* Request
* @param {string | URL} url Children URL.
* @param {HTTPMethods} method HTTP Request Methods.
* @param {RequestInit} input Request input data.
* @return {Promise<Response>}
*/
async request(url, method = 'GET', input) {
if (!/^(get|p(os|u)t|delete|options|head)$/gi.test(method))
throw new TypeError('Invalid HTTP Method');
method = method.toUpperCase();
input = input || {};
input.headers = {
'User-Agent': 'FetchPrefixUrl/1.0',
...input.headers,
'Access-Control-Allow-Origin': '*',
};
try {
return await fetch(resolveUrl(url, this.baseUrl, false).href, {
method,
...input,
});
} catch (err) {
return { error: err.message };
}
}
/**
* Do GET Request
* @param {string | URL} url Requested URL.
* @param {RequestInit} options Request options.
* @return {Promise<Response>}
*/
get(url, options) {
return this.request(url, 'GET', options);
}
/**
* Do HEAD Request
* @param {string | URL} url Requested URL.
* @param {RequestInit} options Request options.
* @return {Promise<Response>}
*/
head(url, options) {
return this.get(url, {
...options,
method: 'HEAD',
});
}
/**
* Do POST Request
* @param {string | URL} url Requested URL.
* @param {string | Object | Array} body Requested body.
* @param {RequestInit} options Request options.
* @return {Promise<Response>}
*/
post(url, body, options) {
if (Array.isArray(body) || typeof body === 'object')
body = JSON.stringify(body);
else if (body instanceof URLSearchParams) body = body.toString();
return this.request(url, 'POST', {
...options,
body,
});
}
/**
* Do DELETE Request
* @param {string | URL} url Requested URL.
* @param {RequestInit} options Request options.
* @return {Promise<Response>}
*/
delete(url, options) {
return this.get(url, {
...options,
method: 'DELETE',
});
}
/**
* Do OPTIONS Request
* @param {string | URL} url Requested URL.
* @param {RequestInit} options Request options.
* @return {Promise<Response>}
*/
option(url, options) {
return this.request(url, 'OPTIONS', {
...options,
body,
});
}
/**
* Set current base url.
* @param {string} url Set the base URL.
* @return {void}
*/
setBase(url) {
this.baseUrl = resolveUrl(url);
}
}
export { FetchPrefixUrl, resolveUrl, validateUrl };