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.

281 lines
7.9 KiB

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