cmorten / opine

Minimalist web framework for Deno ported from ExpressJS.
https://github.com/cmorten/opine/blob/main/.github/API/api.md
MIT License
854 stars 43 forks source link

[FEAT] Response lifecycle hooks #40

Closed FaberVitale closed 2 years ago

FaberVitale commented 4 years ago

Issue

In node there are libraries like on-headers and on-finished that are commonly use to run logic before and after a request has been processed, some example:

It would be nice to port some of these projects to opine but it is not currently as easy as it should for a middleware to be called back when one of these events occurs:

  1. Beforereq.respond: An async response is complete (e.g db returns output) and we are going to call request.respond.

  2. After req.respond: A request has been processed and the response has been sent, essentially the output of request.respond.

Suggested Changes

  1. Add a getter that returns a lazily instantiated and cached promise that resolves before calling request.respond.

  2. Add a getter that exposes the output of request.respond.

asos-craigmorten commented 4 years ago

Sounds good - think we should investigate whether either of these would be best implemented first in the std http library for Deno, but certainly wouldn’t be much work to add to Opine itself (either custom or hanging off of the std http methods).

I believe there is already a done or similar promise on the request object which could be used for one of these hooks?

jly36963 commented 2 years ago

What was the decision on this? I'm wanting to write a response-time middleware in one of the following ways:

To log response-time in opine, is there a way to do the following without splitting the logic into two separate middlewares?

app
  .use(getStart)
  .use(routes)
  .use(getFinishAndLogTimeDelta)
asos-craigmorten commented 2 years ago

@jly36963 no decision as such 🙃 issue remained unchanged and not worked on for over a year so closed as stale. If it's something you're keen on / have ideas on implementation then happy to reopen! 😄

jly36963 commented 2 years ago

@asos-craigmorten If I can find a simple solution I'll open a PR 😄