I want to extend Response in Express to include custom functions. I want to add a function:
sendError(statusCode: number, errorMessage: string)
such that I can invoke it anywhere like
response.sendError(500, "Unable to process this request at the moment.")
Can you tell me how to achieve this? I looked at a few other questions like extension method on number in typescript but I still have some doubts:
- How do I extend a function on Response when it doesn't have a prototype?
- Where do I put the definition of the extended function? Can I make a separate file containing all the definitions?
- Do I have to assign this function to every object of Response to use it or can I just define it once and use it on all response objects everywhere in my project?
Please help me with this. I am new to TypeScript so please excuse any mistakes I might have made in my question :)