Skip to content

Middleware/ext support #34

@luvies

Description

@luvies

It would be good to support some form of middleware to allow processing of requests/responses in a more general fashion. Hapi uses the following (with some extra overloads):

type ExtPoint = "onPreStart" | "onPostStart" | "onPreStop" | "onPostStop"
  | "onRequest" | "onPreAuth" | "onCredentials" | "onPostAuth" | "onPreHandler"
  | "onPostHandler" | "onPreResponse" | "onPostResponse";

interface ExtOptions {
  before?: string | string[];
  after?: string | string[];
  bind?: object;
  sandbox?: string;
  timeout?: number;
}

interface ExtEvent {
  type: ExtPoint;
  method(request: Request, h: Toolkit): void;
  options?: ExtOptions
}

interface Server {
  ext(events: ExtEvent | ExtEvent[]): void;
  ext(
    event: ExtPoint,
    method?: (request: Request, h: Toolkit) => void,
    options?: ExtOptions,
  ): void;
}

A suggestion I had was to use a modified version, which uses class instances for each request. That way you can keep track of information during a request (such as timing), and allows for an API like so:

interface ExtHandler {
  onPreStart?(request: Request): void;
  onPostStart?(request: Request): void;
  onPreStop?(request: Request): void;
  onPostStop?(request: Request): void;
  onRequest?(request: Request): void;
  onPreAuth?(request: Request): void;
  onCredentials?(request: Request): void;
  onPostAuth?(request: Request): void;
  onPreHandler?(request: Request): void;
  onPostHandler?(request: Request, response: Response): void;
  onPreResponse?(request: Request, response: Response): void;
  onPostResponse?(request: Request, response: Response): void;
}

interface Server {
  ext(handler: new (h: Toolkit) => ExtHandler): void;
}

It should be possible to support both, but that would complicate things possibly. Another option would be to support

interface Server {
  ext(handler: ExtHandler): void;
}

in some way to allow for the case where you don't need to store instance data (if this was supported, each method would also get h as well).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions