@flowlist/js-core 4.0.4-beta.0 → 4.0.5-beta.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/dist/index.d.mts CHANGED
@@ -3,77 +3,84 @@
3
3
  */
4
4
  type ObjectKey = string | number;
5
5
  /**
6
- * 通用键值对映射 - 使用 Record 以保持灵活性和性能
7
- * 通过索引签名支持动态属性访问
6
+ * 通用对象结构 (仅用于内部弱类型处理,对外尽量使用泛型)
8
7
  */
9
- type KeyMap = Record<string, unknown>;
8
+ type KeyMap = Record<string, any>;
10
9
  /**
11
- * 数组元素类型
10
+ * 所有的 API 响应都应该遵循这个基础结构
12
11
  */
13
- type ResultArrayType = KeyMap[];
14
- /**
15
- * 对象结果类型:键为字符串,值为 KeyMap 数组
16
- */
17
- type ResultObjectType = Record<string, KeyMap[]>;
18
- /**
19
- * 结果类型:可以是数组或对象
20
- */
21
- type ResultType = ResultArrayType | ResultObjectType;
22
- /**
23
- * API 函数类型
24
- */
25
- type ApiFunction = (_params: KeyMap) => Promise<ApiResponse>;
12
+ interface BaseApiResponse<TData = any> {
13
+ readonly result: TData;
14
+ readonly extra?: any;
15
+ readonly total?: number;
16
+ readonly no_more?: boolean;
17
+ }
26
18
  /**
27
- * 数据源类型:可以是 API 路径字符串,或返回 Promise 的函数
19
+ * 核心状态字段
20
+ * @template TData 具体的 result 数据类型 (e.g. User[] or Record<string, User[]>)
21
+ * @template TExtra extra 字段的类型
28
22
  */
29
- type DataSource = string | ApiFunction;
30
- interface DefaultField {
31
- result: ResultType;
23
+ interface DefaultField<TData = any, TExtra = any> {
24
+ result: TData;
32
25
  noMore: boolean;
33
26
  nothing: boolean;
34
27
  loading: boolean;
35
28
  error: Error | null;
36
- extra: KeyMap | null;
29
+ extra: TExtra | null;
37
30
  fetched: boolean;
38
31
  page: number;
39
32
  total: number;
40
33
  }
41
34
  type FieldKeys = keyof DefaultField;
42
35
  /**
43
- * 字段获取器:根据字段名获取状态对象
36
+ * 请求参数的基础约束
44
37
  */
45
- type FieldGetter = (_key: string) => DefaultField | undefined;
38
+ interface RequestParams {
39
+ [key: string]: any;
40
+ __refresh__?: boolean;
41
+ __reload__?: boolean;
42
+ is_up?: 0 | 1 | boolean;
43
+ page?: number;
44
+ sinceId?: ObjectKey;
45
+ }
46
46
  /**
47
- * Setter 函数的参数
48
- */
49
- interface SetterFuncParams {
50
- readonly key: string;
51
- readonly type: number;
52
- readonly value: Partial<DefaultField> | DefaultField;
53
- readonly callback?: (_obj?: KeyMap) => void;
47
+ * API 契约:核心类型
48
+ * @template TParams 请求参数类型
49
+ * @template TResponse 响应的 result 数据类型
50
+ */
51
+ interface ApiContract<TParams extends RequestParams, TResponse> {
52
+ /**
53
+ * 调用函数
54
+ */
55
+ (params: TParams): Promise<BaseApiResponse<TResponse>>;
56
+ /**
57
+ * 静态属性
58
+ */
59
+ readonly id: string;
60
+ readonly type: FetchType;
61
+ readonly uniqueKey: string;
62
+ readonly paramsIgnore: string[];
54
63
  }
55
64
  /**
56
- * 状态设置器函数类型
65
+ * 数据获取策略类型
57
66
  */
58
- type FieldSetter = (_obj: SetterFuncParams) => void;
67
+ type FetchType = 'jump' | 'sinceId' | 'page' | 'seenIds' | 'auto';
59
68
  /**
60
- * 数据获取类型枚举
69
+ * 字段获取器
61
70
  */
62
- type FetchType = 'jump' | 'sinceId' | 'page' | 'seenIds' | 'auto';
71
+ type FieldGetter = (key: string) => DefaultField | undefined;
63
72
  /**
64
- * 请求参数的基础约束
73
+ * 字段设置器参数
65
74
  */
66
- interface RequestParams {
67
- [key: string]: unknown;
68
- __refresh__?: boolean;
69
- __reload__?: boolean;
70
- is_up?: 0 | 1 | boolean;
71
- page?: number;
72
- sinceId?: ObjectKey;
75
+ interface SetterFuncParams {
76
+ readonly key: string;
77
+ readonly type: number;
78
+ readonly value: Partial<DefaultField> | DefaultField;
79
+ readonly callback?: (obj?: any) => void;
73
80
  }
81
+ type FieldSetter = (obj: SetterFuncParams) => void;
74
82
  /**
75
- * 生成请求参数的输出
76
- * 继承自 RequestParams 以确保与 ApiContract 参数兼容
83
+ * 生成请求参数的返回值
77
84
  */
78
85
  interface GenerateParamsResp extends RequestParams {
79
86
  seen_ids?: string;
@@ -91,145 +98,91 @@ interface GenerateParamsType {
91
98
  readonly type: FetchType;
92
99
  }
93
100
  /**
94
- * API 响应结构
95
- */
96
- interface ApiResponse<R = ResultType> {
97
- readonly result: R;
98
- readonly extra?: KeyMap;
99
- readonly total?: number;
100
- readonly no_more?: boolean;
101
- }
102
- /**
103
- * 获取数据后的回调函数
101
+ * 回调函数类型
104
102
  */
105
- type FetchResultCallback<P, R> = (_obj: {
106
- params: P;
107
- data: ApiResponse<R>;
103
+ type FetchResultCallback<TParams, TResponse> = (obj: {
104
+ params: TParams;
105
+ data: BaseApiResponse<TResponse>;
108
106
  refresh: boolean;
109
107
  }) => void;
110
- interface CommonParams<P = RequestParams, R = ResultType> {
111
- readonly func: ApiContract<P, R>;
112
- readonly type?: FetchType;
113
- readonly query?: P;
114
- readonly uniqueKey?: string;
115
- readonly callback?: FetchResultCallback<P, R>;
116
- }
117
- interface BaseFetchConfig<P = RequestParams, R = ResultType> extends CommonParams<P, R> {
118
- readonly getter: FieldGetter;
119
- readonly setter: FieldSetter;
120
- }
121
- /**
122
- * 初始化状态的参数对外接口
123
- */
124
- interface InitStateParams<P = RequestParams, R = ResultType> {
125
- readonly func: ApiContract<P, R>;
126
- readonly query?: P;
127
- readonly opts?: Partial<DefaultField>;
128
- }
129
108
  /**
130
- * 初始化状态的参数(内部)
109
+ * 通用 Action 参数约束
131
110
  */
132
- interface InitStateType<P = RequestParams, R = ResultType> extends InitStateParams<P, R> {
133
- readonly getter: FieldGetter;
134
- readonly setter: FieldSetter;
111
+ interface ActionParams<TParams extends RequestParams, TResponse> {
112
+ getter: FieldGetter;
113
+ setter: FieldSetter;
114
+ func: ApiContract<TParams, TResponse>;
115
+ query?: TParams;
135
116
  }
136
117
  /**
137
- * 初始化数据的参数对外接口
138
- */
139
- type InitDataParams<P = RequestParams, R = ResultType> = CommonParams<P, R>;
140
- /**
141
- * 初始化数据的参数(内部)
142
- */
143
- type InitDataType<P = RequestParams, R = ResultType> = BaseFetchConfig<P, R>;
144
- /**
145
- * 加载更多的参数对外接口
118
+ * initData 参数
146
119
  */
147
- interface LoadMoreParams<P = RequestParams, R = ResultType> extends CommonParams<P, R> {
148
- readonly errorRetry?: boolean;
120
+ interface InitDataType<TParams extends RequestParams, TResponse> extends ActionParams<TParams, TResponse> {
121
+ callback?: FetchResultCallback<TParams, TResponse>;
149
122
  }
150
123
  /**
151
- * 加载更多的参数(内部)
124
+ * initState 参数
152
125
  */
153
- interface LoadMoreType<P = RequestParams, R = ResultType> extends BaseFetchConfig<P, R> {
154
- readonly errorRetry?: boolean;
126
+ interface InitStateType<TParams extends RequestParams, TResponse> extends ActionParams<TParams, TResponse> {
127
+ opts?: Partial<DefaultField<TResponse>>;
155
128
  }
156
129
  /**
157
- * 更新状态的参数(内部使用)
130
+ * loadMore 参数
158
131
  */
159
- interface UpdateStateType<P = RequestParams, R = ResultType, T = KeyMap> {
160
- readonly getter: FieldGetter;
161
- readonly setter: FieldSetter;
162
- readonly func: ApiContract<P, R>;
163
- readonly query?: P;
164
- readonly method: string;
165
- readonly value: T | T[] | KeyMap | KeyMap[];
166
- readonly id?: ObjectKey | ObjectKey[];
167
- readonly changeKey?: string;
132
+ interface LoadMoreType<TParams extends RequestParams, TResponse> extends ActionParams<TParams, TResponse> {
133
+ errorRetry?: boolean;
134
+ callback?: FetchResultCallback<TParams, TResponse>;
168
135
  }
169
136
  /**
170
- * 更新状态的参数(对外接口)
171
- */
172
- interface UpdateStateParams<P = RequestParams, R = ResultType, T = KeyMap> {
173
- readonly func: ApiContract<P, R>;
174
- readonly query?: P;
175
- readonly method: string;
176
- readonly value: T | T[] | KeyMap | KeyMap[];
177
- readonly id?: ObjectKey | ObjectKey[];
178
- readonly changeKey?: string;
179
- readonly uniqueKey?: string;
137
+ * updateState 参数
138
+ * 这里的 TValue 尝试对更新的值进行约束
139
+ */
140
+ interface UpdateStateType<TParams extends RequestParams, TResponse> {
141
+ getter: FieldGetter;
142
+ setter: FieldSetter;
143
+ func: ApiContract<TParams, TResponse>;
144
+ query?: TParams;
145
+ method: string;
146
+ value: any;
147
+ id?: ObjectKey | ObjectKey[];
148
+ changeKey?: string;
180
149
  }
181
- /**
182
- * 设置数据的参数
183
- */
184
150
  interface SetDataType {
185
151
  readonly getter: FieldGetter;
186
152
  readonly setter: FieldSetter;
187
- readonly data: ApiResponse;
153
+ readonly data: BaseApiResponse;
188
154
  readonly fieldName: string;
189
155
  readonly type: FetchType;
190
156
  readonly page: number;
191
157
  readonly insertBefore: boolean;
192
158
  }
193
- /**
194
- * 设置错误的参数
195
- */
196
159
  interface SetErrorType {
197
160
  readonly setter: FieldSetter;
198
161
  readonly fieldName: string;
199
162
  readonly error: Error | null;
200
163
  }
201
- interface ApiContract<P = RequestParams, R = ResultType> {
202
- (params: P): Promise<ApiResponse<R>>;
203
- readonly id: string;
204
- readonly type: FetchType;
205
- readonly uniqueKey: string;
206
- readonly paramsIgnore: string[];
207
- }
208
- declare function createApi<P = RequestParams, R = ResultType>(options: {
164
+
165
+ declare const initState: <P extends RequestParams, R>({ getter, setter, func, query, opts }: InitStateType<P, R>) => Promise<void>;
166
+ declare const initData: <P extends RequestParams, R>({ getter, setter, func, query, callback }: InitDataType<P, R>) => Promise<void>;
167
+ declare const loadMore: <P extends RequestParams, R>({ getter, setter, query, func, errorRetry, callback }: LoadMoreType<P, R>) => Promise<void>;
168
+ declare const updateState: <P extends RequestParams, R>({ getter, setter, func, query, method, id, value, changeKey }: UpdateStateType<P, R>) => Promise<unknown>;
169
+ declare const createApi: <TParams extends RequestParams, TResponse>(options: {
209
170
  id: string;
210
171
  type?: FetchType;
211
172
  uniqueKey?: string;
212
173
  paramsIgnore?: string[];
213
- fetcher: (params: P) => Promise<ApiResponse<R>>;
214
- }): ApiContract<P, R>;
215
-
216
- declare const initState: <P = RequestParams, R = ResultType>({ getter, setter, func, query, opts }: InitStateType<P, R>) => Promise<void>;
217
- declare const initData: <P = RequestParams, R = ResultType>({ getter, setter, func, query, callback }: InitDataType<P, R>) => Promise<void>;
218
- /**
219
- * 加载更多数据
220
- */
221
- declare const loadMore: <P = RequestParams, R = ResultType>({ getter, setter, query, func, errorRetry, callback }: LoadMoreType<P, R>) => Promise<void>;
222
- declare const updateState: <P = RequestParams, R = ResultType, T = KeyMap>({ getter, setter, func, query, method, id, value, changeKey }: UpdateStateType<P, R, T>) => Promise<unknown>;
174
+ fetcher: (params: TParams) => Promise<BaseApiResponse<TResponse>>;
175
+ }) => ApiContract<TParams, TResponse>;
176
+ declare const generateFieldName$1: <P extends RequestParams, R>({ func, query }: {
177
+ func: ApiContract<P, R>;
178
+ query?: P;
179
+ }) => string;
223
180
 
224
- declare const isArray: (data: unknown) => data is unknown[];
225
- /**
226
- * 检查是否为数组
227
- */
228
- declare const isResultArray: (data: ResultType) => data is ResultArrayType;
181
+ declare const isArray: (data: unknown) => data is any[];
229
182
  /**
230
183
  * 检查是否为对象结果
231
184
  */
232
- declare const isResultObject: (data: ResultType) => data is ResultObjectType;
185
+ declare const isResultObject: (data: unknown) => data is Record<string, any>;
233
186
  /**
234
187
  * 检查是否为 ObjectKey
235
188
  */
@@ -246,20 +199,24 @@ declare const isKeyMapArray: (value: unknown) => value is KeyMap[];
246
199
  * 检查是否为 ObjectKey 数组
247
200
  */
248
201
  declare const isObjectKeyArray: (value: unknown) => value is ObjectKey[];
202
+ declare const stableSerialize: (value: unknown) => string;
249
203
  declare const isObjectResult: (data: unknown) => data is Record<string, unknown>;
250
- declare const generateDefaultField: (opts?: Partial<DefaultField>) => DefaultField;
251
- declare const generateFieldName: <P = RequestParams, R = ResultType>({ func, query }: {
204
+ declare const generateDefaultField: <T = any>(opts?: Partial<DefaultField<T>>) => DefaultField<T>;
205
+ declare const generateFieldName: <P extends RequestParams, R>({ func, query }: {
252
206
  func: ApiContract<P, R>;
253
207
  query?: P;
254
208
  }) => string;
255
209
  declare const getObjectDeepValue: (field: unknown, keys: string | string[]) => unknown;
256
210
  declare const updateObjectDeepValue: (field: KeyMap, changeKey: string, value: unknown) => void;
257
- declare const searchValueByKey: (result: ResultArrayType | ResultObjectType, id: ObjectKey, key: string) => unknown;
258
- declare const computeMatchedItemIndex: (itemId: ObjectKey, fieldArr: ResultArrayType, changingKey: string) => number;
259
- declare const combineArrayData: (fieldArray: ResultArrayType, value: ResultArrayType | Record<ObjectKey, KeyMap>, changingKey: string) => void;
211
+ declare const searchValueByKey: (result: any[] | Record<string, any>, id: ObjectKey, key: string) => unknown;
212
+ declare const computeMatchedItemIndex: (itemId: ObjectKey, fieldArr: any[], changingKey: string) => number;
213
+ declare const combineArrayData: (fieldArray: any[], value: any[] | Record<ObjectKey, KeyMap>, changingKey: string) => void;
260
214
  declare const setReactivityField: (field: DefaultField, key: FieldKeys, value: unknown, type: FetchType, insertBefore: boolean) => void;
261
215
  declare const computeResultLength: (data: unknown) => number;
262
216
  declare const generateRequestParams: ({ field, uniqueKey, query, type }: GenerateParamsType) => GenerateParamsResp;
217
+ declare const toObjectKey: (id: ObjectKey | ObjectKey[] | undefined) => ObjectKey | undefined;
218
+ declare const getResultAsArray: (field: DefaultField) => any[] | null;
219
+ declare const updateArrayItem: (arr: any[], index: number, updater: (item: KeyMap) => KeyMap) => void;
263
220
 
264
221
  declare const utils_combineArrayData: typeof combineArrayData;
265
222
  declare const utils_computeMatchedItemIndex: typeof computeMatchedItemIndex;
@@ -268,19 +225,22 @@ declare const utils_generateDefaultField: typeof generateDefaultField;
268
225
  declare const utils_generateFieldName: typeof generateFieldName;
269
226
  declare const utils_generateRequestParams: typeof generateRequestParams;
270
227
  declare const utils_getObjectDeepValue: typeof getObjectDeepValue;
228
+ declare const utils_getResultAsArray: typeof getResultAsArray;
271
229
  declare const utils_isArray: typeof isArray;
272
230
  declare const utils_isKeyMap: typeof isKeyMap;
273
231
  declare const utils_isKeyMapArray: typeof isKeyMapArray;
274
232
  declare const utils_isObjectKey: typeof isObjectKey;
275
233
  declare const utils_isObjectKeyArray: typeof isObjectKeyArray;
276
234
  declare const utils_isObjectResult: typeof isObjectResult;
277
- declare const utils_isResultArray: typeof isResultArray;
278
235
  declare const utils_isResultObject: typeof isResultObject;
279
236
  declare const utils_searchValueByKey: typeof searchValueByKey;
280
237
  declare const utils_setReactivityField: typeof setReactivityField;
238
+ declare const utils_stableSerialize: typeof stableSerialize;
239
+ declare const utils_toObjectKey: typeof toObjectKey;
240
+ declare const utils_updateArrayItem: typeof updateArrayItem;
281
241
  declare const utils_updateObjectDeepValue: typeof updateObjectDeepValue;
282
242
  declare namespace utils {
283
- export { utils_combineArrayData as combineArrayData, utils_computeMatchedItemIndex as computeMatchedItemIndex, utils_computeResultLength as computeResultLength, utils_generateDefaultField as generateDefaultField, utils_generateFieldName as generateFieldName, utils_generateRequestParams as generateRequestParams, utils_getObjectDeepValue as getObjectDeepValue, utils_isArray as isArray, utils_isKeyMap as isKeyMap, utils_isKeyMapArray as isKeyMapArray, utils_isObjectKey as isObjectKey, utils_isObjectKeyArray as isObjectKeyArray, utils_isObjectResult as isObjectResult, utils_isResultArray as isResultArray, utils_isResultObject as isResultObject, utils_searchValueByKey as searchValueByKey, utils_setReactivityField as setReactivityField, utils_updateObjectDeepValue as updateObjectDeepValue };
243
+ export { utils_combineArrayData as combineArrayData, utils_computeMatchedItemIndex as computeMatchedItemIndex, utils_computeResultLength as computeResultLength, utils_generateDefaultField as generateDefaultField, utils_generateFieldName as generateFieldName, utils_generateRequestParams as generateRequestParams, utils_getObjectDeepValue as getObjectDeepValue, utils_getResultAsArray as getResultAsArray, utils_isArray as isArray, utils_isKeyMap as isKeyMap, utils_isKeyMapArray as isKeyMapArray, utils_isObjectKey as isObjectKey, utils_isObjectKeyArray as isObjectKeyArray, utils_isObjectResult as isObjectResult, utils_isResultObject as isResultObject, utils_searchValueByKey as searchValueByKey, utils_setReactivityField as setReactivityField, utils_stableSerialize as stableSerialize, utils_toObjectKey as toObjectKey, utils_updateArrayItem as updateArrayItem, utils_updateObjectDeepValue as updateObjectDeepValue };
284
244
  }
285
245
 
286
246
  declare const _default: {
@@ -315,4 +275,4 @@ declare const _default: {
315
275
  readonly DEFAULT_UNIQUE_KEY_NAME: "id";
316
276
  };
317
277
 
318
- export { type ApiContract, type ApiFunction, type ApiResponse, type DataSource, type DefaultField, _default as ENUM, type FetchResultCallback, type FetchType, type FieldGetter, type FieldKeys, type FieldSetter, type GenerateParamsResp, type GenerateParamsType, type InitDataParams, type InitDataType, type InitStateParams, type InitStateType, type KeyMap, type LoadMoreParams, type LoadMoreType, type ObjectKey, type RequestParams, type ResultArrayType, type ResultObjectType, type ResultType, type SetDataType, type SetErrorType, type SetterFuncParams, type UpdateStateParams, type UpdateStateType, createApi, initData, initState, loadMore, updateState, utils };
278
+ export { type ApiContract, type BaseApiResponse, type DefaultField, _default as ENUM, type FetchResultCallback, type FetchType, type FieldGetter, type FieldKeys, type FieldSetter, type GenerateParamsResp, type GenerateParamsType, type InitDataType, type InitStateType, type KeyMap, type LoadMoreType, type ObjectKey, type RequestParams, type SetDataType, type SetErrorType, type SetterFuncParams, type UpdateStateType, createApi, generateFieldName$1 as generateFieldName, initData, initState, loadMore, updateState, utils };