UNPKG

@chubbyts/chubbyts-api

Version:

[![CI](https://github.com/chubbyts/chubbyts-api/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-api/badge.svg?branch=maste

15 lines (14 loc) 795 B
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 })))); }; };