coolify/apps/ui/src/lib/api.ts

150 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-07-12 13:08:47 +00:00
import { dev } from '$app/env';
2022-07-06 09:02:36 +00:00
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-11 20:32:27 +00:00
}
2022-08-11 08:18:17 +00:00
if (CODESANDBOX_HOST) {
return `https://${CODESANDBOX_HOST.replace(/\$PORT/, '3001')}`;
2022-08-11 08:18:17 +00:00
}
return dev
2022-11-16 12:40:28 +00:00
? `http://${window.location.hostname}:3001`
2022-09-11 23:56:12 +00:00
: 'http://localhost:3000';
2022-07-06 09:02:36 +00:00
}
2022-07-12 13:08:47 +00:00
export function getWebhookUrl(type: string) {
if (GITPOD_WORKSPACE_URL) {
const { href } = new URL(GITPOD_WORKSPACE_URL);
const newURL = href.replace('https://', 'https://3001-').replace(/\/$/, '');
2022-07-12 13:08:47 +00:00
if (type === 'github') {
return `${newURL}/webhooks/github/events`;
2022-07-12 13:08:47 +00:00
}
if (type === 'gitlab') {
return `${newURL}/webhooks/gitlab/events`;
2022-07-12 13:08:47 +00:00
}
}
2022-08-11 08:18:17 +00:00
if (CODESANDBOX_HOST) {
const newURL = `https://${CODESANDBOX_HOST.replace(/\$PORT/, '3001')}`;
2022-08-11 08:18:17 +00:00
if (type === 'github') {
return `${newURL}/webhooks/github/events`;
2022-08-11 08:18:17 +00:00
}
if (type === 'gitlab') {
return `${newURL}/webhooks/gitlab/events`;
2022-08-11 08:18:17 +00:00
}
}
2022-07-12 13:08:47 +00:00
return `https://webhook.site/0e5beb2c-4e9b-40e2-a89e-32295e570c21/events`;
}
2022-04-06 19:09:15 +00:00
async function send({
method,
path,
2022-09-21 13:48:32 +00:00
data = null,
2022-04-06 19:09:15 +00:00
headers,
2022-04-12 16:21:10 +00:00
timeout = 120000
2022-07-06 09:02:36 +00:00
}: {
method: string;
path: string;
data?: any;
headers?: any;
timeout?: number;
2022-04-06 19:09:15 +00:00
}): Promise<Record<string, unknown>> {
2022-07-06 09:02:36 +00:00
const token = Cookies.get('token');
2022-02-10 14:47:44 +00:00
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
2022-07-06 09:02:36 +00:00
const opts: any = { method, headers: {}, body: null, signal: controller.signal };
2022-09-21 13:48:32 +00:00
if (data && Object.keys(data).length > 0) {
2022-04-06 19:09:15 +00:00
const parsedData = data;
2022-02-10 14:47:44 +00: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 09:02:36 +00: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 09:02:36 +00:00
}
2022-09-21 13:48:32 +00:00
if (method === 'POST' && data && !opts.body) {
opts.body = data;
}
2022-02-10 14:47:44 +00: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 09:02:36 +00:00
if (!response.ok) {
if (
response.status === 401 &&
!path.startsWith('https://api.github') &&
2022-10-12 09:11:18 +00:00
!path.includes('/v4/')
) {
2022-07-06 09:02:36 +00:00
Cookies.remove('token');
}
throw responseData;
}
2022-02-10 14:47:44 +00:00
return responseData;
}
2022-07-06 09:02:36 +00:00
export function get(path: string, headers?: Record<string, unknown>): Promise<Record<string, any>> {
2022-02-10 14:47:44 +00:00
return send({ method: 'GET', path, headers });
}
2022-04-06 19:09:15 +00:00
export function del(
path: string,
data: Record<string, unknown>,
2022-04-12 14:49:52 +00:00
headers?: Record<string, unknown>
2022-07-06 09:02:36 +00:00
): Promise<Record<string, any>> {
2022-02-10 14:47:44 +00:00
return send({ method: 'DELETE', path, data, headers });
}
2022-04-06 19:09:15 +00:00
export function post(
path: string,
2022-09-21 13:48:32 +00:00
data: Record<string, unknown> | FormData,
2022-04-12 08:47:53 +00:00
headers?: Record<string, unknown>
2022-07-06 09:02:36 +00:00
): Promise<Record<string, any>> {
2022-02-10 14:47:44 +00:00
return send({ method: 'POST', path, data, headers });
}
2022-04-06 19:09:15 +00:00
export function put(
path: string,
data: Record<string, unknown>,
2022-04-12 14:49:52 +00:00
headers?: Record<string, unknown>
2022-07-06 09:02:36 +00:00
): Promise<Record<string, any>> {
2022-02-10 14:47:44 +00:00
return send({ method: 'PUT', path, data, headers });
}