index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. import { getTimeRange } from '$lib/utils';
  3. type NoteItem = {
  4. title: string;
  5. data: object;
  6. meta?: null | object;
  7. access_control?: null | object;
  8. };
  9. export const createNewNote = async (token: string, note: NoteItem) => {
  10. let error = null;
  11. const res = await fetch(`${WEBUI_API_BASE_URL}/notes/create`, {
  12. method: 'POST',
  13. headers: {
  14. Accept: 'application/json',
  15. 'Content-Type': 'application/json',
  16. authorization: `Bearer ${token}`
  17. },
  18. body: JSON.stringify({
  19. ...note
  20. })
  21. })
  22. .then(async (res) => {
  23. if (!res.ok) throw await res.json();
  24. return res.json();
  25. })
  26. .catch((err) => {
  27. error = err.detail;
  28. console.log(err);
  29. return null;
  30. });
  31. if (error) {
  32. throw error;
  33. }
  34. return res;
  35. };
  36. export const getNotes = async (token: string = '') => {
  37. let error = null;
  38. const res = await fetch(`${WEBUI_API_BASE_URL}/notes/`, {
  39. method: 'GET',
  40. headers: {
  41. Accept: 'application/json',
  42. 'Content-Type': 'application/json',
  43. authorization: `Bearer ${token}`
  44. }
  45. })
  46. .then(async (res) => {
  47. if (!res.ok) throw await res.json();
  48. return res.json();
  49. })
  50. .then((json) => {
  51. return json;
  52. })
  53. .catch((err) => {
  54. error = err.detail;
  55. console.log(err);
  56. return null;
  57. });
  58. if (error) {
  59. throw error;
  60. }
  61. if (!Array.isArray(res)) {
  62. return {}; // or throw new Error("Notes response is not an array")
  63. }
  64. // Build the grouped object
  65. const grouped: Record<string, any[]> = {};
  66. for (const note of res) {
  67. const timeRange = getTimeRange(note.updated_at / 1000000000);
  68. if (!grouped[timeRange]) {
  69. grouped[timeRange] = [];
  70. }
  71. grouped[timeRange].push({
  72. ...note,
  73. timeRange
  74. });
  75. }
  76. return grouped;
  77. };
  78. export const getNoteById = async (token: string, id: string) => {
  79. let error = null;
  80. const res = await fetch(`${WEBUI_API_BASE_URL}/notes/${id}`, {
  81. method: 'GET',
  82. headers: {
  83. Accept: 'application/json',
  84. 'Content-Type': 'application/json',
  85. authorization: `Bearer ${token}`
  86. }
  87. })
  88. .then(async (res) => {
  89. if (!res.ok) throw await res.json();
  90. return res.json();
  91. })
  92. .then((json) => {
  93. return json;
  94. })
  95. .catch((err) => {
  96. error = err.detail;
  97. console.log(err);
  98. return null;
  99. });
  100. if (error) {
  101. throw error;
  102. }
  103. return res;
  104. };
  105. export const updateNoteById = async (token: string, id: string, note: NoteItem) => {
  106. let error = null;
  107. const res = await fetch(`${WEBUI_API_BASE_URL}/notes/${id}/update`, {
  108. method: 'POST',
  109. headers: {
  110. Accept: 'application/json',
  111. 'Content-Type': 'application/json',
  112. authorization: `Bearer ${token}`
  113. },
  114. body: JSON.stringify({
  115. ...note
  116. })
  117. })
  118. .then(async (res) => {
  119. if (!res.ok) throw await res.json();
  120. return res.json();
  121. })
  122. .then((json) => {
  123. return json;
  124. })
  125. .catch((err) => {
  126. error = err.detail;
  127. console.log(err);
  128. return null;
  129. });
  130. if (error) {
  131. throw error;
  132. }
  133. return res;
  134. };
  135. export const deleteNoteById = async (token: string, id: string) => {
  136. let error = null;
  137. const res = await fetch(`${WEBUI_API_BASE_URL}/notes/${id}/delete`, {
  138. method: 'DELETE',
  139. headers: {
  140. Accept: 'application/json',
  141. 'Content-Type': 'application/json',
  142. authorization: `Bearer ${token}`
  143. }
  144. })
  145. .then(async (res) => {
  146. if (!res.ok) throw await res.json();
  147. return res.json();
  148. })
  149. .then((json) => {
  150. return json;
  151. })
  152. .catch((err) => {
  153. error = err.detail;
  154. console.log(err);
  155. return null;
  156. });
  157. if (error) {
  158. throw error;
  159. }
  160. return res;
  161. };