You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
9.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import { getJwt } from "./amplify";
  2. import type { GoalFilter, GoalInput, GoalResult, GoalUpdate } from "../models/goal";
  3. import type { ProjectFilter, ProjectInput, ProjectResult, ProjectUpdate } from "../models/project";
  4. import type { TaskFilter, TaskInput, TaskLink, TaskResult, TaskUpdate } from "../models/task";
  5. import type { LogFilter, LogInput, LogResult, LogUpdate } from "../models/log";
  6. import type { GroupInput, GroupResult, GroupUpdate } from "../models/group";
  7. import type { ItemInput, ItemResult, ItemUpdate } from "../models/item";
  8. import type { ProjectGroupInput, ProjectGroupResult, ProjectGroupUpdate } from "../models/projectgroup";
  9. export class StufflogClient {
  10. private root: string;
  11. constructor(root: string) {
  12. this.root = root;
  13. }
  14. async findGoal(id: string): Promise<GoalResult> {
  15. const data = await this.fetch("GET", `/api/goal/${id}`);
  16. return data.goal;
  17. }
  18. async listGoals({minTime, maxTime, includesTime}: GoalFilter): Promise<GoalResult[]> {
  19. let queries = [];
  20. if (minTime != null) {
  21. queries.push(`minTime=${minTime.toISOString()}`);
  22. }
  23. if (maxTime != null) {
  24. queries.push(`maxTime=${maxTime.toISOString()}`);
  25. }
  26. if (includesTime != null) {
  27. queries.push(`includesTime=${includesTime.toISOString()}`);
  28. }
  29. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  30. const data = await this.fetch("GET", `/api/goal/${query}`);
  31. return data.goals;
  32. }
  33. async createGoal(input: GoalInput): Promise<GoalResult> {
  34. const data = await this.fetch("POST", "/api/goal/", input);
  35. return data.goal;
  36. }
  37. async updateGoal(id: string, update: GoalUpdate): Promise<GoalResult> {
  38. const data = await this.fetch("PUT", `/api/goal/${id}`, update);
  39. return data.goal;
  40. }
  41. async deleteGoal(id: string): Promise<GoalResult> {
  42. const data = await this.fetch("DELETE", `/api/goal/${id}`);
  43. return data.goal;
  44. }
  45. async findProject(id: string): Promise<ProjectResult> {
  46. const data = await this.fetch("GET", `/api/project/${id}`);
  47. return data.project;
  48. }
  49. async listProjects({active, expiring, favorite, includeSemiActive}: ProjectFilter): Promise<ProjectResult[]> {
  50. let queries = [];
  51. if (active != null) {
  52. queries.push(`active=${active}`);
  53. }
  54. if (expiring != null) {
  55. queries.push(`expiring=${expiring}`);
  56. }
  57. if (favorite != null) {
  58. queries.push(`favorite=${favorite}`)
  59. }
  60. if (includeSemiActive != null) {
  61. queries.push(`include-semi-active=${includeSemiActive}`)
  62. }
  63. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  64. const data = await this.fetch("GET", `/api/project/${query}`);
  65. return data.projects;
  66. }
  67. async createProject(input: ProjectInput): Promise<ProjectResult> {
  68. const data = await this.fetch("POST", "/api/project/", input);
  69. return data.project;
  70. }
  71. async updateProject(id: string, update: ProjectUpdate): Promise<ProjectResult> {
  72. const data = await this.fetch("PUT", `/api/project/${id}`, update);
  73. return data.project;
  74. }
  75. async deleteProject(id: string): Promise<ProjectResult> {
  76. const data = await this.fetch("DELETE", `/api/project/${id}`);
  77. return data.project;
  78. }
  79. async findProjectGroup(id: string): Promise<ProjectGroupResult> {
  80. const data = await this.fetch("GET", `/api/projectgroup/${id}`);
  81. return data.projectGroup;
  82. }
  83. async listProjectGroups(): Promise<ProjectGroupResult[]> {
  84. const data = await this.fetch("GET", `/api/projectgroup/`);
  85. return data.projectGroups;
  86. }
  87. async createProjectGroup(input: ProjectGroupInput): Promise<ProjectGroupResult> {
  88. const data = await this.fetch("POST", "/api/projectgroup/", input);
  89. return data.projectGroup;
  90. }
  91. async updateProjectGroup(id: string, input: ProjectGroupUpdate): Promise<ProjectGroupResult> {
  92. const data = await this.fetch("PUT", `/api/projectgroup/${id}`, input);
  93. return data.projectGroup;
  94. }
  95. async deleteProjectGroup(id: string): Promise<ProjectGroupResult> {
  96. const data = await this.fetch("DELETE", `/api/projectgroup/${id}`);
  97. return data.project;
  98. }
  99. async findLog(id: string): Promise<LogResult> {
  100. const data = await this.fetch("GET", `/api/log/${id}`);
  101. return data.log;
  102. }
  103. async listLogs({minTime, maxTime}: LogFilter): Promise<LogResult[]> {
  104. let queries = [];
  105. if (minTime != null) {
  106. queries.push(`minTime=${minTime.toISOString()}`);
  107. }
  108. if (maxTime != null) {
  109. queries.push(`maxTime=${maxTime.toISOString()}`);
  110. }
  111. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  112. const data = await this.fetch("GET", `/api/log/${query}`);
  113. return data.logs;
  114. }
  115. async createLog(input: LogInput): Promise<LogResult> {
  116. const data = await this.fetch("POST", "/api/log/", input);
  117. return data.log;
  118. }
  119. async updateLog(id: string, update: LogUpdate): Promise<LogResult> {
  120. const data = await this.fetch("PUT", `/api/log/${id}`, update);
  121. return data.log;
  122. }
  123. async deleteLog(id: string): Promise<LogResult> {
  124. const data = await this.fetch("DELETE", `/api/log/${id}`);
  125. return data.log;
  126. }
  127. async createTaskLink(projectId: string, taskId: string): Promise<TaskLink> {
  128. const data = await this.fetch("PUT", `/api/task/${taskId}/link/${projectId}`);
  129. return data.taskLink;
  130. }
  131. async deleteTaskLink(projectId: string, taskId: string): Promise<TaskLink> {
  132. const data = await this.fetch("DELETE", `/api/task/${taskId}/link/${projectId}`);
  133. return data.taskLink;
  134. }
  135. async findTask(id: string): Promise<TaskResult> {
  136. const data = await this.fetch("GET", `/api/task/${id}`);
  137. return data.task;
  138. }
  139. async listTasks({active, expiring}: TaskFilter): Promise<TaskResult[]> {
  140. let queries = [];
  141. if (active != null) {
  142. queries.push(`active=${active}`);
  143. }
  144. if (expiring != null) {
  145. queries.push(`expiring=${expiring}`);
  146. }
  147. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  148. const data = await this.fetch("GET", `/api/task/${query}`);
  149. return data.tasks;
  150. }
  151. async createTask(input: TaskInput): Promise<TaskResult> {
  152. const data = await this.fetch("POST", "/api/task/", input);
  153. return data.task;
  154. }
  155. async updateTask(id: string, update: TaskUpdate): Promise<TaskResult> {
  156. const data = await this.fetch("PUT", `/api/task/${id}`, update);
  157. return data.task;
  158. }
  159. async deleteTask(id: string): Promise<TaskResult> {
  160. const data = await this.fetch("DELETE", `/api/task/${id}`);
  161. return data.task;
  162. }
  163. async findGroup(id: string): Promise<GroupResult> {
  164. const data = await this.fetch("GET", `/api/group/${id}`);
  165. return data.group;
  166. }
  167. async listGroups(): Promise<GroupResult[]> {
  168. const data = await this.fetch("GET", "/api/group/");
  169. return data.groups;
  170. }
  171. async createGroup(input: GroupInput): Promise<GroupResult> {
  172. const data = await this.fetch("POST", "/api/group/", input);
  173. return data.group;
  174. }
  175. async updateGroup(id: string, update: GroupUpdate): Promise<GroupResult> {
  176. const data = await this.fetch("PUT", `/api/group/${id}`, update);
  177. return data.group;
  178. }
  179. async deleteGroup(id: string): Promise<GroupResult> {
  180. const data = await this.fetch("DELETE", `/api/group/${id}`);
  181. return data.group;
  182. }
  183. async findItem(id: string): Promise<ItemResult> {
  184. const data = await this.fetch("GET", `/api/item/${id}`);
  185. return data.item;
  186. }
  187. async listItems(): Promise<ItemResult[]> {
  188. const data = await this.fetch("GET", "/api/item/");
  189. return data.projects;
  190. }
  191. async createItem(input: ItemInput): Promise<ItemResult> {
  192. const data = await this.fetch("POST", "/api/item/", input);
  193. return data.item;
  194. }
  195. async updateItem(id: string, update: ItemUpdate): Promise<ItemResult> {
  196. const data = await this.fetch("PUT", `/api/item/${id}`, update);
  197. return data.item;
  198. }
  199. async deleteItem(id: string): Promise<ItemResult> {
  200. const data = await this.fetch("DELETE", `/api/item/${id}`);
  201. return data.item;
  202. }
  203. async fetch(method: string, path: string, body?: object) {
  204. const fullPath = this.root + path;
  205. const req: RequestInit = {method, headers: {}}
  206. if (body != null) {
  207. const data = new Blob([JSON.stringify(body)])
  208. req.headers["Content-Type"] = req;
  209. req.headers["Content-Length"] = data.size;
  210. req.body = data;
  211. }
  212. req.headers["Authorization"] = await getJwt();
  213. const res = await fetch(fullPath, req);
  214. if (!res.ok) {
  215. if ((res.headers.get("Content-Type") || "").includes("application/json")) {
  216. const data = await res.json();
  217. throw new StuffLogError(data.errorCode, data.errorMessage)
  218. } else {
  219. const text = await res.text();
  220. throw new StuffLogError(res.status, text)
  221. }
  222. }
  223. return res.json();
  224. }
  225. }
  226. export class StuffLogError {
  227. public code: number
  228. public message: string
  229. constructor(code: number, message: string) {
  230. this.code = code;
  231. this.message = message;
  232. }
  233. toString() {
  234. return `Error ${this.code}: ${this.message}`;
  235. }
  236. }
  237. const stuffLogClient = new StufflogClient("");
  238. export default stuffLogClient