Skip to main content

api-response

@rtorcato/api-response provides a single ok() helper that wraps your data in a consistent { success: true, data } envelope — the counterpart to the error shape produced by api-errors.

Install

pnpm add @rtorcato/api-response

Usage

import { ok } from '@rtorcato/api-response'

// Express
res.json(ok(user))
res.status(201).json(ok(item))

// Hono
return c.json(ok(items))
return c.json(ok(item), 201)

Response shape

{ "success": true, "data": { "id": "1", "name": "hello" } }

With an optional message:

ok(item, 'Item created successfully')
// → { "success": true, "data": { ... }, "message": "Item created successfully" }

Zod schemas for OpenAPI / testing

successSchema and errorSchema build zod schemas for the response envelopes — useful for OpenAPI contracts and test assertions.

import { successSchema, errorSchema } from '@rtorcato/api-response'
import { z } from 'zod'

const itemSchema = z.object({ id: z.string(), name: z.string() })

// For a success response containing an item
const responseSchema = successSchema(itemSchema)

// For error responses
const errSchema = errorSchema()

These pair with the OpenAPI spec helpers in the example apps.

Full response contract

FieldSuccessError
successtrue(absent — error handlers don't set it)
datapayload
messageoptionalhuman-readable error message
errorerror class name
codemachine-readable code