All files / api/swagger commonSwaggerOptions.ts

100% Statements 123/123
100% Branches 7/7
100% Functions 5/5
100% Lines 123/123

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 7x 7x 3x 3x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 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 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 3x 3x 3x 1x 1x  
import { OpenAPIV3_1 } from 'openapi-types';
 
import { moduleLookup } from '@api/module/moduleLookup';
import { IApiSegment, apiParams, segmentLabels } from '@constants/api';
import { getArrFromEnum } from '@helpers/enumHelper';
import { ApprovalStatus } from '@constants/enum/approvalStatus';
import { capitalizeFirstLetter } from '@helpers/stringHelper';
import { SwaggerBuilder } from './swaggerBuilder';
 
export const replaceVariableSyntax = (fullPath: string, ...variables: Array<string>) => {
  let result = fullPath.toString();
  for (const variable of variables) {
    result = result.replace(`:${variable}`, `{${variable}}`);
  }
  return result;
};
 
export const requiredPathParam = (
  variable: string,
  descrip?: string,
): OpenAPIV3_1.ParameterObject => {
  return {
    name: variable,
    in: 'path',
    description: descrip,
    required: true,
    allowEmptyValue: false,
  };
};
 
export const commonPathParam = {
  segment: {
    ...requiredPathParam(apiParams.general.segment, 'Segment to store the form submission into'),
    schema: {
      $ref: '#/components/schemas/segment',
    },
  },
  verifyId: requiredPathParam(
    apiParams.verify.id,
    'Id of the record (e.g. rec_cn549q2beinr95oc7vdg)',
  ),
  verifyDecision: {
    ...requiredPathParam(apiParams.verify.decision, 'Decision for a submitted form'),
    schema: {
      $ref: '#/components/schemas/ApprovalStatus',
    },
  },
  verifyCheck: requiredPathParam(
    apiParams.verify.check,
    `The calculated check value of the record. This is to prevent race conditions, only allowing 1 change at a time`,
  ),
};
 
export const segmentComponent: Record<string, OpenAPIV3_1.SchemaObject> = {
  segment: {
    type: 'string',
    enum: Object.keys(segmentLabels),
    additionalProperties: false,
  },
};
export const approvalStatusComponent: Record<string, OpenAPIV3_1.SchemaObject> = {
  ApprovalStatus: {
    type: 'string',
    enum: getArrFromEnum(ApprovalStatus).map(capitalizeFirstLetter),
    additionalProperties: false,
  },
};
export const formWithApprovalResponseComponent: Record<string, OpenAPIV3_1.SchemaObject> = {
  IFormWithApprovalResponse: {
    type: 'object',
    properties: {
      id: {
        type: 'string',
      },
      name: {
        type: 'string',
      },
      iconUrl: {
        type: 'string',
      },
      approvalStatus: {
        $ref: '#/components/schemas/ApprovalStatus',
      },
    },
    additionalProperties: false,
  },
};
export const mediaUploadComponent: Record<string, OpenAPIV3_1.SchemaObject> = {
  IMediaUpload: {
    type: 'object',
    properties: {
      type: {
        type: 'string',
      },
      url: {
        type: 'string',
      },
      file: {
        type: 'string',
        format: 'binary',
      },
    },
    additionalProperties: false,
  },
};
 
export const segmentToDtoName = (segment: string) => capitalizeFirstLetter(segment) + 'Dto';
 
export const registerSwaggerStaticComponents = (swaggerBuilder: SwaggerBuilder) => {
  swaggerBuilder.addComponent(segmentComponent);
  swaggerBuilder.addComponent(approvalStatusComponent);
  swaggerBuilder.addComponent(formWithApprovalResponseComponent);
  swaggerBuilder.addComponent(mediaUploadComponent);
};
 
export const componentsFromModule = (): Array<string> => {
  let components: Array<string> = [];
  for (const segment of Object.keys(segmentLabels)) {
    const module = moduleLookup[segment as keyof IApiSegment];
    components.push(segmentToDtoName(module.segment));
  }
  return components;
};