|
@@ -0,0 +1,102 @@
|
|
|
|
|
+import { resolve } from '$app/paths';
|
|
|
|
|
+
|
|
|
|
|
+import type { AgentCR } from '$lib/types/custom-resources';
|
|
|
|
|
+import type { Agent } from '$lib/types/entities';
|
|
|
|
|
+import type { Changes, CoreResources } from '$lib/types/workspace';
|
|
|
|
|
+
|
|
|
|
|
+import { handleResponse } from './types';
|
|
|
|
|
+
|
|
|
|
|
+const list = (workspaceId: string, fetch = globalThis.fetch): Promise<Agent[]> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ }),
|
|
|
|
|
+ ).then(handleResponse<Agent[]>);
|
|
|
|
|
+
|
|
|
|
|
+const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Agent> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ).then(handleResponse<Agent>);
|
|
|
|
|
+
|
|
|
|
|
+const getChanges = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Changes> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/changes', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ).then(handleResponse<Changes>);
|
|
|
|
|
+
|
|
|
|
|
+const getStatus = (
|
|
|
|
|
+ workspaceId: string,
|
|
|
|
|
+ id: string,
|
|
|
|
|
+ fetch = globalThis.fetch,
|
|
|
|
|
+): Promise<AgentCR['status']> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ).then(handleResponse<AgentCR['status']>);
|
|
|
|
|
+
|
|
|
|
|
+const getResources = (
|
|
|
|
|
+ workspaceId: string,
|
|
|
|
|
+ id: string,
|
|
|
|
|
+ fetch = globalThis.fetch,
|
|
|
|
|
+): Promise<CoreResources> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/resources', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ).then(handleResponse<CoreResources>);
|
|
|
|
|
+
|
|
|
|
|
+const create = (workspaceId: string, agent: Agent, fetch = globalThis.fetch): Promise<Agent> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ }),
|
|
|
|
|
+ {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify(agent),
|
|
|
|
|
+ },
|
|
|
|
|
+ ).then(handleResponse<Agent>);
|
|
|
|
|
+
|
|
|
|
|
+const update = (workspaceId: string, agent: Agent, fetch = globalThis.fetch): Promise<Agent> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id: agent.id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ {
|
|
|
|
|
+ method: 'PUT',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify(agent),
|
|
|
|
|
+ },
|
|
|
|
|
+ ).then(handleResponse<Agent>);
|
|
|
|
|
+
|
|
|
|
|
+const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<void> =>
|
|
|
|
|
+ fetch(
|
|
|
|
|
+ resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
|
|
|
|
|
+ workspaceId,
|
|
|
|
|
+ resource: 'agents',
|
|
|
|
|
+ id,
|
|
|
|
|
+ }),
|
|
|
|
|
+ {
|
|
|
|
|
+ method: 'DELETE',
|
|
|
|
|
+ },
|
|
|
|
|
+ ).then(async (res) => {
|
|
|
|
|
+ if (!res.ok) await handleResponse<void>(res);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+export { create, get, getChanges, getResources, getStatus, list, remove, update };
|