Browse Source

feat: add skills api

Thomas Zhang 2 months ago
parent
commit
00ba130b3c
2 changed files with 125 additions and 0 deletions
  1. 79 0
      src/lib/api/skills.ts
  2. 46 0
      src/lib/stores/skills.svelte.ts

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

@@ -0,0 +1,79 @@
+import { resolve } from '$app/paths';
+
+import type { SkillCR } from '$lib/types/custom-resources/skill';
+import type { Skill } from '$lib/types/entities';
+
+import { handleResponse } from './types';
+
+const list = (workspaceId: string, fetch = globalThis.fetch): Promise<Skill[]> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
+			workspaceId,
+			resource: 'skills',
+		}),
+	).then(handleResponse<Skill[]>);
+
+const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Skill> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
+			workspaceId,
+			resource: 'skills',
+			id,
+		}),
+	).then(handleResponse<Skill>);
+
+const getStatus = (
+	workspaceId: string,
+	id: string,
+	fetch = globalThis.fetch,
+): Promise<SkillCR['status']> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', {
+			workspaceId,
+			resource: 'skills',
+			id,
+		}),
+	).then(handleResponse<SkillCR['status']>);
+
+const create = (workspaceId: string, skill: Skill, fetch = globalThis.fetch): Promise<Skill> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
+			workspaceId,
+			resource: 'skills',
+		}),
+		{
+			method: 'POST',
+			headers: { 'Content-Type': 'application/json' },
+			body: JSON.stringify(skill),
+		},
+	).then(handleResponse<Skill>);
+
+const update = (workspaceId: string, skill: Skill, fetch = globalThis.fetch): Promise<Skill> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
+			workspaceId,
+			resource: 'skills',
+			id: skill.id,
+		}),
+		{
+			method: 'PUT',
+			headers: { 'Content-Type': 'application/json' },
+			body: JSON.stringify(skill),
+		},
+	).then(handleResponse<Skill>);
+
+const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<void> =>
+	fetch(
+		resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
+			workspaceId,
+			resource: 'skills',
+			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/skills.svelte.ts

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