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.

267 lines
7.5 KiB

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