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.

284 lines
8.0 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, includeSemiActive}: 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. if (includeSemiActive != null) {
  60. queries.push(`include-semi-active=${includeSemiActive}`)
  61. }
  62. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  63. const data = await this.fetch("GET", `/api/project/${query}`);
  64. return data.projects;
  65. }
  66. async createProject(input: ProjectInput): Promise<ProjectResult> {
  67. const data = await this.fetch("POST", "/api/project/", input);
  68. return data.project;
  69. }
  70. async updateProject(id: string, update: ProjectUpdate): Promise<ProjectResult> {
  71. const data = await this.fetch("PUT", `/api/project/${id}`, update);
  72. return data.project;
  73. }
  74. async deleteProject(id: string): Promise<ProjectResult> {
  75. const data = await this.fetch("DELETE", `/api/project/${id}`);
  76. return data.project;
  77. }
  78. async findLog(id: string): Promise<LogResult> {
  79. const data = await this.fetch("GET", `/api/log/${id}`);
  80. return data.log;
  81. }
  82. async listLogs({minTime, maxTime}: LogFilter): Promise<LogResult[]> {
  83. let queries = [];
  84. if (minTime != null) {
  85. queries.push(`minTime=${minTime.toISOString()}`);
  86. }
  87. if (maxTime != null) {
  88. queries.push(`maxTime=${maxTime.toISOString()}`);
  89. }
  90. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  91. const data = await this.fetch("GET", `/api/log/${query}`);
  92. return data.logs;
  93. }
  94. async createLog(input: LogInput): Promise<LogResult> {
  95. const data = await this.fetch("POST", "/api/log/", input);
  96. return data.log;
  97. }
  98. async updateLog(id: string, update: LogUpdate): Promise<LogResult> {
  99. const data = await this.fetch("PUT", `/api/log/${id}`, update);
  100. return data.log;
  101. }
  102. async deleteLog(id: string): Promise<LogResult> {
  103. const data = await this.fetch("DELETE", `/api/log/${id}`);
  104. return data.log;
  105. }
  106. async createTaskLink(projectId: string, taskId: string): Promise<TaskLink> {
  107. const data = await this.fetch("PUT", `/api/task/${taskId}/link/${projectId}`);
  108. return data.taskLink;
  109. }
  110. async deleteTaskLink(projectId: string, taskId: string): Promise<TaskLink> {
  111. const data = await this.fetch("DELETE", `/api/task/${taskId}/link/${projectId}`);
  112. return data.taskLink;
  113. }
  114. async findTask(id: string): Promise<TaskResult> {
  115. const data = await this.fetch("GET", `/api/task/${id}`);
  116. return data.task;
  117. }
  118. async listTasks({active, expiring}: TaskFilter): Promise<TaskResult[]> {
  119. let queries = [];
  120. if (active != null) {
  121. queries.push(`active=${active}`);
  122. }
  123. if (expiring != null) {
  124. queries.push(`expiring=${expiring}`);
  125. }
  126. const query = queries.length > 0 ? `?${queries.join("&")}` : "";
  127. const data = await this.fetch("GET", `/api/task/${query}`);
  128. return data.tasks;
  129. }
  130. async createTask(input: TaskInput): Promise<TaskResult> {
  131. const data = await this.fetch("POST", "/api/task/", input);
  132. return data.task;
  133. }
  134. async updateTask(id: string, update: TaskUpdate): Promise<TaskResult> {
  135. const data = await this.fetch("PUT", `/api/task/${id}`, update);
  136. return data.task;
  137. }
  138. async deleteTask(id: string): Promise<TaskResult> {
  139. const data = await this.fetch("DELETE", `/api/task/${id}`);
  140. return data.task;
  141. }
  142. async findGroup(id: string): Promise<GroupResult> {
  143. const data = await this.fetch("GET", `/api/group/${id}`);
  144. return data.group;
  145. }
  146. async listGroups(): Promise<GroupResult[]> {
  147. const data = await this.fetch("GET", "/api/group/");
  148. return data.groups;
  149. }
  150. async createGroup(input: GroupInput): Promise<GroupResult> {
  151. const data = await this.fetch("POST", "/api/group/", input);
  152. return data.group;
  153. }
  154. async updateGroup(id: string, update: GroupUpdate): Promise<GroupResult> {
  155. const data = await this.fetch("PUT", `/api/group/${id}`, update);
  156. return data.group;
  157. }
  158. async deleteGroup(id: string): Promise<GroupResult> {
  159. const data = await this.fetch("DELETE", `/api/group/${id}`);
  160. return data.group;
  161. }
  162. async findItem(id: string): Promise<ItemResult> {
  163. const data = await this.fetch("GET", `/api/item/${id}`);
  164. return data.item;
  165. }
  166. async listItems(): Promise<ItemResult[]> {
  167. const data = await this.fetch("GET", "/api/item/");
  168. return data.projects;
  169. }
  170. async createItem(input: ItemInput): Promise<ItemResult> {
  171. const data = await this.fetch("POST", "/api/item/", input);
  172. return data.item;
  173. }
  174. async updateItem(id: string, update: ItemUpdate): Promise<ItemResult> {
  175. const data = await this.fetch("PUT", `/api/item/${id}`, update);
  176. return data.item;
  177. }
  178. async deleteItem(id: string): Promise<ItemResult> {
  179. const data = await this.fetch("DELETE", `/api/item/${id}`);
  180. return data.item;
  181. }
  182. async fetch(method: string, path: string, body?: object) {
  183. const fullPath = this.root + path;
  184. const req: RequestInit = {method, headers: {}}
  185. if (body != null) {
  186. const data = new Blob([JSON.stringify(body)])
  187. req.headers["Content-Type"] = req;
  188. req.headers["Content-Length"] = data.size;
  189. req.body = data;
  190. }
  191. req.headers["Authorization"] = await getJwt();
  192. const res = await fetch(fullPath, req);
  193. if (!res.ok) {
  194. if ((res.headers.get("Content-Type") || "").includes("application/json")) {
  195. const data = await res.json();
  196. throw new StuffLogError(data.errorCode, data.errorMessage)
  197. } else {
  198. const text = await res.text();
  199. throw new StuffLogError(res.status, text)
  200. }
  201. }
  202. return res.json();
  203. }
  204. }
  205. export class StuffLogError {
  206. public code: number
  207. public message: string
  208. constructor(code: number, message: string) {
  209. this.code = code;
  210. this.message = message;
  211. }
  212. toString() {
  213. return `Error ${this.code}: ${this.message}`;
  214. }
  215. }
  216. const stuffLogClient = new StufflogClient("");
  217. export default stuffLogClient