-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsetup.ts
More file actions
51 lines (47 loc) · 2.2 KB
/
setup.ts
File metadata and controls
51 lines (47 loc) · 2.2 KB
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
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import compression from '@fastify/compress';
import type { INestApplication } from '@nestjs/common';
import type { FastifyReply, FastifyRequest } from 'fastify';
import type { NestFastifyApplication } from '@nestjs/platform-fastify';
export function setupSwagger(app: INestApplication): void {
const adapter = app.getHttpAdapter();
adapter.get('/', (req: FastifyRequest, res: FastifyReply) => {
void res.redirect('/swagger', 302);
});
const config = new DocumentBuilder()
.setTitle('Eh Tag Translation Server')
.addBearerAuth(undefined, 'GitHub-Token')
.setDescription('连接到 EhTagTranslation 数据库的 RESTful API。')
.setVersion('1.0')
.addTag('Database', 'EhTagTranslation 数据库内容的增改删查。')
.addTag('Tools', '不直接操作数据库的帮助工具。')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/swagger', app, document, {
customCssUrl: [`https://unpkg.com/swagger-ui-dist@5/swagger-ui.css`],
customJs: [
`https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js`,
`https://unpkg.com/swagger-ui-dist@5/swagger-ui-standalone-preset.js`,
],
});
}
export function enableCors(app: INestApplication): void {
app.enableCors({
origin: (requestOrigin: string | undefined, callback: (err: Error | null, origin: string | null) => void) => {
requestOrigin = requestOrigin?.trim();
if (!requestOrigin || requestOrigin === 'null') {
callback(null, '*');
} else {
callback(null, requestOrigin);
}
},
credentials: false,
methods: ['OPTIONS', 'HEAD', 'GET', 'PUT', 'POST', 'DELETE'],
allowedHeaders: ['If-Match', 'If-None-Match', 'Content-Type', 'Authorization'],
exposedHeaders: ['ETag', 'Location'],
maxAge: 60 * 60 * 24,
});
}
export async function enableCompression(app: NestFastifyApplication): Promise<void> {
await app.register(compression, { encodings: ['zstd', 'gzip', 'deflate', 'identity'], inflateIfDeflated: true });
}