Skip to content

roastery-cms/capsules-api-response-mapper

Repository files navigation

@roastery-capsules/api-response-mapper

Elysia plugin that automatically maps entity responses to DTOs for the Roastery CMS ecosystem.

Checked with Biome

Overview

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.

Technologies

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

Installation

bun add @roastery-capsules/api-response-mapper

Peer dependencies:

bun add @roastery/barista @roastery/beans

Usage

Register 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.

What it does

  • If the response is an Entity, it is replaced with Mapper.toDTO(entity).
  • If the response is an array where the first element is an Entity, every item is mapped with Mapper.toDTO.
  • If the response is wrapped in ElysiaCustomStatusResponse (e.g. returned via status(201, entity)), it is unwrapped before mapping and the original status code is preserved.
  • Any other response type is left untouched.

Development

# 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 setup

License

MIT

About

Elysia plugin that automatically maps entity responses to DTOs for the Roastery CMS ecosystem.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors