Review a generated Elysia order route

from Elysia
Elysia 1.4.30 / Bun 1.3.10 / TypeScript 6 advanced 10 min 4 issues to find

Review this generated Elysia order route before it ships.

Create one order for the authenticated actor’s account with a positive whole-number quantity, then publish its event consistently even if a failure is retried.

TypeScript
import { Elysia, t } from 'elysia'
import { db, events } from './services'
export const app = new Elysia().post(
  '/accounts/:accountId/orders',
  async ({ params, body, headers, status }) => {
    const actorId = headers['x-user-id']
    const account = await db.accounts.find(params.accountId)
    const product = await db.products.find(body.sku)
    if (!product || !account) {
      return status(404, { error: 'Not found' })
    }
    const order = {
      id: crypto.randomUUID(),
      accountId: params.accountId,
      actorId, sku: body.sku, quantity: body.quantity
    }
    await db.orders.insert(order)
    await events.publish('order.created', order)
    return status(201, order)
  },
  { body: t.Object({
    sku: t.String({ minLength: 1 }),
    quantity: t.Number()
  }) }
)

generated code is illustrative, not from any one model

Open in playground
Report an error