Skip to content

Commit

Permalink
feat(server): Support for little complex (but common) API document
Browse files Browse the repository at this point in the history
  • Loading branch information
mugifly committed Jun 21, 2024
1 parent 2d0c4b4 commit 1da31eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ $ npm run start:dev
Finally, open the web browser and navigate to `http://localhost:4200/`.
Also, when you edit the frontend source-code, auto-reloading applies your changes to the browser immediately.

An API documentation is generated by Swagger UI, and you can access it at `http://localhost:4200/api/docs`.

See the [Wiki](https://github.com/mugifly/angular-nest/wiki/) for additional information.
You'll find tips for implementing database connectivity, guides for future updates, and more.

Expand Down Expand Up @@ -111,7 +113,7 @@ https://github.com/mugifly/angular-nest/wiki/#Deployments
- Unit tests for backend:   `npm run test -w server`
- E2E tests for backend:   `npm run test:e2e -w server`

### Install npm modules1
### Install npm modules

- Install for frontend: `npm install -w client XXXXX`
- Install for backend: `npm install -w server XXXXX`<br>(e.g. `npm install -w server @nestjs/typeorm typeorm`)
Expand Down
16 changes: 16 additions & 0 deletions server/src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

export class Helper {
static getOpenAPIDoc(app: INestApplication<any>) {
const doc_options = new DocumentBuilder()
.setTitle(`API Document`)
// Output securitySchemes to support Bearer authentication
// See: https://docs.nestjs.com/openapi/security#bearer-authentication
.addBearerAuth()
.build();

const doc = SwaggerModule.createDocument(app, doc_options);
return doc;
}
}
8 changes: 4 additions & 4 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { Helper } from './helper';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

// Change the URL prefix to `/api` on backend
app.setGlobalPrefix('api');

// Build the OpenAPI document (published under `/api/docs`) with Swagger
const doc_options = new DocumentBuilder().setTitle(`API Document`).build();
const doc = SwaggerModule.createDocument(app, doc_options);
// Enable Swagger UI for development env
if (process.env.NODE_ENV && process.env.NODE_ENV === 'development') {
// Enable Swagger UI for development env
// Build the OpenAPI document (published under `/api/docs`) with Swagger
const doc = Helper.getOpenAPIDoc(app);
SwaggerModule.setup('api/docs', app, doc);
}

Expand Down
23 changes: 14 additions & 9 deletions server/src/openapi-doc-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test } from '@nestjs/testing';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import * as fs from 'fs';
import { AppModule } from './app.module';
import { Helper } from './helper';

async function bootstrap(): Promise<void> {
// Get the modules included by AppModule
Expand All @@ -18,16 +19,21 @@ async function bootstrap(): Promise<void> {
providers = providers.concat(Reflect.getMetadata('providers', mod));
}

// Generate a mock
const mockedProviders = providers.map((provider: any) => {
return {
provide: provider,
useValue: {},
};
});
// Generate the mock providers
const mockedProviders = providers
.filter((provider: any) => {
return provider !== undefined;
})
.map((provider: any) => {
return {
provide: provider,
useValue: {},
};
});

// Generate an application instance
const testingModule = await Test.createTestingModule({
imports: [AppModule],
controllers: controllers,
providers: mockedProviders,
}).compile();
Expand All @@ -38,8 +44,7 @@ async function bootstrap(): Promise<void> {
app.setGlobalPrefix('api');

// Generate API JSON
const doc_options = new DocumentBuilder().setTitle(`API Document`).build();
const doc = SwaggerModule.createDocument(app, doc_options);
const doc = Helper.getOpenAPIDoc(app);
const docJson = JSON.stringify(doc);

// Check the argument
Expand Down

0 comments on commit 1da31eb

Please sign in to comment.