import { IMAGES_API_BASE_URL } from '$lib/constants'; export const getConfig = async (token: string = '') => { let error = null; const res = await fetch(`${IMAGES_API_BASE_URL}/config`, { 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.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; export const updateConfig = async (token: string = '', config: object) => { let error = null; const res = await fetch(`${IMAGES_API_BASE_URL}/config/update`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(token && { authorization: `Bearer ${token}` }) }, body: JSON.stringify({ ...config }) }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { console.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; 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.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; export const getImageGenerationConfig = async (token: string = '') => { let error = null; const res = await fetch(`${IMAGES_API_BASE_URL}/image/config`, { 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.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; export const updateImageGenerationConfig = async (token: string = '', config: object) => { let error = null; const res = await fetch(`${IMAGES_API_BASE_URL}/image/config/update`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(token && { authorization: `Bearer ${token}` }) }, body: JSON.stringify({ ...config }) }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { console.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; export const getImageGenerationModels = async (token: string = '') => { 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.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; }; 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.log(err); if ('detail' in err) { error = err.detail; } else { error = 'Server connection failed'; } return null; }); if (error) { throw error; } return res; };