plugins.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { Fetcher } from 'swr'
  2. import { get, getMarketplace, post, upload } from './base'
  3. import type {
  4. Dependency,
  5. InstallPackageResponse,
  6. Permissions,
  7. PluginDeclaration,
  8. PluginInfoFromMarketPlace,
  9. PluginManifestInMarket,
  10. PluginTasksResponse,
  11. TaskStatusResponse,
  12. UninstallPluginResponse,
  13. updatePackageResponse,
  14. uploadGitHubResponse,
  15. } from '@/app/components/plugins/types'
  16. import type {
  17. MarketplaceCollectionPluginsResponse,
  18. MarketplaceCollectionsResponse,
  19. } from '@/app/components/plugins/marketplace/types'
  20. export const uploadFile = async (file: File, isBundle: boolean) => {
  21. const formData = new FormData()
  22. formData.append(isBundle ? 'bundle' : 'pkg', file)
  23. return upload({
  24. xhr: new XMLHttpRequest(),
  25. data: formData,
  26. }, false, `/workspaces/current/plugin/upload/${isBundle ? 'bundle' : 'pkg'}`)
  27. }
  28. export const updateFromMarketPlace = async (body: Record<string, string>) => {
  29. return post<InstallPackageResponse>('/workspaces/current/plugin/upgrade/marketplace', {
  30. body,
  31. })
  32. }
  33. export const updateFromGitHub = async (repoUrl: string, selectedVersion: string, selectedPackage: string,
  34. originalPlugin: string, newPlugin: string) => {
  35. return post<updatePackageResponse>('/workspaces/current/plugin/upgrade/github', {
  36. body: {
  37. repo: repoUrl,
  38. version: selectedVersion,
  39. package: selectedPackage,
  40. original_plugin_unique_identifier: originalPlugin,
  41. new_plugin_unique_identifier: newPlugin,
  42. },
  43. })
  44. }
  45. export const uploadGitHub = async (repoUrl: string, selectedVersion: string, selectedPackage: string) => {
  46. return post<uploadGitHubResponse>('/workspaces/current/plugin/upload/github', {
  47. body: {
  48. repo: repoUrl,
  49. version: selectedVersion,
  50. package: selectedPackage,
  51. },
  52. })
  53. }
  54. export const fetchIcon = (tenantId: string, fileName: string) => {
  55. return get(`workspaces/current/plugin/icon?tenant_id=${tenantId}&filename=${fileName}`)
  56. }
  57. export const fetchManifest = async (uniqueIdentifier: string) => {
  58. return get<PluginDeclaration>(`/workspaces/current/plugin/fetch-manifest?plugin_unique_identifier=${uniqueIdentifier}`)
  59. }
  60. export const fetchManifestFromMarketPlace = async (uniqueIdentifier: string) => {
  61. return getMarketplace<{ data: { plugin: PluginManifestInMarket, version: { version: string } } }>(`/plugins/identifier?unique_identifier=${uniqueIdentifier}`)
  62. }
  63. export const fetchBundleInfoFromMarketPlace = async ({
  64. org,
  65. name,
  66. version,
  67. }: Record<string, string>) => {
  68. return getMarketplace<{ data: { version: { dependencies: Dependency[] } } }>(`/bundles/${org}/${name}/${version}`)
  69. }
  70. export const fetchPluginInfoFromMarketPlace = async ({
  71. org,
  72. name,
  73. }: Record<string, string>) => {
  74. return getMarketplace<{ data: { plugin: PluginInfoFromMarketPlace, version: { version: string } } }>(`/plugins/${org}/${name}`)
  75. }
  76. export const fetchMarketplaceCollections: Fetcher<MarketplaceCollectionsResponse, { url: string; }> = ({ url }) => {
  77. return get<MarketplaceCollectionsResponse>(url)
  78. }
  79. export const fetchMarketplaceCollectionPlugins: Fetcher<MarketplaceCollectionPluginsResponse, { url: string }> = ({ url }) => {
  80. return get<MarketplaceCollectionPluginsResponse>(url)
  81. }
  82. export const fetchPluginTasks = async () => {
  83. return get<PluginTasksResponse>('/workspaces/current/plugin/tasks?page=1&page_size=255')
  84. }
  85. export const checkTaskStatus = async (taskId: string) => {
  86. return get<TaskStatusResponse>(`/workspaces/current/plugin/tasks/${taskId}`)
  87. }
  88. export const updatePermission = async (permissions: Permissions) => {
  89. return post('/workspaces/current/plugin/permission/change', { body: permissions })
  90. }
  91. export const uninstallPlugin = async (pluginId: string) => {
  92. return post<UninstallPluginResponse>('/workspaces/current/plugin/uninstall', { body: { plugin_installation_id: pluginId } })
  93. }