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

refactor: use @eggjs/jsonp @eggjs/i18n @eggjs/static #5382

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 0 additions & 47 deletions index-old.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@
// import EggCookies = require('egg-cookies');
// import 'egg-onerror';
// import 'egg-session';
// import 'egg-i18n';
// import 'egg-multipart';
// import 'egg-security';
// import 'egg-logrotator';
// import '@eggjs/schedule';
// import 'egg-static';
// import 'egg-jsonp';
// import 'egg-view';

// declare module 'egg' {
Expand Down Expand Up @@ -400,52 +397,11 @@

// hostHeaders: string;

// /**
// * I18n options
// */
// i18n: {
// /**
// * default value EN_US
// */
// defaultLocale: string;
// /**
// * i18n resource file dir, not recommend to change default value
// */
// dirs: string[];
// /**
// * custom the locale value field, default `query.locale`, you can modify this config, such as `query.lang`
// */
// queryField: string;
// /**
// * The locale value key in the cookie, default is locale.
// */
// cookieField: string;
// /**
// * Locale cookie expire time, default `1y`, If pass number value, the unit will be ms
// */
// cookieMaxAge: string | number;
// };

// /**
// * Detect request' ip from specified headers, not case-sensitive. Only worked when config.proxy set to true.
// */
// ipHeaders: string;

// /**
// * jsonp options
// * @member Config#jsonp
// * @property {String} callback - jsonp callback method key, default to `_callback`
// * @property {Number} limit - callback method name's max length, default to `50`
// * @property {Boolean} csrf - enable csrf check or not. default to false
// * @property {String|RegExp|Array} whiteList - referrer white list
// */
// jsonp: {
// limit: number;
// callback: string;
// csrf: boolean;
// whiteList: string | RegExp | Array<string | RegExp>;
// };

// /**
// * The key that signing cookies. It can contain multiple keys seperated by .
// */
Expand Down Expand Up @@ -964,9 +920,6 @@
// */
// curl: EggHttpClient['request'];

// __(key: string, ...values: string[]): string;
// gettext(key: string, ...values: string[]): string;

// /**
// * get upload file stream
// * @example
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
"@eggjs/cookies": "^3.0.0",
"@eggjs/core": "^6.2.13",
"@eggjs/development": "^4.0.0",
"@eggjs/i18n": "^3.0.1",
"@eggjs/jsonp": "^3.0.0",
"@eggjs/schedule": "^5.0.2",
"@eggjs/static": "^3.0.0",
"@eggjs/utils": "^4.2.4",
"@eggjs/watcher": "^4.0.3",
"circular-json-for-egg": "^1.0.0",
"cluster-client": "^3.7.0",
"egg-errors": "^2.3.1",
"egg-i18n": "^2.1.1",
"egg-jsonp": "^2.0.0",
"egg-logger": "^3.6.0",
"egg-logrotator": "^3.1.0",
"egg-multipart": "^3.1.0",
"egg-onerror": "^2.1.1",
"egg-security": "^3.0.0",
"egg-session": "^3.3.0",
"egg-static": "^2.2.0",
"egg-view": "^2.1.3",
"extend2": "^4.0.0",
"graceful": "^2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions site/docs/basics/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ Specific consolidation rules can be found in [Configuration](./config.md).
- Framework has default built-in plugins for enterprise applications [Common plugins](https://eggjs.org/zh-cn/plugins/):
  - [onerror](https://github.com/eggjs/egg-onerror) Uniform Exception Handling
  - [Session](https://github.com/eggjs/egg-session) Session implementation
  - [i18n](https://github.com/eggjs/egg-i18n) Multilingual
  - [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
  - [security](https://github.com/eggjs/egg-security) Security
  - [development](https://github.com/eggjs/development) Development Environment Configuration
  - [logrotator](https://github.com/eggjs/egg-logrotator) Log segmentation
  - [schedule](https://github.com/eggjs/egg-schedule) Timing tasks
  - [static](https://github.com/eggjs/egg-static) Static server
  - [jsonp](https://github.com/eggjs/egg-jsonp) jsonp support
  - [schedule](https://github.com/eggjs/schedule) Timing tasks
  - [static](https://github.com/eggjs/static) Static server
  - [jsonp](https://github.com/eggjs/jsonp) jsonp support
  - [view](https://github.com/eggjs/egg-view) Template Engine
- More community plugins can be found on GitHub [egg-plugin](https://github.com/topics/egg-plugin).

Expand Down
8 changes: 4 additions & 4 deletions site/docs/basics/plugin.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ exports.mysql = {
- 框架默认内置了企业级应用[常用的插件](https://eggjs.org/zh-cn/plugins/):
- [onerror](https://github.com/eggjs/egg-onerror) 统一异常处理
- [Session](https://github.com/eggjs/egg-session) Session 实现
- [i18n](https://github.com/eggjs/egg-i18n) 多语言
- [i18n](https://github.com/eggjs/i18n) 多语言
- [watcher](https://github.com/eggjs/watcher) 文件和文件夹监控
- [multipart](https://github.com/eggjs/egg-multipart) 文件流式上传
- [security](https://github.com/eggjs/egg-security) 安全
- [development](https://github.com/eggjs/development) 开发环境配置
- [logrotator](https://github.com/eggjs/egg-logrotator) 日志切分
- [schedule](https://github.com/eggjs/egg-schedule) 定时任务
- [static](https://github.com/eggjs/egg-static) 静态服务器
- [jsonp](https://github.com/eggjs/egg-jsonp) jsonp 支持
- [schedule](https://github.com/eggjs/schedule) 定时任务
- [static](https://github.com/eggjs/static) 静态服务器
- [jsonp](https://github.com/eggjs/jsonp) jsonp 支持
- [view](https://github.com/eggjs/egg-view) 模板引擎
- 更多社区的插件可以在 GitHub 上搜索 [egg-plugin](https://github.com/topics/egg-plugin)。

Expand Down
4 changes: 2 additions & 2 deletions site/docs/basics/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ As above, directories by conventions of framework:
- `app/controller/**` used to parse the input from user, return the corresponding results after processing, see [Controller](./controller.md) for details.
- `app/service/**` used for business logic layer, optional, recommend to use,see [Service](./service.md) for details.
- `app/middleware/**` uesd for middleware, optional, see [Middleware](./middleware.md) for details.
- `app/public/**` used to place static resources, optional, see built-in plugin [egg-static](https://github.com/eggjs/egg-static) for details.
- `app/public/**` used to place static resources, optional, see built-in plugin [@eggjs/static](https://github.com/eggjs/static) for details.
- `app/extend/**` used for extensions of the framework, optional, see [Extend EGG](./extend.md) for details.
- `config/config.{env}.js` used to write configuration files, see [Configuration](./config.md) for details.
- `config/plugin.js` used to configure the plugins that need to be loaded, see [Plugin](./plugin.md) for details.
Expand All @@ -60,7 +60,7 @@ As above, directories by conventions of framework:

Directories by conventions of built-in plugins:

- `app/public/**` used to place static resources, optional, see built-in plugin [egg-static](https://github.com/eggjs/egg-static) for details.
- `app/public/**` used to place static resources, optional, see built-in plugin [@eggjs/static](https://github.com/eggjs/static) for details.
- `app/schedule/**` used for scheduled tasks, optional, see [Scheduled Task](./schedule.md) for details.

**To customize your own directory specification, see [Loader API](../advanced/loader.md)**
Expand Down
5 changes: 2 additions & 3 deletions site/docs/basics/structure.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ egg-project
- `app/controller/**` 用于解析用户的输入,处理后返回相应的结果,具体参见 [Controller](./controller.md)。
- `app/service/**` 用于编写业务逻辑层,建议使用,具体参见 [Service](./service.md)。
- `app/middleware/**` 用于编写中间件,具体参见 [Middleware](./middleware.md)。
- `app/public/**` 用于放置静态资源,具体参见内置插件 [egg-static](https://github.com/eggjs/egg-static)。
- `app/public/**` 用于放置静态资源,具体参见内置插件 [@eggjs/static](https://github.com/eggjs/static)。
- `app/extend/**` 用于框架的扩展,具体参见 [框架扩展](./extend.md)。
- `config/config.{env}.js` 用于编写配置文件,具体参见 [配置](./config.md)。
- `config/plugin.js` 用于配置需要加载的插件,具体参见 [插件](./plugin.md)。
- `test/**` 用于单元测试,具体参见 [单元测试](../core/unittest.md)。
- `app.js` 和 `agent.js` 用于自定义启动时的初始化工作,具体参见 [启动自定义](./app-start.md)。关于 `agent.js` 的作用,参见 [Agent 机制](../core/cluster-and-ipc.md#agent-机制)。


由内置插件约定的目录:

- `app/public/**` 用于放置静态资源,具体参见内置插件 [egg-static](https://github.com/eggjs/egg-static)。
- `app/public/**` 用于放置静态资源,具体参见内置插件 [@eggjs/static](https://github.com/eggjs/static)。
- `app/schedule/**` 用于定时任务,具体参见 [定时任务](./schedule.md)。

**若需自定义自己的目录规范,参见 [Loader API](https://eggjs.org/zh-cn/advanced/loader.html)**
Expand Down
2 changes: 1 addition & 1 deletion site/docs/core/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: I18n Internationalization
order: 11
---

For developing the multi-language application, build-in I18n support by [egg-i18n](https://github.com/eggjs/egg-i18n) plugin
For developing the multi-language application, build-in I18n support by [@eggjs/i18n](https://github.com/eggjs/i18n) plugin

## Default Language

Expand Down
2 changes: 1 addition & 1 deletion site/docs/core/i18n.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: 国际化(I18n)
order: 11
---

为了方便开发多语言应用,框架内置了国际化(I18n)支持,由 [egg-i18n](https://github.com/eggjs/egg-i18n) 插件提供。
为了方便开发多语言应用,框架内置了国际化(I18n)支持,由 [@eggjs/i18n](https://github.com/eggjs/i18n) 插件提供。

## 默认语言

Expand Down
6 changes: 3 additions & 3 deletions site/docs/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ open http://localhost:7001

### Adding Static Assets

Egg has a built-in plugin called [static][egg-static].
Egg has a built-in plugin called [static][@eggjs/static].
In production, it is recommended that you deploy static assets to CDN instead of using this plugin.

[static][egg-static] maps `/public/*` to the directory `app/public/*` by default.
[static][@eggjs/static] maps `/public/*` to the directory `app/public/*` by default.

In this case, we just need to put our static assets into the directory `app/public`.

Expand Down Expand Up @@ -476,7 +476,7 @@ Where to go from here? read our documentation to better understand the framework

[node.js]: http://nodejs.org
[egg-bin]: https://github.com/eggjs/egg-bin
[egg-static]: https://github.com/eggjs/egg-static
[@eggjs/static]: https://github.com/eggjs/static
[@eggjs/development]: https://github.com/eggjs/development
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[nunjucks]: https://mozilla.github.io/nunjucks/
4 changes: 2 additions & 2 deletions site/docs/intro/quickstart.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ $ open http://localhost:7001

### 静态资源

Egg 内置了 [static][egg-static] 插件,线上环境建议部署到 CDN,无需该插件。
Egg 内置了 [static][@eggjs/static] 插件,线上环境建议部署到 CDN,无需该插件。

static 插件默认映射 `/public/* -> app/public/*` 目录。

Expand Down Expand Up @@ -456,7 +456,7 @@ $ npm test

[node.js]: http://nodejs.org
[egg-bin]: https://github.com/eggjs/egg-bin
[egg-static]: https://github.com/eggjs/egg-static
[@eggjs/static]: https://github.com/eggjs/static
[@eggjs/development]: https://github.com/eggjs/development
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[urllib]: https://www.npmjs.com/package/urllib
Expand Down
2 changes: 1 addition & 1 deletion site/docs/tutorials/assets.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ exports.assets = {

**注意**:此处添加了 `SET_PUBLIC_PATH` 变量,因为 roadhog 这样才能开启 publicPath。

构建的结果根据 `.webpackrc` 配置的 output 决定,示例中是放到 `app/public` 目录下,由 `egg-static` 提供服务。
构建的结果根据 `.webpackrc` 配置的 output 决定,示例中是放到 `app/public` 目录下,由 `@eggjs/static` 提供服务。

同时根据 `.webpackrc` 配置的 manifest 生成一个 `manifest.json` 文件到 `config` 目录下(Egg 读取此文件作为映射关系)。

Expand Down
8 changes: 3 additions & 5 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default {
// enable plugins

/**
* app global Error Handling
* @member {Object} Plugin#onerror
Expand Down Expand Up @@ -30,7 +28,7 @@ export default {
*/
i18n: {
enable: true,
package: 'egg-i18n',
package: '@eggjs/i18n',
},

/**
Expand Down Expand Up @@ -107,7 +105,7 @@ export default {
*/
static: {
enable: true,
package: 'egg-static',
package: '@eggjs/static',
},

/**
Expand All @@ -118,7 +116,7 @@ export default {
*/
jsonp: {
enable: true,
package: 'egg-jsonp',
package: '@eggjs/jsonp',
},

/**
Expand Down
44 changes: 3 additions & 41 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
// import plugins types
import '@eggjs/watcher';
import '@eggjs/development';
import '@eggjs/jsonp';
import '@eggjs/i18n';
import '@eggjs/static';

export type {
EggAppInfo,
Expand Down Expand Up @@ -209,32 +212,6 @@ export interface EggAppConfig extends EggCoreAppConfig {

hostHeaders: string;

/**
* I18n options
*/
i18n: {
/**
* default value EN_US
*/
defaultLocale: string;
/**
* i18n resource file dir, not recommend to change default value
*/
dirs: string[];
/**
* custom the locale value field, default `query.locale`, you can modify this config, such as `query.lang`
*/
queryField: string;
/**
* The locale value key in the cookie, default is locale.
*/
cookieField: string;
/**
* Locale cookie expire time, default `1y`, If pass number value, the unit will be ms
*/
cookieMaxAge: string | number;
};

/**
* Detect request' ip from specified headers, not case-sensitive. Only worked when config.proxy set to true.
*/
Expand All @@ -249,21 +226,6 @@ export interface EggAppConfig extends EggCoreAppConfig {
httpOnly?: boolean;
};

/**
* jsonp options
* @member Config#jsonp
* @property {String} callback - jsonp callback method key, default to `_callback`
* @property {Number} limit - callback method name's max length, default to `50`
* @property {Boolean} csrf - enable csrf check or not. default to false
* @property {String|RegExp|Array} whiteList - referrer white list
*/
jsonp: {
limit: number;
callback: string;
csrf: boolean;
whiteList: string | RegExp | Array<string | RegExp>;
};

/**
* The key that signing cookies. It can contain multiple keys separated by .
*/
Expand Down
5 changes: 4 additions & 1 deletion test/app/extend/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@ describe('test/app/extend/context.test.ts', () => {
const context = app.mockContext() as any;
context.locals = { a: 'a', b: 'b' };
context.state = { a: 'aa', c: 'cc' };
assert.deepEqual(context.state, { a: 'aa', b: 'b', c: 'cc' });
assert.equal(context.state.a, 'aa');
assert.equal(context.state.b, 'b');
assert.equal(context.state.c, 'cc');
assert.deepEqual(Object.keys(context.state), [ '__', 'gettext', 'a', 'b', 'c' ]);
assert(context.state === context.locals);
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EggPlugin,
EggAppInfo,
start, SingleModeApplication, SingleModeAgent,
MiddlewareFunc,
} from '../src/index.js';
import { HttpClient } from '../src/urllib.js';

Expand All @@ -28,6 +29,26 @@ expectType<string>(app.config.watcher.eventSources.default);
expectType<boolean>(app.config.development.fastReady);
expectType<string[]>(app.config.development.watchDirs);

// jsonp plugin types
expectType<boolean>(app.config.jsonp.csrf);
expectType<string[] | string>(app.config.jsonp.callback);
expectType<number>(app.config.jsonp.limit);
expectType<string | RegExp |(string | RegExp)[]>(app.config.jsonp.whiteList!);
expectType<boolean>(ctx.acceptJSONP);
expectType<void>(ctx.createJsonpBody({}));
expectType<MiddlewareFunc>(app.jsonp());
expectType<MiddlewareFunc>(app.jsonp({ callback: 'callback' }));

// i18n plugin types
expectType<boolean>(app.config.i18n.writeCookie);
expectType<string>(app.config.i18n.defaultLocale);
expectType<string>(app.gettext('en-us', 'email'));
expectType<boolean>(app.isSupportLocale('en-us'));
expectType<string>(ctx.__('email'));
expectType<string>(ctx.gettext('email %s', 'fengmk2'));
expectType<string>(ctx.locale);
expectType<string>(ctx.locale = 'en-us');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix assignment in expression.

The assignment in the expression can be confusing and triggers a linting error.

-expectType<string>(ctx.locale = 'en-us');
+ctx.locale = 'en-us';
+expectType<string>(ctx.locale);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
expectType<string>(ctx.locale = 'en-us');
ctx.locale = 'en-us';
expectType<string>(ctx.locale);
🧰 Tools
🪛 Biome (1.9.4)

[error] 50-50: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)


class AppBoot implements ILifecycleBoot {
private readonly app: Application;

Expand Down
Loading
Loading