@lark-apaas/client-toolkit 1.2.40 → 1.2.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -102,6 +102,30 @@ const messages_messages = {
102
102
  'userProfile.label.email': {
103
103
  zh: '邮箱',
104
104
  en: 'Email'
105
+ },
106
+ 'axios.log.requestSuccess': {
107
+ zh: '请求成功:',
108
+ en: 'Request succeeded: '
109
+ },
110
+ 'axios.log.requestFailed': {
111
+ zh: '请求失败:',
112
+ en: 'Request failed: '
113
+ },
114
+ 'axios.log.networkFailed': {
115
+ zh: '网络请求失败:',
116
+ en: 'Network request failed: '
117
+ },
118
+ 'axios.log.noResponseOrConfig': {
119
+ zh: '请求失败:无响应对象或配置信息',
120
+ en: 'Request failed: no response object or config'
121
+ },
122
+ 'axios.log.forbidden': {
123
+ zh: '请求被拒绝(403):{method} {url}',
124
+ en: 'Request rejected (403): {method} {url}'
125
+ },
126
+ 'axios.log.unknownError': {
127
+ zh: '未知错误',
128
+ en: 'Unknown error'
105
129
  }
106
130
  };
107
131
  const messages = messages_messages;
@@ -5,6 +5,7 @@ import { getStacktrace } from "../logger/selected-logs.js";
5
5
  import { safeStringify } from "./safeStringify.js";
6
6
  import { slardar } from "@lark-apaas/internal-slardar";
7
7
  import { normalizeBasePath } from "./utils.js";
8
+ import { t } from "../locales/index.js";
8
9
  const APP_CLIENT_API_LOG_TYPE = 'app_client_api_log';
9
10
  function stripBasePath(urlPath) {
10
11
  const base = normalizeBasePath(process.env.CLIENT_BASE_PATH);
@@ -95,8 +96,8 @@ async function logResponse(ok, responseOrError) {
95
96
  if (isValidResponse(responseOrError)) {
96
97
  const response = responseOrError;
97
98
  const okToTextMap = {
98
- success: '请求成功:',
99
- error: '请求失败:'
99
+ success: t('axios.log.requestSuccess'),
100
+ error: t('axios.log.requestFailed')
100
101
  };
101
102
  const realOK = 'success' === ok && response.status >= 200 && response.status < 300;
102
103
  const method = (response.config.method || 'GET').toUpperCase();
@@ -154,13 +155,13 @@ async function logResponse(ok, responseOrError) {
154
155
  const error = responseOrError;
155
156
  if (error && error.config && 'object' == typeof error.config) {
156
157
  const errorType = error.code || 'UNKNOWN_ERROR';
157
- const errorMessage = error.message || '未知错误';
158
+ const errorMessage = error.message || t('axios.log.unknownError');
158
159
  const method = (error.config.method || 'GET').toUpperCase();
159
160
  const url = error.config.url || '';
160
161
  const startTime = error.config._startTime;
161
162
  const duration = startTime ? Date.now() - startTime : 0;
162
163
  const parts = [
163
- '网络请求失败:',
164
+ t('axios.log.networkFailed'),
164
165
  method,
165
166
  ' ',
166
167
  url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, ''),
@@ -210,7 +211,7 @@ async function logResponse(ok, responseOrError) {
210
211
  logger.log({
211
212
  level: 'error',
212
213
  args: [
213
- '请求失败:无响应对象或配置信息'
214
+ t('axios.log.noResponseOrConfig')
214
215
  ],
215
216
  meta: {}
216
217
  });
@@ -360,7 +361,10 @@ function initAxiosConfig(axiosInstance) {
360
361
  logger.log({
361
362
  level: 'warn',
362
363
  args: [
363
- `请求被拒绝(403):${method} ${url}`,
364
+ t('axios.log.forbidden', {
365
+ method,
366
+ url
367
+ }),
364
368
  {
365
369
  状态码: 403,
366
370
  返回数据: error.response.data
@@ -1,9 +1,17 @@
1
1
  /**
2
2
  * 统一的语言判断方法。
3
3
  *
4
- * 基于浏览器 Accept-Language(navigator.language)判断。
5
- * 返回标准化的 locale code:'zh' | 'en'。
6
- * 目前只区分中英文,未来需要更多语言时在此扩展。
4
+ * 生产环境(`process.env.NODE_ENV === 'production'`):
5
+ * 只看浏览器容器语言(navigator.language)。
6
+ *
7
+ * 非生产环境(沙箱预览场景):
8
+ * 优先级 query > localStorage > navigator。
9
+ * URL query `locale`(值为 `zh_CN` / `en_US`)由宿主平台(miaoda)
10
+ * 通过 iframe src 透传,确保产物内的语言与宿主一致;
11
+ * 读到 query 时会同步写入 localStorage,便于后续无 query 的跳转/刷新场景使用。
12
+ *
13
+ * 返回标准化 locale code:'zh' | 'en'。
14
+ * 目前只区分中英文,未来扩展时在此调整。
7
15
  */
8
16
  export type Locale = 'zh' | 'en';
9
17
  export declare function getLocale(): Locale;
@@ -1,5 +1,41 @@
1
+ const STORAGE_KEY = 'miaoda:preview-locale';
2
+ function toLocale(value) {
3
+ const lang = value ?? 'zh';
4
+ return lang.toLowerCase().startsWith('zh') ? 'zh' : 'en';
5
+ }
6
+ function readLocaleFromQuery() {
7
+ if ('undefined' == typeof window) return null;
8
+ try {
9
+ const value = new URLSearchParams(window.location.search).get('locale');
10
+ return value && value.length > 0 ? value : null;
11
+ } catch {
12
+ return null;
13
+ }
14
+ }
15
+ function readLocaleFromStorage() {
16
+ if ('undefined' == typeof window) return null;
17
+ try {
18
+ const value = window.localStorage?.getItem(STORAGE_KEY);
19
+ return value && value.length > 0 ? value : null;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+ function writeLocaleToStorage(value) {
25
+ if ('undefined' == typeof window) return;
26
+ try {
27
+ window.localStorage?.setItem(STORAGE_KEY, value);
28
+ } catch {}
29
+ }
1
30
  function getLocale() {
2
- const lang = navigator.language || 'zh';
3
- return lang.startsWith('zh') ? 'zh' : 'en';
31
+ if ('production' === process.env.NODE_ENV) return toLocale(navigator.language);
32
+ const fromQuery = readLocaleFromQuery();
33
+ if (fromQuery) {
34
+ writeLocaleToStorage(fromQuery);
35
+ return toLocale(fromQuery);
36
+ }
37
+ const fromStorage = readLocaleFromStorage();
38
+ if (fromStorage) return toLocale(fromStorage);
39
+ return toLocale(navigator.language);
4
40
  }
5
41
  export { getLocale };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.40",
3
+ "version": "1.2.41",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [