All files / lib/lambda/middleware normalizations.ts

100% Statements 17/17
90.9% Branches 10/11
100% Functions 4/4
100% Lines 17/17

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                  3x                                     3x 15x   15x   26x 16x 16x 14x   3x   3x           23x   3x     20x     3x       18x             14x 13x                    
import { MiddlewareObj, Request } from "@middy/core";
import { createError } from "@middy/util";
import { validateEnvVariable } from "shared-utils";
 
export type NormalizeEventOptions = {
  opensearch?: boolean;
  disableCors?: boolean;
};
 
const defaults: NormalizeEventOptions = {
  opensearch: false,
  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
 * - validates that the event has a body
 * - 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.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) => {
      if (options.opensearch) {
        try {
          validateEnvVariable("osDomain");
          validateEnvVariable("indexNamespace");
        } catch (err) {
          console.error(err);
          // if you don't use the expose option here, you won't be able to see the error message
          throw createError(500, JSON.stringify({ message: "Internal server error" }), {
            expose: true,
          });
        }
      }
 
      if (!request?.event?.body) {
        // check that the event has a body
        throw createError(400, JSON.stringify({ message: "Event body required" }));
      }
 
      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 (!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",
        };
      }
    },
  };
};