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) 1.01 kB
import { v4 as uuid } from 'uuid'; import { createBadRequest } from '@chubbyts/chubbyts-http-error/dist/http-error'; import { parseRequestBody } from '../request.js'; import { stringifyResponseBody, valueToData } from '../response.js'; import { zodToInvalidParameters } from '../zod-to-invalid-parameters.js'; export const createCreateHandler = (decoder, inputModelSchema, persistModel, responseFactory, enrichedModelSchema, encoder, enrichModel = async (model) => model) => { return async (request) => { const inputModelResult = inputModelSchema.safeParse(await parseRequestBody(decoder, request)); if (!inputModelResult.success) { throw createBadRequest({ invalidParameters: zodToInvalidParameters(inputModelResult.error) }); } return stringifyResponseBody(request, responseFactory(201), encoder, valueToData(enrichedModelSchema.parse(await enrichModel(await persistModel({ id: uuid(), createdAt: new Date(), ...inputModelResult.data }), { request })))); }; };