|
|
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, TaskLink, 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<GoalResult> { const data = await this.fetch("GET", `/api/goal/${id}`); return data.goal; }
async listGoals({minTime, maxTime, includesTime}: GoalFilter): Promise<GoalResult[]> { 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<GoalResult> { const data = await this.fetch("POST", "/api/goal/", input); return data.goal; }
async updateGoal(id: string, update: GoalUpdate): Promise<GoalResult> { const data = await this.fetch("PUT", `/api/goal/${id}`, update); return data.goal; }
async deleteGoal(id: string): Promise<GoalResult> { const data = await this.fetch("DELETE", `/api/goal/${id}`); return data.goal; }
async findProject(id: string): Promise<ProjectResult> { const data = await this.fetch("GET", `/api/project/${id}`); return data.project; }
async listProjects({active, expiring, favorite}: ProjectFilter): Promise<ProjectResult[]> { let queries = []; if (active != null) { queries.push(`active=${active}`); } if (expiring != null) { queries.push(`expiring=${expiring}`); } if (favorite != null) { queries.push(`favorite=${favorite}`) }
const query = queries.length > 0 ? `?${queries.join("&")}` : "";
const data = await this.fetch("GET", `/api/project/${query}`); return data.projects; }
async createProject(input: ProjectInput): Promise<ProjectResult> { const data = await this.fetch("POST", "/api/project/", input); return data.project; }
async updateProject(id: string, update: ProjectUpdate): Promise<ProjectResult> { const data = await this.fetch("PUT", `/api/project/${id}`, update); return data.project; }
async deleteProject(id: string): Promise<ProjectResult> { const data = await this.fetch("DELETE", `/api/project/${id}`); return data.project; }
async findLog(id: string): Promise<LogResult> { const data = await this.fetch("GET", `/api/log/${id}`); return data.log; }
async listLogs({minTime, maxTime}: LogFilter): Promise<LogResult[]> { 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<LogResult> { const data = await this.fetch("POST", "/api/log/", input); return data.log; }
async updateLog(id: string, update: LogUpdate): Promise<LogResult> { const data = await this.fetch("PUT", `/api/log/${id}`, update); return data.log; }
async deleteLog(id: string): Promise<LogResult> { const data = await this.fetch("DELETE", `/api/log/${id}`); return data.log; }
async createTaskLink(projectId: string, taskId: string): Promise<TaskLink> { const data = await this.fetch("PUT", `/api/task/${taskId}/link/${projectId}`); return data.taskLink; }
async deleteTaskLink(projectId: string, taskId: string): Promise<TaskLink> { const data = await this.fetch("DELETE", `/api/task/${taskId}/link/${projectId}`); return data.taskLink; }
async findTask(id: string): Promise<TaskResult> { const data = await this.fetch("GET", `/api/task/${id}`); return data.task; }
async listTasks({active, expiring}: TaskFilter): Promise<TaskResult[]> { 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<TaskResult> { const data = await this.fetch("POST", "/api/task/", input); return data.task; }
async updateTask(id: string, update: TaskUpdate): Promise<TaskResult> { const data = await this.fetch("PUT", `/api/task/${id}`, update); return data.task; }
async deleteTask(id: string): Promise<TaskResult> { const data = await this.fetch("DELETE", `/api/task/${id}`); return data.task; } async findGroup(id: string): Promise<GroupResult> { const data = await this.fetch("GET", `/api/group/${id}`); return data.group; }
async listGroups(): Promise<GroupResult[]> { const data = await this.fetch("GET", "/api/group/"); return data.groups; }
async createGroup(input: GroupInput): Promise<GroupResult> { const data = await this.fetch("POST", "/api/group/", input); return data.group; }
async updateGroup(id: string, update: GroupUpdate): Promise<GroupResult> { const data = await this.fetch("PUT", `/api/group/${id}`, update); return data.group; }
async deleteGroup(id: string): Promise<GroupResult> { const data = await this.fetch("DELETE", `/api/group/${id}`); return data.group; }
async findItem(id: string): Promise<ItemResult> { const data = await this.fetch("GET", `/api/item/${id}`); return data.item; }
async listItems(): Promise<ItemResult[]> { const data = await this.fetch("GET", "/api/item/"); return data.projects; }
async createItem(input: ItemInput): Promise<ItemResult> { const data = await this.fetch("POST", "/api/item/", input); return data.item; }
async updateItem(id: string, update: ItemUpdate): Promise<ItemResult> { const data = await this.fetch("PUT", `/api/item/${id}`, update); return data.item; }
async deleteItem(id: string): Promise<ItemResult> { 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; }
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
|