index.ts 3.13 KB
Newer Older
1
2
import { WEBUI_API_BASE_URL } from '$lib/constants';

Timothy J. Baek's avatar
Timothy J. Baek committed
3
export const addNewModel = async (token: string, model: object) => {
4
5
	let error = null;

Timothy J. Baek's avatar
Timothy J. Baek committed
6
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/add`, {
7
8
9
10
11
12
		method: 'POST',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
		},
Timothy J. Baek's avatar
Timothy J. Baek committed
13
		body: JSON.stringify(model)
14
15
16
17
18
19
	})
		.then(async (res) => {
			if (!res.ok) throw await res.json();
			return res.json();
		})
		.catch((err) => {
20
			error = err.detail;
21
22
23
24
25
26
27
28
29
30
31
			console.log(err);
			return null;
		});

	if (error) {
		throw error;
	}

	return res;
};

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
32
export const getModelInfos = async (token: string = '') => {
33
34
	let error = null;

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
35
	const res = await fetch(`${WEBUI_API_BASE_URL}/models`, {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		method: 'GET',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
		}
	})
		.then(async (res) => {
			if (!res.ok) throw await res.json();
			return res.json();
		})
		.then((json) => {
			return json;
		})
		.catch((err) => {
			error = err;
			console.log(err);
			return null;
		});

	if (error) {
		throw error;
	}

Timothy J. Baek's avatar
Timothy J. Baek committed
60
	return res;
61
62
};

Timothy J. Baek's avatar
Timothy J. Baek committed
63
export const getModelById = async (token: string, id: string) => {
64
65
	let error = null;

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
66
67
	const searchParams = new URLSearchParams();
	searchParams.append('id', id);
Timothy J. Baek's avatar
Timothy J. Baek committed
68

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
69
	const res = await fetch(`${WEBUI_API_BASE_URL}/models?${searchParams.toString()}`, {
Timothy J. Baek's avatar
Timothy J. Baek committed
70
		method: 'GET',
71
72
73
74
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
Timothy J. Baek's avatar
Timothy J. Baek committed
75
		}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	})
		.then(async (res) => {
			if (!res.ok) throw await res.json();
			return res.json();
		})
		.then((json) => {
			return json;
		})
		.catch((err) => {
			error = err;

			console.log(err);
			return null;
		});

	if (error) {
		throw error;
	}

Timothy J. Baek's avatar
Timothy J. Baek committed
95
	return res;
96
97
};

Timothy J. Baek's avatar
Timothy J. Baek committed
98
export const updateModelById = async (token: string, id: string, model: object) => {
99
100
	let error = null;

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
101
102
	const searchParams = new URLSearchParams();
	searchParams.append('id', id);
Timothy J. Baek's avatar
Timothy J. Baek committed
103

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
104
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/update?${searchParams.toString()}`, {
105
106
107
108
109
110
		method: 'POST',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
		},
Timothy J. Baek's avatar
Timothy J. Baek committed
111
		body: JSON.stringify(model)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	})
		.then(async (res) => {
			if (!res.ok) throw await res.json();
			return res.json();
		})
		.then((json) => {
			return json;
		})
		.catch((err) => {
			error = err;

			console.log(err);
			return null;
		});

	if (error) {
		throw error;
	}

	return res;
};

Timothy J. Baek's avatar
Timothy J. Baek committed
134
export const deleteModelById = async (token: string, id: string) => {
135
136
	let error = null;

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
137
138
	const searchParams = new URLSearchParams();
	searchParams.append('id', id);
Timothy J. Baek's avatar
Timothy J. Baek committed
139

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
140
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/delete?${searchParams.toString()}`, {
141
142
143
144
145
		method: 'DELETE',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
Timothy J. Baek's avatar
Timothy J. Baek committed
146
		}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	})
		.then(async (res) => {
			if (!res.ok) throw await res.json();
			return res.json();
		})
		.then((json) => {
			return json;
		})
		.catch((err) => {
			error = err;

			console.log(err);
			return null;
		});

	if (error) {
		throw error;
	}

	return res;
};