import type { Fetcher } from 'swr' import { del, get, getMarketplace, post, upload } from './base' import type { CreateEndpointRequest, EndpointOperationResponse, EndpointsRequest, EndpointsResponse, InstallPackageResponse, Permissions, PluginDeclaration, PluginManifestInMarket, PluginTasksResponse, TaskStatusResponse, UninstallPluginResponse, UpdateEndpointRequest, uploadGitHubResponse, } from '@/app/components/plugins/types' import type { DebugInfo as DebugInfoTypes } from '@/app/components/plugins/types' import type { MarketplaceCollectionPluginsResponse, MarketplaceCollectionsResponse, } from '@/app/components/plugins/marketplace/types' export const createEndpoint: Fetcher = ({ url, body }) => { // url = /workspaces/current/endpoints/create return post(url, { body }) } export const fetchEndpointList: Fetcher = ({ url, params }) => { // url = /workspaces/current/endpoints/list/plugin?plugin_id=xxx return get(url, { params }) } export const deleteEndpoint: Fetcher = ({ url, endpointID }) => { // url = /workspaces/current/endpoints/delete return del(url, { body: { endpoint_id: endpointID } }) } export const updateEndpoint: Fetcher = ({ url, body }) => { // url = /workspaces/current/endpoints/update return post(url, { body }) } export const enableEndpoint: Fetcher = ({ url, endpointID }) => { // url = /workspaces/current/endpoints/enable return post(url, { body: { endpoint_id: endpointID } }) } export const disableEndpoint: Fetcher = ({ url, endpointID }) => { // url = /workspaces/current/endpoints/disable return post(url, { body: { endpoint_id: endpointID } }) } export const fetchDebugKey = async () => { return get('/workspaces/current/plugin/debugging-key') } export const uploadPackageFile = async (file: File) => { const formData = new FormData() formData.append('pkg', file) return upload({ xhr: new XMLHttpRequest(), data: formData, }, false, '/workspaces/current/plugin/upload/pkg') } export const installPackageFromLocal = async (uniqueIdentifier: string) => { return post('/workspaces/current/plugin/install/pkg', { body: { plugin_unique_identifiers: [uniqueIdentifier] }, }) } export const updateFromMarketPlace = async (body: Record) => { return post('/workspaces/current/plugin/upgrade/marketplace', { body, }) } export const uploadGitHub = async (repoUrl: string, selectedVersion: string, selectedPackage: string) => { return post('/workspaces/current/plugin/upload/github', { body: { repo: repoUrl, version: selectedVersion, package: selectedPackage, }, }) } export const installPackageFromGitHub = async (repoUrl: string, selectedVersion: string, selectedPackage: string, uniqueIdentifier: string) => { return post('/workspaces/current/plugin/install/github', { body: { repo: repoUrl, version: selectedVersion, package: selectedPackage, plugin_unique_identifier: uniqueIdentifier, }, }) } export const fetchIcon = (tenantId: string, fileName: string) => { return get(`workspaces/current/plugin/icon?tenant_id=${tenantId}&filename=${fileName}`) } export const fetchManifest = async (uniqueIdentifier: string) => { return get(`/workspaces/current/plugin/fetch-manifest?plugin_unique_identifier=${uniqueIdentifier}`) } export const fetchManifestFromMarketPlace = async (uniqueIdentifier: string) => { return getMarketplace<{ data: { plugin: PluginManifestInMarket } }>(`/plugins/identifier?unique_identifier=${uniqueIdentifier}`) } export const installPackageFromMarketPlace = async (uniqueIdentifier: string) => { return post('/workspaces/current/plugin/install/marketplace', { body: { plugin_unique_identifiers: [uniqueIdentifier] }, }) } export const fetchMarketplaceCollections: Fetcher = ({ url }) => { return get(url) } export const fetchMarketplaceCollectionPlugins: Fetcher = ({ url }) => { return get(url) } export const fetchPluginTasks = async () => { return get('/workspaces/current/plugin/tasks?page=1&page_size=255') } export const checkTaskStatus = async (taskId: string) => { return get(`/workspaces/current/plugin/tasks/${taskId}`) } export const fetchPermission = async () => { return get('/workspaces/current/plugin/permission/fetch') } export const updatePermission = async (permissions: Permissions) => { return post('/workspaces/current/plugin/permission/change', { body: permissions }) } export const uninstallPlugin = async (pluginId: string) => { return post('/workspaces/current/plugin/uninstall', { body: { plugin_installation_id: pluginId } }) }