@anjianshi/utils 2.4.16 → 2.5.0

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.
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-deprecated */
1
2
  /**
2
3
  * 自动计算最合适的 rem 大小,应用到页面样式上。
3
4
  *
@@ -7,7 +7,7 @@ export default async function loadScript(url) {
7
7
  const script = document.createElement('script');
8
8
  script.src = url;
9
9
  script.onload = () => resolve();
10
- script.onerror = err => reject(err);
10
+ script.onerror = err => reject(err); // eslint-disable-line @typescript-eslint/prefer-promise-reject-errors
11
11
  window.document.head.appendChild(script);
12
12
  });
13
13
  }
@@ -12,5 +12,5 @@ export declare function isVConsoleEnabled(): boolean;
12
12
  export declare function detectVConsole(): void;
13
13
  export declare function enableVConsole(): void;
14
14
  export declare function disableVConsole(): void;
15
- export declare function runVConsole(): undefined;
15
+ export declare function runVConsole(): void;
16
16
  export declare function destoryVConsole(): void;
@@ -27,7 +27,7 @@ export function runVConsole() {
27
27
  if (window.VConsole !== null)
28
28
  return;
29
29
  if (VConsoleLib === undefined)
30
- return void console.warn('尚未传入 VConsole 对象,无法启动');
30
+ return console.warn('尚未传入 VConsole 对象,无法启动');
31
31
  window.VConsole = new VConsoleLib();
32
32
  }
33
33
  export function destoryVConsole() {
@@ -147,7 +147,7 @@ export class FileHandler extends LogHandler {
147
147
  else {
148
148
  fs.appendFile(this.filepath, content, error => {
149
149
  if (error)
150
- console.error('[logger] write failed: ' + String(error));
150
+ console.error('[logger] write failed: ', error);
151
151
  });
152
152
  }
153
153
  }
@@ -14,5 +14,7 @@ import { type CSSInterpolation } from '@emotion/serialize';
14
14
  export declare const useEmotionCache: () => EmotionCache | undefined;
15
15
  export declare const EmotionCacheProvider: import("react").FC<{
16
16
  children: React.ReactNode;
17
- } & import("react").ClassAttributes<any>>;
17
+ } & import("react").RefAttributes<any>> | import("react").ForwardRefExoticComponent<{
18
+ children: React.ReactNode;
19
+ } & import("react").RefAttributes<any>>;
18
20
  export declare function useEmotionClassName(): (...args: CSSInterpolation[]) => string;
@@ -2,6 +2,5 @@
2
2
  * 开发后端服务能用到的工具库
3
3
  */
4
4
  export * from './env-reader.js';
5
- export * from './redis-cache.js';
6
5
  export * from './controllers.js';
7
6
  export * from './tasks.js';
@@ -2,6 +2,5 @@
2
2
  * 开发后端服务能用到的工具库
3
3
  */
4
4
  export * from './env-reader.js';
5
- export * from './redis-cache.js';
6
5
  export * from './controllers.js';
7
6
  export * from './tasks.js';
@@ -17,6 +17,7 @@ export function getPrismaLoggingOptions(debug) {
17
17
  ],
18
18
  };
19
19
  }
20
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
20
21
  export function adaptPrismaLogging(prisma, baseLogger) {
21
22
  // 记录 Prisma 相关日志
22
23
  const queryLogger = baseLogger.getChild('query');
@@ -14,6 +14,7 @@
14
14
  * 为保证其他扩展也应用到修改过的这些方法,此扩展应尽可能放在最前面。
15
15
  */
16
16
  import { Prisma } from '@prisma/client/extension';
17
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
17
18
  function getModel(that) {
18
19
  const context = Prisma.getExtensionContext(that);
19
20
  // 1. 此扩展修改了 Prisma 原生的方法,所以要通过 context.$parent[context.$name] 获取上一层的 model,不然会自己调用自己导致死循环。
@@ -29,6 +30,7 @@ function getModel(that) {
29
30
  }
30
31
  function query(that, inputArgs, method) {
31
32
  const { model, supportSoftDelete } = getModel(that);
33
+ // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
32
34
  const { withDeleted = false, ...args } = inputArgs;
33
35
  return model[method]({
34
36
  ...args,
@@ -1,9 +1,9 @@
1
1
  import { type ITXClientDenyList } from '@prisma/client/runtime/library.js';
2
- import type { MaySuccess } from '../../../index.js';
2
+ import type { MaySuccess, Failed } from '../../../index.js';
3
3
  export declare const withTransaction: (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {
4
4
  $withTransaction: typeof $withTransaction;
5
5
  }>>;
6
6
  export type GetPrismaClientInTransaction<PrismaClient> = Omit<PrismaClient, ITXClientDenyList>;
7
7
  export type WithTransactionMethod = typeof $withTransaction;
8
- declare function $withTransaction<That extends object, R extends MaySuccess<unknown, unknown>>(this: That, callback: (dbInTransaction: GetPrismaClientInTransaction<That>) => Promise<R>): Promise<R>;
8
+ declare function $withTransaction<That extends object, R extends MaySuccess<unknown, unknown>>(this: That, callback: (dbInTransaction: GetPrismaClientInTransaction<That>) => Promise<R>): Promise<Failed<any> | R>;
9
9
  export {};
@@ -12,7 +12,7 @@
12
12
  * }
13
13
  * )
14
14
  */
15
- import { Prisma } from '@prisma/client/extension';
15
+ import { Prisma } from '@prisma/client/extension.js';
16
16
  export const withTransaction = Prisma.defineExtension({
17
17
  name: 'withTransaction',
18
18
  client: {
@@ -26,6 +26,8 @@ class FailedInTransaction extends Error {
26
26
  this.failed = failed;
27
27
  }
28
28
  }
29
+ // 注意:此函数的返回值为 `R | Failed<any>`,例如实际可能为 `MaySuccess<xxx, xxx> | Failed<any>`,这是有意为之的,`Failed<any>` 并不多余。
30
+ // 因为有时 callback() 只会返回 success 结果,此时 R=Success<xxx>,但是 $withTransaction 整体的返回值仍有可能有 Failed<any>,所以不能用 R 作为整体返回值。
29
31
  async function $withTransaction(callback) {
30
32
  const executeCallback = async (dbInTransaction) => {
31
33
  const result = await callback(dbInTransaction);
@@ -33,7 +33,7 @@ export declare class Cache<T> {
33
33
  /** 移除一项内容 */
34
34
  delete(identity?: string | string[]): Promise<number>;
35
35
  /** 刷新一项内容的过期时间 */
36
- refresh(identity?: string): Promise<boolean>;
36
+ refresh(identity?: string): Promise<number | false>;
37
37
  /** 确认一项内容是否存在 */
38
38
  exists(identity?: string): Promise<boolean>;
39
39
  }
@@ -1,7 +1,6 @@
1
1
  import { logger as rootLogger } from '../logging/index.js';
2
2
  export function initRedisLogging(redis, logger) {
3
- if (!logger)
4
- logger = rootLogger.getChild('redis');
3
+ logger ??= rootLogger.getChild('redis');
5
4
  redis.on('connect', () => logger.info('connecting'));
6
5
  redis.on('ready', () => logger.info('connected'));
7
6
  redis.on('end', () => logger.info('connection closed'));
package/lang/async.js CHANGED
@@ -23,7 +23,7 @@ export async function timeout(promise, timeoutMS, silent = false) {
23
23
  promise.then(result => {
24
24
  if (!isTimeout)
25
25
  resolve(result);
26
- }, error => {
26
+ }, (error) => {
27
27
  if (!isTimeout)
28
28
  reject(error);
29
29
  });
package/lang/string.js CHANGED
@@ -81,6 +81,7 @@ export function readableSize(bytes, si = false, dp = 1) {
81
81
  /**
82
82
  * 解析 JSON,失败时不抛出异常,而是返回 undefined
83
83
  */
84
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
84
85
  export function safeParseJSON(json) {
85
86
  try {
86
87
  return JSON.parse(json);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.4.16",
3
+ "version": "2.5.0",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -20,28 +20,28 @@
20
20
  "lodash": "^4.17.21"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/lodash": "^4.17.10",
24
- "@types/node": "^20.16.11",
25
- "@types/react": "^18.3.11",
26
- "dotenv": "^16.4.5",
27
- "redis": "^4.7.0",
28
- "typescript": "^5.6.3",
23
+ "@types/lodash": "^4.17.17",
24
+ "@types/node": "^22.15.21",
25
+ "@types/react": "^19.1.5",
26
+ "dotenv": "^16.5.0",
27
+ "redis": "^5.1.0",
28
+ "typescript": "^5.8.3",
29
29
  "vconsole": "^3.15.1",
30
- "@anjianshi/presets-eslint-node": "4.0.15",
31
- "@anjianshi/presets-eslint-react": "4.0.14",
32
- "@anjianshi/presets-eslint-typescript": "5.0.12",
33
- "@anjianshi/presets-prettier": "3.0.1",
34
- "@anjianshi/presets-typescript": "3.2.3"
30
+ "@anjianshi/presets-eslint-node": "4.2.3",
31
+ "@anjianshi/presets-eslint-react": "4.2.3",
32
+ "@anjianshi/presets-prettier": "3.0.3",
33
+ "@anjianshi/presets-typescript": "3.2.4",
34
+ "@anjianshi/presets-eslint-typescript": "5.2.3"
35
35
  },
36
36
  "peerDependencies": {
37
- "@emotion/react": "^11.13.3",
38
- "@emotion/serialize": "^1.3.1",
39
- "@emotion/utils": "^1.4.0",
40
- "@prisma/client": "^5.20.0",
41
- "chalk": "^5.3.0",
42
- "dayjs": "^1.11.10",
43
- "dotenv": "^16.4.5",
44
- "react": "^18.3.1"
37
+ "@emotion/react": "^11.14.0",
38
+ "@emotion/serialize": "^1.3.3",
39
+ "@emotion/utils": "^1.4.2",
40
+ "@prisma/client": "^6.8.2",
41
+ "chalk": "^5.4.1",
42
+ "dayjs": "^1.11.13",
43
+ "dotenv": "^16.5.0",
44
+ "react": "^19.1.0"
45
45
  },
46
46
  "peerDependenciesMeta": {
47
47
  "@emotion/react": {