| 56 | }); |
| 57 | |
| 58 | const apiResponse = async ( |
| 59 | pathParams: string[], |
| 60 | method: Method, |
| 61 | body?: string |
| 62 | ) => { |
| 63 | const [id] = pathParams; |
| 64 | if (id) { |
| 65 | if (method === "DELETE") { |
| 66 | await API.delete(id); |
| 67 | return response("", { |
| 68 | contentType: "application/json", |
| 69 | }); |
| 70 | } |
| 71 | if (method === "PUT") { |
| 72 | const { text, completedDate } = JSON.parse(body); |
| 73 | const item = await API.update({ id, text, completedDate }); |
| 74 | return response(JSON.stringify(item), { |
| 75 | contentType: "application/json", |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (pathParams.length == 0 && method === "POST") { |
| 81 | const { text } = JSON.parse(body); |
| 82 | const item = await API.create(text); |
| 83 | return response(JSON.stringify(item), { |
| 84 | contentType: "application/json", |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | throw new HttpError(404, "Not found"); |
| 89 | }; |
| 90 | |
| 91 | const appResponse = async () => { |
| 92 | let todoItems; |