Comments (16)
@dodas update to elysia 1.0.6
from elysia.
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.
Returning an object in v1.0.3 is not converting to response to
application/json
type. Rather, it sends it out as atext/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.
Hi! Is there any place I can contribute to updating the docs for v1?
from elysia.
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.
@SaltyAom any thoughts on this. Seems like this completely prevents usage of plugins using derive
in v1.
from elysia.
@PureDizzi Thank you!
This seems to be working but it's ugly.
We need a fix on this!
from elysia.
Returning an object in v1.0.3 is not converting to response to
application/json
type. Rather, it sends it out as atext/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.
Returning an object in v1.0.3 is not converting to response to
application/json
type. Rather, it sends it out as atext/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.
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.
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.
But is it really a fool-proof solution?
Because then I'd have to set Content-Type
explicitly as well.
from elysia.
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 withContent-Type: application/json
from elysia.
@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.
@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:
Line 159 in da63011
from elysia.
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)
- onError: For 'VALIDATION' type errors, status code change does not work (200 is always returned) HOT 4
- Unable to access root level macros in plugins
- Cookie schema validation doesn't work with `aot: true` HOT 4
- RangeError: Maximum call stack size exceeded when render jsx after bun build HOT 1
- `redirect` doesn't work with `aot: false` HOT 1
- custom model causing eden to not infer route types
- `decorate` unexpectedly overwrites decorators everywhere HOT 1
- Eden Treat types break when using absolute import paths
- Eden treaty swallows errors HOT 1
- CORS not working correctly when aot is false
- Typing error when using `onError` with `derive` HOT 3
- requestIP return null when using sign cookie
- `ctx.query` doesn't work in some cases with `aot: true` HOT 2
- `aot` does not recognize the use of `ctx.body` within a `try catch` HOT 1
- rate limit feature or plugin !? HOT 1
- no `base64` in `t.String()` `format` option. Also no type support for the `byte` option.
- t.Literal type is ignored in favor of string during validation
- Eden Treaty ignores t.Transform()
- "default" option is not being applied on validation HOT 4
- Ability to create and remove websocket endpoints at runtime.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elysia.