All files / lib/lambda item.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 1/1
100% Lines 4/4

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                                  1x                       1x                     1x   1x          
import { zodValidator } from "@dannywrayuk/middy-zod-validator";
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";
import httpJsonBodyParser from "@middy/http-json-body-parser";
import { APIGatewayEvent } from "shared-types";
import { z } from "zod";
 
import {
  canViewPackage,
  ContextWithPackage,
  fetchAppkChildren,
  fetchChangelog,
  fetchPackage,
  isAuthenticated,
  normalizeEvent,
} from "./middleware";
 
const itemEventSchema = z
  .object({
    body: z
      .object({
        id: z.string(),
      })
      .strict(),
  })
  .passthrough();
 
export type ItemEvent = APIGatewayEvent & z.infer<typeof itemEventSchema>;
 
export const handler = middy()
  .use(httpErrorHandler())
  .use(normalizeEvent({ opensearch: true }))
  .use(httpJsonBodyParser())
  .use(zodValidator({ eventSchema: itemEventSchema }))
  .use(isAuthenticated())
  .use(fetchPackage({ setToContext: true }))
  .use(canViewPackage())
  .use(fetchAppkChildren({ setToContext: true }))
  .use(fetchChangelog({ setToContext: true }))
  .handler(async (event: ItemEvent, context: ContextWithPackage) => {
    const { packageResult } = context;
 
    return {
      statusCode: 200,
      body: JSON.stringify(packageResult),
    };
  });