@cmtlyt/lingshu-toolkit 0.4.0 → 0.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,5 +1,10 @@
1
1
  type Logger = {
2
2
  [K in keyof Omit<Console, 'table'> as Console[K] extends (...args: any[]) => any ? K : never]: Console[K] extends (...args: [any, ...infer AS]) => infer R ? (fnName: string, ...args: AS) => R : never;
3
3
  };
4
+ declare global {
5
+ var $$lingshu$$: {
6
+ disableLogger: boolean;
7
+ };
8
+ }
4
9
  export declare const logger: Logger;
5
10
  export {};
@@ -1,5 +1,6 @@
1
1
  const logger = new Proxy(console, {
2
2
  get (target, prop, receiver) {
3
+ if ((globalThis.$$lingshu$$ || {}).disableLogger) return ()=>void 0;
3
4
  const oldLog = Reflect.get(target, prop, receiver).bind(console);
4
5
  return (fnName, ...args)=>{
5
6
  oldLog(`[@cmtlyt/lingshu-toolkit#${fnName}]:`, ...args);
@@ -0,0 +1,22 @@
1
+ type TryCallResultValue<R, E> = Awaited<[E] extends [never] ? R : R | E>;
2
+ type TryCallResult<R, E> = [R] extends [never] ? E : R extends PromiseLike<infer P> ? Promise<TryCallResultValue<P, E>> : TryCallResultValue<R, E>;
3
+ type TryCallFinalArgs<R, E> = TryCallResultValue<R, E> | Error;
4
+ /**
5
+ * 包装一个拦截错误的函数
6
+ *
7
+ * @platform web, node, webworker
8
+ *
9
+ * @param cb 回调函数
10
+ * @param onError 错误处理函数
11
+ */
12
+ export declare function tryCallFunc<A extends any[], R, E = never>(cb: (...args: A) => R, onError?: ((err: any) => E) | null, onFinal?: (result: TryCallFinalArgs<R, E>) => void): (...args: A) => TryCallResult<R, E>;
13
+ /**
14
+ * 尝试调用函数
15
+ *
16
+ * @platform web, node, webworker
17
+ *
18
+ * @param cb 回调函数
19
+ * @param onError 错误处理函数
20
+ */
21
+ export declare function tryCall<R, E = never>(this: any, cb: () => R, onError?: ((err: any) => E) | null, onFinal?: (result: TryCallFinalArgs<R, E>) => void): TryCallResult<R, E>;
22
+ export {};
@@ -0,0 +1,59 @@
1
+ import { throwType } from "../throw-error/index.js";
2
+ import { isFunction, isPromiseLike } from "../utils/verify.js";
3
+ const EMPTY = Symbol('EMPTY');
4
+ function tryCallFunc(cb, onError, onFinal) {
5
+ if (!isFunction(cb)) throwType('tryCallFunc', 'callback is not a function');
6
+ const catchFn = (self, ctx, error)=>{
7
+ if (isFunction(onError)) try {
8
+ ctx.errorResult = Reflect.apply(onError, self, [
9
+ error
10
+ ]);
11
+ } catch (err) {
12
+ ctx.error = err;
13
+ }
14
+ else ctx.error = error;
15
+ return ctx.errorResult;
16
+ };
17
+ const finallyFn = (self, ctx, result)=>{
18
+ try {
19
+ if (ctx.error !== EMPTY) throw ctx.error;
20
+ } finally{
21
+ if (isFunction(onFinal)) if (ctx.errorResult !== EMPTY) Reflect.apply(onFinal, self, [
22
+ ctx.errorResult
23
+ ]);
24
+ else if (ctx.error !== EMPTY) Reflect.apply(onFinal, self, [
25
+ ctx.error
26
+ ]);
27
+ else Reflect.apply(onFinal, self, [
28
+ result
29
+ ]);
30
+ }
31
+ };
32
+ return function(...args) {
33
+ const ctx = {
34
+ oriResult: EMPTY,
35
+ errorResult: EMPTY,
36
+ error: EMPTY
37
+ };
38
+ const asyncFn = async ()=>{
39
+ try {
40
+ ctx.oriResult = Reflect.apply(cb, this, args);
41
+ return ctx.oriResult;
42
+ } catch (error) {
43
+ return catchFn(this, ctx, error);
44
+ }
45
+ };
46
+ const fnPromise = asyncFn().catch((error)=>catchFn(this, ctx, error));
47
+ if (isPromiseLike(ctx.oriResult)) return fnPromise.then((result)=>{
48
+ finallyFn(this, ctx, result);
49
+ return result;
50
+ });
51
+ finallyFn(this, ctx, ctx.oriResult);
52
+ return ctx.oriResult !== EMPTY ? ctx.oriResult : ctx.errorResult;
53
+ };
54
+ }
55
+ function tryCall(cb, onError, onFinal) {
56
+ if (!isFunction(cb)) throwType('tryCall', 'callback is not a function');
57
+ return tryCallFunc(cb, onError, onFinal).call(this);
58
+ }
59
+ export { tryCall, tryCallFunc };
@@ -0,0 +1 @@
1
+ export {};
@@ -7,5 +7,7 @@ export type Printify<T> = T extends any[] ? T : [T] extends [never] ? T : {
7
7
  [K in keyof T]: T[K];
8
8
  };
9
9
  export type PickRequired<T, K extends keyof T> = Printify<Omit<T, K> & Required<Pick<T, K>>>;
10
+ export type Func<A extends any[], R = any> = (...args: A) => R;
10
11
  export type AnyFunc = (...args: any[]) => any;
11
12
  export type UnULCase<T extends string> = T extends `${infer F}${infer R}` ? `${Uppercase<F> | Lowercase<F>}${UnULCase<R>}` : Uppercase<T> | Lowercase<T>;
13
+ export type Cast<X, Y> = X extends Y ? X : Y;
@@ -3,7 +3,7 @@ export type Pack<T> = {
3
3
  [__PACK__]: T;
4
4
  };
5
5
  export type Unpack<T extends Pack<any>> = T extends Pack<infer U> ? U : never;
6
- export type SafeUnpack<T extends Pack<any>> = T extends Pack<infer U> ? U : T;
6
+ export type SafeUnpack<T> = T extends Pack<infer U> ? U : T;
7
7
  export type IsPack<T> = Pack<any> extends T ? true : false;
8
8
  export type HasPack<T> = (T extends any[] ? HasPack<T[number]> : IsPack<T>) extends false ? false : true;
9
9
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmtlyt/lingshu-toolkit",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "main": "./dist/shared.js",
6
6
  "module": "./dist/shared.js",
@@ -53,11 +53,13 @@
53
53
  "@vitest/browser-playwright": "4.0.18",
54
54
  "@vitest/coverage-v8": "4.0.18",
55
55
  "@vitest/ui": "4.0.18",
56
+ "changelogithub": "^14.0.0",
56
57
  "cross-env": "^10.1.0",
57
58
  "esno": "^4.8.0",
58
59
  "husky": "^9.1.7",
59
60
  "jsdom": "^27.4.0",
60
61
  "lint-staged": "^16.2.7",
62
+ "msw": "^2.12.14",
61
63
  "playwright": "^1.58.1",
62
64
  "react": "^19.2.4",
63
65
  "react-dom": "^19.2.4",
@@ -78,7 +80,8 @@
78
80
  "version": ">=22.12.0"
79
81
  },
80
82
  "packageManager": {
81
- "name": "pnpm"
83
+ "name": "pnpm",
84
+ "version": ">=10.2.0"
82
85
  }
83
86
  },
84
87
  "publishConfig": {