-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoolify.js
More file actions
90 lines (78 loc) · 2.36 KB
/
Copy pathcoolify.js
File metadata and controls
90 lines (78 loc) · 2.36 KB
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
import axios from "axios";
import {
mapApplication,
mapService,
mapDatabase,
} from "../services/resourceMapper";
const api = axios.create({
baseURL: "/api/coolify",
headers: {
"Content-Type": "application/json",
},
});
api.interceptors.request.use((config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
localStorage.removeItem("token");
localStorage.removeItem("user");
window.location.href = "/login";
return Promise.reject(error);
}
const message =
error.response?.data?.message ||
error.message ||
"An error occurred while fetching data";
throw new Error(message);
}
);
export const fetchAllResources = async () => {
const [appsResponse, servicesResponse, databasesResponse] = await Promise.all(
[
api.get("/applications"),
api.get("/services"),
api.get("/databases").catch(() => ({ data: [] })),
]
);
const isDashboardResource = (resource) => {
const name = resource.name?.toLowerCase() || "";
return name.includes("coolify-dashboard") || name.includes("dashboard");
};
const applications = appsResponse.data
.filter((app) => !isDashboardResource(app))
.map(mapApplication);
const services = servicesResponse.data
.filter((service) => !isDashboardResource(service))
.map(mapService);
const databases = (databasesResponse.data || [])
.filter((db) => !isDashboardResource(db))
.map(mapDatabase);
return [...applications, ...services, ...databases];
};
export const getUserType = async () => {
const response = await api.get("/user-type");
return response.data.userType;
};
export const startResource = async (type, uuid) => {
const response = await api.post(`/${type}s/${uuid}/start`);
return response.data;
};
export const stopResource = async (type, uuid) => {
const response = await api.post(`/${type}s/${uuid}/stop`);
return response.data;
};
export const deleteResource = async (type, uuid) => {
const response = await api.delete(`/${type}s/${uuid}`);
return response.data;
};
export const getResourceLogs = async (type, uuid) => {
const response = await api.get(`/${type}s/${uuid}/logs`);
return response.data;
};