jarle / remix-adonisjs

Build fullstack Remix applications powered by AdonisJS
https://remix-adonisjs.matstack.dev/
MIT License
35 stars 2 forks source link

how do i access the HttpContext in the services? #53

Closed moha-fl-dev closed 2 months ago

moha-fl-dev commented 2 months ago

`@inject() export default class WelcomeService { constructor(protected ctx: HttpContext) {} // breaks the app and produces: help: 'Container is not able to resolve "WelcomeService" class dependencies. Did you mean to use @inject decorator'

getMessage() { return '...powered by AdonisJS 😎' } }`

jarle commented 2 months ago

The services cannot inject the HTTP context directly into the constructor because the context is changed on each new HTTP request.

You could pass the request to a method on the service like this:

const { make, http } = context
const service = await make('hello-service')
await service.handleRequest(http)

This would require that your service had a method async handleRequest(context: HttpContext) {}