cal.pub0.org/templates/endpoints/[id]/index.ts

53 lines
1.5 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { ResourceResponse } from "@lib/types";
import { schemaResourcePublic } from "@lib/validations/resource";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /v1/resources/{id}:
* get:
* summary: Find a resource
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: ID of the resource to get
* tags:
* - resources
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Resource was not found
*/
export async function resourceById({ query }: NextApiRequest, res: NextApiResponse<ResourceResponse>) {
const safe = schemaQueryIdParseInt.safeParse(query);
if (!safe.success) throw new Error("Invalid request query");
const resource = await prisma.resource.findUnique({ where: { id: safe.data.id } });
const data = schemaResourcePublic.parse(resource);
if (resource) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Resource was not found",
error,
});
}
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(resourceById));