All files / src/utils openAPIGenerator.ts

33.33% Statements 24/72
23.52% Branches 12/51
33.33% Functions 5/15
33.33% Lines 24/72

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172  7x 7x   7x 7x 7x 7x 7x     301x 7x   301x     7x     7x 7x       7x         7x       7x 7x                                     182x     182x     182x                       287x 63x     224x                                                                                                                                                             7x                                
import appConfig from '@/config/app.config';
import { config } from 'dotenv';
config();
 
export class OpenAPIDocInstance {
  static openApi: OpenAPIDocument;
  static getInstance() {
    if (!this.openApi) {
      this.openApi = new OpenAPIDocument();
    }
    return this.openApi;
  }
}
 
export class OpenAPIDocument implements OpenAPISchema {
  openapi: string;
  info: InfoObject;
  servers?: ServerObject[];
  paths: PathsObject;
  components?: ComponentsObject;
  security?: SecurityRequirementObject[];
  tags?: TagObject[];
  externalDocs?: ExternalDocumentationObject;
 
  constructor() {
    this.openapi = '3.0.0';
    this.info = {
      title: appConfig.appName,
      version: appConfig.apiVersion,
    };
    this.servers = [
      {
        url: `http://localhost:${process.env.PORT}`,
      },
    ];
    this.externalDocs = {
      description: 'swagger.json',
      url: '/swagger.json',
    };
    this.paths = {};
    this.components = {
      schemas: {},
      parameters: {},
      responses: {},
      requestBodies: {},
      headers: {},
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        },
      },
      examples: {},
      liInks: {},
      callbacks: {},
    };
  }I

  addSchema(name: string, schema: SchemaObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.schemas) {
      this.components.schemas = {};
    }
    this.components.schemas[name] = schema;
  }
 
  addParameter(name: string, parameter: ParameterObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.parameters) {
      this.components.parameters = {};
    }
    this.components.parameters[name] = parameter;
  }
 
  addPath(path: string, pathItem: PathItemObject): void {
    if (this.paths[path]) {
      this.paths[path] = { ...pathItem, ...this.paths[path] };
    } else {
      this.paths[path] = pathItem;
    }
  }

  addResponse(name: string, response: ResponseObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.responses) {
      this.components.responses = {};
    }
    this.components.responses[name] = response;
  }
 
  addRequestBody(name: string, requestBody: RequestBodyObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.requestBodies) {
      this.components.requestBodies = {};
    }
    this.components.requestBodies[name] = requestBody;
  }

  addHeader(name: string, header: HeaderObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.headers) {
      this.components.headers = {};
    }
    this.components.headers[name] = header;
  }

  addSecurityScheme(name: string, securityScheme: SecuritySchemeObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.securitySchemes) {
      this.components.securitySchemes = {};
    }
    this.components.securitySchemes[name] = securityScheme;
  }

  addExample(name: string, example: ExampleObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.examples) {
      this.components.examples = {};
    }
    this.components.examples[name] = example;
  }
 
  addLink(name: string, link: LinkObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.links) {
      this.components.links = {};
    }
    this.components.links[name] = link;
  }

  addCallback(name: string, callback: CallbackObject | ReferenceObject): void {
    if (!this.components) {
      this.components = {};
    }
    if (!this.components.callbacks) {
      this.components.callbacks = {};
    }
    this.components.callbacks[name] = callback;
  }
 
  addTag(tag: TagObject): void {
    if (!this.tags) {
      this.tags = [];
    }
    this.tags.push(tag);
  }
 
  addSecurityRequirement(securityRequirement: SecurityRequirementObject): void {
    if (!this.security) {
      this.security = [];
    }
    this.security.push(securityRequirement);
  }
}