elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.59k stars 226 forks source link

Support class serialization in response #825

Open victor-teles opened 2 months ago

victor-teles commented 2 months ago

What version of Elysia is running?

1.1.12

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Instantiate a class and returns it with Elysia:

import { Elysia } from "elysia";

class MyClass {
  constructor(public name: string) {}
}

const object = new MyClass("Elysia");

const app = new Elysia().get("/", () => object).listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

The response from GET / is: [object Object]

CleanShot 2024-09-13 at 10 05 36@2x

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

yes

EugenVolosciuc commented 5 days ago

I got to the same issue when trying to return class instances of TypeORM entities. I ended up doing this for the time being:

new Elysia()
  .mapResponse(({ response }) => {
    return JSON.parse(JSON.stringify(response));
  })