forked from DishpitDev/Slopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlbtunnel.js
65 lines (56 loc) · 1.98 KB
/
lbtunnel.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
const tunnel = (() => {
let baseUrl = '';
/**
* Sets the base URL for the tunnel.
* @param {string} url - The base URL for the tunnel.
*/
const setBaseUrl = (url) => {
baseUrl = url.endsWith('/') ? url.slice(0, -1) : url;
};
/**
* Sends a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint to send the GET request to.
* @param {Object} params - The query parameters to include in the request.
* @returns {Promise<any>} - A promise that resolves with the response data.
*/
const get = async (endpoint, params = {}) => {
const url = new URL(`${baseUrl}${endpoint}`);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'any-value'
},
});
if (!response.ok) {
throw new Error(`GET request failed: ${response.statusText}`);
}
return response.json();
};
/**
* Sends a POST request to the specified endpoint.
* @param {string} endpoint - The endpoint to send the POST request to.
* @param {Object} body - The body data to include in the request.
* @returns {Promise<any>} - A promise that resolves with the response data.
*/
const post = async (endpoint, body = {}) => {
const response = await fetch(`${baseUrl}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'any-value'
},
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`POST request failed: ${response.statusText}`);
}
return response.json();
};
return {
setBaseUrl,
get,
post,
};
})();