Skip to content

Commit 044f8a1

Browse files
committed
feat: swagger generator
1 parent bb010eb commit 044f8a1

File tree

4 files changed

+73
-111
lines changed

4 files changed

+73
-111
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ jobs:
9797
- name: Generate openapi.json
9898
run: |
9999
pnpm build
100-
node ./bin/generate-swagger.js
100+
NODE_ENV=development node ./bin/generate-swagger.js
101101
102102
- name: Check if openapi.json match the code
103103
run: |

bin/generate-swagger.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,16 @@
1-
const fs = require('fs');
2-
31
const { NestFactory } = require('@nestjs/core');
4-
const { SwaggerModule, DocumentBuilder } = require('@nestjs/swagger');
52
const commander = require('commander');
63

74
const { AppModule } = require('../dist/app.module');
8-
const { SHA256 } = require('crypto-js');
9-
const { ListUsersQuery } = require('../dist/user/dto/list-users.dto');
10-
const { ListNamespacesQuery } = require('../dist/namespace/dto/list-namespaces.dto');
11-
const { GetAuthorizerQuery } = require('../dist/auth/dto/authorize-query.dto');
12-
const { ListCaptchasQuery } = require('../dist/captcha/dto/list-captchas.dto');
13-
const { ListEmailRecordsQuery } = require('../dist/email/dto/list-email-records.dto');
14-
const { ListGroupsQuery } = require('../dist/group/dto/list-groups.dto');
15-
const { ListIndustriesQuery } = require('../dist/industry/dto/list-industries.dto');
16-
const { ListRolesQuery } = require('../dist/role/dto/list-roles.dto');
17-
const { ListSessionsQuery } = require('../dist/session/dto/list-sessions.dto');
18-
const { ListSmsRecordsQuery } = require('../dist/sms/dto/list-sms-records.dto');
19-
const { ListThirdPartyQuery } = require('../dist/third-party/dto/list-third-party.dto');
5+
const { writeOpenapi } = require('../dist/swagger');
206

7+
/**
8+
* @param {string | undefined} prefix
9+
*/
2110
async function bootstrap(prefix) {
2211
const app = await NestFactory.create(AppModule, { logger: false });
23-
app.setGlobalPrefix(prefix);
24-
25-
const config = new DocumentBuilder()
26-
.setTitle('Auth API Server')
27-
.setDescription('Auth API for auth service')
28-
.setVersion('2.0')
29-
.addApiKey(
30-
{
31-
in: 'header',
32-
name: 'x-api-key',
33-
type: 'apiKey',
34-
},
35-
'ApiKey'
36-
)
37-
.build();
38-
39-
const document = SwaggerModule.createDocument(app, config, {
40-
extraModels: [
41-
ListUsersQuery,
42-
ListNamespacesQuery,
43-
GetAuthorizerQuery,
44-
ListCaptchasQuery,
45-
ListEmailRecordsQuery,
46-
ListGroupsQuery,
47-
ListIndustriesQuery,
48-
ListRolesQuery,
49-
ListSessionsQuery,
50-
ListSmsRecordsQuery,
51-
ListThirdPartyQuery,
52-
],
53-
});
54-
const documentWithSha = {
55-
hash: SHA256(JSON.stringify(document, null, 2)).toString(),
56-
...document,
57-
};
58-
fs.writeFileSync('./openapi.json', JSON.stringify(documentWithSha, null, 2));
59-
12+
if (prefix) app.setGlobalPrefix(prefix);
13+
writeOpenapi(app, prefix);
6014
await app.close();
6115
}
6216

src/main.ts

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import fs from 'fs';
2-
31
import { ValidationPipe } from '@nestjs/common';
42
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
5-
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
63
import compression from 'compression';
7-
import { SHA256 } from 'crypto-js';
84
import dayjs from 'dayjs';
95
import isoWeek from 'dayjs/plugin/isoWeek';
106
import minMax from 'dayjs/plugin/minMax';
@@ -13,85 +9,35 @@ import * as sourceMapSupport from 'source-map-support';
139
import { MongoErrorsInterceptor } from 'src/mongo';
1410

1511
import { AppModule } from './app.module';
16-
import { GetAuthorizerQuery } from './auth';
17-
import { ListCaptchasQuery } from './captcha';
1812
import { AllExceptionsFilter } from './common/all-exceptions.filter';
1913
import { exceptionFactory } from './common/exception-factory';
2014
import { port, prefix } from './config/config';
21-
import { ListEmailRecordsQuery } from './email';
22-
import { ListGroupsQuery } from './group';
23-
import { ListIndustriesQuery } from './industry';
24-
import { ListNamespacesQuery } from './namespace';
25-
import { ListRolesQuery } from './role';
26-
import { ListSessionsQuery } from './session';
27-
import { ListSmsRecordsQuery } from './sms';
28-
import { ListThirdPartyQuery } from './third-party';
29-
import { ListUsersQuery } from './user';
15+
import { writeOpenapi } from './swagger';
3016

3117
dayjs.extend(isoWeek);
3218
dayjs.extend(minMax);
3319

34-
const openapiPath = `openapi.json`;
35-
3620
async function bootstrap() {
3721
sourceMapSupport.install();
3822
const app = await NestFactory.create(AppModule, {
3923
cors: { exposedHeaders: ['Link', 'X-Total-Count'] },
4024
});
4125

42-
let swaggerPrefix = 'openapi';
4326
if (prefix) {
4427
app.setGlobalPrefix(prefix);
45-
swaggerPrefix = `${prefix}/${swaggerPrefix}`;
4628
}
4729

48-
const swaggerConfig = new DocumentBuilder()
49-
.setTitle('Auth API Server')
50-
.setDescription('Auth API for auth service')
51-
.setVersion('2.0')
52-
.addApiKey(
53-
{
54-
in: 'header',
55-
name: 'x-api-key',
56-
type: 'apiKey',
57-
},
58-
'ApiKey'
59-
)
60-
.build();
61-
const document = SwaggerModule.createDocument(app, swaggerConfig, {
62-
extraModels: [
63-
ListUsersQuery,
64-
ListNamespacesQuery,
65-
GetAuthorizerQuery,
66-
ListCaptchasQuery,
67-
ListEmailRecordsQuery,
68-
ListGroupsQuery,
69-
ListIndustriesQuery,
70-
ListRolesQuery,
71-
ListSessionsQuery,
72-
ListSmsRecordsQuery,
73-
ListThirdPartyQuery,
74-
],
75-
});
76-
SwaggerModule.setup(swaggerPrefix, app, document);
77-
7830
app.use(compression());
7931
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true, exceptionFactory }));
8032

8133
const { httpAdapter } = app.get(HttpAdapterHost);
8234
app.useGlobalFilters(new AllExceptionsFilter(httpAdapter));
8335
app.useGlobalInterceptors(new MongoErrorsInterceptor());
8436

85-
await app.listen(port);
37+
// setup swagger
38+
writeOpenapi(app, prefix);
8639

87-
// write openapi.json
88-
if (process.env.NODE_ENV === 'development') {
89-
const documentWithSha = {
90-
hash: SHA256(JSON.stringify(document, null, 2)).toString(),
91-
...document,
92-
};
93-
fs.writeFileSync(openapiPath, JSON.stringify(documentWithSha, null, 2));
94-
}
40+
await app.listen(port);
9541
}
9642

9743
bootstrap();

src/swagger.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from 'fs';
2+
3+
import { INestApplication } from '@nestjs/common';
4+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
5+
import { SHA256 } from 'crypto-js';
6+
7+
import { GetAuthorizerQuery } from './auth';
8+
import { ListCaptchasQuery } from './captcha';
9+
import { ListEmailRecordsQuery } from './email';
10+
import { ListGroupsQuery } from './group';
11+
import { ListIndustriesQuery } from './industry';
12+
import { ListNamespacesQuery } from './namespace';
13+
import { ListRolesQuery } from './role';
14+
import { ListSessionsQuery } from './session';
15+
import { ListSmsRecordsQuery } from './sms';
16+
import { ListThirdPartyQuery } from './third-party';
17+
import { ListUsersQuery } from './user';
18+
19+
const openapiPath = `openapi.json`;
20+
21+
export function writeOpenapi(app: INestApplication<any>, prefix?: string) {
22+
const swaggerPrefix = prefix ? `${prefix}/openapi` : 'openapi';
23+
const swaggerConfig = new DocumentBuilder()
24+
.setTitle('Auth API Server')
25+
.setDescription('Auth API for auth service')
26+
.setVersion('2.0')
27+
.addApiKey(
28+
{
29+
in: 'header',
30+
name: 'x-api-key',
31+
type: 'apiKey',
32+
},
33+
'ApiKey'
34+
)
35+
.build();
36+
const document = SwaggerModule.createDocument(app, swaggerConfig, {
37+
extraModels: [
38+
ListUsersQuery,
39+
ListNamespacesQuery,
40+
GetAuthorizerQuery,
41+
ListCaptchasQuery,
42+
ListEmailRecordsQuery,
43+
ListGroupsQuery,
44+
ListIndustriesQuery,
45+
ListRolesQuery,
46+
ListSessionsQuery,
47+
ListSmsRecordsQuery,
48+
ListThirdPartyQuery,
49+
],
50+
});
51+
52+
SwaggerModule.setup(swaggerPrefix, app, document);
53+
54+
// write openapi.json
55+
if (process.env.NODE_ENV === 'development') {
56+
const documentWithSha = {
57+
hash: SHA256(JSON.stringify(document, null, 2)).toString(),
58+
...document,
59+
};
60+
fs.writeFileSync(openapiPath, JSON.stringify(documentWithSha, null, 2));
61+
}
62+
}

0 commit comments

Comments
 (0)