nest.js swagger返回类型code
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nest.js swagger返回类型code相关的知识,希望对你有一定的参考价值。
参考技术A 在 controller 返回的数据都是从数据库表结构而来:"id": "d8d5a56c-ee9f-4e41-be48-5414a7a5712c",
"username": "Akeem.Cremin",
"password": "$2b$10$kRcsmN6ewFC2GOs0TEg6TuvDbNzf1VGCbQf2fI1UeyPAiZCq9rMKm",
"email": "Garrett87@hotmail.com",
"nickname": "Wallace Nicolas",
"role": "user",
"isActive": true,
"createdTime": "2021-03-24T15:24:26.806Z",
"updatedTime": "2021-03-24T15:24:26.806Z"
如果需要定义最终返回接口的数据格式例如:
"statusCode": 200,
"message": "获取成功",
"data":
"id": "d8d5a56c-ee9f-4e41-be48-5414a7a5712c",
"username": "Akeem.Cremin",
"password": "$2b$10$kRcsmN6ewFC2GOs0TEg6TuvDbNzf1VGCbQf2fI1UeyPAiZCq9rMKm",
"email": "Garrett87@hotmail.com",
"nickname": "Wallace Nicolas",
"role": "user",
"isActive": true,
"createdTime": "2021-03-24T15:24:26.806Z",
"updatedTime": "2021-03-24T15:24:26.806Z"
这里就需要做个自定义成功请求拦截器:
nest g in shared/interceptor/transform
import CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor from '@nestjs/common'
import Observable from 'rxjs'
import map from 'rxjs/operators'
import Request from 'express'
interface Response<T>
data: T
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>>
intercept(context: ExecutionContext, next: CallHandler<T>): Observable<any>
const request = context.switchToHttp().getRequest<Request>()
Logger.log(request.url, '正常接口请求')
return next.handle().pipe(
map(data =>
return
data: data,
statusCode: 200,
message: '请求成功'
)
)
然后在 app.module.ts 引入即可使用:
import ValidationPipe from '@nestjs/common'
import APP_INTERCEPTOR from '@nestjs/core'
import TransformInterceptor from '@/shared/interceptor/transform.interceptor'
@Module(
imports: [
// ...
],
controllers: [AppController],
providers: [
provide: APP_INTERCEPTOR,
useClass: TransformInterceptor
]
)
不过 APP_INTERCEPTOR 排序要注意,TransformInterceptor 最好放在第一个,否则会失效。
错误过滤器:
nest g f shared/filters/httpException
import ArgumentsHost, Catch, ExceptionFilter, HttpException, Logger from '@nestjs/common'
import Response, Request from 'express'
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter
catch(exception: HttpException, host: ArgumentsHost)
const context = host.switchToHttp()
const response = context.getResponse<Response>()
const request = context.getRequest<Request>()
const status = exception.getStatus()
const message = exception.message
Logger.log(`$request.url - $message`, '非正常接口请求')
response.status(status).json(
statusCode: status,
message: message,
path: request.url,
timestamp: new Date().toISOString()
)
然后在 app.module.ts 引入即可使用:
import ValidationPipe from '@nestjs/common'
import APP_FILTER from '@nestjs/core'
import HttpExceptionFilter from '@/shared/filters/http-exception.filter'
@Module(
imports: [
// ...
],
controllers: [AppController],
providers: [
provide: APP_FILTER,
useClass: HttpExceptionFilter
]
)
nest.js @Post 设置响应的内容类型
【中文标题】nest.js @Post 设置响应的内容类型【英文标题】:nest.js @Post setting the content-type of the response 【发布时间】:2019-03-03 01:21:54 【问题描述】:我正在尝试为我的 API 实现一个 POST 端点,它在被调用时会返回一个 HTML 字符串。
我的代码现在是这样的:
import Controller, Post from '@nestjs/common';
@Controller()
export class MyController
@Post('/endpoint')
public create(): string
return `
<!DOCTYPE html>
…
</html>`;
如何告诉 POST 端点发送正确的内容类型及其响应?我搜索了所有文档,但找不到任何对我有用的东西。
提前感谢您的帮助
【问题讨论】:
【参考方案1】:我自己找到了答案。我刚刚在@Post
装饰器后面添加了@Header
装饰器:
import Header from '@nestjs/common'
@Post('/endpoint')
@Header('content-type', 'text/html')
public create(): string
//
【讨论】:
以上是关于nest.js swagger返回类型code的主要内容,如果未能解决你的问题,请参考以下文章
在 Nest.js 项目中添加 Swagger 基本路径字段
如果在 Nest.js 中不能使用“any”,架构中字段的类型应该是啥?