-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
32 lines (28 loc) · 945 Bytes
/
api.js
File metadata and controls
32 lines (28 loc) · 945 Bytes
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
const API_URL = 'https://localhost:5000/api';
export const API_URLS = {
'models': { url: ({ provider }) => `${API_URL}/${provider}/models`, method: 'GET' },
'sendMessage': { url: ({ provider }) => `${API_URL}/${provider}/sendMessage`, method: 'POST' },
};
const makeRequest = async(key, options) => {
const { params = {}, queryParams, provider, ...rest } = options;
const { method, url: getUrl } = API_URLS[key];
let url = typeof getUrl === 'function' ? new URL(getUrl({ provider })) : new URL(getUrl);
if (queryParams) {
url.search = new URLSearchParams(queryParams).toString();
}
for (const [key, value] of Object.entries(params)) {
url.pathname = url.pathname.replace(`:${key}`, value || '');
}
const result = await fetch(url, {
...rest,
method,
headers: {
'Content-Type': 'application/json',
},
});
return result.json();
}
const exports = {
makeRequest,
};
export default exports;