@flowlist/js-core 4.0.4-beta.0 → 4.0.6-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,102 @@ 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> {
108
+ interface SetDataType {
118
109
  readonly getter: FieldGetter;
119
110
  readonly setter: FieldSetter;
111
+ readonly data: BaseApiResponse;
112
+ readonly fieldName: string;
113
+ readonly type: FetchType;
114
+ readonly page: number;
115
+ readonly insertBefore: boolean;
120
116
  }
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
- /**
130
- * 初始化状态的参数(内部)
131
- */
132
- interface InitStateType<P = RequestParams, R = ResultType> extends InitStateParams<P, R> {
133
- readonly getter: FieldGetter;
117
+ interface SetErrorType {
134
118
  readonly setter: FieldSetter;
119
+ readonly fieldName: string;
120
+ readonly error: Error | null;
135
121
  }
136
122
  /**
137
- * 初始化数据的参数对外接口
123
+ * 初始化状态的外部参数
138
124
  */
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
- * 加载更多的参数对外接口
146
- */
147
- interface LoadMoreParams<P = RequestParams, R = ResultType> extends CommonParams<P, R> {
148
- readonly errorRetry?: boolean;
125
+ interface InitStateParams<P extends RequestParams = RequestParams, R = any> {
126
+ readonly func: ApiContract<P, R>;
127
+ readonly query?: P;
128
+ readonly opts?: Partial<DefaultField<R>>;
149
129
  }
150
130
  /**
151
- * 加载更多的参数(内部)
131
+ * 初始化数据的外部参数
152
132
  */
153
- interface LoadMoreType<P = RequestParams, R = ResultType> extends BaseFetchConfig<P, R> {
154
- readonly errorRetry?: boolean;
133
+ interface InitDataParams<P extends RequestParams = RequestParams, R = any> {
134
+ readonly func: ApiContract<P, R>;
135
+ readonly query?: P;
136
+ readonly callback?: FetchResultCallback<P, R>;
155
137
  }
156
138
  /**
157
- * 更新状态的参数(内部使用)
139
+ * 加载更多的外部参数
158
140
  */
159
- interface UpdateStateType<P = RequestParams, R = ResultType, T = KeyMap> {
160
- readonly getter: FieldGetter;
161
- readonly setter: FieldSetter;
141
+ interface LoadMoreParams<P extends RequestParams = RequestParams, R = any> {
162
142
  readonly func: ApiContract<P, R>;
163
143
  readonly query?: P;
164
- readonly method: string;
165
- readonly value: T | T[] | KeyMap | KeyMap[];
166
- readonly id?: ObjectKey | ObjectKey[];
167
- readonly changeKey?: string;
144
+ readonly errorRetry?: boolean;
145
+ readonly callback?: FetchResultCallback<P, R>;
168
146
  }
169
147
  /**
170
- * 更新状态的参数(对外接口)
148
+ * 更新状态的外部参数
171
149
  */
172
- interface UpdateStateParams<P = RequestParams, R = ResultType, T = KeyMap> {
150
+ interface UpdateStateParams<P extends RequestParams = RequestParams, R = any> {
173
151
  readonly func: ApiContract<P, R>;
174
152
  readonly query?: P;
175
153
  readonly method: string;
176
- readonly value: T | T[] | KeyMap | KeyMap[];
154
+ readonly value: any;
177
155
  readonly id?: ObjectKey | ObjectKey[];
178
156
  readonly changeKey?: string;
179
157
  readonly uniqueKey?: string;
180
158
  }
181
- /**
182
- * 设置数据的参数
183
- */
184
- interface SetDataType {
159
+ interface InitStateType<P extends RequestParams, R> extends InitStateParams<P, R> {
185
160
  readonly getter: FieldGetter;
186
161
  readonly setter: FieldSetter;
187
- readonly data: ApiResponse;
188
- readonly fieldName: string;
189
- readonly type: FetchType;
190
- readonly page: number;
191
- readonly insertBefore: boolean;
192
162
  }
193
- /**
194
- * 设置错误的参数
195
- */
196
- interface SetErrorType {
163
+ interface InitDataType<P extends RequestParams, R> extends InitDataParams<P, R> {
164
+ readonly getter: FieldGetter;
197
165
  readonly setter: FieldSetter;
198
- readonly fieldName: string;
199
- readonly error: Error | null;
200
166
  }
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[];
167
+ interface LoadMoreType<P extends RequestParams, R> extends LoadMoreParams<P, R> {
168
+ readonly getter: FieldGetter;
169
+ readonly setter: FieldSetter;
207
170
  }
208
- declare function createApi<P = RequestParams, R = ResultType>(options: {
171
+ interface UpdateStateType<P extends RequestParams, R> extends UpdateStateParams<P, R> {
172
+ readonly getter: FieldGetter;
173
+ readonly setter: FieldSetter;
174
+ }
175
+
176
+ declare const generateFieldName$1: <P extends RequestParams, R>({ func, query }: {
177
+ func: ApiContract<P, R>;
178
+ query?: P;
179
+ }) => string;
180
+ declare const createApi: <P extends RequestParams, R>(options: {
209
181
  id: string;
210
182
  type?: FetchType;
211
183
  uniqueKey?: string;
212
184
  paramsIgnore?: string[];
213
- fetcher: (params: P) => Promise<ApiResponse<R>>;
214
- }): ApiContract<P, R>;
185
+ fetcher: (params: P) => Promise<BaseApiResponse<R>>;
186
+ }) => ApiContract<P, R>;
187
+ declare const initState: <P extends RequestParams, R>({ getter, setter, func, query, opts }: InitStateType<P, R>) => Promise<void>;
188
+ declare const initData: <P extends RequestParams, R>({ getter, setter, func, query, callback }: InitDataType<P, R>) => Promise<void>;
189
+ declare const loadMore: <P extends RequestParams, R>({ getter, setter, query, func, errorRetry, callback }: LoadMoreType<P, R>) => Promise<void>;
190
+ declare const updateState: <P extends RequestParams, R>({ getter, setter, func, query, method, id, value, changeKey }: UpdateStateType<P, R>) => Promise<unknown>;
215
191
 
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>;
223
-
224
- declare const isArray: (data: unknown) => data is unknown[];
225
- /**
226
- * 检查是否为数组
227
- */
228
- declare const isResultArray: (data: ResultType) => data is ResultArrayType;
192
+ declare const isArray: (data: unknown) => data is any[];
229
193
  /**
230
194
  * 检查是否为对象结果
231
195
  */
232
- declare const isResultObject: (data: ResultType) => data is ResultObjectType;
196
+ declare const isResultObject: (data: unknown) => data is Record<string, any>;
233
197
  /**
234
198
  * 检查是否为 ObjectKey
235
199
  */
@@ -246,20 +210,24 @@ declare const isKeyMapArray: (value: unknown) => value is KeyMap[];
246
210
  * 检查是否为 ObjectKey 数组
247
211
  */
248
212
  declare const isObjectKeyArray: (value: unknown) => value is ObjectKey[];
213
+ declare const stableSerialize: (value: unknown) => string;
249
214
  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 }: {
215
+ declare const generateDefaultField: <T = any>(opts?: Partial<DefaultField<T>>) => DefaultField<T>;
216
+ declare const generateFieldName: <P extends RequestParams, R>({ func, query }: {
252
217
  func: ApiContract<P, R>;
253
218
  query?: P;
254
219
  }) => string;
255
220
  declare const getObjectDeepValue: (field: unknown, keys: string | string[]) => unknown;
256
221
  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;
222
+ declare const searchValueByKey: (result: any[] | Record<string, any>, id: ObjectKey, key: string) => unknown;
223
+ declare const computeMatchedItemIndex: (itemId: ObjectKey, fieldArr: any[], changingKey: string) => number;
224
+ declare const combineArrayData: (fieldArray: any[], value: any[] | Record<ObjectKey, KeyMap>, changingKey: string) => void;
260
225
  declare const setReactivityField: (field: DefaultField, key: FieldKeys, value: unknown, type: FetchType, insertBefore: boolean) => void;
261
226
  declare const computeResultLength: (data: unknown) => number;
262
227
  declare const generateRequestParams: ({ field, uniqueKey, query, type }: GenerateParamsType) => GenerateParamsResp;
228
+ declare const toObjectKey: (id: ObjectKey | ObjectKey[] | undefined) => ObjectKey | undefined;
229
+ declare const getResultAsArray: (field: DefaultField) => any[] | null;
230
+ declare const updateArrayItem: (arr: any[], index: number, updater: (item: KeyMap) => KeyMap) => void;
263
231
 
264
232
  declare const utils_combineArrayData: typeof combineArrayData;
265
233
  declare const utils_computeMatchedItemIndex: typeof computeMatchedItemIndex;
@@ -268,19 +236,22 @@ declare const utils_generateDefaultField: typeof generateDefaultField;
268
236
  declare const utils_generateFieldName: typeof generateFieldName;
269
237
  declare const utils_generateRequestParams: typeof generateRequestParams;
270
238
  declare const utils_getObjectDeepValue: typeof getObjectDeepValue;
239
+ declare const utils_getResultAsArray: typeof getResultAsArray;
271
240
  declare const utils_isArray: typeof isArray;
272
241
  declare const utils_isKeyMap: typeof isKeyMap;
273
242
  declare const utils_isKeyMapArray: typeof isKeyMapArray;
274
243
  declare const utils_isObjectKey: typeof isObjectKey;
275
244
  declare const utils_isObjectKeyArray: typeof isObjectKeyArray;
276
245
  declare const utils_isObjectResult: typeof isObjectResult;
277
- declare const utils_isResultArray: typeof isResultArray;
278
246
  declare const utils_isResultObject: typeof isResultObject;
279
247
  declare const utils_searchValueByKey: typeof searchValueByKey;
280
248
  declare const utils_setReactivityField: typeof setReactivityField;
249
+ declare const utils_stableSerialize: typeof stableSerialize;
250
+ declare const utils_toObjectKey: typeof toObjectKey;
251
+ declare const utils_updateArrayItem: typeof updateArrayItem;
281
252
  declare const utils_updateObjectDeepValue: typeof updateObjectDeepValue;
282
253
  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 };
254
+ 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
255
  }
285
256
 
286
257
  declare const _default: {
@@ -315,4 +286,4 @@ declare const _default: {
315
286
  readonly DEFAULT_UNIQUE_KEY_NAME: "id";
316
287
  };
317
288
 
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 };
289
+ 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 InitDataParams, type InitDataType, type InitStateParams, type InitStateType, type KeyMap, type LoadMoreParams, type LoadMoreType, type ObjectKey, type RequestParams, type SetDataType, type SetErrorType, type SetterFuncParams, type UpdateStateParams, type UpdateStateType, createApi, generateFieldName$1 as generateFieldName, initData, initState, loadMore, updateState, utils };