Coder Social home page Coder Social logo

Comments (16)

SaltyAom avatar SaltyAom commented on June 12, 2024 2

@dodas update to elysia 1.0.6

from elysia.

kabir-asani avatar kabir-asani commented on June 12, 2024 1

Returning an object in v1.0.3 is not converting to response to application/json type. Rather, it sends it out as a text/plain. How do I fix this?

from elysia.

PureDizzi avatar PureDizzi commented on June 12, 2024 1

Returning an object in v1.0.3 is not converting to response to application/json type. Rather, it sends it out as a text/plain. How do I fix this?

@kabir-asani A quick workaround I found is to spread the object into another object.

e.g.
return { ...someObject }

from elysia.

cybercoder-naj avatar cybercoder-naj commented on June 12, 2024

Hi! Is there any place I can contribute to updating the docs for v1?

from elysia.

dodas avatar dodas commented on June 12, 2024

How is this supposed to work with Plugins?
Consider this example:

import { Elysia } from "elysia";

const myPlugin = () => (a: Elysia) =>
  a.derive(() => ({ myPluginProp: 1 }));

const app = new Elysia()
  .use(myPlugin())
  .get("/my-plugin", ({ myPluginProp }) => {
//                      ^ Property 'myPluginProp' does not exist on type '{ body: unknown; ...
    return myPluginProp
  });

It seems the app actually gets myPluginProp on runtime (expected), just TS types say otherwise.

from elysia.

dodas avatar dodas commented on June 12, 2024

@SaltyAom any thoughts on this. Seems like this completely prevents usage of plugins using derive in v1.

from elysia.

kabir-asani avatar kabir-asani commented on June 12, 2024

@PureDizzi Thank you!
This seems to be working but it's ugly.

We need a fix on this!

from elysia.

april83c avatar april83c commented on June 12, 2024

Returning an object in v1.0.3 is not converting to response to application/json type. Rather, it sends it out as a text/plain. How do I fix this?

@kabir-asani A quick workaround I found is to spread the object into another object.

e.g. return { ...someObject }

For correct typing in the Eden client, return { ...new Example() } as Example seems to work (this bug is so weird... has anyone made an issue for it yet?)

from elysia.

SaltyAom avatar SaltyAom commented on June 12, 2024

Returning an object in v1.0.3 is not converting to response to application/json type. Rather, it sends it out as a text/plain. How do I fix this?

@kabir-asani A quick workaround I found is to spread the object into another object.

e.g. return { ...someObject }

For correct typing in the Eden client, return { ...new Example() } as Example seems to work (this bug is so weird... has anyone made an issue for it yet?)

I want to get this fix.
Can you provide some example that cause this problem?

from elysia.

kabir-asani avatar kabir-asani commented on June 12, 2024
import { Elysia } from "elysia";

class Data {
    message: String;

    constructor(message: String) {
        this.message = message;
    }
}

const app = new Elysia()
    .get("/ping", ({ set }) => {
        const data = new Data("pong");

        set.status = 200;

        return data;
    })
    .listen(3000);

@SaltyAom
This snippet is enough to replicate the issue -- please check.

NOTE: What I've noticed is that the issue arises only when I try to access the set object.

from elysia.

SaltyAom avatar SaltyAom commented on June 12, 2024
import { Elysia } from "elysia";

class Data {
    message: String;

    constructor(message: String) {
        this.message = message;
    }
}

const app = new Elysia()
    .get("/ping", ({ set }) => {
        const data = new Data("pong");

        set.status = 200;

        return data;
    })
    .listen(3000);

@SaltyAom This snippet is enough to replicate the issue -- please check.

NOTE: What I've noticed is that the issue arises only when I try to access the set object.

Hi, sorry for the slow response.

Unfortunately, this is an expected behavior and the case without using set is a bug.

Web Standard on different runtime has a slight implementation for mapping value to Response.

Some classes may expected to pass to Response directly while some classes are not on different implementation, instead of serializing all the unknown case, we leave this gray area for users to handle themself using either mapResponse or defining a serialization method using toString

In this case, if you are using a custom class, you may use toString method like this instead.

class Data {
	message: String

	constructor(message: String) {
		this.message = message
	}

	toString() {
		return JSON.stringify(this)
	}
}

from elysia.

kabir-asani avatar kabir-asani commented on June 12, 2024

But is it really a fool-proof solution?
Because then I'd have to set Content-Type explicitly as well.

from elysia.

dodas avatar dodas commented on June 12, 2024

I propose this:

  • have users define toJSON() method on their data classes, which returns the plain, json-serializable representation of the object
  • have Elysia call toJSON() method if it's available and if so, respond with that along with Content-Type: application/json

from elysia.

binyamin avatar binyamin commented on June 12, 2024

@SaltyAom I understand that you don't want to introduce breaking changes after v1. Have you seen #99 (comment)? I am biased, but that seems like an easy thing to slip in with other breaking changes.

from elysia.

bogeychan avatar bogeychan commented on June 12, 2024

@april83c, @kabir-asani, you can do something like this as a workaround:

const Data = class Object {
  message: String;

  constructor(message: String) {
    this.message = message;
  }
};

return new Data("yay") // inside handler

The problem is this switch on constructor name:

switch (response?.constructor?.name) {

from elysia.

bogeychan avatar bogeychan commented on June 12, 2024

or implement toJSON like this:

class Data {
  message: String;

  constructor(message: String) {
    this.message = message;
  }

  toJSON() {
    return structuredClone(this);
  }
}

That way you dont have to set Content-Type:

new Elysia()
  .get("/", () => {
    return new Data("yay").toJSON();
  })
  .listen(8080);

Its the same behaviour as return { "message": "yay" }

from elysia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.