All files / api/swagger baseFormHandlerSwagger.ts

100% Statements 66/66
100% Branches 2/2
100% Functions 1/1
100% Lines 66/66

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 671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { SwaggerBuilder } from '@api/swagger/swaggerBuilder';
import { ApiStatusErrorCode, apiParams } from '@constants/api';
import { OpenAPIV3_1 } from 'openapi-types';
import {
  commonPathParam,
  componentsFromModule,
  replaceVariableSyntax,
} from './commonSwaggerOptions';
 
export const baseFormHandlerSwagger = (props: {
  path: string;
  method: string;
  swaggerBuilder: SwaggerBuilder;
}) => {
  const correctedPath = replaceVariableSyntax(props.path, apiParams.general.segment);
  const componentsFromMod = componentsFromModule();
  const componentsReqAndResp = componentsFromMod.map((comp) => ({
    $ref: `#/components/schemas/${comp}`,
  }));
  const swaggerPath: OpenAPIV3_1.PathsObject = {
    [`/${correctedPath}`]: {
      [props.method]: {
        tags: ['Form'],
        description: `Handles a form submission (either ${componentsFromMod.join(', ')}) and returns the created record`,
        parameters: [commonPathParam.segment],
        requestBody: {
          content: {
            'application/json': {
              schema: {
                oneOf: componentsReqAndResp,
              },
            },
          },
        },
        responses: {
          '200': {
            description: 'Form was successfully handled',
            content: {
              'application/json': {
                schema: {
                  oneOf: componentsReqAndResp,
                },
              },
            },
          },
          [ApiStatusErrorCode.badCaptcha.code]: {
            description: ApiStatusErrorCode.badCaptcha.message,
          },
          [ApiStatusErrorCode.invalidFormFiles.code]: {
            description: ApiStatusErrorCode.invalidFormFiles.message,
          },
          [ApiStatusErrorCode.invalidFormData.code]: {
            description: ApiStatusErrorCode.invalidFormData.message,
          },
          [ApiStatusErrorCode.validation.code]: {
            description: ApiStatusErrorCode.validation.message,
          },
          [ApiStatusErrorCode.couldNotPersistData.code]: {
            description: ApiStatusErrorCode.couldNotPersistData.message,
          },
        },
      },
    },
  };
  props.swaggerBuilder.addPath(swaggerPath);
};