Created
October 10, 2022 20:57
-
-
Save brandonchadlange/cf0603ec40fa85739b359c4252d18a8e to your computer and use it in GitHub Desktop.
A route handler that uses a CRUD service to quickly map and handle requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NextApiRequest, NextApiResponse } from "next"; | |
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE"; | |
export const CrudRouteHandler = async <T>( | |
request: NextApiRequest, | |
response: NextApiResponse, | |
service: ICrudService<T> | |
) => { | |
const id = request.query.id as string; | |
if ((request.method as HttpMethod) === "GET") { | |
if (!id) { | |
const getAllResponse = await service.getAll(); | |
return response.status(200).json(getAllResponse); | |
} | |
const getSingleResponse = await service.getOne(id); | |
if (getSingleResponse === null) { | |
return response.status(404).send(null); | |
} | |
return response.status(200).json(getSingleResponse); | |
} | |
if ((request.method as HttpMethod) === "POST") { | |
const data = request.body as T; | |
const createResponse = await service.create(data); | |
return response.status(201).json(createResponse); | |
} | |
if ((request.method as HttpMethod) === "PUT") { | |
const data = request.body as T; | |
const updateResponse = await service.update(id, data); | |
return response.status(200); | |
} | |
if ((request.method as HttpMethod) === "DELETE") { | |
const deleteResponse = await service.delete(id); | |
return response.status(20).json(deleteResponse); | |
} | |
return response.status(405).send("Method not allowed"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment