Skip to content

Commit

Permalink
fix: use @eggjs/multipart and @eggjs/view (#5391)
Browse files Browse the repository at this point in the history
<!-- 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 -->
  • Loading branch information
fengmk2 authored Feb 4, 2025
1 parent 15c5e5d commit c464cda
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 1,175 deletions.
1,156 changes: 0 additions & 1,156 deletions index-old.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
"@eggjs/i18n": "^3.0.1",
"@eggjs/jsonp": "^3.0.0",
"@eggjs/logrotator": "^4.0.0",
"@eggjs/multipart": "^4.0.0",
"@eggjs/onerror": "^3.0.0",
"@eggjs/schedule": "^5.0.2",
"@eggjs/security": "^4.0.0",
"@eggjs/session": "^4.0.1",
"@eggjs/static": "^3.0.0",
"@eggjs/utils": "^4.2.5",
"@eggjs/view": "^3.0.1",
"@eggjs/watcher": "^4.0.4",
"circular-json-for-egg": "^1.0.0",
"cluster-client": "^3.7.0",
"egg-errors": "^2.3.2",
"egg-logger": "^3.6.1",
"egg-multipart": "^3.5.0",
"egg-view": "^2.1.4",
"extend2": "^4.0.0",
"graceful": "^2.0.0",
"humanize-ms": "^2.0.0",
Expand Down
1 change: 1 addition & 0 deletions site/docs/advanced/view-plugin.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ order: 5
本文将阐述框架对 View 插件的规范约束。我们可以依此来封装对应的模板引擎插件。以下以 [egg-view-ejs](https://github.com/eggjs/egg-view-ejs) 为例。

## 插件目录结构

```bash
egg-view-ejs
├── config
Expand Down
4 changes: 2 additions & 2 deletions site/docs/basics/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ If user request exceeds the maximum length for parsing that we configured, the f

### Acquiring the Submitted Files

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).
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).

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

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

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

### `header`

Expand Down
6 changes: 4 additions & 2 deletions site/docs/basics/controller.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ module.exports = {
**注意:调整 bodyParser 支持的 body 长度时,如果应用之前有一层反向代理(如 Nginx),同样需要调整配置确保支持相等长度的请求 body。**

**常见错误:将 `ctx.request.body``ctx.body` 混淆,后者实际上是 `ctx.response.body` 的简写。**

### 获取上传的文件

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

#### File 模式

Expand Down Expand Up @@ -579,7 +580,8 @@ module.exports = {

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

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

### Header

除了从 URL 和请求 body 上获取参数之外,还有许多参数是通过请求 header 传递的。框架提供了一些辅助属性和方法来获取:
Expand Down
2 changes: 1 addition & 1 deletion site/docs/basics/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Specific consolidation rules can be found in [Configuration](./config.md).
  - [session](https://github.com/eggjs/session) Session implementation
  - [i18n](https://github.com/eggjs/i18n) Multilingual
  - [watcher](https://github.com/eggjs/watcher) File and folder monitoring
  - [multipart](https://github.com/eggjs/egg-multipart) File Streaming Upload
  - [multipart](https://github.com/eggjs/multipart) File Streaming Upload
  - [security](https://github.com/eggjs/security) Security
  - [development](https://github.com/eggjs/development) Development Environment Configuration
  - [logrotator](https://github.com/eggjs/logrotator) Log segmentation
Expand Down
2 changes: 1 addition & 1 deletion site/docs/basics/plugin.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ exports.mysql = {
- [session](https://github.com/eggjs/session) Session 实现
- [i18n](https://github.com/eggjs/i18n) 多语言
- [watcher](https://github.com/eggjs/watcher) 文件和文件夹监控
- [multipart](https://github.com/eggjs/egg-multipart) 文件流式上传
- [multipart](https://github.com/eggjs/multipart) 文件流式上传
- [security](https://github.com/eggjs/security) 安全
- [development](https://github.com/eggjs/development) 开发环境配置
- [logrotator](https://github.com/eggjs/logrotator) 日志切分
Expand Down
2 changes: 1 addition & 1 deletion site/docs/core/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ The built-in plugin [@eggjs/security] provides common security helper functions,

[@eggjs/security]: https://github.com/eggjs/security
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[egg-view]: https://github.com/eggjs/egg-view
[egg-view]: https://github.com/eggjs/view
2 changes: 1 addition & 1 deletion site/docs/intro/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ while ((part = await parts()) != null) {
}
```

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

### egg-userrole

Expand Down
2 changes: 1 addition & 1 deletion site/docs/intro/migration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ while ((part = await parts()) != null) {
}
```

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

### egg-userrole

Expand Down
2 changes: 1 addition & 1 deletion site/docs/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Official maintained ORM model is [egg-orm] base on [Leoric], and the following d
[egg-sequelize]: https://github.com/eggjs/egg-sequelize
[egg-mongoose]: https://github.com/eggjs/egg-mongoose
[egg-mysql]: https://github.com/eggjs/egg-mysql
[egg-view]: https://github.com/eggjs/egg-view
[egg-view]: https://github.com/eggjs/view
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[egg-view-ejs]: https://github.com/eggjs/egg-view-ejs
[egg-view-handlebars]: https://github.com/eggjs/egg-view-handlebars
Expand Down
2 changes: 1 addition & 1 deletion site/docs/tutorials/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $ npm init egg --type=simple
[egg-sequelize]: https://github.com/eggjs/egg-sequelize
[egg-mongoose]: https://github.com/eggjs/egg-mongoose
[egg-mysql]: https://github.com/eggjs/egg-mysql
[egg-view]: https://github.com/eggjs/egg-view
[egg-view]: https://github.com/eggjs/view
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[egg-view-ejs]: https://github.com/eggjs/egg-view-ejs
[egg-view-handlebars]: https://github.com/eggjs/egg-view-handlebars
Expand Down
3 changes: 2 additions & 1 deletion site/docs/tutorials/typescript.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,9 @@ describe('typescript', () => {
以下几个项目可作为单元测试参考:

- [https://github.com/eggjs/egg](https://github.com/eggjs/egg)
- [https://github.com/eggjs/egg-view](https://github.com/eggjs/egg-view)
- [https://github.com/eggjs/view](https://github.com/eggjs/view)
- [https://github.com/eggjs/egg-logger](https://github.com/eggjs/egg-logger)

### 编译速度慢?

根据我们的实践,`ts-node` 是目前相对较优的解决方案,既不用另起终端执行 `tsc`,也能获得还能接受的启动速度(仅限于 `ts-node@7`,新的版本由于把文件缓存去掉了,导致特别慢([#754](https://github.com/TypeStrong/ts-node/issues/754)),因此未升级)。
Expand Down
2 changes: 1 addition & 1 deletion src/app/extend/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Request extends EggCoreRequest {
declare response: Response;

/**
* Request body, parsed from koa-bodyparser or egg-multipart
* Request body, parsed from koa-bodyparser or @eggjs/multipart
*/
declare body: any;

Expand Down
4 changes: 2 additions & 2 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default {
*/
multipart: {
enable: true,
package: 'egg-multipart',
package: '@eggjs/multipart',
},

/**
Expand Down Expand Up @@ -127,6 +127,6 @@ export default {
*/
view: {
enable: true,
package: 'egg-view',
package: '@eggjs/view',
},
};
16 changes: 16 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import '@eggjs/schedule';
import '@eggjs/session';
import '@eggjs/onerror';
import '@eggjs/logrotator';
import '@eggjs/multipart';
import '@eggjs/view';

export type {
EggAppInfo,
Expand Down Expand Up @@ -81,6 +83,20 @@ export interface HttpClientConfig {
useHttpClientNext?: boolean;
}

/**
* Powerful Partial, Support adding ? modifier to a mapped property in deep level
* @example
* import { PowerPartial, EggAppConfig } from 'egg';
*
* // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } }
* type EggConfig = PowerPartial<EggAppConfig>
*/
export type PowerPartial<T> = {
[U in keyof T]?: T[U] extends object
? PowerPartial<T[U]>
: T[U]
};

export interface EggAppConfig extends EggCoreAppConfig {
workerStartTimeout: number;
baseDir: string;
Expand Down
16 changes: 16 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ expectType<boolean>(app.config.logrotator.gzip);
expectType<string>(app.config.logrotator.hourDelimiter);
expectType<string[] | null>(app.config.logrotator.filesRotateBySize);

// multipart plugin types
expectType<boolean>(app.config.multipart.cleanSchedule.disable);
expectType<string>(app.config.multipart.cleanSchedule.cron);
expectType<string>(app.config.multipart.defaultCharset);
expectType<'file' | 'stream'>(app.config.multipart.mode);

// view plugin types
expectType<string>(app.config.view.defaultViewEngine);
expectType<string>(app.config.view.root);
expectType<string>(app.config.view.mapping.html);
expectType<string>(app.config.view.defaultExtension);
expectType<string>(await ctx.renderString('hello'));
expectType<string>(await ctx.view.renderString('hello'));
const ViewEngine = app.view.get('html')!;
expectType<string>(await new ViewEngine(ctx).render('hello'));

class AppBoot implements ILifecycleBoot {
private readonly app: Application;

Expand Down
8 changes: 6 additions & 2 deletions test/lib/core/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ describe('test/lib/core/view.test.ts', () => {
describe('use', () => {
it('should register success', () => {
class View {
render() {}
renderString() {}
async render() {
return '';
}
async renderString() {
return '';
}
}
app.view.use('e', View);
// assert(app.view.has('e'));
Expand Down

0 comments on commit c464cda

Please sign in to comment.