@chubbyts/chubbyts-api
Version:
[](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [ • 795 B
JavaScript
import { createNotFound } from '@chubbyts/chubbyts-http-error/dist/http-error';
import { z } from 'zod';
import { stringifyResponseBody, valueToData } from '../response.js';
const attributesSchema = z.object({ id: z.string() });
export const createReadHandler = (findModelById, responseFactory, modelResponseSchema, encoder, enrichModel = async (model) => model) => {
return async (request) => {
const id = attributesSchema.parse(request.attributes).id;
const model = await findModelById(id);
if (!model) {
throw createNotFound({ detail: `There is no entry with id "${id}"` });
}
return stringifyResponseBody(request, responseFactory(200), encoder, modelResponseSchema.parse(valueToData(await enrichModel(model, { request }))));
};
};