Comments (18)
I'll take a look at the issues. Also instead of injecting an adapter, you can use module instead:
const module = await app.inject<HttpModule>(HttpModule);
module.use(json());
from node-decorators.
I'm currently injecting both the module and the adapter because of the experiments.
from node-decorators.
Could you upload an example so I can see the whole flow?
from node-decorators.
Unfortuantely, I can't. I can only provide general information.
from node-decorators.
@Just-Niko please try version beta.4 if it resolves your issues
from node-decorators.
It works partially with @Render()
now, but the custom decorator I have @CurrentUser()
returns undefined for some properties. I tried with @Req()
directly on the controller and it works.
export function Req() {
return createParamDecorator((context) => {
return (context as HttpContext).getRequest<Request>()
})
}
export function CurrentUser() {
return createParamDecorator((context) => {
const req = (context as HttpContext).getRequest<Request>()
const { jwt, organization } = req.session
return { jwt, organization, userId: jwt?.idTokenPayload?.['cognito:username'] }
})
}
@Controller()
@Pipe(AuthPipe)
export class DashboardController {
@Get('/')
@Render('dashboard')
dashboard(@CurrentUser() user: User, @Req() req: Request) {
console.log(user.organization) // returns undefined
console.log(req.session.organization) // returns the organization data
return {
user
}
}
}
Unfortunately with res.render()
I still get ERR_HTTP_HEADERS_SENT and @CurrentUser()
and @Req()
have the same behavior.
from node-decorators.
Do you have @Render and res.render at the same time?
from node-decorators.
@serhiisol No as it's shown in the code. I switch between them for testing like that:
@Controller()
@Pipe(AuthPipe)
export class DashboardController {
@Get('/')
dashboard(@CurrentUser() user: User, @Req() req: Request, @Res() res: Response) {
console.log(user.organization) // returns undefined
console.log(req.session.organization) // returns the organization data
return res.render('dashboard', {
user
})
}
}
from node-decorators.
Try beta.6 release.
I've update param validation, it should resolve the problem with custom parameter.
Also I've tried to update an example app with the session and custom render method, I don't see any problems, see here https://github.com/serhiisol/decorators-server-example. If you still have a problem, could you reuse example app repo with repro steps.
from node-decorators.
Hmm, I still getting undefined properties (with @Render()) for the @CurrentUser()
. I did some console logging and here's the result:
export function CurrentUser() {
return createParamDecorator((context) => {
console.log('[CurrentUser] start')
const req = (context as HttpContext).getRequest<Request>()
const { jwt, organization } = req.session
console.log('[CurrentUser] end', organization)
return { jwt, organization, userId: jwt?.idTokenPayload?.['cognito:username'] }
})
}
[CurrentUser] start
[CurrentUser] end undefined
[CurrentUser] start
[CurrentUser] end undefined
[AuthPipe] return handle()
[Dashboard] user.organization undefined
[Dashboard] req.session.organization Organization { /* the data for is here */ }
Without @Render() and res.render I still get ERR_HTTP_HEADERS_SENT.
from node-decorators.
There's no magic with the res
and the req
objects. Param extraction happens in the route handler, basically something like:
app.get('/endpoint', (req, res) => {
const params = extractParams(metadata, req, res);
return actualRouteHandler(...params);
});
If it's not there then you might have session misconfigured.
from node-decorators.
That's really strange. On the legacy version it was working, and now it doesn't. I'll continue investigating tomorrow.
from node-decorators.
With this decorator:
and this route
node-decorators/server/example/modules/posts/posts.controller.ts
Lines 30 to 36 in 7358215
I receive the user object and rendering goes well. Also tried res.render + session middleware.
from node-decorators.
I added a check in the AuthPipe right before return handle() and it's not throwing. So it looks like it's lost somehow.
if (!req.session.organization) {
throw new InternalServerError()
}
from node-decorators.
Btw, pipes mechanism also doesn't modify params. It could be that one of your pipes modifies request. Pipe execution order is:
Global Pipe -> controller pipe - method pipe -> route handler -> method pipe -> controller pipe -> global pipe.
from node-decorators.
Yes the AuthPipe is modifying the request.
from node-decorators.
Now I also see the ERR_HTTP_HEADERS_SENT when I try to render an error page.
@Injectable()
export class ServerPipe<T> implements ProcessPipe {
async run(context: HttpContext, handle: PipeHandle<T>) {
const req = context.getRequest<Request>()
const res = context.getResponse<Response>()
try {
return await handle()
} catch (error) {
console.error(error)
const e = <ApiError>error
if (req.path.startsWith('/api')) {
return res.status(e.status).json({
error: res.__(`errors.${e.status}`)
})
}
return res.status(e.status).render(`error/${e.status}.ejs`)
}
}
}
from node-decorators.
It feels like you really have misconfigured server. Something either outside @decorators/server
package sends answers before even @decorators/server
lifecycle is finished or handler/pipe responds.
Try configuring example app I provided https://github.com/serhiisol/decorators-server-example
It's hard for me to tell where's the problem
from node-decorators.
Related Issues (20)
- Typescript v5 injection of type undefined HOT 11
- socket decorator not working HOT 1
- TypeError: Cannot read properties of undefined HOT 4
- Decorators with inheritance HOT 4
- Error while inject property in middelwares HOT 6
- Error injection HOT 18
- Where is attachMiddleware o how it change? HOT 2
- Where did the flexibility go?! HOT 11
- @Controller & Rest methods no longer work HOT 1
- Can't attach a controller without an error middleware HOT 17
- HttpModule is not exported HOT 21
- Decorator error handling
- Missing data, EJS, express-ejs-layouts
- Implement OpenAPI nullable schema property
- Param validation HOT 13
- Which container instance should I use ? HOT 2
- elysia Js HOT 3
- [BUG] Inject() and Injectable() not working as expected - upgrade 2.x -> 3.x HOT 4
- [BUG] dependency injection constructor service initialize fail HOT 1
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 node-decorators.