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
Changes from 1 commit
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
Next Next commit
fix: use @eggjs/jsonp and @eggjs/i18n
fengmk2 committed Jan 11, 2025
commit 6325ab536f300abf8ff54092b44e0a1c8aff30b6
43 changes: 0 additions & 43 deletions index-old.d.ts
Original file line number Diff line number Diff line change
@@ -34,13 +34,11 @@
// 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' {
@@ -400,52 +398,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 .
// */
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -23,14 +23,14 @@
"@eggjs/cookies": "^3.0.0",
"@eggjs/core": "^6.2.13",
"@eggjs/development": "^4.0.0",
"@eggjs/i18n": "^3.0.0",
"@eggjs/jsonp": "^3.0.0",
"@eggjs/schedule": "^5.0.2",
"@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",
4 changes: 2 additions & 2 deletions site/docs/basics/plugin.md
Original file line number Diff line number Diff line change
@@ -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
  - [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).

4 changes: 2 additions & 2 deletions site/docs/basics/plugin.zh-CN.md
Original file line number Diff line number Diff line change
@@ -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 支持
- [jsonp](https://github.com/eggjs/jsonp) jsonp 支持
- [view](https://github.com/eggjs/egg-view) 模板引擎
- 更多社区的插件可以在 GitHub 上搜索 [egg-plugin](https://github.com/topics/egg-plugin)。

2 changes: 1 addition & 1 deletion site/docs/core/i18n.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion site/docs/core/i18n.zh-CN.md
Original file line number Diff line number Diff line change
@@ -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) 插件提供。

## 默认语言

6 changes: 2 additions & 4 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
@@ -30,7 +28,7 @@ export default {
*/
i18n: {
enable: true,
package: 'egg-i18n',
package: '@eggjs/i18n',
},

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

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

export type {
EggAppInfo,
@@ -209,32 +211,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.
*/
@@ -249,21 +225,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 .
*/
21 changes: 21 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import {
EggPlugin,
EggAppInfo,
start, SingleModeApplication, SingleModeAgent,
MiddlewareFunc,
} from '../src/index.js';
import { HttpClient } from '../src/urllib.js';

@@ -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;

1 change: 0 additions & 1 deletion test/lib/core/loader/config_loader.test.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ describe('test/lib/core/loader/config_loader.test.ts', () => {
'clusterAppMock',
'session',
'securities',
'i18n',
]);
});