-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21faf25
commit f5809e5
Showing
4 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
slug: release/3.17.0 | ||
title: Release 3.17.0 | ||
authors: [harry] | ||
tags: [release] | ||
|
||
|
||
--- | ||
|
||
升级请参考 [如何更新 Midway](/docs/how_to_update_midway) 中描述,请不要单独升级某个组件包。 | ||
|
||
本次 3.17 版本,我们增加了一些新的特性,以及修复了一些问题。 | ||
|
||
|
||
## 定制服务端响应格式 | ||
|
||
在 3.17 版本中,我们增加了一个新的特性,可以定制服务端的响应的通用格式。 | ||
|
||
在之前的版本中,我们依靠中间件和过滤器来实现这个功能,但是这种方式有一些局限性,代码也会分散在不同的地方。 | ||
|
||
如果由一个统一的可调整的返回逻辑,可能更为合理,为此,添加了 `ServerResponse` 和 `HttpServerResponse` 的实现。 | ||
|
||
```typescript | ||
import { ServerResponse, HttpServerResponse } from '@midwayjs/core'; | ||
|
||
@Controller() | ||
export class HomeController { | ||
@Inject() | ||
ctx: Context; | ||
|
||
@Get('/') | ||
async index() { | ||
return new HttpServerResponse(this.ctx).json({ | ||
success: true, | ||
data: 'hello world', | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
`HttpServerResponse` 是 `ServerResponse` 的一个 Http 实现,提供了一些常用的方法。 | ||
|
||
最为特殊的是他可以针对不同的数据格式,设置成功和失败的模版。 | ||
|
||
比如针对 JSON 数据,框架提供了以下的默认结构。 | ||
|
||
```typescript | ||
HttpServerResponse.JSON_TPL = (data, isSuccess) => { | ||
if (isSuccess) { | ||
return { | ||
success: 'true', | ||
data, | ||
}; | ||
} else { | ||
return { | ||
success: 'false', | ||
message: data || 'fail', | ||
}; | ||
} | ||
}; | ||
``` | ||
|
||
这样,当返回 JSON 格式时,就会按照这个模版进行返回。 | ||
|
||
```typescript | ||
// 失败的返回 | ||
return new HttpServerResponse(this.ctx).fail().json('hello world'); | ||
``` | ||
|
||
就会获取到以下的数据。 | ||
|
||
```typescript | ||
{ | ||
success: 'false', | ||
message: 'hello world', | ||
} | ||
``` | ||
|
||
此外,基于这个模式,也同时实现了 SSE 的响应返回。 | ||
|
||
|
||
## 上传组件 | ||
|
||
由于在小文件场景下上传碰到一些问题,从 v3.17 开始,基于 [busboy](https://github.com/mscdex/busboy) 实现了一个新的上传组件,替换原有的 `@midwayjs/upload`。 | ||
|
||
和原有的组件比有一些不同。 | ||
|
||
* 1、不再默认加载中间件,因为上传只是少部分接口的特殊逻辑,不需要全局加载 | ||
* 2、配置的 key 为了避免冲突,从 `upload` 变为 `busboy` | ||
* 3、原有上传的数据中的 `filedName`,在流式模式下不再提供 | ||
|
||
其余的使用方式和原有的上传组件一致, | ||
|
||
更多细节请访问 [文档](/docs/extensions/busboy) | ||
|
||
|
||
|
||
## 更多的变化 | ||
|
||
* 修复了一个健康检查服务丢失 this 的问题 | ||
* 参数装饰器增加了一个当前实例的参数 | ||
|
||
以及一大批依赖进行了更新,可以参考我们的 [ChangeLog](https://midwayjs.org/changelog/v3.17.0)。 |