Extending Response class


#1

When a user creates a post in my RESTful application, I want to set the response status code to 201.

I followed the documentation and created start/hooks.js as follows:

'use strict'                                                                                                                      
                                                                                                                                                 
const { hooks } = require('@adonisjs/ignitor')                                                                                                   
                                                                                                                                                 
hooks.after.httpServer(() => {                                                                                                                   
  const Response = use('Adonis/Src/Response')                                                                                                    
                                                                                                                                                 
  Response.macro('sendStatus', (status) => {                                                                                                     
    this.status(status).send(status)                                                                                                             
  })                                                                                                                                             
})

Now in my PostController.js, I have this:

 async store( {request, response, auth} ) {
   const user = await auth.current.user
   response.sendStatus(201)
 }

But I am getting 500 HTTP code at this endpoint.
What am I doing wrong?

I noticed when I run:

Response.hasMacro('sendStatus')

I get false.

Link to StackOverflow question.

Thank you


#2

Hey @begueradj! :wave:

What’s the exact error you have ?


#3

I get a 500 HTTP code, but let us forget about it, the real issue is that in my PostController.js when I write:

console.log( Response.hasMacro('sendStatus') )

I get false. So there is clearly something wrong about how I defined the macro, if you can take a look at the definition above (hooks.js). Thank you


#4

My solution to this was to replace the hook event httpServer by providersBooted.

This results in:

console.log(Response.hasMacro('sendStatus'))

to be true.

But still my server returns 500 instead of 201 when I run this in PostController.js:

store( {response, request} ) {
  console.log(Response.hasMacro('sendStatus')) // Outpus true
  response.sendStatus(201) // The assertion of this gives 500
} 

#5

There were 2 places where this is mentioned in the documentation, I studied the wrong one, so here is the fix:

'use strict'                                                                                                                      
                                                                                                                                                 
const { hooks } = require('@adonisjs/ignitor')                                                                                                   
                                                                                                                                                 
hooks.after.httpServer(() => {                                                                                                                   
  const Response = use('Adonis/Src/Response')                                                                                                    
                                                                                                                                                 
  Response.macro('sendStatus', function (status) => {                                                                                                     
    this.status(status).send(status)                                                                                                             
  })                                                                                                                                             
})

Billal Begueradj


#6

The new doc has been deployed, thanks for your PR! :hugs: