ソースを参照

feat: add stacks api

Thomas Zhang 2 ヶ月 前
コミット
06ffa94247
2 ファイル変更125 行追加0 行削除
  1. 79 0
      src/lib/api/stacks.ts
  2. 46 0
      src/lib/stores/stacks.svelte.ts

+ 79 - 0
src/lib/api/stacks.ts

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

+ 46 - 0
src/lib/stores/stacks.svelte.ts

@@ -0,0 +1,46 @@
+import * as stacksApi from '$lib/api/stacks';
+import { ResourceStore } from '$lib/stores/resources.svelte';
+import type { StackCR } from '$lib/types/custom-resources/stack';
+import type { Stack } from '$lib/types/entities';
+
+class StacksStore extends ResourceStore<Stack, StackCR['status']> {
+	constructor() {
+		super('Stack', [
+			{
+				group: 'locostack.com',
+				version: 'v1alpha1',
+				resources: 'stacks',
+			},
+		]);
+	}
+
+	protected resourceHref(id: string) {
+		return `/resources/stacks/${id}`;
+	}
+
+	protected apiGet(workspaceId: string, id: string) {
+		return stacksApi.get(workspaceId, id);
+	}
+
+	protected apiGetStatus(workspaceId: string, id: string) {
+		return stacksApi.getStatus(workspaceId, id);
+	}
+
+	protected apiCreate(workspaceId: string, stack: Stack) {
+		return stacksApi.create(workspaceId, stack);
+	}
+
+	protected apiUpdate(workspaceId: string, stack: Stack) {
+		return stacksApi.update(workspaceId, stack);
+	}
+
+	protected apiDelete(workspaceId: string, id: string) {
+		return stacksApi.remove(workspaceId, id);
+	}
+
+	protected onStatusChange(id: string, status: StackCR['status'], notify = true): void {}
+}
+
+const stacksStore = new StacksStore();
+
+export { stacksStore };