All files / lib/lambda/middleware normalizations.ts

95.45% Statements 21/22
95.45% Branches 21/22
100% Functions 4/4
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                      12x                                                     12x 32x   32x   107x 107x 89x 87x     104x 41x     98x         87x   11x   76x       76x     3x       74x               50x 45x     50x 49x                    
import { MiddlewareObj, Request } from "@middy/core";
import { createError } from "@middy/util";
import { validateEnvVariable } from "shared-utils";
 
export type NormalizeEventOptions = {
  opensearch?: boolean;
  kafka?: boolean;
  body?: boolean;
  disableCors?: boolean;
};
 
const defaults: NormalizeEventOptions = {
  opensearch: false,
  kafka: false,
  body: true,
  disableCors: false,
};
 
/**
 * Normalizes the input and output of the handler.
 *
 * *Before handler*: performs normalizations and validations on the event, including:
 * - (optionally) validates that the opensearch environment variables are set, if the opensearch option is true
 * - (optionally) validates that the kafka environment variables are set, if the kafka option is true
 * - validates that the event has a body, unless the body option is false
 * - adds `"Content-Type": "application/json"` to the headers, if it is missing, this is required to use the `httpJsonBodyParser` middleware
 *
 *
 * *After handler*: adds the CORS headers to the response, unless the disableCors option is true
 *
 *
 * @param {object} opts Options for running the middleware
 * @param {boolean} opts.opensearch [false] if true, validate opensearch environment variables
 * @param {boolean} opts.kafka [false] if true, validate kafka topic name environment variable
 * @param {boolean} opts.body [true] if false, skips validating the event body
 * @param {boolean} opts.disableCors [false] if true, disable the CORS headers on the response
 * @returns {MiddlewareObj} middleware with the input and output normalizations
 */
export const normalizeEvent = (opts: NormalizeEventOptions = {}): MiddlewareObj => {
  const options = { ...defaults, ...opts };
 
  return {
    before: async (request: Request) => {
      console.log(JSON.stringify(request?.event, null, 2));
      if (options.opensearch) {
        validateEnvVariable("osDomain");
        validateEnvVariable("indexNamespace");
      }
 
      if (options.kafka) {
        validateEnvVariable("topicName");
      }
 
      if (
        options.body &&
        request?.event?.httpMethod !== "GET" &&
        request?.event?.httpMethod !== "HEAD"
      ) {
        if (!request?.event?.body) {
          // check that the event has a body
          throw createError(400, JSON.stringify({ message: "Event body required" }));
        }
        Iif (typeof request.event.body === "object") {
          request.event.body = JSON.stringify(request.event.body);
        }
 
        if (
          !request?.event?.headers ||
          !Object.keys(request.event.headers)
            .map((header) => header.toLowerCase())
            .includes("content-type")
        ) {
          // if the headers don't have the Content-Type set, set it
          request.event.headers = {
            ...request.event.headers,
            "Content-Type": "application/json",
          };
        }
      }
    },
    after: async (request: Request) => {
      if (typeof request.response.body === "object") {
        request.response.body = JSON.stringify(request.response.body);
      }
 
      if (!options.disableCors) {
        request.response.headers = {
          ...request.response.headers,
          "Access-Control-Allow-Headers": "Content-Type",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
        };
      }
    },
  };
};