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

Timothy J. Baek's avatar
Timothy J. Baek committed
131
	const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}/delete`, {
132
133
134
135
136
		method: 'DELETE',
		headers: {
			Accept: 'application/json',
			'Content-Type': 'application/json',
			authorization: `Bearer ${token}`
Timothy J. Baek's avatar
Timothy J. Baek committed
137
		}
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
	})
		.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;
};