Giter Club home page Giter Club logo

Comments (18)

serhiisol avatar serhiisol commented on September 22, 2024

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.

Just-Niko avatar Just-Niko commented on September 22, 2024

I'm currently injecting both the module and the adapter because of the experiments.

from node-decorators.

serhiisol avatar serhiisol commented on September 22, 2024

Could you upload an example so I can see the whole flow?

from node-decorators.

Just-Niko avatar Just-Niko commented on September 22, 2024

Unfortuantely, I can't. I can only provide general information.

from node-decorators.

serhiisol avatar serhiisol commented on September 22, 2024

@Just-Niko please try version beta.4 if it resolves your issues

from node-decorators.

Just-Niko avatar Just-Niko commented on September 22, 2024

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.

serhiisol avatar serhiisol commented on September 22, 2024

Do you have @Render and res.render at the same time?

from node-decorators.

Just-Niko avatar Just-Niko commented on September 22, 2024

@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.

serhiisol avatar serhiisol commented on September 22, 2024

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.

Just-Niko avatar Just-Niko commented on September 22, 2024

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.

serhiisol avatar serhiisol commented on September 22, 2024

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.

Just-Niko avatar Just-Niko commented on September 22, 2024

That's really strange. On the legacy version it was working, and now it doesn't. I'll continue investigating tomorrow.

from node-decorators.

serhiisol avatar serhiisol commented on September 22, 2024

With this decorator:

const req = context.getRequest<Request>();
req['user'] = { id: 1, name: 'John Doe' };
return req['user'];

and this route

@Access('granted')
@Pipe(AccessPipe)
@Get(':id', 200)
@Render('post')
post(@Params('id') id: string, @AccessParam() access: User) {
return { access, id };
}

I receive the user object and rendering goes well. Also tried res.render + session middleware.

from node-decorators.

Just-Niko avatar Just-Niko commented on September 22, 2024

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.

serhiisol avatar serhiisol commented on September 22, 2024

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.

Just-Niko avatar Just-Niko commented on September 22, 2024

Yes the AuthPipe is modifying the request.

from node-decorators.

Just-Niko avatar Just-Niko commented on September 22, 2024

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.

serhiisol avatar serhiisol commented on September 22, 2024

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)

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.