Skip to content

Commit

Permalink
Refactor asset API
Browse files Browse the repository at this point in the history
  • Loading branch information
xremming committed Mar 28, 2021
1 parent a423487 commit f0ad2d3
Showing 1 changed file with 63 additions and 21 deletions.
84 changes: 63 additions & 21 deletions pages/api/v1/item.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,72 @@
import { requestLogger } from "logger";
import { NextApiRequest, NextApiResponse } from "next";

import { createItem, getItems, removeItem, updateItem } from "service/item";
import { GetItemsQuery, RemoveItemQuery } from "type/api";
import { ItemCreate, ItemUpdate, Items } from "type/item";

function get(query: NextApiRequest["query"]): Promise<Items> {
const params = GetItemsQuery.parse(query);
return getItems(params);
}
export default async function Item(req: NextApiRequest, res: NextApiResponse) {
const logger = requestLogger(req, res);

function create(body: NextApiRequest["body"]) {
const params = ItemCreate.parse(body);
return createItem(params);
}
async function get(query: NextApiRequest["query"]) {
const params = GetItemsQuery.safeParse(query);
if (!params.success) {
logger.warn(params.error, "invalid request when trying to get items");

function update(body: NextApiRequest["body"]) {
const params = ItemUpdate.parse(body);
return updateItem(params);
}
return res.status(400).json({ error: params.error });
}

function remove(query: NextApiRequest["query"]) {
const params = RemoveItemQuery.parse(query);
return removeItem(params);
}
res.json({ data: await getItems(params.data) });
}

async function create(body: NextApiRequest["body"]) {
logger.info("creating an item");

const params = ItemCreate.safeParse(body);
if (!params.success) {
logger.warn(
params.error,
"invalid request body when trying to create an item"
);

return res.status(400).json({ error: params.error });
}

res.json({ data: await createItem(params.data) });
}

async function update(body: NextApiRequest["body"]) {
logger.info("updating an item");

const params = ItemUpdate.safeParse(body);
if (!params.success) {
logger.warn(
params.error,
"invalid request body when trying to update an item"
);

return res.status(400).json({ error: params.error });
}

res.json({ data: await updateItem(params.data) });
}

async function remove(query: NextApiRequest["query"]) {
logger.info("removing an item");

const params = RemoveItemQuery.safeParse(query);
if (!params.success) {
logger.warn(
params.error,
"invalid request when trying to remove an item"
);

return res.status(400).json({ error: params.error });
}

res.json({ data: await removeItem(params.data) });
}

export default async function Item(req: NextApiRequest, res: NextApiResponse) {
const allow = "OPTIONS, GET, POST, PUT, DELETE";

try {
Expand All @@ -34,16 +76,16 @@ export default async function Item(req: NextApiRequest, res: NextApiResponse) {
return res.status(204).end();

case "GET":
return res.json({ data: await get(req.query) });
return await get(req.query);

case "POST":
return res.json({ data: await create(req.body) });
return await create(req.body);

case "PUT":
return res.json({ data: await update(req.body) });
return await update(req.body);

case "DELETE":
return res.json({ data: await remove(req.query) });
return await remove(req.query);

default:
res.setHeader("Allow", allow);
Expand Down

0 comments on commit f0ad2d3

Please sign in to comment.