sync-mutation.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { postJSON, deleteJSON } from '../../../infrastructure/fetch-json'
  2. import { Folder } from '@ol-types/folder'
  3. import { Doc } from '@ol-types/doc'
  4. export function syncRename(
  5. projectId: string,
  6. entityType: string,
  7. entityId: string,
  8. newName: string
  9. ) {
  10. return postJSON(
  11. `/project/${projectId}/${getEntityPathName(entityType)}/${entityId}/rename`,
  12. {
  13. body: {
  14. name: newName,
  15. },
  16. }
  17. )
  18. }
  19. export function syncDelete(
  20. projectId: string,
  21. entityType: string,
  22. entityId: string
  23. ) {
  24. return deleteJSON(
  25. `/project/${projectId}/${getEntityPathName(entityType)}/${entityId}`
  26. )
  27. }
  28. export function syncMove(
  29. projectId: string,
  30. entityType: string,
  31. entityId: string,
  32. toFolderId: string
  33. ) {
  34. return postJSON(
  35. `/project/${projectId}/${getEntityPathName(entityType)}/${entityId}/move`,
  36. {
  37. body: {
  38. folder_id: toFolderId,
  39. },
  40. }
  41. )
  42. }
  43. export type NewDocEntity = {
  44. endpoint: 'doc'
  45. name: string
  46. }
  47. export type NewFolderEntity = {
  48. endpoint: 'folder'
  49. name: string
  50. }
  51. export type NewLinkedFileEntity = {
  52. endpoint: 'linked_file'
  53. name: string
  54. provider: string
  55. data: Record<string, any>
  56. }
  57. export type NewEntity = NewDocEntity | NewFolderEntity | NewLinkedFileEntity
  58. type SyncCreateEntityReturn<T> = T extends NewDocEntity
  59. ? Promise<Doc>
  60. : T extends NewFolderEntity
  61. ? Promise<Folder>
  62. : T extends NewLinkedFileEntity
  63. ? Promise<{ new_file_id: string }>
  64. : never
  65. export function syncCreateEntity<T extends NewEntity>(
  66. projectId: string,
  67. parentFolderId: string,
  68. newEntityData: T
  69. ): SyncCreateEntityReturn<T> {
  70. const { endpoint, ...newEntity } = newEntityData
  71. return postJSON(`/project/${projectId}/${endpoint}`, {
  72. body: {
  73. parent_folder_id: parentFolderId,
  74. ...newEntity,
  75. },
  76. }) as SyncCreateEntityReturn<T>
  77. }
  78. function getEntityPathName(entityType: string) {
  79. return entityType === 'fileRef' ? 'file' : entityType
  80. }
  81. export function syncRootDocId(projectId: string, rootDocId: string) {
  82. return postJSON(`/project/${projectId}/settings`, {
  83. body: { rootDocId },
  84. })
  85. }