UNPKG

@zeushq/nextjs-zapi

Version:

Next.js SDK for creating a Zeus API

36 lines (27 loc) 895 B
import { NextApiRequest, NextApiResponse } from 'next'; import { ZApiResource } from '../../handlers/resource'; import createDebug from '../utils/debug'; const debug = createDebug('handlers'); export type HandleShow = (req: NextApiRequest, res: NextApiResponse) => Promise<void>; export default function showHandlerFactory(): HandleShow { return async (req, res) => { const zapi = (res as any).zapi as ZApiResource; debug('show', zapi); let object; let where = {} as any; where[zapi.primaryKey] = zapi.routeParams.id; if (zapi.scope?.on && zapi.scope?.with) { where[zapi.scope.on] = await zapi?.scope.with(req, res); object = await zapi.model.findFirst({ where }); } else { object = await zapi.model.findUnique({ where }); } const output = { object } as any; res.json(output); res.end(); }; }