noteService.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { API_BASE_URL, Note } from '../types'
  2. export const noteService = {
  3. // すべてのノートを取得(オプションでグループによるフィルタリングが可能)
  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. // ノートを作成
  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. // ノートを更新
  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. categoryId?: string,
  71. pageNumber?: number,
  72. ): Promise<Note> => {
  73. const formData = new FormData()
  74. formData.append('screenshot', screenshot, 'selection.png')
  75. formData.append('fileId', fileId)
  76. if (groupId) {
  77. formData.append('groupId', groupId)
  78. }
  79. if (categoryId) {
  80. formData.append('categoryId', categoryId)
  81. }
  82. if (pageNumber !== undefined) {
  83. formData.append('pageNumber', pageNumber.toString())
  84. }
  85. const response = await fetch(`${API_BASE_URL}/notes/from-pdf-selection`, {
  86. method: 'POST',
  87. headers: {
  88. Authorization: `Bearer ${token}`
  89. },
  90. body: formData
  91. })
  92. if (!response.ok) {
  93. throw new Error('Failed to create note from PDF selection')
  94. }
  95. return response.json()
  96. }
  97. }