Skip to content

Instantly share code, notes, and snippets.

@joeelmahallawy
Created August 14, 2022 03:14
Show Gist options
  • Save joeelmahallawy/fef2ca9d4fe936ab17c51fd0fc2877c3 to your computer and use it in GitHub Desktop.
Save joeelmahallawy/fef2ca9d4fe936ab17c51fd0fc2877c3 to your computer and use it in GitHub Desktop.
import { NextApiRequest, NextApiResponse } from "next";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
switch (req.method) {
case "GET":
const getTodos = await fetch(
"https://api.withserve.com/v1/workflows/7cb831a8-2507-4e46-84d1-5bbc25419fb5?apiKey=9c2fb0d1-bbaf-4d78-a58d-2b590622cc6c",
{ method: req.method }
);
const todos = await getTodos.json();
res.json(todos);
case "POST":
const { todo } = JSON.parse(req.body);
const createTodo = await fetch(
"https://api.withserve.com/v1/workflows/e919a348-bea9-4413-9a6a-53134f58e18f?apiKey=983b5369-1c82-4726-b368-2ca691b2705d",
{
method: req.method,
body: JSON.stringify({
// generates random string
id: Math.random().toString(36),
task: todo,
}),
}
);
const create = await createTodo.json();
res.json(create);
case "DELETE":
const { id } = JSON.parse(req.body);
const deleteTodo = await fetch(
"https://api.withserve.com/v1/workflows/5222fbcc-c34c-4672-9f31-f4f093c5cea0?apiKey=6578bc95-ff72-4116-bb7c-6cf8c919a5a2",
{ method: req.method, body: JSON.stringify({ id }) }
);
const del = await deleteTodo.json();
res.json(del);
default:
throw new Error("Request method not supported");
}
} catch (error) {
res.send({ error: error.message });
}
};
export default handler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment