Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use @eggjs/multipart and @eggjs/view #5391

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions index-old.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
// BaseContextClass as CoreBaseContextClass,
// } from 'egg-core';
// import EggCookies = require('egg-cookies');
// import 'egg-multipart';
// import 'egg-view';

// declare module 'egg' {
// export type EggLogger = Logger;
Expand Down Expand Up @@ -1043,20 +1041,6 @@

// export function start(options?: StartOptions): Promise<Application>;

// /**
// * 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]
// };

// // send data can be number|string|boolean|object but not Set|Map
// export interface Messenger extends EventEmitter {
// /**
Expand Down
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
Loading