@ngrx-traits/core 19.2.2 → 20.0.0-beta.1

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/index.d.ts CHANGED
@@ -1,6 +1,618 @@
1
- export * from './create-entity-feature';
2
- export * from './model';
3
- export * from './local-store';
4
- export * from './trait-effect';
5
- export * from './util';
6
- export * from './cache';
1
+ import * as i0 from '@angular/core';
2
+ import { Type as Type$1, Injector, OnDestroy, InjectionToken } from '@angular/core';
3
+ import * as _ngrx_store from '@ngrx/store';
4
+ import { Store, ActionCreator, Action, Selector, SelectorWithProps, MemoizedSelector, ActionType, ReducerTypes } from '@ngrx/store';
5
+ import { OnIdentifyEffects, OnRunEffects, Actions } from '@ngrx/effects';
6
+ import { Observable } from 'rxjs';
7
+
8
+ declare class TraitEffect implements OnIdentifyEffects, OnRunEffects {
9
+ private name;
10
+ componentId: string;
11
+ protected actions$: Actions<any>;
12
+ protected store: Store<any>;
13
+ ngrxOnIdentifyEffects(): string;
14
+ ngrxOnRunEffects(resolvedEffects$: Observable<any>): Observable<any>;
15
+ static ɵfac: i0.ɵɵFactoryDeclaration<TraitEffect, never>;
16
+ static ɵprov: i0.ɵɵInjectableDeclaration<TraitEffect>;
17
+ }
18
+ declare function getDestroyActionName(id: string): string;
19
+
20
+ type TraitActions = {
21
+ [key: string]: ActionCreator<string, (...args: any[]) => Action<string>>;
22
+ };
23
+ type TraitSelectors<State> = {
24
+ [key: string]: Selector<State, any> | SelectorWithProps<State, any, any> | MemoizedSelector<State, any>;
25
+ };
26
+ type TraitStateMutators<State> = {
27
+ [key: string]: <S extends State>(...arg: any[]) => S;
28
+ };
29
+ interface BaseFeatureTraits<State = any, A = any, S = any> {
30
+ actions: A;
31
+ selectors: S;
32
+ reducer: (state: State, action: ActionType<any>) => State;
33
+ effects: Type$1<any>[];
34
+ initialState: State;
35
+ }
36
+ type FeatureTraits<State, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>> = BaseFeatureTraits<State, A, FeatureSelectors<State, S>>;
37
+ type BaseEntityFeatureFactory<State, A, S> = (config: Config<State>) => BaseFeatureTraits<State, A, S>;
38
+ type EntityFeatureFactory<EntityName extends string | undefined, EntitiesName extends string = `${EntityName}s`, State = any, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>> = (config: Config<State>) => FeatureTraits<State, EntityName extends string ? ReplaceEntityNames<A, EntityName, EntitiesName> : A, EntityName extends string ? ReplaceEntityNames<S, EntityName, EntitiesName> : S>;
39
+ type Config<State, F extends MemoizedSelector<object, State> = MemoizedSelector<object, State>> = {
40
+ actionsGroupKey: string;
41
+ featureSelector: F | string;
42
+ };
43
+ type FeatureSelectors<State, S extends TraitSelectors<State>> = {
44
+ [key in keyof S]: MemoizedSelector<object, ReturnType<S[key]>, S[key]>;
45
+ };
46
+ type AllTraitConfigs = {
47
+ [id: string]: any;
48
+ };
49
+ type TraitActionsFactory<A extends TraitActions = TraitActions, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitActionsFactoryConfig<C>) => A;
50
+ type TraitActionsFactoryConfig<C extends AllTraitConfigs = AllTraitConfigs> = {
51
+ actionsGroupKey: string;
52
+ entityName: string;
53
+ entitiesName: string;
54
+ allConfigs: C;
55
+ };
56
+ type TraitSelectorsFactory<State = unknown, S extends TraitSelectors<State> = TraitSelectors<State>, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitSelectorsFactoryConfig<C>) => S;
57
+ type TraitSelectorsFactoryConfig<C extends AllTraitConfigs = AllTraitConfigs> = {
58
+ previousSelectors: TraitSelectors<unknown> | undefined;
59
+ allConfigs: C;
60
+ };
61
+ type TraitInitialStateFactory<State = unknown, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitInitialStateFactoryConfig<C>) => State;
62
+ type TraitInitialStateFactoryConfig<C extends AllTraitConfigs = AllTraitConfigs> = {
63
+ previousInitialState: any;
64
+ allConfigs: C;
65
+ };
66
+ type TraitStateMutatorsFactory<State = unknown, M extends TraitStateMutators<State> = TraitStateMutators<State>, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitStateMutatorsFactoryConfig<C>) => M;
67
+ type TraitStateMutatorsFactoryConfig<C extends AllTraitConfigs = AllTraitConfigs> = {
68
+ allSelectors: TraitSelectors<any>;
69
+ previousMutators: TraitStateMutators<unknown> | undefined;
70
+ allConfigs: C;
71
+ };
72
+ type TraitReducerFactory<State = unknown, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>, M extends TraitStateMutators<State> = TraitStateMutators<State>, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitReducerFactoryConfig<State, A, S, M, C>) => (initialState: State, action: Action) => State;
73
+ type TraitReducerFactoryConfig<State = unknown, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>, M extends TraitStateMutators<State> = TraitStateMutators<State>, C extends AllTraitConfigs = AllTraitConfigs> = {
74
+ initialState: State;
75
+ allActions: A;
76
+ allSelectors: S;
77
+ allMutators: M;
78
+ allConfigs: C;
79
+ };
80
+ type TraitEffectsFactory<State = unknown, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>, C extends AllTraitConfigs = AllTraitConfigs> = (options: TraitEffectsFactoryConfig<State, A, S, C>) => Type$1<TraitEffect>[];
81
+ type TraitEffectsFactoryConfig<State = unknown, A extends TraitActions = TraitActions, S extends TraitSelectors<State> = TraitSelectors<State>, C extends AllTraitConfigs = AllTraitConfigs> = {
82
+ allActions: A;
83
+ allSelectors: S;
84
+ allConfigs: C;
85
+ };
86
+ type KeyedConfig<KEY extends string, C> = Record<string, any> & {
87
+ [K in KEY]?: C;
88
+ };
89
+ interface TraitFactory<State = any, A extends TraitActions = any, S extends TraitSelectors<State> = any, M extends TraitStateMutators<State> = any, KEY extends string = string, C = any, KC extends AllTraitConfigs = KeyedConfig<KEY, C>> {
90
+ key: KEY;
91
+ config?: C;
92
+ depends?: string[];
93
+ actions: TraitActionsFactory<A, KC>;
94
+ selectors: TraitSelectorsFactory<State, S, KC>;
95
+ initialState: TraitInitialStateFactory<State, KC>;
96
+ mutators: TraitStateMutatorsFactory<State, M, KC>;
97
+ reducer?: TraitReducerFactory<State, A, S, M, KC>;
98
+ effects?: TraitEffectsFactory<State, A, S, KC>;
99
+ }
100
+ type ExtractArrayElementTypes<X> = X extends ReadonlyArray<infer T> ? T : never;
101
+ type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
102
+ type ExtractStateType<T> = T extends TraitFactory<infer State> ? State : T extends ReadonlyArray<TraitFactory> ? UnionToIntersection<ExtractStateType<ExtractArrayElementTypes<T>>> : T extends BaseFeatureTraits<infer State> ? State : never;
103
+ type ExtractActionsType<T> = T extends TraitFactory<any, infer A> ? A : T extends ReadonlyArray<TraitFactory> ? UnionToIntersection<ExtractActionsType<ExtractArrayElementTypes<T>>> : T extends BaseFeatureTraits<any, infer A> ? A : never;
104
+ type ExtractSelectorsType<T> = T extends TraitFactory<any, any, infer S> ? S : T extends ReadonlyArray<TraitFactory> ? UnionToIntersection<ExtractSelectorsType<ExtractArrayElementTypes<T>>> : T extends BaseFeatureTraits<any, any, infer S> ? S : never;
105
+ type ExtractMutatorsType<T> = T extends TraitFactory<any, any, any, infer M> ? M : T extends ReadonlyArray<TraitFactory> ? UnionToIntersection<ExtractMutatorsType<ExtractArrayElementTypes<T>>> : never;
106
+ type ExtractKeyedConfigType<T> = T extends TraitFactory<any, any, any, any, infer KEY, infer C> ? KeyedConfig<KEY, C> : T extends ReadonlyArray<TraitFactory> ? UnionToIntersection<ExtractKeyedConfigType<ExtractArrayElementTypes<T>>> : never;
107
+ type PrefixProps<T, P extends string> = {
108
+ [K in keyof T as `${P}${Capitalize<string & K>}`]: T[K];
109
+ };
110
+ type PostfixProps<T, P extends string> = {
111
+ [K in keyof T as `${Uncapitalize<string & K>}${Capitalize<P>}`]: T[K];
112
+ };
113
+ type Replace<Target extends string, FindKey extends string, FindKey2 extends string, ReplaceKey extends string, ReplaceKey2 extends string> = Target extends `${infer Prefix}${FindKey}${infer Postfix}` ? `${Prefix}${Capitalize<ReplaceKey>}${Postfix}` : Target extends `${infer Prefix}${FindKey2}${infer Postfix}` ? `${Prefix}${Capitalize<ReplaceKey2>}${Postfix}` : Target;
114
+ type ReplaceProps<Target, FindKey extends string, FindKey2 extends string, ReplaceKey extends string, ReplaceKey2 extends string> = {
115
+ [Prop in keyof Target as Replace<string & Prop, FindKey, FindKey2, ReplaceKey, ReplaceKey2>]: Target[Prop];
116
+ };
117
+ type ReplaceEntityNames<T, EntityName extends string, EntitiesName extends string> = ReplaceProps<T, 'Entities', 'Entity', EntitiesName, EntityName>;
118
+
119
+ /**
120
+ * Builds traitFactory and registers effects and reducers with
121
+ * a generated component id, returns build actions and selectors
122
+ * and a destroy method that will unergister the effects and reducers
123
+ * when called, and a addEffect which can be use to register extra effects
124
+ *
125
+ * Used inside TraitsLocalStore, can be used to create your
126
+ * own Component Service without extending TraitsLocalStore
127
+ * @param injector
128
+ * @param componentName
129
+ * @param traitFactory
130
+ */
131
+ declare function buildLocalTraits<State, F extends BaseEntityFeatureFactory<any, any, any>>(injector: Injector, componentName: string, traitFactory: F): {
132
+ destroy: () => void;
133
+ actions: any;
134
+ selectors: any;
135
+ addEffects(localEffect: TraitEffect): void;
136
+ };
137
+ interface Type<T> extends Function {
138
+ new (...args: any[]): T;
139
+ }
140
+ interface LocalTraitsConfig<F extends BaseEntityFeatureFactory<any, any, any>> {
141
+ componentName: string;
142
+ traitsFactory: F;
143
+ }
144
+ /**
145
+ * Class used to create local traits service, receives a trait factory, which will be
146
+ * built and its reducers and effect register using a dynamic id when the service is built
147
+ * and get destroyed when the onDestroy lifecycle method of the service is called, if the service
148
+ * has effects this.traits.addEffects(this) should be call in the constructor
149
+ *
150
+ * @example
151
+ * const productFeatureFactory = createEntityFeatureFactory(
152
+ * { entityName: 'product' },
153
+ * addLoadEntitiesTrait<Product>(),
154
+ * addSelectEntityTrait<Product>(),
155
+ * );
156
+ *
157
+ * Injectable()
158
+ * export class ProductsLocalTraits extends TraitsLocalStore<
159
+ * typeof productFeatureFactory
160
+ * > {
161
+ * loadProducts$ = createEffect(() =>
162
+ * this.actions$.pipe(
163
+ * ofType(this.localActions.loadProducts),
164
+ * switchMap(() =>
165
+ * //call your service to get the products data
166
+ * this.productService.getProducts().pipe(
167
+ * map((res) =>
168
+ * this.localActions.loadProductsSuccess({ entities: res.resultList })
169
+ * ),
170
+ * catchError(() => of(this.localActions.loadProductsFail()))
171
+ * )
172
+ * )
173
+ * )
174
+ * );
175
+ *
176
+ * constructor(injector: Injector, private productService: ProductService) {
177
+ * super(injector);
178
+ * this.traits.addEffects(this); // IMPORTANT! add this line if the service has effects
179
+ * }
180
+ *
181
+ * setup(): LocalTraitsConfig<typeof productFeatureFactory> {
182
+ * return {
183
+ * componentName: 'ProductsPickerComponent',
184
+ * traitsFactory: productFeatureFactory,
185
+ * };
186
+ * }
187
+ * }
188
+ *
189
+ * // use in component later
190
+ *
191
+ * Component({
192
+ * selector: 'some-component',
193
+ * template: `<div> some content</div> `,
194
+ * providers: [ProductsLocalTraits],
195
+ * changeDetection: ChangeDetectionStrategy.OnPush,
196
+ * })
197
+ * export class ProductSelectDialogComponent implements OnInit {
198
+ * constructor(private store: Store, private traits: ProductsLocalTraits) {}
199
+ *
200
+ * ngOnInit() {
201
+ * this.store.dispatch(this.traits.localActions.loadProducts());
202
+ * }
203
+ * }
204
+ */
205
+ declare abstract class TraitsLocalStore<F extends BaseEntityFeatureFactory<any, any, any>> extends TraitEffect implements OnDestroy {
206
+ traits: {
207
+ actions: ReturnType<F>['actions'];
208
+ selectors: ReturnType<F>['selectors'];
209
+ destroy: () => void;
210
+ addEffects: (localEffect: TraitEffect) => void;
211
+ };
212
+ localActions: ReturnType<F>['actions'];
213
+ localSelectors: ReturnType<F>['selectors'];
214
+ private injector;
215
+ constructor();
216
+ abstract setup(): LocalTraitsConfig<F>;
217
+ ngOnDestroy(): void;
218
+ static ɵfac: i0.ɵɵFactoryDeclaration<TraitsLocalStore<any>, never>;
219
+ static ɵprov: i0.ɵɵInjectableDeclaration<TraitsLocalStore<any>>;
220
+ }
221
+
222
+ /**
223
+ * @ignore
224
+ * @internal
225
+ */
226
+ declare const DISABLE_LOCAL_TRAIT_EFFECTS: InjectionToken<boolean>;
227
+
228
+ /**
229
+ * Creates a function that when execute will combine all the traits, and return a EntityFeatureFactory
230
+ * which combines all the traits actions, selectors , reducers and effects,
231
+ * the names param will replace any action and selector with the word Entity or Entities,
232
+ * with the corresponding entityName and entitiesName param (entityName+'s' if entitiesName is omitted).
233
+ * @param namesConfig - Optional Names for entities
234
+ * @param namesConfig.entityName - singular name for entity
235
+ * @param [namesConfig.entitiesName] - plural name for entities, defaults to entityName + 's'
236
+ * @param traits set of traits to be combined
237
+ *
238
+ * @example
239
+ *
240
+ * const featureFactory = createEntityFeatureFactory(
241
+ * { entityName: 'product' },
242
+ * addLoadEntitiesTrait<Product>(),
243
+ * addSelectEntityTrait<Product>(),
244
+ * addAsyncActionTrait({
245
+ * name: 'checkout',
246
+ * actionSuccessProps: props<{ orderId: string }>(),
247
+ * })
248
+ * );
249
+ *
250
+ * export const productsFeature = featureFactory({
251
+ * actionsGroupKey: '[Products]',
252
+ * featureSelector: 'products',
253
+ * });
254
+ */
255
+ declare function createEntityFeatureFactory<F extends readonly TraitFactory[], EntityName extends string, EntitiesName extends string = `${EntityName}s`>({ entityName, entitiesName, }: {
256
+ entityName: EntityName;
257
+ entitiesName?: EntitiesName;
258
+ }, ...traits: F): EntityFeatureFactory<EntityName, EntitiesName, ExtractStateType<F>, ExtractActionsType<F>, ExtractSelectorsType<F>>;
259
+ /**
260
+ * Creates a function that when execute will combine all the traits, and return a EntityFeatureFactory
261
+ * which combines all the traits actions, selectors , reducers and effects.
262
+ * @param traits set of traits to be combined
263
+ *
264
+ * @example
265
+ *
266
+ * const featureFactory = createEntityFeatureFactory(
267
+ * { entityName: 'product' },
268
+ * addLoadEntitiesTrait<Product>(),
269
+ * addSelectEntityTrait<Product>(),
270
+ * addAsyncActionTrait({
271
+ * name: 'checkout',
272
+ * actionSuccessProps: props<{ orderId: string }>(),
273
+ * })
274
+ * );
275
+ *
276
+ * export const productsFeature = featureFactory({
277
+ * actionsGroupKey: '[Products]',
278
+ * featureSelector: 'products',
279
+ * });
280
+ *
281
+ * productsFeature.actions.loadProducts();
282
+ */
283
+ declare function createEntityFeatureFactory<F extends readonly TraitFactory[]>(...traits: F): EntityFeatureFactory<'Entity', 'Entities', ExtractStateType<F>, ExtractActionsType<F>, ExtractSelectorsType<F>>;
284
+ /**
285
+ * Combine a map entityFeatureFactories into one,
286
+ * grouping the actions and selectors by the key of the respective entityFeatureFactory
287
+ * @param traitFactoriesMap
288
+ *
289
+ * @example
290
+ *
291
+ * const clientsFeatureFactory = createEntityFeatureFactory(
292
+ * { entityName: 'client', entitiesName: 'clients' },
293
+ * addLoadEntitiesTrait<Client>(),
294
+ * addCrudEntitiesTrait<Client>()
295
+ * );
296
+ *
297
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
298
+ * { entityName: 'productOrder' },
299
+ * addLoadEntitiesTrait<ProductOrder>(),
300
+ * addSelectEntitiesTrait<ProductOrder>()
301
+ * );
302
+ *
303
+ * const productFeatureFactory = createEntityFeatureFactory(
304
+ * { entityName: 'product' },
305
+ * addLoadEntitiesTrait<Product>(),
306
+ * addSelectEntitiesTrait<Product>()
307
+ * );
308
+ *
309
+ * const productCombinedFactory = combineEntityFeatures({
310
+ * products: productFeatureFactory,
311
+ * productOrders: productOrderFeatureFactory,
312
+ * clients: clientsFeatureFactory,
313
+ * });
314
+ *
315
+ * const combinedFeature = productCombinedFactory({
316
+ * actionsGroupKey: '[Combined]',
317
+ * featureSelector: 'combined',
318
+ * });
319
+ *
320
+ * combinedFeature.actions.client.loadClients();
321
+ * combinedFeature.actions.product.loadProducts();
322
+ */
323
+ declare function combineEntityFeatures<T extends {
324
+ [key: string]: EntityFeatureFactory<any, any>;
325
+ }, K extends keyof T, State extends {
326
+ [P in K]: ExtractStateType<ReturnType<T[P]>>;
327
+ }, A extends {
328
+ [P in K]: ExtractActionsType<ReturnType<T[P]>>;
329
+ }, S extends {
330
+ [P in K]: FeatureSelectors<State, ExtractSelectorsType<ReturnType<T[P]>>>;
331
+ }, R extends (config: Config<State>) => {
332
+ actions: A;
333
+ selectors: S;
334
+ reducer: (state: State, action: ActionType<any>) => State;
335
+ effects: Type<any>[];
336
+ initialState: State;
337
+ }>(traitFactoriesMap: T): R;
338
+ /**
339
+ * Mix a map entityFeatureFactories into one, different from combine the actions and selectors a mix, not group by key like in combine, the keys are still use
340
+ * internal in the reducers and selector to separate the state
341
+ * @param traitFactoriesMap
342
+ *
343
+ * @example
344
+ *
345
+ * const clientsFeatureFactory = createEntityFeatureFactory(
346
+ * { entityName: 'client', entitiesName: 'clients' },
347
+ * addLoadEntitiesTrait<Client>(),
348
+ * addCrudEntitiesTrait<Client>()
349
+ * );
350
+ *
351
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
352
+ * { entityName: 'productOrder' },
353
+ * addLoadEntitiesTrait<ProductOrder>(),
354
+ * addSelectEntitiesTrait<ProductOrder>()
355
+ * );
356
+ *
357
+ * const productFeatureFactory = createEntityFeatureFactory(
358
+ * { entityName: 'product' },
359
+ * addLoadEntitiesTrait<Product>(),
360
+ * addSelectEntitiesTrait<Product>()
361
+ * );
362
+ *
363
+ * const productMixedFactory = mixEntityFeatures({
364
+ * products: productFeatureFactory,
365
+ * productOrders: productOrderFeatureFactory,
366
+ * clients: clientsFeatureFactory,
367
+ * });
368
+ *
369
+ * const mixedFeature = productMixedFactory({
370
+ * actionsGroupKey: '[Mixed]',
371
+ * featureSelector: 'mixed',
372
+ * });
373
+ * mixedFeature.actions.loadClients();
374
+ * mixedFeature.actions.loadProducts();
375
+ *
376
+ */
377
+ declare function mixEntityFeatures<T extends {
378
+ [key: string]: EntityFeatureFactory<any, any>;
379
+ }, K extends keyof T, State extends {
380
+ [P in K]: ExtractStateType<ReturnType<T[P]>>;
381
+ }, A extends TraitActions & UnionToIntersection<ExtractActionsType<ReturnType<T[K]>>>, S extends TraitSelectors<any> & UnionToIntersection<FeatureSelectors<State, ExtractSelectorsType<ReturnType<T[K]>>>>, R extends EntityFeatureFactory<any, any, State, A, S>>(traitFactoriesMap: T): R;
382
+ /**
383
+ * Combines targetTraitFactory with the traitFactoriesMap using the keys as props for the targetTraitFactory state,
384
+ * and grouping the combined actions by key
385
+ * @param targetTraitFactory
386
+ * @param traitFactoriesMap
387
+ *
388
+ * @example
389
+ *
390
+ * const clientsFeatureFactory = createEntityFeatureFactory(
391
+ * { entityName: 'client', entitiesName: 'clients' },
392
+ * addLoadEntitiesTrait<Client>(),
393
+ * addCrudEntitiesTrait<Client>()
394
+ * );
395
+ *
396
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
397
+ * { entityName: 'productOrder' },
398
+ * addLoadEntitiesTrait<ProductOrder>(),
399
+ * addSelectEntitiesTrait<ProductOrder>()
400
+ * );
401
+ *
402
+ * const productFeatureFactory = createEntityFeatureFactory(
403
+ * { entityName: 'product' },
404
+ * addLoadEntitiesTrait<Product>(),
405
+ * addSelectEntitiesTrait<Product>()
406
+ * );
407
+ *
408
+ * const productAddEntityPropertiesFactory = addEntityFeaturesProperties(
409
+ * productFeatureFactory,
410
+ * {
411
+ * productOrders: productOrderFeatureFactory,
412
+ * clients: clientsFeatureFactory,
413
+ * }
414
+ * );
415
+ *
416
+ * const combinedFeature = productAddEntityPropertiesFactory({
417
+ * actionsGroupKey: '[addEntityFeatures]',
418
+ * featureSelector: 'addEntityFeatures',
419
+ * });
420
+ *
421
+ * combinedFeature.actions.loadProducts();
422
+ * combinedFeature.actions.clients.loadClients();
423
+ * combinedFeature.actions.productOrders.loadProductOrders();
424
+ */
425
+ declare function addEntityFeaturesProperties<F extends EntityFeatureFactory<any, any>, T extends {
426
+ [key: string]: EntityFeatureFactory<any, any, any, any, any>;
427
+ }, K extends keyof T, State extends ExtractStateType<ReturnType<F>> & {
428
+ [P in K]: ExtractStateType<ReturnType<T[P]>>;
429
+ }, A extends ExtractActionsType<ReturnType<F>> & {
430
+ [P in K]: ExtractActionsType<ReturnType<T[P]>>;
431
+ }, S extends FeatureSelectors<State, ExtractSelectorsType<ReturnType<F>>> & {
432
+ [P in K]: FeatureSelectors<State, ExtractSelectorsType<ReturnType<T[P]>>>;
433
+ }, R extends (config: Config<State>) => {
434
+ actions: A;
435
+ selectors: S;
436
+ reducer: (state: State, action: ActionType<any>) => State;
437
+ effects: Type<any>[];
438
+ initialState: State;
439
+ }>(targetTraitFactory: F, traitFactoriesMap: T): R;
440
+ /**
441
+ * Helper function to create an implementation a TraitFactory
442
+ * @param f TraitFactory implementation
443
+ */
444
+ declare function createTraitFactory<State = {}, A extends TraitActions = {}, S extends TraitSelectors<State> = {}, M extends TraitStateMutators<State> = {}, KEY extends string = string, C = unknown, KC extends AllTraitConfigs = KeyedConfig<KEY, C>>(f: {
445
+ key: KEY;
446
+ config?: C;
447
+ depends?: string[];
448
+ actions?: TraitActionsFactory<A, KC>;
449
+ selectors?: TraitSelectorsFactory<State, S, KC>;
450
+ initialState?: TraitInitialStateFactory<State, KC>;
451
+ mutators?: TraitStateMutatorsFactory<State, M, KC>;
452
+ reducer?: TraitReducerFactory<State, A, S, M, KC>;
453
+ effects?: TraitEffectsFactory<State, A, S, KC>;
454
+ }): TraitFactory<State, A, S, M, KEY, C, KC>;
455
+ /**
456
+ * Helper function to combine selectors in components as map
457
+ *
458
+ * @example
459
+ *
460
+ * view = combineSelectors({
461
+ * products: ProductSelectors.selectProductsCurrentPage,
462
+ * isLoading: ProductSelectors.isLoadingProductsCurrentPage,
463
+ * selectedProduct: ProductSelectors.selectProductSelected,
464
+ * isLoadingCheckout: ProductSelectors.isLoadingCheckout,
465
+ * selectedSort: ProductSelectors.selectProductsSort,
466
+ * filters: ProductSelectors.selectProductsFilter,
467
+ * });
468
+ * @param t
469
+ */
470
+ declare function combineSelectors<T extends {
471
+ [k: string]: MemoizedSelector<any, any>;
472
+ }>(t: T): MemoizedSelector<any, {
473
+ [j in keyof T]: ReturnType<T[j]>;
474
+ }>;
475
+
476
+ declare function insertIf<State>(condition: any, getElement: () => ReducerTypes<State, ActionCreator[]>): ReducerTypes<State, ActionCreator[]>[];
477
+ declare function toMap(a: Array<string | number>): {
478
+ [key: string]: boolean;
479
+ };
480
+ declare function capitalize(name: string): string;
481
+ declare function camelCaseToSentence(text: string): string;
482
+ /**
483
+ * Set propertyReducer in sourceReducer in a property of the source state,
484
+ * @param sourceReducer
485
+ * @param property
486
+ * @param propertyReducer
487
+ *
488
+ * @example
489
+ *
490
+ * const newReducer = setPropertyReducer(productsReducer, 'selectedProduct', selectedProductReducer)
491
+ */
492
+ declare function setPropertyReducer<State, P extends keyof State>(sourceReducer: (state: State, action: ActionType<any>) => State, property: P, propertyReducer: (state: State[P], action: ActionType<any>) => State[P]): (state: State, action: ActionType<any>) => State;
493
+ /**
494
+ * Set propertyReducers in sourceReducer each in a property of the source state,
495
+ * @param sourceReducer
496
+ * @param property
497
+ * @param propertyReducer
498
+ *
499
+ * @example
500
+ *
501
+ * const newReducer = setPropertyReducer(productsReducer,
502
+ * {
503
+ * selectedProduct: selectedProductReducer
504
+ * favoriteProduct: favoriteProductReducer
505
+ * })
506
+ */
507
+ declare function setPropertiesReducer<State, P extends keyof State>(sourceReducer: (state: State, action: ActionType<any>) => State, propertiesReducers: {
508
+ [key in P]: (state: State[P], action: ActionType<any>) => State[P];
509
+ }): (state: State, action: ActionType<any>) => State;
510
+ /**
511
+ * joins two reducers so the work in the same state
512
+ * @param firstReducer
513
+ * @param secondReducer
514
+ */
515
+ declare function joinReducers<State>(firstReducer: (state: State, action: ActionType<any>) => State, secondReducer: (state: any, action: ActionType<any>) => any): (state: State, action: ActionType<any>) => State;
516
+
517
+ type CacheKey = string | (string | object)[];
518
+ interface CacheData {
519
+ value: any;
520
+ date: number;
521
+ invalid: boolean;
522
+ hitCount: number;
523
+ }
524
+ interface CacheKeys {
525
+ keys?: {
526
+ [key: string]: CacheKeys;
527
+ };
528
+ data?: CacheData;
529
+ }
530
+
531
+ /**
532
+ * Cache the result of source parameter using the provided key, when call
533
+ * again if the cache is valid (exist and is not expired or invalidated)
534
+ * it will return the cache value without calling again source
535
+ * @example
536
+ * // cache for 3 min
537
+ * loadStores$ = createEffect(() => {
538
+ * return this.actions$.pipe(
539
+ * ofType(ProductStoreActions.loadStores),
540
+ * exhaustMap(() =>
541
+ * cache({
542
+ * key: ['stores'],
543
+ * store: this.store,
544
+ * source: this.storeService.getStores(),
545
+ * expire: 1000 * 60 * 3 // optional param , cache forever if not present
546
+ * }).pipe(
547
+ * map((res) => ProductStoreActions.loadStoresSuccess({ entities: res })),
548
+ * catchError(() => of(ProductStoreActions.loadStoresFail()))
549
+ * )
550
+ * )
551
+ * );
552
+ * });
553
+ * // cache top 10, for 3 mins
554
+ * loadDepartments$ = createEffect(() => {
555
+ * return this.actions$.pipe(
556
+ * ofType(this.localActions.loadDepartments),
557
+ * concatLatestFrom(() =>
558
+ * this.store.select(this.localSelectors.selectDepartmentsFilter)
559
+ * ),
560
+ * exhaustMap(([_, filters]) =>
561
+ * cache({
562
+ * key: ['stores','departments',{ storeId: filters!.storeId },
563
+ * store: this.store,
564
+ * source: this.storeService.getStoreDepartments(filters!.storeId),
565
+ * expires: 1000 * 60 * 3,
566
+ * maxCacheSize: 10,
567
+ * }).pipe(
568
+ * map((res) =>
569
+ * this.localActions.loadDepartmentsSuccess({
570
+ * entities: res,
571
+ * })
572
+ * ),
573
+ * catchError(() => of(this.localActions.loadDepartmentsFail()))
574
+ * )
575
+ * )
576
+ * );
577
+ * });
578
+ *
579
+ * @param options - configuration
580
+ * @param options.store - required ngrx store
581
+ * @param options.key - key can be string, array of string or array of string with plain objects
582
+ * @param options.source - called when cache is invalid
583
+ * @param options.expires - time to expire the cache valued, if not present is infinite
584
+ * @param options.maxCacheSize - max number of keys to store , only works if last key is variable
585
+ */
586
+ declare function cache<T>({ store, key, source, expires, maxCacheSize, skip, }: {
587
+ store: Store;
588
+ key: CacheKey;
589
+ source: Observable<T>;
590
+ expires?: number;
591
+ maxCacheSize?: number;
592
+ skip?: boolean;
593
+ }): Observable<any>;
594
+
595
+ declare class CacheModule {
596
+ static ɵfac: i0.ɵɵFactoryDeclaration<CacheModule, never>;
597
+ static ɵmod: i0.ɵɵNgModuleDeclaration<CacheModule, never, [typeof _ngrx_store.StoreFeatureModule], never>;
598
+ static ɵinj: i0.ɵɵInjectorDeclaration<CacheModule>;
599
+ }
600
+
601
+ declare const CacheActions: {
602
+ invalidateCache: _ngrx_store.ActionCreator<"[Cache] Invalidate Cache", (props: {
603
+ key: CacheKey;
604
+ }) => {
605
+ key: CacheKey;
606
+ } & _ngrx_store.Action<"[Cache] Invalidate Cache">>;
607
+ deleteCache: _ngrx_store.ActionCreator<"[Cache] Delete Cache", (props: {
608
+ key: CacheKey;
609
+ }) => {
610
+ key: CacheKey;
611
+ } & _ngrx_store.Action<"[Cache] Delete Cache">>;
612
+ };
613
+ declare const CacheSelectors: {
614
+ getCache: (key: CacheKey) => _ngrx_store.MemoizedSelector<object, CacheData | undefined, (s1: CacheKeys) => CacheData | undefined>;
615
+ };
616
+
617
+ export { CacheActions, CacheModule, CacheSelectors, DISABLE_LOCAL_TRAIT_EFFECTS, TraitEffect, TraitsLocalStore, addEntityFeaturesProperties, buildLocalTraits, cache, camelCaseToSentence, capitalize, combineEntityFeatures, combineSelectors, createEntityFeatureFactory, createTraitFactory, getDestroyActionName, insertIf, joinReducers, mixEntityFeatures, setPropertiesReducer, setPropertyReducer, toMap };
618
+ export type { AllTraitConfigs, BaseEntityFeatureFactory, BaseFeatureTraits, Config, EntityFeatureFactory, ExtractActionsType, ExtractKeyedConfigType, ExtractMutatorsType, ExtractSelectorsType, ExtractStateType, FeatureSelectors, FeatureTraits, KeyedConfig, LocalTraitsConfig, PostfixProps, PrefixProps, ReplaceEntityNames, ReplaceProps, TraitActions, TraitActionsFactory, TraitActionsFactoryConfig, TraitEffectsFactory, TraitEffectsFactoryConfig, TraitFactory, TraitInitialStateFactory, TraitInitialStateFactoryConfig, TraitReducerFactory, TraitReducerFactoryConfig, TraitSelectors, TraitSelectorsFactory, TraitSelectorsFactoryConfig, TraitStateMutators, TraitStateMutatorsFactory, TraitStateMutatorsFactoryConfig, Type, UnionToIntersection };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "./node_modules/ng-packagr/package.schema.json",
3
3
  "name": "@ngrx-traits/core",
4
- "version": "19.2.2",
4
+ "version": "20.0.0-beta.1",
5
5
  "license": "MIT",
6
6
  "peerDependencies": {
7
7
  "@angular/core": "^19.0.0",
@@ -1 +1,11 @@
1
- export * from './provide-mock-local-traits';
1
+ import { TraitsLocalStore, EntityFeatureFactory } from '@ngrx-traits/core';
2
+ import { Type, Provider } from '@angular/core';
3
+
4
+ declare function provideMockLocalTraits<T extends TraitsLocalStore<EntityFeatureFactory<any, any>>>({ traitFactory, selectors, }: {
5
+ traitFactory: Type<T>;
6
+ selectors?: {
7
+ [key in keyof T['localSelectors']]?: ReturnType<T['localSelectors'][key]>;
8
+ };
9
+ }): Provider[];
10
+
11
+ export { provideMockLocalTraits };
@@ -1,29 +0,0 @@
1
- import { CacheKey } from './cache.models';
2
- export declare const cache: import("@ngrx/store").ActionCreator<"[Cache] Cache", (props: {
3
- key: CacheKey;
4
- value: any;
5
- date: number;
6
- maxCacheSize?: number;
7
- expires?: number;
8
- }) => {
9
- key: CacheKey;
10
- value: any;
11
- date: number;
12
- maxCacheSize?: number;
13
- expires?: number;
14
- } & import("@ngrx/store").Action<"[Cache] Cache">>;
15
- export declare const hitCache: import("@ngrx/store").ActionCreator<"[Cache] Hit Cache", (props: {
16
- key: CacheKey;
17
- }) => {
18
- key: CacheKey;
19
- } & import("@ngrx/store").Action<"[Cache] Hit Cache">>;
20
- export declare const invalidateCache: import("@ngrx/store").ActionCreator<"[Cache] Invalidate Cache", (props: {
21
- key: CacheKey;
22
- }) => {
23
- key: CacheKey;
24
- } & import("@ngrx/store").Action<"[Cache] Invalidate Cache">>;
25
- export declare const deleteCache: import("@ngrx/store").ActionCreator<"[Cache] Delete Cache", (props: {
26
- key: CacheKey;
27
- }) => {
28
- key: CacheKey;
29
- } & import("@ngrx/store").Action<"[Cache] Delete Cache">>;