Function applyMiddleware

This function applies a middleware to a response object to update its headers.

Primarily used for applying helmet headers to a response object. Supports the express middleware signature to allow for easy integration with the existing helmet middleware.

import helmet from 'helmet';
import { applyMiddleware, RestApi } from 'effect-lambda';
import { Effect } from 'effect';

const middleware = helmet({
xXssProtection: true,
});
const result = applyMiddleware(middleware)({ headers: {} });

expect(result.headers).toEqual(
expect.objectContaining({ 'X-XSS-Protection': '0' }),
);

// When using together with an ApiGatewayProxyHandler, you can apply the middleware to the handler effect:
const handlerEffect: HandlerEffect = Effect.succeed({
statusCode: 200,
body: 'Woohoo',
});
export const handler = handlerEffect.pipe(
Effect.map(applyMiddleware(middleware)),
RestApi.toLambdaHandler,
  • Parameters

    • middleware: Middleware

      express middleware to apply header changes

    Returns (<T>(response: T) => T)

    response with the header changes applied

      • <T>(response): T
      • Type Parameters

        • T extends ObjectWithOptionalHeaders

        Parameters

        • response: T

        Returns T