@flowlist/js-core 4.0.3-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,146 +98,91 @@ interface GenerateParamsType {
91
98
  readonly type: FetchType;
92
99
  }
93
100
  /**
94
- * API 响应结构
101
+ * 回调函数类型
95
102
  */
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
- * 获取数据后的回调函数
104
- */
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
- * 初始化数据的参数(内部)
118
+ * initData 参数
142
119
  */
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;
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 type: FetchType;
175
- readonly query?: P;
176
- readonly method: string;
177
- readonly value: T | T[] | KeyMap | KeyMap[];
178
- readonly id?: ObjectKey | ObjectKey[];
179
- readonly changeKey?: string;
180
- 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;
181
149
  }
182
- /**
183
- * 设置数据的参数
184
- */
185
150
  interface SetDataType {
186
151
  readonly getter: FieldGetter;
187
152
  readonly setter: FieldSetter;
188
- readonly data: ApiResponse;
153
+ readonly data: BaseApiResponse;
189
154
  readonly fieldName: string;
190
155
  readonly type: FetchType;
191
156
  readonly page: number;
192
157
  readonly insertBefore: boolean;
193
158
  }
194
- /**
195
- * 设置错误的参数
196
- */
197
159
  interface SetErrorType {
198
160
  readonly setter: FieldSetter;
199
161
  readonly fieldName: string;
200
162
  readonly error: Error | null;
201
163
  }
202
- interface ApiContract<P = RequestParams, R = ResultType> {
203
- (params: P): Promise<ApiResponse<R>>;
204
- readonly id: string;
205
- readonly type: FetchType;
206
- readonly uniqueKey: string;
207
- readonly paramsIgnore: string[];
208
- }
209
- 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: {
210
170
  id: string;
211
171
  type?: FetchType;
212
172
  uniqueKey?: string;
213
173
  paramsIgnore?: string[];
214
- fetcher: (params: P) => Promise<ApiResponse<R>>;
215
- }): ApiContract<P, R>;
216
-
217
- declare const initState: <P = RequestParams, R = ResultType>({ getter, setter, func, query, opts }: InitStateType<P, R>) => Promise<void>;
218
- declare const initData: <P = RequestParams, R = ResultType>({ getter, setter, func, query, callback }: InitDataType<P, R>) => Promise<void>;
219
- /**
220
- * 加载更多数据
221
- */
222
- declare const loadMore: <P = RequestParams, R = ResultType>({ getter, setter, query, func, errorRetry, callback }: LoadMoreType<P, R>) => Promise<void>;
223
- 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;
224
180
 
225
- declare const isArray: (data: unknown) => data is unknown[];
226
- /**
227
- * 检查是否为数组
228
- */
229
- declare const isResultArray: (data: ResultType) => data is ResultArrayType;
181
+ declare const isArray: (data: unknown) => data is any[];
230
182
  /**
231
183
  * 检查是否为对象结果
232
184
  */
233
- declare const isResultObject: (data: ResultType) => data is ResultObjectType;
185
+ declare const isResultObject: (data: unknown) => data is Record<string, any>;
234
186
  /**
235
187
  * 检查是否为 ObjectKey
236
188
  */
@@ -247,20 +199,24 @@ declare const isKeyMapArray: (value: unknown) => value is KeyMap[];
247
199
  * 检查是否为 ObjectKey 数组
248
200
  */
249
201
  declare const isObjectKeyArray: (value: unknown) => value is ObjectKey[];
202
+ declare const stableSerialize: (value: unknown) => string;
250
203
  declare const isObjectResult: (data: unknown) => data is Record<string, unknown>;
251
- declare const generateDefaultField: (opts?: Partial<DefaultField>) => DefaultField;
252
- 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 }: {
253
206
  func: ApiContract<P, R>;
254
207
  query?: P;
255
208
  }) => string;
256
209
  declare const getObjectDeepValue: (field: unknown, keys: string | string[]) => unknown;
257
210
  declare const updateObjectDeepValue: (field: KeyMap, changeKey: string, value: unknown) => void;
258
- declare const searchValueByKey: (result: ResultArrayType | ResultObjectType, id: ObjectKey, key: string) => unknown;
259
- declare const computeMatchedItemIndex: (itemId: ObjectKey, fieldArr: ResultArrayType, changingKey: string) => number;
260
- 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;
261
214
  declare const setReactivityField: (field: DefaultField, key: FieldKeys, value: unknown, type: FetchType, insertBefore: boolean) => void;
262
215
  declare const computeResultLength: (data: unknown) => number;
263
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;
264
220
 
265
221
  declare const utils_combineArrayData: typeof combineArrayData;
266
222
  declare const utils_computeMatchedItemIndex: typeof computeMatchedItemIndex;
@@ -269,19 +225,22 @@ declare const utils_generateDefaultField: typeof generateDefaultField;
269
225
  declare const utils_generateFieldName: typeof generateFieldName;
270
226
  declare const utils_generateRequestParams: typeof generateRequestParams;
271
227
  declare const utils_getObjectDeepValue: typeof getObjectDeepValue;
228
+ declare const utils_getResultAsArray: typeof getResultAsArray;
272
229
  declare const utils_isArray: typeof isArray;
273
230
  declare const utils_isKeyMap: typeof isKeyMap;
274
231
  declare const utils_isKeyMapArray: typeof isKeyMapArray;
275
232
  declare const utils_isObjectKey: typeof isObjectKey;
276
233
  declare const utils_isObjectKeyArray: typeof isObjectKeyArray;
277
234
  declare const utils_isObjectResult: typeof isObjectResult;
278
- declare const utils_isResultArray: typeof isResultArray;
279
235
  declare const utils_isResultObject: typeof isResultObject;
280
236
  declare const utils_searchValueByKey: typeof searchValueByKey;
281
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;
282
241
  declare const utils_updateObjectDeepValue: typeof updateObjectDeepValue;
283
242
  declare namespace utils {
284
- 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 };
285
244
  }
286
245
 
287
246
  declare const _default: {
@@ -316,4 +275,4 @@ declare const _default: {
316
275
  readonly DEFAULT_UNIQUE_KEY_NAME: "id";
317
276
  };
318
277
 
319
- 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 };