Comments (5)
I've been trying to find out the cause of this for some time, from the tests I've done, apparently the decorates I've been using on different controllers are replacing each other.
For some reason in the controllers function when I say to group the urlController after the userController, the decorate that the userController uses (userService) is being deleted and replaced by the decorate of the urlController (urlService).
When I call the /user/create route, the result is that services.user.create is not a defined function
from elysia.
@vitorpldev, can you please post code snippets in text form? checkout Creating and highlighting code blocks
from elysia.
Okay, the code I gave as an example is this:
import Elysia, { t } from "elysia";
const user_decorator = (application: Elysia) => {
return application.decorate({
services: {
find: (name: string) => `The name of user is ${name}`,
},
});
};
const url_decorator = (application: Elysia) => {
return application.decorate({
services: {
find: (url: string) => `The url is ${url}`,
},
});
};
const user_controller = (application: Elysia) => {
return application.use(user_decorator).get(
"/",
({ query, services }) => {
return new Response(services.find(query.value), { status: 200 });
},
{
query: t.Object({ value: t.String() }),
}
);
};
const url_controller = (application: Elysia) => {
return application.use(url_decorator).get(
"/",
({ query, services }) => {
return new Response(services.find(query.value), { status: 200 });
},
{
query: t.Object({ value: t.String() }),
}
);
};
const controller = (application: Elysia) => {
return application
.group("/user", (user) => {
return user.use(user_controller);
})
.group("/url", (url) => {
return url.use(url_controller);
});
};
const app = new Elysia().use(controller);
app.listen({ hostname: "localhost", port: 3030 }, (server) => {
console.log(`Server is running at ${server.url}`);
});
If in this code I make a request for GET
for http://localhost:3030/user/ passing the value "Elysia" in the query, the expected would be "The name of user is Elysia", but the result is "The url is Elysia".
This is because the way in controllers I am grouping the url_controller after the user_controller, the decorate of the url_controller is replacing the decorate of the user_controller
from elysia.
@vitorpldev
Both user_decorator
and url_decorator
is decorating same services
, I think it's expected to get overridden.
You can either
- Use different name to decorate instead of
services
likeuser_services
andurl_services
- If you want to keep it as it is, you might consider using
derive
instead as you can define the scopes
from elysia.
Perfect, thank you my friend.
from elysia.
Related Issues (20)
- Eden Treaty ignores t.Transform()
- "default" option is not being applied on validation HOT 5
- Ability to create and remove websocket endpoints at runtime.
- Macro modify schema HOT 1
- Rxjs pipe syntax
- Nullable field for types as documented in OpenAPI 3.0 HOT 1
- Support for classes and getter fields
- Transform type (include Date) won't transform in Array schema
- Query params validation for array of string fail at 1.0.23 HOT 5
- Plugin registered by async inline function don't work
- Cookie Schema doesn't work in instances HOT 1
- Return 405 error for wrong method on a resource, rather than 404
- Failed to bind socket in linux
- `mapResponse` is completely ignored HOT 2
- Please Export Typebox Types HOT 1
- staticPlugin didn't supply chinese path
- Context object not correctly passed to handler HOT 2
- Cannot use Plain Typebox Types for Schema
- Normalize not working for children instances
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.