import { getJwt } from "./amplify"; import type { GoalFilter, GoalInput, GoalResult, GoalUpdate } from "../models/goal"; import type { ProjectFilter, ProjectInput, ProjectResult, ProjectUpdate } from "../models/project"; import type { TaskFilter, TaskInput, TaskResult, TaskUpdate } from "../models/task"; import type { LogFilter, LogInput, LogResult, LogUpdate } from "../models/log"; import type { GroupInput, GroupResult, GroupUpdate } from "../models/group"; import type { ItemInput, ItemResult, ItemUpdate } from "../models/item"; export class StufflogClient { private root: string; constructor(root: string) { this.root = root; } async findGoal(id: string): Promise { const data = await this.fetch("GET", `/api/goal/${id}`); return data.goal; } async listGoals({minTime, maxTime, includesTime}: GoalFilter): Promise { let queries = []; if (minTime != null) { queries.push(`minTime=${minTime.toISOString()}`); } if (maxTime != null) { queries.push(`maxTime=${maxTime.toISOString()}`); } if (includesTime != null) { queries.push(`includesTime=${includesTime.toISOString()}`); } const query = queries.length > 0 ? `?${queries.join("&")}` : ""; const data = await this.fetch("GET", `/api/goal/${query}`); return data.goals; } async createGoal(input: GoalInput): Promise { const data = await this.fetch("POST", "/api/goal/", input); return data.goal; } async updateGoal(id: string, update: GoalUpdate): Promise { const data = await this.fetch("PUT", `/api/goal/${id}`, update); return data.goal; } async deleteGoal(id: string): Promise { const data = await this.fetch("DELETE", `/api/goal/${id}`); return data.goal; } async findProject(id: string): Promise { const data = await this.fetch("GET", `/api/project/${id}`); return data.project; } async listProjects({active, expiring}: ProjectFilter): Promise { let queries = []; if (active != null) { queries.push(`active=${active}`); } if (expiring != null) { queries.push(`expiring=${expiring}`); } const query = queries.length > 0 ? `?${queries.join("&")}` : ""; const data = await this.fetch("GET", `/api/project/${query}`); return data.projects; } async createProject(input: ProjectInput): Promise { const data = await this.fetch("POST", "/api/project/", input); return data.project; } async updateProject(id: string, update: ProjectUpdate): Promise { const data = await this.fetch("PUT", `/api/project/${id}`, update); return data.project; } async deleteProject(id: string): Promise { const data = await this.fetch("DELETE", `/api/project/${id}`); return data.project; } async findLog(id: string): Promise { const data = await this.fetch("GET", `/api/log/${id}`); return data.log; } async listLogs({minTime, maxTime}: LogFilter): Promise { let queries = []; if (minTime != null) { queries.push(`minTime=${minTime.toISOString()}`); } if (maxTime != null) { queries.push(`maxTime=${maxTime.toISOString()}`); } const query = queries.length > 0 ? `?${queries.join("&")}` : ""; const data = await this.fetch("GET", `/api/log/${query}`); return data.logs; } async createLog(input: LogInput): Promise { const data = await this.fetch("POST", "/api/log/", input); return data.log; } async updateLog(id: string, update: LogUpdate): Promise { const data = await this.fetch("PUT", `/api/log/${id}`, update); return data.log; } async deleteLog(id: string): Promise { const data = await this.fetch("DELETE", `/api/log/${id}`); return data.log; } async findTask(id: string): Promise { const data = await this.fetch("GET", `/api/task/${id}`); return data.task; } async listTasks({active, expiring}: TaskFilter): Promise { let queries = []; if (active != null) { queries.push(`active=${active}`); } if (expiring != null) { queries.push(`expiring=${expiring}`); } const query = queries.length > 0 ? `?${queries.join("&")}` : ""; const data = await this.fetch("GET", `/api/task/${query}`); return data.tasks; } async createTask(input: TaskInput): Promise { const data = await this.fetch("POST", "/api/task/", input); return data.task; } async updateTask(id: string, update: TaskUpdate): Promise { const data = await this.fetch("PUT", `/api/task/${id}`, update); return data.task; } async deleteTask(id: string): Promise { const data = await this.fetch("DELETE", `/api/task/${id}`); return data.task; } async findGroup(id: string): Promise { const data = await this.fetch("GET", `/api/group/${id}`); return data.group; } async listGroups(): Promise { const data = await this.fetch("GET", "/api/group/"); return data.groups; } async createGroup(input: GroupInput): Promise { const data = await this.fetch("POST", "/api/group/", input); return data.group; } async updateGroup(id: string, update: GroupUpdate): Promise { const data = await this.fetch("PUT", `/api/group/${id}`, update); return data.group; } async deleteGroup(id: string): Promise { const data = await this.fetch("DELETE", `/api/group/${id}`); return data.group; } async findItem(id: string): Promise { const data = await this.fetch("GET", `/api/item/${id}`); return data.item; } async listItems(): Promise { const data = await this.fetch("GET", "/api/item/"); return data.projects; } async createItem(input: ItemInput): Promise { const data = await this.fetch("POST", "/api/item/", input); return data.item; } async updateItem(id: string, update: ItemUpdate): Promise { const data = await this.fetch("PUT", `/api/item/${id}`, update); return data.item; } async deleteItem(id: string): Promise { const data = await this.fetch("DELETE", `/api/item/${id}`); return data.item; } async fetch(method: string, path: string, body?: object) { const fullPath = this.root + path; const req: RequestInit = {method, headers: {}} if (body != null) { const data = new Blob([JSON.stringify(body)]) req.headers["Content-Type"] = req; req.headers["Content-Length"] = data.size; req.body = data; } console.warn("AUTH SKIPPED, remember to change back in prod!") req.headers["Authorization"] = await getJwt(); const res = await fetch(fullPath, req); if (!res.ok) { if ((res.headers.get("Content-Type") || "").includes("application/json")) { const data = await res.json(); throw new StuffLogError(data.errorCode, data.errorMessage) } else { const text = await res.text(); throw new StuffLogError(res.status, text) } } return res.json(); } } export class StuffLogError { public code: number public message: string constructor(code: number, message: string) { this.code = code; this.message = message; } toString() { return `Error ${this.code}: ${this.message}`; } } const stuffLogClient = new StufflogClient(""); export default stuffLogClient