All files / src/services swagger.service.ts

77.77% Statements 49/63
62.79% Branches 27/43
81.81% Functions 9/11
77.77% Lines 49/63

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  7x 7x   7x 7x 7x 7x 7x 7x 7x 7x 7x     7x     7x 7x 7x           7x 7x 7x 28x 182x 182x           350x 112x 112x 112x 112x 35x   77x 56x   21x     21x 21x         112x   238x                             238x     238x 35x         203x           203x 84x 84x 84x 168x 168x 168x     84x           119x     119x     7x        
import swaggerUi from 'swagger-ui-express';
import { Application, Request, Response } from 'express';
import { OpenAPIDocInstance } from '@/utils/openAPIGenerator';
import {
  ZodType,
  ZodObject,
  ZodString,
  ZodEnum,
  ZodNumber,
  ZodBoolean,
  ZodArray,
  ZodOptional,
  z,
} from 'zod';
import { AuthZODSchema } from '@/schemas/auth/auth.schema';
import { DoctorZODSchema } from '@/schemas/doctor/doctor.schema';
import { PatientZODSchema } from '@/schemas/patient/patient.schema';
import { AppointmentZODSchema } from '@/schemas/appointment/appointment.schema';
 
export class SwaggerService {
  constructor(private app: Application) {}

  setupSwagger(): void {
    const instance = OpenAPIDocInstance.getInstance();
 
    this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(instance));
 
    this.app.get('/swagger.json', (_: Request, res: Response) => {
      res.setHeader('Content-Type', 'application/json');
      res.send(instance);
    });E
  }
 
  static initializeSchemas() {
    const instance = OpenAPIDocInstance.getInstance();
 
    const schemas = [AuthZODSchema, DoctorZODSchema, PatientZODSchema, AppointmentZODSchema];
 
    schemas.forEach((schema) => {
      ObjectE.entries(schema).forEach(([key, value]) => {
        if (value instanceof ZodObject) {
          instance.addSchema(
            key,
            this.zodToOpenAPISchema(AuthZODSchema[key as keyof typeof AuthZODSchema] as ZodType)
          );
        }
      });
    });I
  }
 
  static zodToOpenAPIScheEma(schema: ZodType): any {
    if (schema instanceof ZodString) {
      const result: any = { type: 'string' };
      if (schema._def.checks) {
        schema._def.checks.forEach((check: any) => {
          if (check.kind === 'uuid') {
            result.format = 'uuid';
          } else if (check.kind === 'min') {
            rIesult.minLength = check.value;
          } else if (check.kind === 'max') {
            result.maxLength = check.value;
          } else if (check.kind === 'email') {
            result.format = 'email';
          }
          // Add more checks as needed
        });
      }
      return result;
    } else if (schema instanceof ZodNumber) {
      const result: any = { type: 'number' };
      if (schema._def.checks) {
        schema._def.checks.forEach((check: any) => {
          if (check.kind === 'min') {
            rIesult.minimum = check.value;
          } else if (check.kind === 'max') {
            result.maximum = check.value;
          }
          // Add more checks as needed
        });
      }
      return result;
    } else if (schema instanceof ZodBoolean) {
      return I{ type: 'boolean' };
    } else if (schema instanceof ZodEnum) {
      return {
        type: 'string',
        enum: schema._def.values as string[],
      };
    } else if (schema instanceof ZodArray) {
      return {
        type: 'array',
        items: this.zodToOpenAPISchema(schema._def.type),
      };
    } else if (sEchema instanceof ZodObject) {
      const properties: any = {};
      const required: string[] = [];
      Object.entries(schema.shape).forEach(([key, value]) => {
        properties[key] = this.zodToOpenAPISchema(value as ZodType);
        if (!(value instanceof ZodOptional)) {
          required.push(key);
        }
      });
      return {
        type:I 'object',
        properties,
        required: required.length > 0 ? required : undefined,
      };
    } else if (schema instanceof ZodOptional) {
      return this.zodToOpenAPISchema(schema._def.innerType);
    }
    return { type: 'object' };
  }
}