Elysia plugin that automatically maps entity responses to DTOs for the Roastery CMS ecosystem.
baristaResponseMapper is an Elysia plugin that intercepts all responses after they are handled and automatically converts @roastery/beans Entity instances (and arrays of them) to plain DTOs via Mapper.toDTO. It also preserves the original HTTP status code when the response is wrapped in an ElysiaCustomStatusResponse.
| Tool | Purpose |
|---|---|
| Elysia | HTTP framework and plugin system |
| @roastery/barista | Elysia factory used across the Roastery ecosystem |
| @roastery/beans | Entity and Mapper base classes |
| Bun | Runtime, test runner, and package manager |
| tsup | Bundling to ESM + CJS with .d.ts generation |
bun add @roastery-capsules/api-response-mapperPeer dependencies:
bun add @roastery/barista @roastery/beansRegister the plugin on your Elysia app:
import { Elysia } from "elysia";
import { baristaResponseMapper } from "@roastery-capsules/api-response-mapper";
const app = new Elysia()
.use(baristaResponseMapper)
.get("/users/:id", ({ params }) => {
return new UserEntity({ id: params.id, name: "Alan" });
})
.listen(3000);The plugin registers a global onAfterHandle hook, so all routes and sub-apps are covered automatically.
- If the response is an
Entity, it is replaced withMapper.toDTO(entity). - If the response is an array where the first element is an
Entity, every item is mapped withMapper.toDTO. - If the response is wrapped in
ElysiaCustomStatusResponse(e.g. returned viastatus(201, entity)), it is unwrapped before mapping and the original status code is preserved. - Any other response type is left untouched.
# Run tests
bun run test:unit
# Run tests with coverage
bun run test:coverage
# Build for distribution
bun run build
# Check for unused exports and dependencies
bun run knip
# Full setup (build + bun link)
bun run setupMIT