@cmtlyt/lingshu-toolkit 0.5.0 → 0.6.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.
- package/README.md +10 -0
- package/dist/665.js +1 -0
- package/dist/893.js +1 -0
- package/dist/react/index.js +1 -205
- package/dist/react/use-boolean/index.d.ts +2 -1
- package/dist/react/use-boolean/index.js +1 -16
- package/dist/react/use-controllable-value/index.d.ts +3 -3
- package/dist/react/use-controllable-value/index.js +1 -32
- package/dist/react/use-counter/index.d.ts +2 -2
- package/dist/react/use-counter/index.js +1 -49
- package/dist/react/use-force-update/index.d.ts +2 -1
- package/dist/react/use-force-update/index.js +1 -6
- package/dist/react/use-mount/index.d.ts +2 -1
- package/dist/react/use-mount/index.js +1 -16
- package/dist/react/use-ref-state/index.d.ts +3 -2
- package/dist/react/use-ref-state/index.js +1 -33
- package/dist/react/use-storage/index.d.ts +2 -1
- package/dist/react/use-storage/index.js +1 -15
- package/dist/react/use-title/index.d.ts +2 -2
- package/dist/react/use-title/index.js +1 -24
- package/dist/react/use-toggle/index.d.ts +4 -4
- package/dist/react/use-toggle/index.js +1 -26
- package/dist/react/use-valid-data/index.d.ts +5 -4
- package/dist/react/use-valid-data/index.js +1 -14
- package/dist/shared/allx/index.d.ts +2 -1
- package/dist/shared/allx/index.js +1 -44
- package/dist/shared/allx/types.d.ts +6 -0
- package/dist/shared/allx/utils.d.ts +9 -7
- package/dist/shared/allx/utils.js +1 -94
- package/dist/shared/animation/index.d.ts +3 -2
- package/dist/shared/animation/index.js +1 -77
- package/dist/shared/animation/types.d.ts +8 -0
- package/dist/shared/animation/utils.d.ts +3 -10
- package/dist/shared/animation/utils.js +1 -134
- package/dist/shared/api-controller/create-api.js +1 -79
- package/dist/shared/api-controller/index.js +1 -3
- package/dist/shared/api-controller/request.js +1 -66
- package/dist/shared/api-controller/types.d.ts +26 -27
- package/dist/shared/api-controller/utils.d.ts +6 -15
- package/dist/shared/api-controller/utils.js +1 -96
- package/dist/shared/condition-merge/index.d.ts +6 -6
- package/dist/shared/condition-merge/index.js +1 -30
- package/dist/shared/create-storage-handler/index.d.ts +4 -3
- package/dist/shared/create-storage-handler/index.js +1 -68
- package/dist/shared/data-handler/index.d.ts +4 -3
- package/dist/shared/data-handler/index.js +1 -77
- package/dist/shared/data-handler/tools.d.ts +6 -23
- package/dist/shared/data-handler/tools.js +1 -48
- package/dist/shared/data-handler/types.d.ts +20 -2
- package/dist/shared/data-mixed-manager/constants.js +1 -9
- package/dist/shared/data-mixed-manager/index.js +1 -226
- package/dist/shared/data-mixed-manager/types.d.ts +1 -2
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.js +1 -957
- package/dist/shared/logger/index.d.ts +2 -2
- package/dist/shared/logger/index.js +1 -10
- package/dist/shared/priority-queue/index.d.ts +45 -0
- package/dist/shared/priority-queue/index.js +1 -0
- package/dist/shared/priority-queue/types.d.ts +10 -0
- package/dist/shared/priority-queue/types.js +0 -0
- package/dist/shared/priority-queue/utils.d.ts +7 -0
- package/dist/shared/priority-queue/utils.js +1 -0
- package/dist/shared/throw-error/index.d.ts +4 -3
- package/dist/shared/throw-error/index.js +1 -10
- package/dist/shared/try-call/index.d.ts +3 -3
- package/dist/shared/try-call/index.js +1 -59
- package/dist/shared/types/index.js +1 -2
- package/dist/shared/types/pack.d.ts +2 -2
- package/dist/shared/types/pack.js +1 -1
- package/dist/shared/utils/base.d.ts +1 -1
- package/dist/shared/utils/base.js +1 -6
- package/dist/shared/utils/index.js +1 -2
- package/dist/shared/utils/verify.d.ts +1 -1
- package/dist/shared/utils/verify.js +1 -67
- package/dist/shared/with-resolvers/index.d.ts +5 -3
- package/dist/shared/with-resolvers/index.js +1 -15
- package/dist/vue/index.js +1 -29
- package/dist/vue/use-title/index.d.ts +2 -2
- package/dist/vue/use-title/index.js +1 -29
- package/package.json +26 -26
- package/dist/247.js +0 -66
- package/dist/707.js +0 -142
- package/dist/react/use-boolean/index.test.d.ts +0 -1
- package/dist/react/use-controllable-value/index.test.d.ts +0 -1
- package/dist/react/use-counter/index.test.d.ts +0 -1
- package/dist/react/use-force-update/index.test.d.ts +0 -1
- package/dist/react/use-mount/index.test.d.ts +0 -1
- package/dist/react/use-ref-state/index.test.d.ts +0 -1
- package/dist/react/use-storage/index.test.d.ts +0 -1
- package/dist/react/use-title/index.test.d.ts +0 -1
- package/dist/react/use-toggle/index.test.d.ts +0 -1
- package/dist/react/use-valid-data/index.test.d.ts +0 -1
- package/dist/shared/allx/__test__/allsettled.test.d.ts +0 -1
- package/dist/shared/allx/__test__/basic.test.d.ts +0 -1
- package/dist/shared/allx/__test__/circular-dependency.test.d.ts +0 -1
- package/dist/shared/allx/__test__/dependency.test.d.ts +0 -1
- package/dist/shared/allx/__test__/edge-cases.test.d.ts +0 -1
- package/dist/shared/allx/__test__/error-handling.test.d.ts +0 -1
- package/dist/shared/allx/__test__/execution-order.test.d.ts +0 -1
- package/dist/shared/allx/__test__/falsy-values.test.d.ts +0 -1
- package/dist/shared/allx/__test__/performance.test.d.ts +0 -1
- package/dist/shared/allx/__test__/type-checking.test.d.ts +0 -1
- package/dist/shared/allx/__test__/use-cases.test.d.ts +0 -1
- package/dist/shared/animation/__test__/animation-pause-resume.test.d.ts +0 -1
- package/dist/shared/animation/__test__/animation.test.d.ts +0 -1
- package/dist/shared/animation/__test__/step-animation.test.d.ts +0 -1
- package/dist/shared/animation/__test__/utils.test.d.ts +0 -1
- package/dist/shared/api-controller/__test__/index.browser.test.d.ts +0 -1
- package/dist/shared/api-controller/__test__/index.node.test.d.ts +0 -1
- package/dist/shared/condition-merge/index.test-d.js +0 -108
- package/dist/shared/condition-merge/index.test.d.ts +0 -1
- package/dist/shared/create-storage-handler/index.browser.test.d.ts +0 -1
- package/dist/shared/create-storage-handler/index.test.d.ts +0 -1
- package/dist/shared/data-handler/index.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/basic.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/build-options.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/constructor-options.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/data-management.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/edge-cases.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/events.browser.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/events.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/fixed-slots.test.d.ts +0 -1
- package/dist/shared/data-mixed-manager/__test__/insert-mode.test.d.ts +0 -1
- package/dist/shared/throw-error/index.test.d.ts +0 -1
- package/dist/shared/try-call/index.test.d.ts +0 -1
- package/dist/shared/utils/__test__/base.test.d.ts +0 -1
- package/dist/shared/utils/__test__/verify.test.d.ts +0 -1
- package/dist/shared/with-resolvers/index.test.d.ts +0 -1
- package/dist/test/utils.d.ts +0 -13
- package/dist/vue/use-title/index.test.d.ts +0 -1
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { AnyFunc, Cast, Equal, Func } from '../types/base';
|
|
2
2
|
import type { Pack as TPack } from '../types/pack';
|
|
3
|
-
|
|
3
|
+
export interface Empty {
|
|
4
4
|
__EMPTY__: never;
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
6
|
/** 请求方法 */
|
|
7
7
|
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | (string & {});
|
|
8
|
-
type Parser = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData' | 'bytes' | 'stream' | (string & {});
|
|
9
|
-
type RequestMode<E extends string = string & {}> = 'mock' | 'network' | E;
|
|
10
|
-
type EmptyUnwrap<UserR = Empty, T = any> = Equal<UserR, Empty> extends true ? T : UserR;
|
|
11
|
-
interface ParserResultMap<UserR = Empty> {
|
|
8
|
+
export type Parser = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData' | 'bytes' | 'stream' | (string & {});
|
|
9
|
+
export type RequestMode<E extends string = string & {}> = 'mock' | 'network' | E;
|
|
10
|
+
export type EmptyUnwrap<UserR = Empty, T = any> = Equal<UserR, Empty> extends true ? T : UserR;
|
|
11
|
+
export interface ParserResultMap<UserR = Empty> {
|
|
12
12
|
json: EmptyUnwrap<UserR>;
|
|
13
13
|
text: string;
|
|
14
14
|
blob: Blob;
|
|
@@ -17,10 +17,10 @@ interface ParserResultMap<UserR = Empty> {
|
|
|
17
17
|
bytes: Uint8Array;
|
|
18
18
|
stream: ReadableStream | null;
|
|
19
19
|
}
|
|
20
|
-
type ParserResult<P extends Parser, UserR = Empty> = P extends keyof ParserResultMap ? ParserResultMap<UserR>[P] : EmptyUnwrap<UserR>;
|
|
21
|
-
type ParserReturn<RM extends RequestMode, P extends Parser, ReqOutput, UserR = Empty> = RM extends 'network' ? ParserResult<P, UserR> : EmptyUnwrap<UserR, RM extends 'mock' ? ReqOutput : any>;
|
|
22
|
-
type URLParamParser<U extends string, Param extends string = never> = U extends `${string}/:${infer P}/${infer Rest}` ? URLParamParser<`/${Rest}`, Param | P> : U extends `${string}/:${infer P}` ? P | Param : Param;
|
|
23
|
-
type CheckNonParamUrlAPIConfig<A extends APIConfig> = Equal<URLParamParser<A['url']>, never>;
|
|
20
|
+
export type ParserResult<P extends Parser, UserR = Empty> = P extends keyof ParserResultMap ? ParserResultMap<UserR>[P] : EmptyUnwrap<UserR>;
|
|
21
|
+
export type ParserReturn<RM extends RequestMode, P extends Parser, ReqOutput, UserR = Empty> = RM extends 'network' ? ParserResult<P, UserR> : EmptyUnwrap<UserR, RM extends 'mock' ? ReqOutput : any>;
|
|
22
|
+
export type URLParamParser<U extends string, Param extends string = never> = U extends `${string}/:${infer P}/${infer Rest}` ? URLParamParser<`/${Rest}`, Param | P> : U extends `${string}/:${infer P}` ? P | Param : Param;
|
|
23
|
+
export type CheckNonParamUrlAPIConfig<A extends APIConfig> = Equal<URLParamParser<A['url']>, never>;
|
|
24
24
|
export interface BaseAPIConfig<Input = any, Output = any, ReqOutput = any, ResOutput = any, DefaultConfig extends DefaultAPIConfig = DefaultAPIConfig, ReqModeMapKeys extends string = string & {}, Url extends string = string> extends RequestInit {
|
|
25
25
|
/**
|
|
26
26
|
* 请求地址
|
|
@@ -92,36 +92,36 @@ export interface APIMap<U extends string = string & {}> {
|
|
|
92
92
|
}
|
|
93
93
|
export type IsUnknownAny<T> = Equal<T, any> extends true ? true : Equal<T, unknown> extends true ? true : false;
|
|
94
94
|
export type FindNonAny<T extends any[], Other = Empty> = T extends [infer F, ...infer Last] ? IsUnknownAny<F> extends true ? FindNonAny<Last, Other> : F extends Other ? FindNonAny<Last, Other> : F : any;
|
|
95
|
-
type APIHandlerArgs<I, C extends CallAPIConfig, Custom, NonParamUrl extends boolean> = FindNonAny<[
|
|
95
|
+
export type APIHandlerArgs<I, C extends CallAPIConfig, Custom, NonParamUrl extends boolean> = FindNonAny<[
|
|
96
96
|
NonParamUrl extends true ? any : [I, C & Required<Pick<C, 'params'>>],
|
|
97
97
|
Custom extends true ? [I?, C?] : [I?],
|
|
98
98
|
[I?]
|
|
99
99
|
]>;
|
|
100
|
-
type DefineRequestModes<D extends DefaultAPIConfig> = keyof NonNullable<D['requestModeMap']> & string;
|
|
101
|
-
type RealProp<P extends keyof DefaultAPIConfig & keyof APIConfig, C extends Pick<APIConfig, P>, A extends Pick<APIConfig, P>, D extends DefaultAPIConfig, Other = Empty> = FindNonAny<[C[P], A[P], D[P]], Other>;
|
|
102
|
-
type OnResponseReturn<OnResponse> = OnResponse extends AnyFunc ? ReturnType<OnResponse> : any;
|
|
103
|
-
type CustomCallConfigUnwrap<C extends CallAPIConfig, Custom extends boolean> = Custom extends true ? {
|
|
100
|
+
export type DefineRequestModes<D extends DefaultAPIConfig> = keyof NonNullable<D['requestModeMap']> & string;
|
|
101
|
+
export type RealProp<P extends keyof DefaultAPIConfig & keyof APIConfig, C extends Pick<APIConfig, P>, A extends Pick<APIConfig, P>, D extends DefaultAPIConfig, Other = Empty> = FindNonAny<[C[P], A[P], D[P]], Other>;
|
|
102
|
+
export type OnResponseReturn<OnResponse> = OnResponse extends AnyFunc ? ReturnType<OnResponse> : any;
|
|
103
|
+
export type CustomCallConfigUnwrap<C extends CallAPIConfig, Custom extends boolean> = Custom extends true ? {
|
|
104
104
|
[K in keyof C as undefined extends C[K] ? never : K]: C[K];
|
|
105
105
|
} : Record<never, Empty>;
|
|
106
|
-
interface Pack<T> extends TPack<T> {
|
|
106
|
+
export interface Pack<T> extends TPack<T> {
|
|
107
107
|
__value: T;
|
|
108
108
|
}
|
|
109
|
-
type PackUnwrap<P> = P extends Pack<any> ? P['__value'] : P;
|
|
110
|
-
interface OriginalPack<T> extends TPack<T> {
|
|
109
|
+
export type PackUnwrap<P> = P extends Pack<any> ? P['__value'] : P;
|
|
110
|
+
export interface OriginalPack<T> extends TPack<T> {
|
|
111
111
|
__originValue: T;
|
|
112
112
|
}
|
|
113
|
-
type OriginalPackUnwrap<P> = P extends OriginalPack<any> ? P['__originValue'] : Promise<P>;
|
|
114
|
-
type CustomRequestModeReturn<RealRM extends RequestMode, InputD extends DefaultAPIConfig, CustomReq = NonNullable<InputD['requestModeMap']>[RealRM]> = IsUnknownAny<RealRM> extends true ? any : Equal<RealRM, string> extends true ? any : CustomReq extends AnyFunc ? OriginalPack<ReturnType<CustomReq>> : any;
|
|
115
|
-
type UserInputResult<UserR, CustomRequestResult> = IsUnknownAny<CustomRequestResult> extends true ? UserR : IsUnknownAny<UserR> extends true ? any : UserR extends CustomRequestResult ? OriginalPack<UserR> : UserR;
|
|
116
|
-
type APIHandlerResult<AConfig extends APIConfig, CallConfig extends CallAPIConfig = APIConfig, UserR = Empty, InputDefault extends DefaultAPIConfig = DefaultAPIConfig, ReqOutput = any, Custom extends boolean = false, CC extends CallAPIConfig = CustomCallConfigUnwrap<CallConfig, Custom>, CustomRequestResult = CustomRequestModeReturn<NonNullable<RealProp<'requestMode', CC, AConfig, InputDefault>>, InputDefault>> = OriginalPackUnwrap<Awaited<PackUnwrap<FindNonAny<[
|
|
113
|
+
export type OriginalPackUnwrap<P> = P extends OriginalPack<any> ? P['__originValue'] : Promise<P>;
|
|
114
|
+
export type CustomRequestModeReturn<RealRM extends RequestMode, InputD extends DefaultAPIConfig, CustomReq = NonNullable<InputD['requestModeMap']>[RealRM]> = IsUnknownAny<RealRM> extends true ? any : Equal<RealRM, string> extends true ? any : CustomReq extends AnyFunc ? OriginalPack<ReturnType<CustomReq>> : any;
|
|
115
|
+
export type UserInputResult<UserR, CustomRequestResult> = IsUnknownAny<CustomRequestResult> extends true ? UserR : IsUnknownAny<UserR> extends true ? any : UserR extends CustomRequestResult ? OriginalPack<UserR> : UserR;
|
|
116
|
+
export type APIHandlerResult<AConfig extends APIConfig, CallConfig extends CallAPIConfig = APIConfig, UserR = Empty, InputDefault extends DefaultAPIConfig = DefaultAPIConfig, ReqOutput = any, Custom extends boolean = false, CC extends CallAPIConfig = CustomCallConfigUnwrap<CallConfig, Custom>, CustomRequestResult = CustomRequestModeReturn<NonNullable<RealProp<'requestMode', CC, AConfig, InputDefault>>, InputDefault>> = OriginalPackUnwrap<Awaited<PackUnwrap<FindNonAny<[
|
|
117
117
|
UserInputResult<EmptyUnwrap<UserR>, OriginalPackUnwrap<CustomRequestResult>>,
|
|
118
118
|
CustomRequestResult,
|
|
119
119
|
ReturnType<Cast<RealProp<'tvo', CC, AConfig, InputDefault>, AnyFunc>>,
|
|
120
120
|
OnResponseReturn<RealProp<'onResponse', CC, AConfig, InputDefault>>,
|
|
121
121
|
ParserReturn<NonNullable<RealProp<'requestMode', CC, AConfig, InputDefault>>, NonNullable<RealProp<'parser', CC, AConfig, InputDefault>>, ReqOutput, UserR>
|
|
122
122
|
], undefined | null>>>>;
|
|
123
|
-
type APIInputType<A extends Pick<APIConfig, 'tdto'> = APIConfig, D extends Pick<DefaultAPIConfig, 'tdto'> = DefaultAPIConfig> = Parameters<Cast<FindNonAny<[A['tdto'], D['tdto']], undefined | null>, (...args: any[]) => any>>[0];
|
|
124
|
-
type PropResult<A extends APIConfig, P extends keyof A> = A[P] extends AnyFunc ? ReturnType<A[P]> : unknown;
|
|
123
|
+
export type APIInputType<A extends Pick<APIConfig, 'tdto'> = APIConfig, D extends Pick<DefaultAPIConfig, 'tdto'> = DefaultAPIConfig> = Parameters<Cast<FindNonAny<[A['tdto'], D['tdto']], undefined | null>, (...args: any[]) => any>>[0];
|
|
124
|
+
export type PropResult<A extends APIConfig, P extends keyof A> = A[P] extends AnyFunc ? ReturnType<A[P]> : unknown;
|
|
125
125
|
export type APITransformMethod<A extends APIConfig, InputD extends DefaultAPIConfig = DefaultAPIConfig, Custom extends boolean = false, NonParamUrl extends boolean = CheckNonParamUrlAPIConfig<A>, I extends APIInputType<A, InputD> = APIInputType<A, InputD>> = (<R = Empty, C extends CallAPIConfig<I, any, PropResult<A, 'onRequest'>, PropResult<A, 'onResponse'>, InputD, DefineRequestModes<InputD>, A['url']> = CallAPIConfig<I, any, PropResult<A, 'onRequest'>, PropResult<A, 'onResponse'>, InputD, DefineRequestModes<InputD>, A['url']>>(...args: APIHandlerArgs<Equal<I, APIInputType<A, InputD>> extends true ? APIInputType<C, {
|
|
126
126
|
tdto: Func<[I]>;
|
|
127
127
|
}> : I, C, Custom, NonParamUrl>) => APIHandlerResult<A, Cast<C, Partial<APIConfig>>, R, InputD, ReturnType<Cast<RealProp<'onRequest', C, A, InputD>, AnyFunc>>, Custom>) & APIInstance<A, InputD>;
|
|
@@ -130,12 +130,11 @@ export type APIInstance<A, D> = {
|
|
|
130
130
|
$$: DefaultAPIConfig extends D ? undefined : D;
|
|
131
131
|
$$r: DefaultAPIConfig;
|
|
132
132
|
} & APIInstanceHandler;
|
|
133
|
-
interface APIInstanceHandler {
|
|
134
|
-
$updateBaseUrl(baseUrl?: string)
|
|
133
|
+
export interface APIInstanceHandler {
|
|
134
|
+
$updateBaseUrl: (baseUrl?: string) => void;
|
|
135
135
|
}
|
|
136
136
|
export type APIMapTransformMethods<M extends APIMap | Record<string, APIConfig>, D extends DefaultAPIConfig = DefaultAPIConfig> = {
|
|
137
137
|
[K in keyof M as M[K] extends APIConfig ? CheckNonParamUrlAPIConfig<M[K]> extends true ? K : never : K]: M[K] extends APIConfig ? APITransformMethod<M[K], D, false> : APIMapTransformMethods<Cast<M[K], APIMap>, D>;
|
|
138
138
|
} & {
|
|
139
139
|
[K in keyof M as M[K] extends APIConfig ? `${K & string}Custom` : never]: APITransformMethod<Cast<M[K], APIConfig>, D, true>;
|
|
140
140
|
} & APIInstance<M, D>;
|
|
141
|
-
export {};
|
|
@@ -1,22 +1,13 @@
|
|
|
1
1
|
import type { APIConfig, APIInstance, APIMap, DefaultAPIConfig } from './types';
|
|
2
2
|
export declare function isAbsUrl(url?: string): boolean;
|
|
3
3
|
export declare function targetUrlParser(_url: string, _baseUrl: string): URL;
|
|
4
|
-
export declare function urlParamsParser(url: string, params: Record<string, string> | undefined): string;
|
|
4
|
+
export declare function urlParamsParser(url: string, params: Record<string, string | number> | undefined): string;
|
|
5
5
|
export declare function getBody(data: any, tdto?: APIConfig['tdto']): any;
|
|
6
6
|
export declare function instanceMemberGetter(prop: string, instanceObj: Record<string, any>): any;
|
|
7
|
-
export declare function createInstance(apiMap: APIConfig | APIMap, realDefaultConfig: DefaultAPIConfig, defaultConfig?: DefaultAPIConfig):
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
export declare function createInstance(apiMap: APIConfig | APIMap, realDefaultConfig: DefaultAPIConfig, defaultConfig?: DefaultAPIConfig): APIInstance<any, any>;
|
|
8
|
+
export declare function getInstanceMemberOrApi(target: APIMap, prop: string, receiver: any, instanceObj: APIInstance<any, any>): undefined | {
|
|
9
|
+
api?: APIConfig | APIMap;
|
|
10
|
+
isCustom?: boolean;
|
|
11
|
+
instanceMember?: any;
|
|
12
12
|
};
|
|
13
|
-
export declare function getInstanceMemberOrApi(target: APIMap, prop: string, receiver: any, instanceObj: APIInstance<any, any>): {
|
|
14
|
-
instanceMember: any;
|
|
15
|
-
api?: undefined;
|
|
16
|
-
isCustom?: undefined;
|
|
17
|
-
} | {
|
|
18
|
-
api: APIMap<string & {}> | import("./types").DefineAPIConfig<string & {}>;
|
|
19
|
-
isCustom: boolean;
|
|
20
|
-
instanceMember?: undefined;
|
|
21
|
-
} | undefined;
|
|
22
13
|
export declare function apiNamesCheck(_apiMap: APIMap, isDeep?: boolean): string[];
|
|
@@ -1,96 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { throwError, throwType } from "../throw-error/index.js";
|
|
3
|
-
import { getType, isPlainNumber, isString } from "../utils/index.js";
|
|
4
|
-
const ABSOLUTE_URL_REG = /^[a-z][a-z\d+\-.]*:/im;
|
|
5
|
-
function isAbsUrl(url) {
|
|
6
|
-
if (!url) return false;
|
|
7
|
-
return ABSOLUTE_URL_REG.test(url);
|
|
8
|
-
}
|
|
9
|
-
function targetUrlParser(_url, _baseUrl) {
|
|
10
|
-
if (isAbsUrl(_url)) return new URL(_url);
|
|
11
|
-
if (!isAbsUrl(_baseUrl)) throwType('apiController.request', 'baseUrl 配置不合法, 必须是绝对路径');
|
|
12
|
-
const baseUrl = new URL(_baseUrl);
|
|
13
|
-
const basePath = '/' === baseUrl.pathname ? '' : baseUrl.pathname.replace(/\/$/, '');
|
|
14
|
-
const relativePath = _url.startsWith('/') ? _url : `/${_url}`;
|
|
15
|
-
const url = `${basePath}${relativePath}`;
|
|
16
|
-
return new URL(url, baseUrl);
|
|
17
|
-
}
|
|
18
|
-
function urlParamsParser(url, params) {
|
|
19
|
-
if (!url.includes('/:')) return url;
|
|
20
|
-
if (!params) throwType('apiController.parseParams', 'url 中存在 params 参数, params 配置不能为空, 请使用 custom 方法调用并传递 params 配置');
|
|
21
|
-
const urlSplit = url.split('/');
|
|
22
|
-
const emptyKeys = [];
|
|
23
|
-
for(let i = 1; i < urlSplit.length; ++i){
|
|
24
|
-
if (':' !== urlSplit[i][0]) continue;
|
|
25
|
-
const param = urlSplit[i].slice(1);
|
|
26
|
-
const originValue = params[param];
|
|
27
|
-
if (!(isPlainNumber(originValue) || originValue)) {
|
|
28
|
-
emptyKeys.push(param);
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
const paramValue = encodeURIComponent(String(originValue));
|
|
32
|
-
urlSplit[i] = paramValue;
|
|
33
|
-
}
|
|
34
|
-
if (emptyKeys.length) throwType('apiController.parseParams', `params 配置中缺少 [${emptyKeys.join(', ')}] 参数`);
|
|
35
|
-
return urlSplit.join('/');
|
|
36
|
-
}
|
|
37
|
-
function getBody(data, tdto) {
|
|
38
|
-
const _body = tdto ? tdto(data) : data;
|
|
39
|
-
const bodyType = getType(_body);
|
|
40
|
-
switch(bodyType){
|
|
41
|
-
case 'object':
|
|
42
|
-
case 'array':
|
|
43
|
-
case 'number':
|
|
44
|
-
case 'boolean':
|
|
45
|
-
case 'function':
|
|
46
|
-
return JSON.stringify(_body);
|
|
47
|
-
default:
|
|
48
|
-
return _body;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function instanceMemberGetter(prop, instanceObj) {
|
|
52
|
-
return instanceObj[prop];
|
|
53
|
-
}
|
|
54
|
-
function createInstance(apiMap, realDefaultConfig, defaultConfig) {
|
|
55
|
-
return {
|
|
56
|
-
$: apiMap,
|
|
57
|
-
$$: defaultConfig,
|
|
58
|
-
$$r: realDefaultConfig,
|
|
59
|
-
$updateBaseUrl (baseUrl) {
|
|
60
|
-
if (isAbsUrl(baseUrl)) realDefaultConfig.baseUrl = baseUrl;
|
|
61
|
-
else {
|
|
62
|
-
const { origin } = globalThis.location || {};
|
|
63
|
-
if (!origin) throwError('apiController.$updateBaseUrl', 'location.origin is undefined');
|
|
64
|
-
const normalizedPath = (baseUrl || '/').startsWith('/') ? baseUrl || '' : `/${baseUrl}`;
|
|
65
|
-
realDefaultConfig.baseUrl = `${origin}${normalizedPath}`;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
function getInstanceMemberOrApi(target, prop, receiver, instanceObj) {
|
|
71
|
-
if (Reflect.getOwnPropertyDescriptor(instanceObj, prop)) return {
|
|
72
|
-
instanceMember: instanceMemberGetter(prop, instanceObj)
|
|
73
|
-
};
|
|
74
|
-
const hasExactProp = isString(prop) && Reflect.has(target, prop);
|
|
75
|
-
const isCustom = isString(prop) && prop.endsWith('Custom') && !hasExactProp;
|
|
76
|
-
const name = isCustom ? prop.slice(0, -6) : prop;
|
|
77
|
-
if (!Reflect.getOwnPropertyDescriptor(target, name)) return;
|
|
78
|
-
const api = Reflect.get(target, name, receiver);
|
|
79
|
-
if (isCustom && !isString(api.url)) return;
|
|
80
|
-
return {
|
|
81
|
-
api,
|
|
82
|
-
isCustom
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
function apiNamesCheck(_apiMap, isDeep = false) {
|
|
86
|
-
const apiNames = Reflect.ownKeys(_apiMap);
|
|
87
|
-
const warnNames = [];
|
|
88
|
-
for(let i = 0; i < apiNames.length; i++){
|
|
89
|
-
const name = apiNames[i];
|
|
90
|
-
if (name.endsWith('Custom')) warnNames.push(name);
|
|
91
|
-
if (!isString(_apiMap[name].url)) warnNames.push(...apiNamesCheck(_apiMap[name], true));
|
|
92
|
-
}
|
|
93
|
-
if (!isDeep && warnNames.length > 0) logger.warn('apiController.createApiWithMap', 'api 命名不应该使用 Custom 结尾, 因为这是一个内部实现的方法', warnNames);
|
|
94
|
-
return warnNames;
|
|
95
|
-
}
|
|
96
|
-
export { apiNamesCheck, createInstance, getBody, getInstanceMemberOrApi, instanceMemberGetter, isAbsUrl, targetUrlParser, urlParamsParser };
|
|
1
|
+
import{logger as e}from"../logger/index.js";import{throwError as t,throwType as r}from"../throw-error/index.js";import{getType as n}from"../utils/base.js";import{isPlainNumber as a,isString as i}from"../utils/verify.js";let s=/^[a-z][a-z\d+\-.]*:/imu;function l(e){return!!e&&s.test(e)}function o(e,t){if(l(e))return new URL(e);l(t)||r("apiController.request","baseUrl 配置不合法, 必须是绝对路径");let n=new URL(t),a="/"===n.pathname?"":n.pathname.replace(/\/$/u,""),i=e.startsWith("/")?e:`/${e}`;return new URL(`${a}${i}`,n)}function u(e,t){if(!e.includes("/:"))return e;t||r("apiController.parseParams","url 中存在 params 参数, params 配置不能为空, 请使用 custom 方法调用并传递 params 配置");let n=e.split("/"),i=[];for(let e=1;e<n.length;++e){if(":"!==n[e][0])continue;let r=n[e].slice(1),s=t[r];if(!(a(s)||s)){i.push(r);continue}let l=encodeURIComponent(String(s));n[e]=l}return i.length>0&&r("apiController.parseParams",`params 配置中缺少 [${i.join(", ")}] 参数`),n.join("/")}function c(e,t){let r=t?t(e):e;switch(n(r)){case"object":case"array":case"number":case"boolean":case"function":return JSON.stringify(r);default:return r}}function p(e,t){return t[e]}function f(e,r,n){return{$:e,$$:n,$$r:r,$updateBaseUrl(e){if(l(e))r.baseUrl=e;else{let{origin:n}=globalThis.location||{};n||t("apiController.$updateBaseUrl","location.origin is undefined");let a=(e||"/").startsWith("/")?e||"":`/${e}`;r.baseUrl=`${n}${a}`}}}}function m(e,t,r,n){if(Reflect.getOwnPropertyDescriptor(n,t))return{instanceMember:p(t,n)};let a=i(t)&&Reflect.has(e,t),s=i(t)&&t.endsWith("Custom")&&!a,l=s?t.slice(0,-6):t;if(!Reflect.getOwnPropertyDescriptor(e,l))return;let o=Reflect.get(e,l,r);if(!s||i(o.url))return{api:o,isCustom:s}}function h(t,r=!1){let n=Reflect.ownKeys(t),a=[];for(let e=0;e<n.length;e++){let r=n[e];r.endsWith("Custom")&&a.push(r),i(t[r].url)||a.push(...h(t[r],!0))}return!r&&a.length>0&&e.warn("apiController.createApiWithMap","api 命名不应该使用 Custom 结尾, 因为这是一个内部实现的方法",a),a}export{h as apiNamesCheck,f as createInstance,c as getBody,m as getInstanceMemberOrApi,p as instanceMemberGetter,l as isAbsUrl,o as targetUrlParser,u as urlParamsParser};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { IsPrimitive, Printify, UnionToIntersection } from '../types';
|
|
1
|
+
import type { IsPrimitive, Printify, UnionToIntersection } from '../types/base';
|
|
2
2
|
type AssertValue = Record<PropertyKey, any> | any[];
|
|
3
3
|
type ConditionArrayItem = [boolean, AssertValue, AssertValue?];
|
|
4
|
-
|
|
4
|
+
interface ConditionObjItem {
|
|
5
5
|
condition: boolean;
|
|
6
6
|
value: AssertValue;
|
|
7
7
|
fullback?: AssertValue;
|
|
8
|
-
}
|
|
8
|
+
}
|
|
9
9
|
type ConditionItem = ConditionObjItem | ConditionArrayItem;
|
|
10
10
|
type GetDefaultValue<V> = IsPrimitive<V> extends true ? never : Record<never, any>;
|
|
11
11
|
type ParseDefaultValue<V, D> = FormatValue<unknown extends D ? (V extends any[] ? [] : GetDefaultValue<V>) : D extends undefined | null ? GetDefaultValue<V> : D>;
|
|
@@ -26,6 +26,6 @@ type MergedResult<T extends ConditionItem[]> = T extends [
|
|
|
26
26
|
] ? ParseConditionValue<First> & MergedResult<Last> : ParseConditionValue<T[0]>;
|
|
27
27
|
type CMInput = ConditionItem[];
|
|
28
28
|
type FormatResult<T extends any[]> = T[0] & Record<PropertyKey, any>;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export {};
|
|
29
|
+
declare function conditionMerge<T extends CMInput>(...input: T): FormatResult<MergedResult<T>>;
|
|
30
|
+
declare function conditionMerge<T extends CMInput>(input: T): FormatResult<MergedResult<T>>;
|
|
31
|
+
export { conditionMerge };
|
|
@@ -1,30 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
function getEmpty(_v) {
|
|
3
|
-
return Array.isArray(_v) ? [] : {};
|
|
4
|
-
}
|
|
5
|
-
function valueCheck(_v) {
|
|
6
|
-
return Array.isArray(_v) || 'object' == typeof _v;
|
|
7
|
-
}
|
|
8
|
-
function conditionMerge(...input) {
|
|
9
|
-
const conditionItems = (input.length > 1 ? input : input[0]).map((item)=>{
|
|
10
|
-
let result = null;
|
|
11
|
-
if (Array.isArray(item)) {
|
|
12
|
-
const [condition, value, fullback] = item;
|
|
13
|
-
result = {
|
|
14
|
-
condition,
|
|
15
|
-
value,
|
|
16
|
-
fullback
|
|
17
|
-
};
|
|
18
|
-
} else if (Object.getOwnPropertyDescriptor(item, 'condition')) result = item;
|
|
19
|
-
else throwType('conditionMerge', 'input must be an ConditionItem');
|
|
20
|
-
const validValue = valueCheck(result.value);
|
|
21
|
-
const validFullback = void 0 === result.fullback || valueCheck(result.fullback);
|
|
22
|
-
if (!(validValue && validFullback)) throwType('conditionMerge', 'value and fullback must be an array or object');
|
|
23
|
-
return result;
|
|
24
|
-
});
|
|
25
|
-
const result = getEmpty(conditionItems[0].value);
|
|
26
|
-
const mergeFn = Array.isArray(result) ? (a1, a2)=>Reflect.apply(Array.prototype.splice.bind(a1, a1.length, 0), null, a2) : Object.assign;
|
|
27
|
-
for(let i = 0, item = conditionItems[i]; i < conditionItems.length; item = conditionItems[++i])mergeFn(result, item.condition ? item.value : item.fullback || getEmpty(item.value));
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
export { conditionMerge };
|
|
1
|
+
import{throwType as r}from"../throw-error/index.js";function e(r){return Array.isArray(r)?[]:{}}function t(r){return Array.isArray(r)||"object"==typeof r}function n(...o){let i=(1===o.length&&Array.isArray(o[0])?o[0]:o).map(e=>{let n=null;if(Array.isArray(e)){let[r,t,o]=e;n={condition:r,value:t,fullback:o}}else Object.getOwnPropertyDescriptor(e,"condition")?n=e:r("conditionMerge","input must be an ConditionItem");let o=t(n.value),i=void 0===n.fullback||t(n.fullback);return o&&i||r("conditionMerge","value and fullback must be an array or object"),n}),l=e((i[0]||{}).value),a=Array.isArray(l)?(r,e)=>Reflect.apply(Array.prototype.splice.bind(r,r.length,0),null,e):Object.assign;for(let r=0,t=i[r];r<i.length;t=i[++r])a(l,t.condition?t.value:t.fullback||e(t.value));return l}export{n as conditionMerge};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
interface CreateStorageOptions {
|
|
2
2
|
storageType: 'local' | 'session' | 'memory';
|
|
3
3
|
autoSaveInterval: number;
|
|
4
4
|
}
|
|
5
|
-
|
|
5
|
+
interface StorageHandler<T extends Record<string, any>> {
|
|
6
6
|
get: <K extends keyof T | (string & {})>(key?: K) => string extends K ? T : T[K & keyof T];
|
|
7
7
|
set: <K extends keyof T | (string & {})>(value: string extends K ? T : T[K & keyof T], key?: K) => void;
|
|
8
8
|
clear: () => void;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
declare function createStorageHandler<T extends Record<string, any>>(storageKey: string, initialData?: T, options?: Partial<CreateStorageOptions>): StorageHandler<T>;
|
|
11
|
+
export { type CreateStorageOptions, createStorageHandler, type StorageHandler };
|
|
@@ -1,68 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { logger } from "../logger/index.js";
|
|
3
|
-
import { throwError } from "../throw-error/index.js";
|
|
4
|
-
const validInfo = $dt({
|
|
5
|
-
storageKey: 'validString',
|
|
6
|
-
storageType: $t["enum"]([
|
|
7
|
-
'local',
|
|
8
|
-
'session',
|
|
9
|
-
'memory'
|
|
10
|
-
], 'local'),
|
|
11
|
-
autoSaveInterval: $t.number(0)
|
|
12
|
-
});
|
|
13
|
-
const memoryStorage = {
|
|
14
|
-
data: {},
|
|
15
|
-
getItem (key) {
|
|
16
|
-
return this.data[key];
|
|
17
|
-
},
|
|
18
|
-
setItem (key, value) {
|
|
19
|
-
this.data[key] = value;
|
|
20
|
-
},
|
|
21
|
-
removeItem (key) {
|
|
22
|
-
delete this.data[key];
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
function getStorage(storageType) {
|
|
26
|
-
try {
|
|
27
|
-
if ('memory' === storageType) return memoryStorage;
|
|
28
|
-
return 'local' === storageType ? localStorage : sessionStorage;
|
|
29
|
-
} catch {
|
|
30
|
-
logger.warn('createStorage', 'Failed to access localStorage or sessionStorage, using memoryStorage instead.');
|
|
31
|
-
return memoryStorage;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const CLEAR_FLAG = Symbol('cleared');
|
|
35
|
-
function createStorageHandler(storageKey, initialData, options = {}) {
|
|
36
|
-
const { storageKey: validStorageKey, storageType, autoSaveInterval } = dataHandler({
|
|
37
|
-
storageKey,
|
|
38
|
-
...options
|
|
39
|
-
}, validInfo, {
|
|
40
|
-
unwrap: true
|
|
41
|
-
});
|
|
42
|
-
const storage = getStorage(storageType);
|
|
43
|
-
const storageData = storage.getItem(validStorageKey);
|
|
44
|
-
const context = {
|
|
45
|
-
data: storageData ? JSON.parse(storageData) : initialData || {}
|
|
46
|
-
};
|
|
47
|
-
return {
|
|
48
|
-
get (key) {
|
|
49
|
-
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
50
|
-
if (null == key) return context.data;
|
|
51
|
-
return context.data[key];
|
|
52
|
-
},
|
|
53
|
-
set (value, key) {
|
|
54
|
-
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
55
|
-
if (null == key) context.data = value;
|
|
56
|
-
else context.data[key] = value;
|
|
57
|
-
if (autoSaveInterval > 0) setTimeout(()=>{
|
|
58
|
-
storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
59
|
-
}, autoSaveInterval);
|
|
60
|
-
else storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
61
|
-
},
|
|
62
|
-
clear () {
|
|
63
|
-
context.data = CLEAR_FLAG;
|
|
64
|
-
storage.removeItem(validStorageKey);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
export { createStorageHandler };
|
|
1
|
+
import{$dt as e,$t as t,dataHandler as a}from"../data-handler/index.js";import{logger as r}from"../logger/index.js";import{throwError as o}from"../throw-error/index.js";import{isNullOrUndef as i}from"../utils/verify.js";let l=e({storageKey:"validString",storageType:t.enum(["local","session","memory"],"local"),autoSaveInterval:t.number(0)}),n={data:{},getItem(e){return this.data[e]},setItem(e,t){this.data[e]=t},removeItem(e){delete this.data[e]}},s=Symbol("cleared");function m(e,t,d={}){let{storageKey:g,storageType:c,autoSaveInterval:u}=a({storageKey:e,...d},l,{unwrap:!0}),S=function(e){try{if("memory"===e)return n;return"local"===e?localStorage:sessionStorage}catch{return r.warn("createStorage","Failed to access localStorage or sessionStorage, using memoryStorage instead."),n}}(c),y=S.getItem(g),f={data:y?JSON.parse(y):t||{},timer:null},h=()=>{null!==f.timer&&(clearTimeout(f.timer),f.timer=null)};return{get:e=>(f.data===s&&o("createStorageHandler","Storage has been cleared."),i(e))?f.data:f.data[e],set(e,t){f.data===s&&o("createStorageHandler","Storage has been cleared."),i(t)?f.data=e:f.data[t]=e,u>0?(h(),f.timer=setTimeout(()=>{S.setItem(g,JSON.stringify(f.data))},u)):S.setItem(g,JSON.stringify(f.data))},clear(){h(),f.data=s,S.removeItem(g)}}}export{m as createStorageHandler};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type { Equal, Printify } from '../types';
|
|
1
|
+
import type { Equal, Printify } from '../types/base';
|
|
2
2
|
import type { Transform2Type } from './tools';
|
|
3
3
|
import type { DataHandlerOptions, Handler } from './types';
|
|
4
4
|
type MergeResult<BaseResult extends Record<PropertyKey, any>, HandlerResult extends Handler<any>> = Printify<HandlerResult extends (...args: any[]) => any ? BaseResult : BaseResult & {
|
|
5
5
|
[K in keyof HandlerResult]: Equal<HandlerResult[K], any> extends true ? Required<BaseResult>[K] : HandlerResult[K];
|
|
6
6
|
}>;
|
|
7
|
-
|
|
7
|
+
declare function dataHandler<M extends Record<PropertyKey, any>, H extends Handler<M> = Handler<M>, O extends DataHandlerOptions<M> = DataHandlerOptions<M>>(data: M & Partial<O['defaultValue']>, handler: H, options?: O): O['unwrap'] extends true ? MergeResult<M & O['defaultValue'], Transform2Type<H>> : {
|
|
8
8
|
result: MergeResult<M & O['defaultValue'], Transform2Type<H>>;
|
|
9
9
|
errors: string[];
|
|
10
10
|
};
|
|
11
|
-
export
|
|
11
|
+
export { $dt, $t, defineTransform } from './tools';
|
|
12
|
+
export { dataHandler };
|
|
@@ -1,77 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export * from "./tools.js";
|
|
3
|
-
function createActions() {
|
|
4
|
-
const ctx = {
|
|
5
|
-
errors: [],
|
|
6
|
-
transforms: [],
|
|
7
|
-
handledErrorKeys: new Set()
|
|
8
|
-
};
|
|
9
|
-
const handler = {
|
|
10
|
-
addError (key, msg = `${String(key)} is not valid`) {
|
|
11
|
-
if (ctx.handledErrorKeys.has(key)) return;
|
|
12
|
-
ctx.handledErrorKeys.add(key);
|
|
13
|
-
ctx.errors.push(msg);
|
|
14
|
-
},
|
|
15
|
-
addTransform (key, value) {
|
|
16
|
-
ctx.transforms.push([
|
|
17
|
-
key,
|
|
18
|
-
value
|
|
19
|
-
]);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
return [
|
|
23
|
-
ctx,
|
|
24
|
-
handler,
|
|
25
|
-
(key)=>({
|
|
26
|
-
assert: (flag, msg = `${String(key)} is not valid`)=>{
|
|
27
|
-
if (!flag) handler.addError(key, msg);
|
|
28
|
-
return flag;
|
|
29
|
-
},
|
|
30
|
-
transform: (value)=>{
|
|
31
|
-
if (!ctx.handledErrorKeys.has(key)) handler.addTransform(key, value);
|
|
32
|
-
return value;
|
|
33
|
-
}
|
|
34
|
-
})
|
|
35
|
-
];
|
|
36
|
-
}
|
|
37
|
-
function transformApply(data, transforms) {
|
|
38
|
-
if (!transforms.length) return;
|
|
39
|
-
for(let i = 0, [key, value] = transforms[i]; i < transforms.length; [key, value] = transforms[++i] || [])data[key] = value;
|
|
40
|
-
}
|
|
41
|
-
function handleProcess(data, keys, handleFn, getActions, actionHandlers) {
|
|
42
|
-
for(let i = 0, key = keys[i]; i < keys.length; key = keys[++i]){
|
|
43
|
-
const flag = handleFn(data[key], key, getActions(key), data);
|
|
44
|
-
if (false === flag) actionHandlers.addError(key);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function errorProcess(errors, errorHandler, strict) {
|
|
48
|
-
if (!errors.length) return;
|
|
49
|
-
if (errorHandler) errorHandler(errors);
|
|
50
|
-
else if (strict) throwType('dataHandler', errors.join('\n'));
|
|
51
|
-
}
|
|
52
|
-
function filterData(data, ctx, defaultValue = {}) {
|
|
53
|
-
ctx.handledErrorKeys.forEach((key)=>{
|
|
54
|
-
data[key] = defaultValue[key];
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
function dataHandler(data, handler, options) {
|
|
58
|
-
if (!handler) throwType('dataHandler', 'handler is required');
|
|
59
|
-
const { strict = false, errorHandler, defaultValue, unwrap = false } = options || {};
|
|
60
|
-
const handlerIsFunction = 'function' == typeof handler;
|
|
61
|
-
const handleFn = handlerIsFunction ? handler : (value, key, ...args)=>handler[key](value, ...args);
|
|
62
|
-
const tempData = {
|
|
63
|
-
...defaultValue,
|
|
64
|
-
...data
|
|
65
|
-
};
|
|
66
|
-
const keys = handlerIsFunction ? Reflect.ownKeys(data) : Reflect.ownKeys(handler);
|
|
67
|
-
const [ctx, actionHandler, getActions] = createActions();
|
|
68
|
-
handleProcess(tempData, keys, handleFn, getActions, actionHandler);
|
|
69
|
-
errorProcess(ctx.errors, errorHandler, strict);
|
|
70
|
-
transformApply(tempData, ctx.transforms);
|
|
71
|
-
filterData(tempData, ctx, defaultValue);
|
|
72
|
-
return unwrap ? tempData : {
|
|
73
|
-
result: tempData,
|
|
74
|
-
errors: ctx.errors
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
export { dataHandler };
|
|
1
|
+
import{throwType as r}from"../throw-error/index.js";import{isEmptyArray as e}from"../utils/verify.js";function o(o,s,n){let t,a;s||r("dataHandler","handler is required");let{strict:d=!1,errorHandler:f,defaultValue:l,unwrap:i=!1}=n||{},h="function"==typeof s,m=h?s:(r,e,...o)=>s[e](r,...o),u={...l,...o},y=h?Reflect.ownKeys(o):Reflect.ownKeys(s),[E,p,K]=(t={errors:[],transforms:[],handledErrorKeys:new Set},a={addError(r,e=`${String(r)} is not valid`){t.handledErrorKeys.has(r)||(t.handledErrorKeys.add(r),t.errors.push(e))},addTransform(r,e){t.transforms.push([r,e])}},[t,a,r=>({assert:(e,o=`${String(r)} is not valid`)=>(e||a.addError(r,o),e),transform:e=>(t.handledErrorKeys.has(r)||a.addTransform(r,e),e)})]);for(let r=0,e=y[r];r<y.length;e=y[++r])!1===m(u[e],e,K(e),u)&&p.addError(e);c=E.errors,!e(c)&&(f?f(c):d&&r("dataHandler",c.join("\n")));var c,g=E.transforms;if(!e(g))for(let r=0,[e,o]=g[r];r<g.length;[e,o]=g[++r]||[])u[e]=o;return!function(r,e,o={}){e.handledErrorKeys.forEach(e=>{r[e]=o[e]})}(u,E,l),i?u:{result:u,errors:E.errors}}export{$dt,$t,defineTransform}from"./tools.js";export{o as dataHandler};
|
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
type TypeHandlerParams = Parameters<TypeHandler> extends [any, ...infer Rest] ? Rest : never;
|
|
4
|
-
type ParseType<T extends string> = T extends keyof TypeMap ? TypeMap[T] : any;
|
|
5
|
-
type TypeHandlerInfo<T extends string> = (value: ParseType<T>, ...args: TypeHandlerParams) => ReturnType<TypeHandler>;
|
|
6
|
-
interface TypeMap {
|
|
7
|
-
notNullable: any & {};
|
|
8
|
-
string: string;
|
|
9
|
-
validString: string;
|
|
10
|
-
number: number;
|
|
11
|
-
validNumber: number;
|
|
12
|
-
boolean: boolean;
|
|
13
|
-
object: Record<PropertyKey, any>;
|
|
14
|
-
array: any[];
|
|
15
|
-
function: (...args: any[]) => any;
|
|
16
|
-
symbol: symbol;
|
|
17
|
-
enum: any & {};
|
|
18
|
-
}
|
|
1
|
+
import type { Fullback, ParseType, TypeHandler, TypeHandlerInfo } from './types';
|
|
2
|
+
export declare function typeHandler<T extends string>(type: T, verifyFn?: (_v: any) => boolean): (fullback?: Fullback<T>) => TypeHandlerInfo<T>;
|
|
19
3
|
export declare const $t: {
|
|
20
4
|
notNullable: (fullback?: any) => TypeHandlerInfo<"notNullable">;
|
|
21
5
|
string: (fullback?: string | ((_v: any) => string) | undefined) => TypeHandlerInfo<"string">;
|
|
@@ -29,14 +13,13 @@ export declare const $t: {
|
|
|
29
13
|
symbol: (fullback?: symbol | ((_v: any) => symbol) | undefined) => TypeHandlerInfo<"symbol">;
|
|
30
14
|
enum: <T>(list: T[], fullback?: T) => TypeHandlerInfo<"enum">;
|
|
31
15
|
};
|
|
32
|
-
type TransformMap = typeof $t;
|
|
33
|
-
type TransformKey = Exclude<keyof TransformMap, 'enum'>;
|
|
34
|
-
type DataTransformResult<D extends Record<PropertyKey, TransformKey | TypeHandler | undefined>> = {
|
|
16
|
+
export type TransformMap = typeof $t;
|
|
17
|
+
export type TransformKey = Exclude<keyof TransformMap, 'enum'>;
|
|
18
|
+
export type DataTransformResult<D extends Record<PropertyKey, TransformKey | TypeHandler | undefined>> = {
|
|
35
19
|
[K in keyof D]: D[K] extends TransformKey ? TransformMap[D[K]] : D[K];
|
|
36
20
|
};
|
|
37
21
|
export type Transform2Type<R extends DataTransformResult<any>> = {
|
|
38
22
|
[K in keyof R]: R[K] extends TypeHandlerInfo<infer T> ? ParseType<T> & {} : any & {};
|
|
39
23
|
};
|
|
40
24
|
export declare function defineTransform<T extends Record<PropertyKey, any>, D extends Partial<Record<keyof T, TransformKey | TypeHandler>> = Partial<Record<keyof T, TransformKey | TypeHandler>>>(dataInfo: D): DataTransformResult<D>;
|
|
41
|
-
export
|
|
42
|
-
export {};
|
|
25
|
+
export { defineTransform as $dt };
|
|
@@ -1,48 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { throwType } from "../throw-error/index.js";
|
|
3
|
-
import { getType } from "../utils/base.js";
|
|
4
|
-
function typeHandler(type, verifyFn) {
|
|
5
|
-
return (fullback)=>(_v, actions)=>{
|
|
6
|
-
if (verifyFn ? verifyFn(_v) : getType(_v) === type) return true;
|
|
7
|
-
if (null == fullback) return false;
|
|
8
|
-
let fullbackValue = fullback;
|
|
9
|
-
if ('function' == typeof fullback) fullbackValue = fullback(_v);
|
|
10
|
-
actions.transform(fullbackValue);
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
const $t = {
|
|
14
|
-
notNullable: typeHandler('notNullable', (_v)=>null != _v),
|
|
15
|
-
string: typeHandler('string'),
|
|
16
|
-
validString: typeHandler('validString', (_v)=>'string' == typeof _v && _v.length > 0),
|
|
17
|
-
number: typeHandler('number'),
|
|
18
|
-
validNumber: typeHandler('validNumber', (_v)=>'number' == typeof _v && !Number.isNaN(_v)),
|
|
19
|
-
boolean: typeHandler('boolean'),
|
|
20
|
-
object: typeHandler('object'),
|
|
21
|
-
array: typeHandler('array'),
|
|
22
|
-
function: typeHandler('function'),
|
|
23
|
-
symbol: typeHandler('symbol'),
|
|
24
|
-
enum: (list, fullback)=>{
|
|
25
|
-
if (!Array.isArray(list)) throwType('$t.enum', 'list must be an array');
|
|
26
|
-
const set = new Set(list);
|
|
27
|
-
return typeHandler('enum', (_v)=>set.has(_v))(fullback);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
function defineTransform(dataInfo) {
|
|
31
|
-
const verifyInfo = {};
|
|
32
|
-
const keys = Reflect.ownKeys(dataInfo);
|
|
33
|
-
for(let i = 0, key = keys[i], item = dataInfo[key]; i < keys.length; key = keys[++i], item = dataInfo[key]){
|
|
34
|
-
if ('function' == typeof item) {
|
|
35
|
-
verifyInfo[key] = item;
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
const handler = $t[item];
|
|
39
|
-
if (!handler) {
|
|
40
|
-
logger.warn('defineTransform', `${item} is not a valid type`);
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
verifyInfo[key] = handler();
|
|
44
|
-
}
|
|
45
|
-
return verifyInfo;
|
|
46
|
-
}
|
|
47
|
-
const $dt = defineTransform;
|
|
48
|
-
export { $dt, $t, defineTransform };
|
|
1
|
+
import{logger as r}from"../logger/index.js";import{throwType as e}from"../throw-error/index.js";import{getType as n}from"../utils/base.js";import{isNullOrUndef as t}from"../utils/verify.js";function o(r,e){return o=>(i,a)=>{if(e?e(i):n(i)===r)return!0;if(t(o))return!1;let l=o;"function"==typeof o&&(l=o(i)),a.transform(l)}}let i={notNullable:o("notNullable",r=>!t(r)),string:o("string"),validString:o("validString",r=>"string"==typeof r&&r.length>0),number:o("number"),validNumber:o("validNumber",r=>"number"==typeof r&&!Number.isNaN(r)),boolean:o("boolean"),object:o("object"),array:o("array"),function:o("function"),symbol:o("symbol"),enum:(r,n)=>{Array.isArray(r)||e("$t.enum","list must be an array");let t=new Set(r);return o("enum",r=>t.has(r))(n)}};function a(e){let n={},t=Reflect.ownKeys(e);for(let o=0,a=t[o],l=e[a];o<t.length;l=e[a=t[++o]]){if("function"==typeof l){n[a]=l;continue}let e=i[l];if(!e){r.warn("defineTransform",`${l} is not a valid type`);continue}n[a]=e()}return n}export{i as $t,a as $dt,a as defineTransform,o as typeHandler};
|
|
@@ -16,8 +16,8 @@ export interface ActionContext {
|
|
|
16
16
|
handledErrorKeys: Set<PropertyKey>;
|
|
17
17
|
}
|
|
18
18
|
export interface ActionHandlers {
|
|
19
|
-
addError(key: PropertyKey, msg?: string)
|
|
20
|
-
addTransform(key: PropertyKey, value: any)
|
|
19
|
+
addError: (key: PropertyKey, msg?: string) => void;
|
|
20
|
+
addTransform: (key: PropertyKey, value: any) => void;
|
|
21
21
|
}
|
|
22
22
|
export interface DataHandlerOptions<M extends Record<PropertyKey, any>> {
|
|
23
23
|
strict?: boolean;
|
|
@@ -25,3 +25,21 @@ export interface DataHandlerOptions<M extends Record<PropertyKey, any>> {
|
|
|
25
25
|
defaultValue?: M;
|
|
26
26
|
unwrap?: boolean;
|
|
27
27
|
}
|
|
28
|
+
export interface TypeMap {
|
|
29
|
+
notNullable: any & {};
|
|
30
|
+
string: string;
|
|
31
|
+
validString: string;
|
|
32
|
+
number: number;
|
|
33
|
+
validNumber: number;
|
|
34
|
+
boolean: boolean;
|
|
35
|
+
object: Record<PropertyKey, any>;
|
|
36
|
+
array: any[];
|
|
37
|
+
function: (...args: any[]) => any;
|
|
38
|
+
symbol: symbol;
|
|
39
|
+
enum: any & {};
|
|
40
|
+
}
|
|
41
|
+
export type TypeHandler = NonNullable<Exclude<Handler<any>, (...args: any[]) => any>[string]>;
|
|
42
|
+
export type TypeHandlerParams = Parameters<TypeHandler> extends [any, ...infer Rest] ? Rest : never;
|
|
43
|
+
export type ParseType<T extends string> = T extends keyof TypeMap ? TypeMap[T] : any;
|
|
44
|
+
export type TypeHandlerInfo<T extends string> = (value: ParseType<T>, ...args: TypeHandlerParams) => ReturnType<TypeHandler>;
|
|
45
|
+
export type Fullback<T extends string> = T extends 'function' ? (_v: any) => ParseType<T> : ((_v: any) => ParseType<T>) | (ParseType<T> & {});
|