For a deep-dive on the middleware stack, please read the Introducing Middleware Stack in Modular AWS SDK for JavaScript .
For an example of how to create middleware, please see the Token Middleware or Retry Middleware
The middleware stack consists of three parts: initialization, transformation, and finalization. It uses a standard stack architecture, meaning that it goes from initialization -> transformation -> finalization -> terminator -> finalization -> transformation -> initialization. The request moves through the stages in order until it reaches the "terminator" and the command is executed, at which point a response is sent back down the stack to be changed to what the sender is expecting to receive.
This stage of the middleware stack initializes an API call. Typically this step adds default input values to a command. The HTTP request has not been constructed by this point. When the response is passed through, it is finalized to ensure it is readable to the program that sent the request.
This stage deals with the serialization of the request when moving up the stack, and deserialization when moving down the stack. "Transformation" simply refers to changing the request and response to be in the correct format. In this stage an HTTP request is constructed for the API call. Tasks include input validation and building the HTTP request from user input. When the response object returns through this middleware, it is converted from an HTTP response into a structured object that matches the expectations of the client.
This stage alters the now-serialized request to ensure it matches the recipient's expectations. Examples of typical finalization tasks include adding an authorization header and performing retries.