oauth_nest_demo/api/index.ts

64 lines
1.8 KiB
TypeScript

import { NestFactory } from '@nestjs/core';
import { AppModule } from '../src/app.module';
import { HttpExceptionFilter } from '../src/common/filters/http-exception.filter';
import { TransformInterceptor } from '../src/common/interceptors/transform.interceptor';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as bodyParser from 'body-parser';
let cachedApp;
async function bootstrap() {
if (!cachedApp) {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'Accept',
'Origin',
'Referer',
'User-Agent',
],
});
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TransformInterceptor());
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
// Swagger 配置
const config = new DocumentBuilder()
.setTitle('我的应用 API')
.setDescription('这是我的应用程序的 API 文档')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.init();
cachedApp = app;
}
return cachedApp;
}
export default async (req, res) => {
const app = await bootstrap();
const server = app.getHttpAdapter().getInstance();
return server(req, res);
};