Skip to content

Instantly share code, notes, and snippets.

@richard-gibson
Created June 25, 2020 17:07
Show Gist options
  • Save richard-gibson/a8de3366c12b5d34eb1d65021ef0e895 to your computer and use it in GitHub Desktop.
Save richard-gibson/a8de3366c12b5d34eb1d65021ef0e895 to your computer and use it in GitHub Desktop.
function todoRoutes(todoService: TodoService): Handler {
return router([
bind("/{id}", router([
bind(HttpMethod.GET, async request => {
const id = requiredPathParam(request, "id");
const todoEntry = todoService.fetch(id);
return (todoEntry)
? { statusCode: 200, body: JSON.stringify(todoEntry) }
: { statusCode: 404, body: '' };
}),
bind(HttpMethod.PUT, async (request) => {
const id = requiredPathParam(request, "id");
const todoEntry: TodoEntry = jsonBody(request);
todoService.save(id, todoEntry);
return { statusCode: 201, body: JSON.stringify(todoEntry) };
}),
bind(HttpMethod.DELETE, async (request) => {
const id = requiredPathParam(request, "id");
const deletedTodo = todoService.delete(id);
return (deletedTodo)
? { statusCode: 200, body: JSON.stringify(deletedTodo) }
: { statusCode: 404, body: '' };
})
])),
bind(["/", HttpMethod.GET], async () => ({ statusCode: 200, body: JSON.stringify(todoService.fetchAll(), null, 2) })),
bind(["/", HttpMethod.POST], async (request) => {
const todoEntry: TodoEntry = jsonBody(request);
return { statusCode: 200, body: JSON.stringify(todoService.save(undefined, todoEntry)) };
})
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment