2022-07-06 11:02:36 +02:00
|
|
|
import { browser, dev } from '$app/env';
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
|
|
|
|
export function getAPIUrl() {
|
2022-07-11 20:32:27 +00:00
|
|
|
if (GITPOD_WORKSPACE_URL) {
|
|
|
|
const {href} = new URL(GITPOD_WORKSPACE_URL)
|
|
|
|
const newURL = href.replace('https://','https://3001-').replace(/\/$/,'')
|
|
|
|
return newURL
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
return dev ? 'http://localhost:3001' : 'http://localhost:3000';
|
|
|
|
}
|
2022-04-06 21:09:15 +02:00
|
|
|
async function send({
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
data = {},
|
|
|
|
headers,
|
2022-04-12 18:21:10 +02:00
|
|
|
timeout = 120000
|
2022-07-06 11:02:36 +02:00
|
|
|
}: {
|
|
|
|
method: string;
|
|
|
|
path: string;
|
|
|
|
data?: any;
|
|
|
|
headers?: any;
|
|
|
|
timeout?: number;
|
2022-04-06 21:09:15 +02:00
|
|
|
}): Promise<Record<string, unknown>> {
|
2022-07-06 11:02:36 +02:00
|
|
|
const token = Cookies.get('token');
|
2022-02-10 15:47:44 +01:00
|
|
|
const controller = new AbortController();
|
|
|
|
const id = setTimeout(() => controller.abort(), timeout);
|
2022-07-06 11:02:36 +02:00
|
|
|
const opts: any = { method, headers: {}, body: null, signal: controller.signal };
|
2022-02-10 15:47:44 +01:00
|
|
|
if (Object.keys(data).length > 0) {
|
2022-04-06 21:09:15 +02:00
|
|
|
const parsedData = data;
|
2022-02-10 15:47:44 +01:00
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
if (value === '') {
|
|
|
|
parsedData[key] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parsedData) {
|
|
|
|
opts.headers['Content-Type'] = 'application/json';
|
|
|
|
opts.body = JSON.stringify(parsedData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
opts.headers = {
|
|
|
|
...opts.headers,
|
|
|
|
...headers
|
|
|
|
};
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
if (token && !path.startsWith('https://')) {
|
|
|
|
opts.headers = {
|
|
|
|
...opts.headers,
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!path.startsWith('https://')) {
|
|
|
|
path = `/api/v1${path}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev && !path.startsWith('https://')) {
|
2022-07-11 20:32:27 +00:00
|
|
|
path = `${getAPIUrl()}${path}`;
|
2022-07-06 11:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 15:47:44 +01:00
|
|
|
const response = await fetch(`${path}`, opts);
|
|
|
|
|
|
|
|
clearTimeout(id);
|
|
|
|
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
|
|
|
|
|
|
let responseData = {};
|
|
|
|
if (contentType) {
|
|
|
|
if (contentType?.indexOf('application/json') !== -1) {
|
|
|
|
responseData = await response.json();
|
|
|
|
} else if (contentType?.indexOf('text/plain') !== -1) {
|
|
|
|
responseData = await response.text();
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
if (!response.ok) {
|
2022-07-12 10:19:34 +02:00
|
|
|
if (response.status === 401 && !path.startsWith('https://api.github') && !path.includes('/v4/user')) {
|
2022-07-06 11:02:36 +02:00
|
|
|
Cookies.remove('token');
|
|
|
|
}
|
|
|
|
|
|
|
|
throw responseData;
|
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
return responseData;
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:02:36 +02:00
|
|
|
export function get(path: string, headers?: Record<string, unknown>): Promise<Record<string, any>> {
|
2022-02-10 15:47:44 +01:00
|
|
|
return send({ method: 'GET', path, headers });
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:09:15 +02:00
|
|
|
export function del(
|
|
|
|
path: string,
|
|
|
|
data: Record<string, unknown>,
|
2022-04-12 16:49:52 +02:00
|
|
|
headers?: Record<string, unknown>
|
2022-07-06 11:02:36 +02:00
|
|
|
): Promise<Record<string, any>> {
|
2022-02-10 15:47:44 +01:00
|
|
|
return send({ method: 'DELETE', path, data, headers });
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:09:15 +02:00
|
|
|
export function post(
|
|
|
|
path: string,
|
|
|
|
data: Record<string, unknown>,
|
2022-04-12 10:47:53 +02:00
|
|
|
headers?: Record<string, unknown>
|
2022-07-06 11:02:36 +02:00
|
|
|
): Promise<Record<string, any>> {
|
2022-02-10 15:47:44 +01:00
|
|
|
return send({ method: 'POST', path, data, headers });
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:09:15 +02:00
|
|
|
export function put(
|
|
|
|
path: string,
|
|
|
|
data: Record<string, unknown>,
|
2022-04-12 16:49:52 +02:00
|
|
|
headers?: Record<string, unknown>
|
2022-07-06 11:02:36 +02:00
|
|
|
): Promise<Record<string, any>> {
|
2022-02-10 15:47:44 +01:00
|
|
|
return send({ method: 'PUT', path, data, headers });
|
|
|
|
}
|