index.ts 3.03 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;

66
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}`, {
Timothy J. Baek's avatar
Timothy J. Baek committed
67
		method: 'GET',
68
69
70
71
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
Timothy J. Baek's avatar
Timothy J. Baek committed
72
		}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	})
		.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
92
	return res;
93
94
};

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

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
98
99
	const searchParams = new URLSearchParams();
	searchParams.append('id', id);
Timothy J. Baek's avatar
Timothy J. Baek committed
100

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
101
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/update?${searchParams.toString()}`, {
102
103
104
105
106
107
		method: 'POST',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
		},
Timothy J. Baek's avatar
Timothy J. Baek committed
108
		body: JSON.stringify(model)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	})
		.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
131
export const deleteModelById = async (token: string, id: string) => {
132
133
	let error = null;

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
134
135
	const searchParams = new URLSearchParams();
	searchParams.append('id', id);
Timothy J. Baek's avatar
Timothy J. Baek committed
136

Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
137
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/delete?${searchParams.toString()}`, {
138
139
140
141
142
		method: 'DELETE',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
Timothy J. Baek's avatar
Timothy J. Baek committed
143
		}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	})
		.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;
};