@recats/cdeebee 3.0.0-beta.8 → 3.0.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.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { AsyncThunk } from '@reduxjs/toolkit';
2
2
  import { AsyncThunkConfig } from '@reduxjs/toolkit';
3
+ import { Draft } from '@reduxjs/toolkit';
4
+ import { PayloadAction } from '@reduxjs/toolkit';
3
5
  import { Slice } from '@reduxjs/toolkit';
4
6
  import { SliceSelectors } from '@reduxjs/toolkit';
5
- import { WritableDraft } from '@reduxjs/toolkit';
6
7
 
7
8
  declare type Append<P extends readonly (string | number)[], K extends string | number> = [...P, K];
8
9
 
@@ -10,22 +11,22 @@ declare type ArrayElement<T> = T extends readonly (infer U)[] ? U : T extends (i
10
11
 
11
12
  export declare function batchingUpdate<T extends Record<string, unknown>>(state: T, valueList: CdeebeeValueList<T>): void;
12
13
 
13
- declare interface CdeebeeActiveRequest {
14
+ export declare interface CdeebeeActiveRequest {
14
15
  api: string;
15
16
  requestId: string;
16
17
  }
17
18
 
18
- declare interface CdeebeeHistoryState {
19
+ export declare interface CdeebeeHistoryState {
19
20
  requestId: string;
20
21
  api: string;
21
22
  request: unknown;
22
23
  }
23
24
 
24
- declare type CdeebeeListStrategy<T> = Record<keyof T, CdeebeeStrategy>;
25
+ export declare type CdeebeeListStrategy<T> = Record<keyof T, CdeebeeStrategy>;
25
26
 
26
27
  export declare type CdeebeeModule = 'history' | 'listener' | 'storage' | 'cancelation' | 'queryQueue';
27
28
 
28
- export declare interface CdeebeeRequestOptions<T> extends Partial<Pick<CdeebeeSettings<T>, 'fileKey' | 'bodyKey' | 'normalize' | 'listStrategy'>> {
29
+ export declare interface CdeebeeRequestOptions<T> extends Partial<Pick<CdeebeeSettings<T>, 'fileKey' | 'bodyKey' | 'normalize'>> {
29
30
  api: string;
30
31
  files?: File[];
31
32
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
@@ -34,22 +35,26 @@ export declare interface CdeebeeRequestOptions<T> extends Partial<Pick<CdeebeeSe
34
35
  onResult?: (response: T) => void;
35
36
  ignore?: boolean;
36
37
  responseType?: 'json' | 'text' | 'blob';
38
+ listStrategy?: Partial<CdeebeeListStrategy<T>>;
39
+ historyClear?: boolean;
37
40
  }
38
41
 
39
42
  declare interface CdeebeeRequestState {
40
43
  active: CdeebeeActiveRequest[];
41
44
  errors: Record<string, CdeebeeHistoryState[]>;
42
45
  done: Record<string, CdeebeeHistoryState[]>;
46
+ lastResultIdList: Record<string, Record<string, string[]>>;
43
47
  }
44
48
 
45
49
  declare interface CdeebeeSettings<T> {
46
50
  modules: CdeebeeModule[];
47
51
  fileKey: string;
48
52
  bodyKey: string;
49
- mergeWithData: unknown;
50
- mergeWithHeaders: unknown;
53
+ mergeWithData: Record<string, unknown> | (() => Record<string, unknown>);
54
+ mergeWithHeaders: Record<string, string> | (() => Record<string, string>);
51
55
  listStrategy?: CdeebeeListStrategy<T>;
52
- normalize?: <T>(storage: CdeebeeState<T>, result: T, strategyList: CdeebeeListStrategy<T>) => T;
56
+ normalize?: (storage: CdeebeeState<T>, result: unknown, strategyList: CdeebeeListStrategy<T>) => Record<string, unknown>;
57
+ maxHistorySize?: number;
53
58
  }
54
59
 
55
60
  export declare interface CdeebeeState<T> {
@@ -58,7 +63,7 @@ export declare interface CdeebeeState<T> {
58
63
  request: CdeebeeRequestState;
59
64
  }
60
65
 
61
- declare type CdeebeeStrategy = 'merge' | 'replace';
66
+ declare type CdeebeeStrategy = 'merge' | 'replace' | 'skip';
62
67
 
63
68
  declare type CdeebeeValueItem<T> = NonEmptyPaths<T> extends infer P ? P extends readonly (string | number)[] ? {
64
69
  key: P;
@@ -67,12 +72,118 @@ declare type CdeebeeValueItem<T> = NonEmptyPaths<T> extends infer P ? P extends
67
72
 
68
73
  export declare type CdeebeeValueList<T> = ReadonlyArray<CdeebeeValueItem<T>>;
69
74
 
75
+ /**
76
+ * Generic hook factory that creates a selector hook for cdeebee state.
77
+ * This allows the hooks to work with any Redux root state structure.
78
+ *
79
+ * @template RootState - The shape of the Redux root state
80
+ * @template Storage - The shape of the cdeebee storage
81
+ * @param selectCdeebee - Function to select the cdeebee slice from root state
82
+ * @returns An object containing all cdeebee hooks
83
+ */
84
+ export declare function createCdeebeeHooks<RootState, Storage>(selectCdeebee: (state: RootState) => CdeebeeState<Storage>): {
85
+ useLoading: (apiList: string[]) => boolean;
86
+ useRequestHistory: (api: string) => CdeebeeHistoryState[];
87
+ useRequestErrors: (api: string) => CdeebeeHistoryState[];
88
+ useStorageList: <K extends keyof Storage>(listName: K) => Storage[K];
89
+ useStorage: () => Storage;
90
+ useIsLoading: () => boolean;
91
+ useLastResultIdList: <K extends keyof Storage>(api: string, listName: K) => string[];
92
+ };
93
+
94
+ export declare function defaultNormalize<T>(cdeebee: CdeebeeState<T>, response: IResponse, strategyList: CdeebeeListStrategy<T>): Record<string, ResponseValue>;
95
+
70
96
  export declare const factory: <T>(settings: CdeebeeSettings<T>, storage?: T) => Slice<CdeebeeState<T>, {
71
- set(state: WritableDraft<CdeebeeState<T>>, action: {
97
+ set(state: {
98
+ settings: {
99
+ modules: CdeebeeModule[];
100
+ fileKey: string;
101
+ bodyKey: string;
102
+ mergeWithData: (() => Record<string, unknown>) | {
103
+ [x: string]: unknown;
104
+ };
105
+ mergeWithHeaders: (() => Record<string, string>) | {
106
+ [x: string]: string;
107
+ };
108
+ listStrategy?: (CdeebeeListStrategy<T> | undefined extends infer V ? V extends object ? Draft<V> : V : never) | undefined;
109
+ normalize?: ((storage: CdeebeeState<T>, result: unknown, strategyList: CdeebeeListStrategy<T>) => Record<string, unknown>) | undefined;
110
+ maxHistorySize?: number | undefined;
111
+ };
112
+ storage: T extends infer V ? V extends object ? Draft<V> : V : never;
113
+ request: {
114
+ active: {
115
+ api: string;
116
+ requestId: string;
117
+ }[];
118
+ errors: {
119
+ [x: string]: {
120
+ requestId: string;
121
+ api: string;
122
+ request: unknown;
123
+ }[];
124
+ };
125
+ done: {
126
+ [x: string]: {
127
+ requestId: string;
128
+ api: string;
129
+ request: unknown;
130
+ }[];
131
+ };
132
+ lastResultIdList: {
133
+ [x: string]: {
134
+ [x: string]: string[];
135
+ };
136
+ };
137
+ };
138
+ }, action: {
72
139
  payload: CdeebeeValueList<T>;
73
140
  }): void;
141
+ historyClear(state: {
142
+ settings: {
143
+ modules: CdeebeeModule[];
144
+ fileKey: string;
145
+ bodyKey: string;
146
+ mergeWithData: (() => Record<string, unknown>) | {
147
+ [x: string]: unknown;
148
+ };
149
+ mergeWithHeaders: (() => Record<string, string>) | {
150
+ [x: string]: string;
151
+ };
152
+ listStrategy?: (CdeebeeListStrategy<T> | undefined extends infer V ? V extends object ? Draft<V> : V : never) | undefined;
153
+ normalize?: ((storage: CdeebeeState<T>, result: unknown, strategyList: CdeebeeListStrategy<T>) => Record<string, unknown>) | undefined;
154
+ maxHistorySize?: number | undefined;
155
+ };
156
+ storage: T extends infer V ? V extends object ? Draft<V> : V : never;
157
+ request: {
158
+ active: {
159
+ api: string;
160
+ requestId: string;
161
+ }[];
162
+ errors: {
163
+ [x: string]: {
164
+ requestId: string;
165
+ api: string;
166
+ request: unknown;
167
+ }[];
168
+ };
169
+ done: {
170
+ [x: string]: {
171
+ requestId: string;
172
+ api: string;
173
+ request: unknown;
174
+ }[];
175
+ };
176
+ lastResultIdList: {
177
+ [x: string]: {
178
+ [x: string]: string[];
179
+ };
180
+ };
181
+ };
182
+ }, action: PayloadAction<string | undefined>): void;
74
183
  }, "cdeebee", "cdeebee", SliceSelectors<CdeebeeState<T>>>;
75
184
 
185
+ declare type IResponse = Record<string, ResponseValue>;
186
+
76
187
  declare type IsArray<T> = T extends readonly unknown[] ? true : T extends unknown[] ? true : false;
77
188
 
78
189
  declare type KeyOf<T> = Extract<keyof T, string | number>;
@@ -89,6 +200,81 @@ startedAt: string;
89
200
  endedAt: string;
90
201
  }, CdeebeeRequestOptions<unknown>, AsyncThunkConfig>;
91
202
 
203
+ declare type ResponseValue = Record<string, unknown>;
204
+
205
+ /**
206
+ * Standalone hook that can be used without createCdeebeeHooks.
207
+ * Assumes the cdeebee slice is at state.cdeebee.
208
+ *
209
+ * @returns true if any request is active
210
+ */
211
+ export declare function useIsLoading<Storage = unknown>(): boolean;
212
+
213
+ /**
214
+ * Standalone hook that can be used without createCdeebeeHooks.
215
+ * Assumes the cdeebee slice is at state.cdeebee.
216
+ *
217
+ * Get the list of IDs returned by the last successful request to an API for a specific list.
218
+ * Useful for filtering storage data to show only results from a specific request.
219
+ *
220
+ * @param api - The API endpoint
221
+ * @param listName - The name of the list in storage (typed from Storage)
222
+ * @returns Array of primary key IDs from the last response for that list
223
+ *
224
+ * @example
225
+ * const productList = useStorageList('productList');
226
+ * const lastIDList = useLastResultIdList('/api/search', 'productList');
227
+ * const displayResults = lastIDList.map(id => productList[id]).filter(Boolean);
228
+ */
229
+ export declare function useLastResultIdList<Storage, K extends keyof Storage>(api: string, listName: K): string[];
230
+
231
+ /**
232
+ * Standalone hook that can be used without createCdeebeeHooks.
233
+ * Assumes the cdeebee slice is at state.cdeebee.
234
+ *
235
+ * @param apiList - Array of API endpoints to check
236
+ * @returns true if any of the APIs are currently active/loading
237
+ *
238
+ * @example
239
+ * const isLoading = useLoading(['/api/forums', '/api/threads']);
240
+ */
241
+ export declare function useLoading<Storage = unknown>(apiList: string[]): boolean;
242
+
243
+ /**
244
+ * Standalone hook that can be used without createCdeebeeHooks.
245
+ * Assumes the cdeebee slice is at state.cdeebee.
246
+ *
247
+ * @param api - The API endpoint
248
+ * @returns Array of error history entries
249
+ */
250
+ export declare function useRequestErrors<Storage = unknown>(api: string): CdeebeeHistoryState[];
251
+
252
+ /**
253
+ * Standalone hook that can be used without createCdeebeeHooks.
254
+ * Assumes the cdeebee slice is at state.cdeebee.
255
+ *
256
+ * @param api - The API endpoint
257
+ * @returns Array of successful request history entries
258
+ */
259
+ export declare function useRequestHistory<Storage = unknown>(api: string): CdeebeeHistoryState[];
260
+
261
+ /**
262
+ * Standalone hook that can be used without createCdeebeeHooks.
263
+ * Assumes the cdeebee slice is at state.cdeebee.
264
+ *
265
+ * @returns The complete storage object
266
+ */
267
+ export declare function useStorage<Storage>(): Storage;
268
+
269
+ /**
270
+ * Standalone hook that can be used without createCdeebeeHooks.
271
+ * Assumes the cdeebee slice is at state.cdeebee.
272
+ *
273
+ * @param listName - The name of the list in storage
274
+ * @returns The list data
275
+ */
276
+ export declare function useStorageList<Storage, K extends keyof Storage>(listName: K): Storage[K];
277
+
92
278
  declare type ValueAtPath<T, P extends readonly (string | number)[]> = P extends [] ? T : P extends readonly [infer K, ...infer R] ? K extends keyof T ? ValueAtPath<T[K], Extract<R, readonly (string | number)[]>> : T extends readonly (infer U)[] | (infer U)[] ? K extends number | `${number}` ? ValueAtPath<U, Extract<R, readonly (string | number)[]>> : never : never : never;
93
279
 
94
280
  export { }