Skip to content

Commit

Permalink
refactor: add swagger securitySchemes
Browse files Browse the repository at this point in the history
  • Loading branch information
a20688392 authored and moontai0724 committed Aug 25, 2023
1 parent 42c5054 commit 30c6d21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import type { INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { type SecuritySchemeObject } from "@nestjs/swagger/dist/interfaces/open-api-spec.interface";

import { AppModule } from "./app.module";
import { validationPipe } from "./pipes/validation-pipe";

const securitySchemes: SecuritySchemeObject = {
bearerFormat: "JWT",
scheme: "bearer",
type: "http",
};

function setupSwagger(app: INestApplication) {
const builder = new DocumentBuilder();
const config = builder
.setTitle(process.env.APP_SWAGGER_Title ?? "Cophr")
.setDescription(process.env.APP_SWAGGER_Description ?? "")
.setVersion(process.env.APP_SWAGGER_Version ?? "N/A")
.addBearerAuth()
.addBearerAuth(securitySchemes)
.build();
const document = SwaggerModule.createDocument(app, config);

Expand Down
8 changes: 8 additions & 0 deletions src/swagger/generate-swagger-file.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { type SecuritySchemeObject } from "@nestjs/swagger/dist/interfaces/open-api-spec.interface";
import * as fs from "fs";

import { SwaggerGenerateModule } from "./swagger.module";

const securitySchemes: SecuritySchemeObject = {
bearerFormat: "JWT",
scheme: "bearer",
type: "http",
};

async function generateSwaggerJson() {
const app = await NestFactory.create(SwaggerGenerateModule);

const config = new DocumentBuilder()
.setTitle(process.env.APP_SWAGGER_Title ?? "Cophr")
.setDescription(process.env.APP_SWAGGER_Description ?? "")
.setVersion(process.env.APP_SWAGGER_Version ?? "N/A")
.addBearerAuth(securitySchemes)
.build();

const document = SwaggerModule.createDocument(app, config);
Expand Down

0 comments on commit 30c6d21

Please sign in to comment.