Skip to content

Commit c464cda

Browse files
authored
fix: use @eggjs/multipart and @eggjs/view (#5391)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Chores** - Switched to updated multipart and view handling packages to enhance performance and consistency. - **Documentation** - Clarified file upload sections with expanded guidance on both File and Stream modes. - Updated plugin and migration documentation with corrected links and examples to better reflect modern async standards. - Added directory structure details for the `egg-view-ejs` plugin. - Corrected URLs in various documentation sections to point to the new repository locations for plugins. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 15c5e5d commit c464cda

18 files changed

+59
-1175
lines changed

index-old.d.ts

-1,156
This file was deleted.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@
2626
"@eggjs/i18n": "^3.0.1",
2727
"@eggjs/jsonp": "^3.0.0",
2828
"@eggjs/logrotator": "^4.0.0",
29+
"@eggjs/multipart": "^4.0.0",
2930
"@eggjs/onerror": "^3.0.0",
3031
"@eggjs/schedule": "^5.0.2",
3132
"@eggjs/security": "^4.0.0",
3233
"@eggjs/session": "^4.0.1",
3334
"@eggjs/static": "^3.0.0",
3435
"@eggjs/utils": "^4.2.5",
36+
"@eggjs/view": "^3.0.1",
3537
"@eggjs/watcher": "^4.0.4",
3638
"circular-json-for-egg": "^1.0.0",
3739
"cluster-client": "^3.7.0",
3840
"egg-errors": "^2.3.2",
3941
"egg-logger": "^3.6.1",
40-
"egg-multipart": "^3.5.0",
41-
"egg-view": "^2.1.4",
4242
"extend2": "^4.0.0",
4343
"graceful": "^2.0.0",
4444
"humanize-ms": "^2.0.0",

site/docs/advanced/view-plugin.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ order: 5
88
本文将阐述框架对 View 插件的规范约束。我们可以依此来封装对应的模板引擎插件。以下以 [egg-view-ejs](https://github.com/eggjs/egg-view-ejs) 为例。
99

1010
## 插件目录结构
11+
1112
```bash
1213
egg-view-ejs
1314
├── config

site/docs/basics/controller.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ If user request exceeds the maximum length for parsing that we configured, the f
315315

316316
### Acquiring the Submitted Files
317317

318-
The `body` in the request can carry parameters as well as files. Generally speaking, our browsers always send files in `multipart/form-data`, and we now have two kinds of ways supporting submitting and acquiring files with the help of the framework's plugin [Multipart](https://github.com/eggjs/egg-multipart).
318+
The `body` in the request can carry parameters as well as files. Generally speaking, our browsers always send files in `multipart/form-data`, and we now have two kinds of ways supporting submitting and acquiring files with the help of the framework's plugin [Multipart](https://github.com/eggjs/multipart).
319319

320320
- #### `File` Mode:
321321
If you have no ideas about Nodejs's Stream at all, the `File` mode suits you well:
@@ -589,7 +589,7 @@ module.exports = {
589589

590590
**Notice:`fileExtensions` will be IGNORED when `whitelist` is overwritten.**
591591

592-
For more tech details about this, please refer [Egg-Multipart](https://github.com/eggjs/egg-multipart).
592+
For more tech details about this, please refer [@eggjs/multipart](https://github.com/eggjs/multipart).
593593

594594
### `header`
595595

site/docs/basics/controller.zh-CN.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ module.exports = {
311311
**注意:调整 bodyParser 支持的 body 长度时,如果应用之前有一层反向代理(如 Nginx),同样需要调整配置确保支持相等长度的请求 body。**
312312

313313
**常见错误:将 `ctx.request.body``ctx.body` 混淆,后者实际上是 `ctx.response.body` 的简写。**
314+
314315
### 获取上传的文件
315316

316-
请求体除了可以带参数之外,还可以发送文件。通常情况下,浏览器会通过 `Multipart/form-data` 格式发送文件。通过内置的 [Multipart](https://github.com/eggjs/egg-multipart) 插件,框架支持获取用户上传的文件。我们为你提供了两种方式:
317+
请求体除了可以带参数之外,还可以发送文件。通常情况下,浏览器会通过 `Multipart/form-data` 格式发送文件。通过内置的 [Multipart](https://github.com/eggjs/multipart) 插件,框架支持获取用户上传的文件。我们为你提供了两种方式:
317318

318319
#### File 模式
319320

@@ -579,7 +580,8 @@ module.exports = {
579580

580581
**注意:当重写了 whitelist 时,fileExtensions 不生效。**
581582

582-
欲了解更多有关的技术细节和信息,请参阅 [Egg-Multipart](https://github.com/eggjs/egg-multipart)
583+
欲了解更多有关的技术细节和信息,请参阅 [@eggjs/multipart](https://github.com/eggjs/multipart)
584+
583585
### Header
584586

585587
除了从 URL 和请求 body 上获取参数之外,还有许多参数是通过请求 header 传递的。框架提供了一些辅助属性和方法来获取:

site/docs/basics/plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Specific consolidation rules can be found in [Configuration](./config.md).
164164
  - [session](https://github.com/eggjs/session) Session implementation
165165
  - [i18n](https://github.com/eggjs/i18n) Multilingual
166166
  - [watcher](https://github.com/eggjs/watcher) File and folder monitoring
167-
  - [multipart](https://github.com/eggjs/egg-multipart) File Streaming Upload
167+
  - [multipart](https://github.com/eggjs/multipart) File Streaming Upload
168168
  - [security](https://github.com/eggjs/security) Security
169169
  - [development](https://github.com/eggjs/development) Development Environment Configuration
170170
  - [logrotator](https://github.com/eggjs/logrotator) Log segmentation

site/docs/basics/plugin.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ exports.mysql = {
164164
- [session](https://github.com/eggjs/session) Session 实现
165165
- [i18n](https://github.com/eggjs/i18n) 多语言
166166
- [watcher](https://github.com/eggjs/watcher) 文件和文件夹监控
167-
- [multipart](https://github.com/eggjs/egg-multipart) 文件流式上传
167+
- [multipart](https://github.com/eggjs/multipart) 文件流式上传
168168
- [security](https://github.com/eggjs/security) 安全
169169
- [development](https://github.com/eggjs/development) 开发环境配置
170170
- [logrotator](https://github.com/eggjs/logrotator) 日志切分

site/docs/core/view.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,4 @@ The built-in plugin [@eggjs/security] provides common security helper functions,
222222

223223
[@eggjs/security]: https://github.com/eggjs/security
224224
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
225-
[egg-view]: https://github.com/eggjs/egg-view
225+
[egg-view]: https://github.com/eggjs/view

site/docs/intro/migration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ while ((part = await parts()) != null) {
6060
}
6161
```
6262

63-
- [egg-multipart#upload-multiple-files](https://github.com/eggjs/egg-multipart#upload-multiple-files)
63+
- [egg-multipart#upload-multiple-files](https://github.com/eggjs/multipart#upload-multiple-files)
6464

6565
### egg-userrole
6666

site/docs/intro/migration.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ while ((part = await parts()) != null) {
6161
}
6262
```
6363

64-
- [egg-multipart#upload-multiple-files](https://github.com/eggjs/egg-multipart#upload-multiple-files)
64+
- [egg-multipart#upload-multiple-files](https://github.com/eggjs/multipart#upload-multiple-files)
6565

6666
### egg-userrole
6767

site/docs/tutorials/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Official maintained ORM model is [egg-orm] base on [Leoric], and the following d
5151
[egg-sequelize]: https://github.com/eggjs/egg-sequelize
5252
[egg-mongoose]: https://github.com/eggjs/egg-mongoose
5353
[egg-mysql]: https://github.com/eggjs/egg-mysql
54-
[egg-view]: https://github.com/eggjs/egg-view
54+
[egg-view]: https://github.com/eggjs/view
5555
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
5656
[egg-view-ejs]: https://github.com/eggjs/egg-view-ejs
5757
[egg-view-handlebars]: https://github.com/eggjs/egg-view-handlebars

site/docs/tutorials/index.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $ npm init egg --type=simple
5353
[egg-sequelize]: https://github.com/eggjs/egg-sequelize
5454
[egg-mongoose]: https://github.com/eggjs/egg-mongoose
5555
[egg-mysql]: https://github.com/eggjs/egg-mysql
56-
[egg-view]: https://github.com/eggjs/egg-view
56+
[egg-view]: https://github.com/eggjs/view
5757
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
5858
[egg-view-ejs]: https://github.com/eggjs/egg-view-ejs
5959
[egg-view-handlebars]: https://github.com/eggjs/egg-view-handlebars

site/docs/tutorials/typescript.zh-CN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,9 @@ describe('typescript', () => {
706706
以下几个项目可作为单元测试参考:
707707

708708
- [https://github.com/eggjs/egg](https://github.com/eggjs/egg)
709-
- [https://github.com/eggjs/egg-view](https://github.com/eggjs/egg-view)
709+
- [https://github.com/eggjs/view](https://github.com/eggjs/view)
710710
- [https://github.com/eggjs/egg-logger](https://github.com/eggjs/egg-logger)
711+
711712
### 编译速度慢?
712713

713714
根据我们的实践,`ts-node` 是目前相对较优的解决方案,既不用另起终端执行 `tsc`,也能获得还能接受的启动速度(仅限于 `ts-node@7`,新的版本由于把文件缓存去掉了,导致特别慢([#754](https://github.com/TypeStrong/ts-node/issues/754)),因此未升级)。

src/app/extend/request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class Request extends EggCoreRequest {
1717
declare response: Response;
1818

1919
/**
20-
* Request body, parsed from koa-bodyparser or egg-multipart
20+
* Request body, parsed from koa-bodyparser or @eggjs/multipart
2121
*/
2222
declare body: any;
2323

src/config/plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default {
5050
*/
5151
multipart: {
5252
enable: true,
53-
package: 'egg-multipart',
53+
package: '@eggjs/multipart',
5454
},
5555

5656
/**
@@ -127,6 +127,6 @@ export default {
127127
*/
128128
view: {
129129
enable: true,
130-
package: 'egg-view',
130+
package: '@eggjs/view',
131131
},
132132
};

src/lib/types.ts

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import '@eggjs/schedule';
2727
import '@eggjs/session';
2828
import '@eggjs/onerror';
2929
import '@eggjs/logrotator';
30+
import '@eggjs/multipart';
31+
import '@eggjs/view';
3032

3133
export type {
3234
EggAppInfo,
@@ -81,6 +83,20 @@ export interface HttpClientConfig {
8183
useHttpClientNext?: boolean;
8284
}
8385

86+
/**
87+
* Powerful Partial, Support adding ? modifier to a mapped property in deep level
88+
* @example
89+
* import { PowerPartial, EggAppConfig } from 'egg';
90+
*
91+
* // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } }
92+
* type EggConfig = PowerPartial<EggAppConfig>
93+
*/
94+
export type PowerPartial<T> = {
95+
[U in keyof T]?: T[U] extends object
96+
? PowerPartial<T[U]>
97+
: T[U]
98+
};
99+
84100
export interface EggAppConfig extends EggCoreAppConfig {
85101
workerStartTimeout: number;
86102
baseDir: string;

test/index.test-d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ expectType<boolean>(app.config.logrotator.gzip);
8585
expectType<string>(app.config.logrotator.hourDelimiter);
8686
expectType<string[] | null>(app.config.logrotator.filesRotateBySize);
8787

88+
// multipart plugin types
89+
expectType<boolean>(app.config.multipart.cleanSchedule.disable);
90+
expectType<string>(app.config.multipart.cleanSchedule.cron);
91+
expectType<string>(app.config.multipart.defaultCharset);
92+
expectType<'file' | 'stream'>(app.config.multipart.mode);
93+
94+
// view plugin types
95+
expectType<string>(app.config.view.defaultViewEngine);
96+
expectType<string>(app.config.view.root);
97+
expectType<string>(app.config.view.mapping.html);
98+
expectType<string>(app.config.view.defaultExtension);
99+
expectType<string>(await ctx.renderString('hello'));
100+
expectType<string>(await ctx.view.renderString('hello'));
101+
const ViewEngine = app.view.get('html')!;
102+
expectType<string>(await new ViewEngine(ctx).render('hello'));
103+
88104
class AppBoot implements ILifecycleBoot {
89105
private readonly app: Application;
90106

test/lib/core/view.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ describe('test/lib/core/view.test.ts', () => {
1818
describe('use', () => {
1919
it('should register success', () => {
2020
class View {
21-
render() {}
22-
renderString() {}
21+
async render() {
22+
return '';
23+
}
24+
async renderString() {
25+
return '';
26+
}
2327
}
2428
app.view.use('e', View);
2529
// assert(app.view.has('e'));

0 commit comments

Comments
 (0)