Search code examples
typescriptmiddlewaremiddy

What is the excution order when using the middy warmup middleware?


So i'm currently using the middy warmup middleware and had a .before() at the end. I'm just wondering what the order of execution is? does it go straight to the .before block or does it check if its warmed up first?

I'm new to all this so sorry if i'm not making sense hopefully this code helps.

const handler = middy(originalHandler)
      .use(warmup())
      .use(
        cors({
          origins: [fetchEnvironmentVariable('SOME_ENDPOINT')],
          credentials: true
        })
      )
      .use(requestMiddleware())
      .before(async () => {
        // some code
      })

    return handler()

Solution

  • Short answer: before always runs before the handler execution.

    does it go straight to the .before block or does it check if its warmed up first?

    Straight to before block.

    From documentation

    The before phase, happens before the handler is executed. In this code the response is not created yet, so you will have access only to the request.

    Nice example you can find here.