noteService.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { API_BASE_URL, Note } from '../types'
  2. export const noteService = {
  3. // Fetch all notes (optional group filtering)
  4. getAll: async (token: string, groupId?: string, categoryId?: string): Promise<Note[]> => {
  5. const url = new URL(`${API_BASE_URL}/notes`, window.location.origin)
  6. if (groupId) {
  7. url.searchParams.append('groupId', groupId)
  8. }
  9. if (categoryId) {
  10. url.searchParams.append('categoryId', categoryId)
  11. }
  12. const response = await fetch(url.toString(), {
  13. headers: {
  14. Authorization: `Bearer ${token}`
  15. }
  16. })
  17. if (!response.ok) {
  18. throw new Error(`Failed to fetch notes: ${response.status} ${response.statusText}`)
  19. }
  20. return response.json()
  21. },
  22. // Create note
  23. create: async (token: string, data: { title: string, content: string, groupId: string, categoryId?: string }): Promise<Note> => {
  24. const response = await fetch(`${API_BASE_URL}/notes`, {
  25. method: 'POST',
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. Authorization: `Bearer ${token}`
  29. },
  30. body: JSON.stringify(data)
  31. })
  32. if (!response.ok) {
  33. throw new Error('Failed to create note')
  34. }
  35. return response.json()
  36. },
  37. // Update note
  38. update: async (token: string, id: string, data: { title?: string, content?: string, categoryId?: string }): Promise<Note> => {
  39. const response = await fetch(`${API_BASE_URL}/notes/${id}`, {
  40. method: 'PATCH',
  41. headers: {
  42. 'Content-Type': 'application/json',
  43. Authorization: `Bearer ${token}`
  44. },
  45. body: JSON.stringify(data)
  46. })
  47. if (!response.ok) {
  48. throw new Error('Failed to update note')
  49. }
  50. return response.json()
  51. },
  52. // Delete note
  53. delete: async (token: string, id: string): Promise<void> => {
  54. const response = await fetch(`${API_BASE_URL}/notes/${id}`, {
  55. method: 'DELETE',
  56. headers: {
  57. Authorization: `Bearer ${token}`
  58. }
  59. })
  60. if (!response.ok) {
  61. throw new Error('Failed to delete note')
  62. }
  63. },
  64. // Index note to knowledge base (vectorize)
  65. createFromPDFSelection: async (
  66. token: string,
  67. fileId: string,
  68. screenshot: Blob,
  69. groupId?: string,
  70. pageNumber?: number,
  71. ): Promise<Note> => {
  72. const formData = new FormData()
  73. formData.append('screenshot', screenshot, 'selection.png')
  74. formData.append('fileId', fileId)
  75. if (groupId) {
  76. formData.append('groupId', groupId)
  77. }
  78. if (pageNumber !== undefined) {
  79. formData.append('pageNumber', pageNumber.toString())
  80. }
  81. const response = await fetch(`${API_BASE_URL}/notes/from-pdf-selection`, {
  82. method: 'POST',
  83. headers: {
  84. Authorization: `Bearer ${token}`
  85. },
  86. body: formData
  87. })
  88. if (!response.ok) {
  89. throw new Error('Failed to create note from PDF selection')
  90. }
  91. return response.json()
  92. }
  93. }