open-webui/src/lib/apis/images/index.ts

233 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-02-22 02:12:01 +00:00
import { IMAGES_API_BASE_URL } from '$lib/constants';
2024-08-20 23:21:03 +00:00
export const getConfig = async (token: string = '') => {
2024-02-22 02:36:40 +00:00
let error = null;
2024-03-09 00:22:19 +00:00
const res = await fetch(`${IMAGES_API_BASE_URL}/config`, {
2024-02-22 02:36:40 +00:00
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-02-22 02:36:40 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
return res;
};
2024-08-20 23:21:03 +00:00
export const updateConfig = async (token: string = '', config: object) => {
2024-02-22 02:36:40 +00:00
let error = null;
2024-03-09 00:22:19 +00:00
const res = await fetch(`${IMAGES_API_BASE_URL}/config/update`, {
method: 'POST',
2024-02-22 02:36:40 +00:00
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
2024-03-09 00:22:19 +00:00
},
body: JSON.stringify({
2024-08-20 23:21:03 +00:00
...config
2024-03-09 01:38:10 +00:00
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-03-09 01:38:10 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
2024-04-23 11:14:31 +00:00
return res;
2024-03-09 01:38:10 +00:00
};
2024-08-21 15:27:39 +00:00
export const verifyConfigUrl = async (token: string = '') => {
let error = null;
const res = await fetch(`${IMAGES_API_BASE_URL}/config/url/verify`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-08-21 15:27:39 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
return res;
};
2024-08-20 23:21:03 +00:00
export const getImageGenerationConfig = async (token: string = '') => {
2024-02-22 02:12:01 +00:00
let error = null;
2024-08-20 23:21:03 +00:00
const res = await fetch(`${IMAGES_API_BASE_URL}/image/config`, {
2024-02-22 02:12:01 +00:00
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-02-22 02:12:01 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
2024-03-23 22:38:59 +00:00
return res;
2024-02-22 02:12:01 +00:00
};
2024-08-20 23:21:03 +00:00
export const updateImageGenerationConfig = async (token: string = '', config: object) => {
2024-02-22 02:12:01 +00:00
let error = null;
2024-08-20 23:21:03 +00:00
const res = await fetch(`${IMAGES_API_BASE_URL}/image/config/update`, {
2024-02-22 02:12:01 +00:00
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
},
2024-08-20 23:21:03 +00:00
body: JSON.stringify({ ...config })
2024-02-22 02:12:01 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-02-22 02:12:01 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
2024-03-23 22:38:59 +00:00
return res;
2024-02-22 02:12:01 +00:00
};
2024-03-09 00:22:19 +00:00
export const getImageGenerationModels = async (token: string = '') => {
2024-02-22 02:12:01 +00:00
let error = null;
const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-02-22 02:12:01 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
return res;
};
2024-02-22 02:36:40 +00:00
export const imageGenerations = async (token: string = '', prompt: string) => {
let error = null;
const res = await fetch(`${IMAGES_API_BASE_URL}/generations`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
},
body: JSON.stringify({
prompt: prompt
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
2024-02-22 02:36:40 +00:00
if ('detail' in err) {
error = err.detail;
} else {
error = 'Server connection failed';
}
return null;
});
if (error) {
throw error;
}
return res;
};