index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { IMAGES_API_BASE_URL } from '$lib/constants';
  2. export const getConfig = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${IMAGES_API_BASE_URL}/config`, {
  5. method: 'GET',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. ...(token && { authorization: `Bearer ${token}` })
  10. }
  11. })
  12. .then(async (res) => {
  13. if (!res.ok) throw await res.json();
  14. return res.json();
  15. })
  16. .catch((err) => {
  17. console.error(err);
  18. if ('detail' in err) {
  19. error = err.detail;
  20. } else {
  21. error = 'Server connection failed';
  22. }
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const updateConfig = async (token: string = '', config: object) => {
  31. let error = null;
  32. const res = await fetch(`${IMAGES_API_BASE_URL}/config/update`, {
  33. method: 'POST',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. ...(token && { authorization: `Bearer ${token}` })
  38. },
  39. body: JSON.stringify({
  40. ...config
  41. })
  42. })
  43. .then(async (res) => {
  44. if (!res.ok) throw await res.json();
  45. return res.json();
  46. })
  47. .catch((err) => {
  48. console.error(err);
  49. if ('detail' in err) {
  50. error = err.detail;
  51. } else {
  52. error = 'Server connection failed';
  53. }
  54. return null;
  55. });
  56. if (error) {
  57. throw error;
  58. }
  59. return res;
  60. };
  61. export const verifyConfigUrl = async (token: string = '') => {
  62. let error = null;
  63. const res = await fetch(`${IMAGES_API_BASE_URL}/config/url/verify`, {
  64. method: 'GET',
  65. headers: {
  66. Accept: 'application/json',
  67. 'Content-Type': 'application/json',
  68. ...(token && { authorization: `Bearer ${token}` })
  69. }
  70. })
  71. .then(async (res) => {
  72. if (!res.ok) throw await res.json();
  73. return res.json();
  74. })
  75. .catch((err) => {
  76. console.error(err);
  77. if ('detail' in err) {
  78. error = err.detail;
  79. } else {
  80. error = 'Server connection failed';
  81. }
  82. return null;
  83. });
  84. if (error) {
  85. throw error;
  86. }
  87. return res;
  88. };
  89. export const getImageGenerationConfig = async (token: string = '') => {
  90. let error = null;
  91. const res = await fetch(`${IMAGES_API_BASE_URL}/image/config`, {
  92. method: 'GET',
  93. headers: {
  94. Accept: 'application/json',
  95. 'Content-Type': 'application/json',
  96. ...(token && { authorization: `Bearer ${token}` })
  97. }
  98. })
  99. .then(async (res) => {
  100. if (!res.ok) throw await res.json();
  101. return res.json();
  102. })
  103. .catch((err) => {
  104. console.error(err);
  105. if ('detail' in err) {
  106. error = err.detail;
  107. } else {
  108. error = 'Server connection failed';
  109. }
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return res;
  116. };
  117. export const updateImageGenerationConfig = async (token: string = '', config: object) => {
  118. let error = null;
  119. const res = await fetch(`${IMAGES_API_BASE_URL}/image/config/update`, {
  120. method: 'POST',
  121. headers: {
  122. Accept: 'application/json',
  123. 'Content-Type': 'application/json',
  124. ...(token && { authorization: `Bearer ${token}` })
  125. },
  126. body: JSON.stringify({ ...config })
  127. })
  128. .then(async (res) => {
  129. if (!res.ok) throw await res.json();
  130. return res.json();
  131. })
  132. .catch((err) => {
  133. console.error(err);
  134. if ('detail' in err) {
  135. error = err.detail;
  136. } else {
  137. error = 'Server connection failed';
  138. }
  139. return null;
  140. });
  141. if (error) {
  142. throw error;
  143. }
  144. return res;
  145. };
  146. export const getImageGenerationModels = async (token: string = '') => {
  147. let error = null;
  148. const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
  149. method: 'GET',
  150. headers: {
  151. Accept: 'application/json',
  152. 'Content-Type': 'application/json',
  153. ...(token && { authorization: `Bearer ${token}` })
  154. }
  155. })
  156. .then(async (res) => {
  157. if (!res.ok) throw await res.json();
  158. return res.json();
  159. })
  160. .catch((err) => {
  161. console.error(err);
  162. if ('detail' in err) {
  163. error = err.detail;
  164. } else {
  165. error = 'Server connection failed';
  166. }
  167. return null;
  168. });
  169. if (error) {
  170. throw error;
  171. }
  172. return res;
  173. };
  174. export const imageGenerations = async (token: string = '', prompt: string) => {
  175. let error = null;
  176. const res = await fetch(`${IMAGES_API_BASE_URL}/generations`, {
  177. method: 'POST',
  178. headers: {
  179. Accept: 'application/json',
  180. 'Content-Type': 'application/json',
  181. ...(token && { authorization: `Bearer ${token}` })
  182. },
  183. body: JSON.stringify({
  184. prompt: prompt
  185. })
  186. })
  187. .then(async (res) => {
  188. if (!res.ok) throw await res.json();
  189. return res.json();
  190. })
  191. .catch((err) => {
  192. console.error(err);
  193. if ('detail' in err) {
  194. error = err.detail;
  195. } else {
  196. error = 'Server connection failed';
  197. }
  198. return null;
  199. });
  200. if (error) {
  201. throw error;
  202. }
  203. return res;
  204. };