-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): create injection tokens with factory (#3259)
- Loading branch information
Showing
13 changed files
with
176 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 21 additions & 33 deletions
54
libs/search/driver/federated/src/injection-tokens/drivers.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,25 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
Type, | ||
} from '@angular/core'; | ||
|
||
import { createServicesInjectionToken } from '@daffodil/core'; | ||
import { DaffSearchResult } from '@daffodil/search'; | ||
import { DaffSearchDriverKindedInterface } from '@daffodil/search/driver'; | ||
|
||
/** | ||
* A multi-provider injection token for providing feature-specific search platform drivers. | ||
* This allows disparate modules to contribute to the business logic of performing searches while | ||
* preventing tight coupling of said modules. | ||
*/ | ||
export const DAFF_SEARCH_FEDERATED_DRIVERS = new InjectionToken<DaffSearchDriverKindedInterface[]>( | ||
'DAFF_SEARCH_FEDERATED_DRIVERS', | ||
{ factory: () => []}, | ||
); | ||
export const { | ||
/** | ||
* A multi-provider injection token for providing feature-specific search platform drivers. | ||
* This allows disparate modules to contribute to the business logic of performing searches while | ||
* preventing tight coupling of said modules. | ||
*/ | ||
token: DAFF_SEARCH_FEDERATED_DRIVERS, | ||
|
||
/** | ||
* Provides feature drivers for the federated search driver. | ||
* | ||
* See {@link DAFF_SEARCH_FEDERATED_DRIVERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchFederatedDrivers(MySearchDriver) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffProvideSearchFederatedDrivers(...drivers: Type<DaffSearchDriverKindedInterface>[]): Provider[] { | ||
return drivers.map(driver => ({ | ||
provide: DAFF_SEARCH_FEDERATED_DRIVERS, | ||
useExisting: driver, | ||
multi: true, | ||
})); | ||
} | ||
/** | ||
* Provides feature drivers for the federated search driver. | ||
* | ||
* See {@link DAFF_SEARCH_FEDERATED_DRIVERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchFederatedDrivers(MySearchDriver) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffProvideSearchFederatedDrivers, | ||
} = createServicesInjectionToken<DaffSearchDriverKindedInterface>('DAFF_SEARCH_FEDERATED_DRIVERS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 21 additions & 32 deletions
53
libs/search/driver/in-memory/src/injection-tokens/backends.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,25 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
Type, | ||
} from '@angular/core'; | ||
import { createServicesInjectionToken } from '@daffodil/core'; | ||
|
||
import { DaffSearchInMemoryChildBackend } from '../interfaces/public_api'; | ||
|
||
/** | ||
* A multi-provider injection token for providing feature-specific search in-memory backends. | ||
* Feature/search "join" packages should provide a backend here to interface | ||
* with their feature backend for realistic search behavior. | ||
*/ | ||
export const DAFF_SEARCH_IN_MEMORY_BACKENDS = new InjectionToken<DaffSearchInMemoryChildBackend[]>( | ||
'DAFF_SEARCH_IN_MEMORY_BACKENDS', | ||
{ factory: () => []}, | ||
); | ||
export const { | ||
/** | ||
* A multi-provider injection token for providing feature-specific search in-memory backends. | ||
* Feature/search "join" packages should provide a backend here to interface | ||
* with their feature backend for realistic search behavior. | ||
*/ | ||
token: DAFF_SEARCH_IN_MEMORY_BACKENDS, | ||
|
||
/** | ||
* Provides child feature backends for the search in-memory backend. | ||
* | ||
* See {@link DAFF_SEARCH_IN_MEMORY_BACKENDS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchInMemoryBackends(MySearchFeatureBackend) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffProvideSearchInMemoryBackends(...drivers: Type<DaffSearchInMemoryChildBackend>[]): Provider[] { | ||
return drivers.map(driver => ({ | ||
provide: DAFF_SEARCH_IN_MEMORY_BACKENDS, | ||
useExisting: driver, | ||
multi: true, | ||
})); | ||
} | ||
/** | ||
* Provides child feature backends for the search in-memory backend. | ||
* | ||
* See {@link DAFF_SEARCH_IN_MEMORY_BACKENDS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchInMemoryBackends(MySearchFeatureBackend) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffProvideSearchInMemoryBackends, | ||
} = createServicesInjectionToken<DaffSearchInMemoryChildBackend>('DAFF_SEARCH_IN_MEMORY_BACKENDS'); |
13 changes: 8 additions & 5 deletions
13
libs/search/driver/src/interfaces/search-service.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 8 additions & 7 deletions
15
libs/search/routing/src/injection-tokens/search-options/builder.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { createSingleInjectionToken } from '@daffodil/core'; | ||
|
||
import { DaffSearchRoutingOptionBuilder } from './builders.token'; | ||
|
||
/** | ||
* An internal token to combine the {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS} into a single builder. | ||
*/ | ||
export const DAFF_SEARCH_ROUTING_OPTIONS_BUILDER = new InjectionToken<DaffSearchRoutingOptionBuilder>( | ||
'DAFF_SEARCH_ROUTING_OPTIONS_BUILDER', | ||
); | ||
export const { | ||
/** | ||
* An internal token to combine the {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS} into a single builder. | ||
*/ | ||
token: DAFF_SEARCH_ROUTING_OPTIONS_BUILDER, | ||
provider: daffProvideSearchRoutingOptionsBuilder, | ||
} = createSingleInjectionToken<DaffSearchRoutingOptionBuilder>('DAFF_SEARCH_ROUTING_OPTIONS_BUILDER'); |
60 changes: 25 additions & 35 deletions
60
libs/search/routing/src/injection-tokens/search-options/builders.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,32 @@ | ||
import { | ||
InjectionToken, | ||
ValueProvider, | ||
} from '@angular/core'; | ||
import { ActivatedRouteSnapshot } from '@angular/router'; | ||
|
||
import { createMultiInjectionToken } from '@daffodil/core'; | ||
import { DaffSearchDriverOptions } from '@daffodil/search/driver'; | ||
|
||
export type DaffSearchRoutingOptionBuilder<T extends DaffSearchDriverOptions = DaffSearchDriverOptions> = (route: ActivatedRouteSnapshot) => T; | ||
|
||
/** | ||
* A multi-provider injection token for search option builders. | ||
* These builders are called with the requested route during the resolve step | ||
* and return options to pass to the search driver. | ||
*/ | ||
export const DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS = new InjectionToken<DaffSearchRoutingOptionBuilder[]>( | ||
'DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS', | ||
{ factory: () => []}, | ||
); | ||
export const { | ||
/** | ||
* A multi-provider injection token for search option builders. | ||
* These builders are called with the requested route during the resolve step | ||
* and return options to pass to the search driver. | ||
*/ | ||
token: DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS, | ||
|
||
/** | ||
* Provides search option builders for the routing layer. | ||
* | ||
* See {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchRoutingOptionBuilders( | ||
* route => ({ | ||
* limit: route.queryParams.limit | ||
* }) | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffProvideSearchRoutingOptionBuilders(...builders: DaffSearchRoutingOptionBuilder[]): ValueProvider[] { | ||
return builders.map(builder => ({ | ||
provide: DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS, | ||
useValue: builder, | ||
multi: true, | ||
})); | ||
} | ||
/** | ||
* Provides search option builders for the routing layer. | ||
* | ||
* See {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideSearchRoutingOptionBuilders( | ||
* route => ({ | ||
* limit: route.queryParams.limit | ||
* }) | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffProvideSearchRoutingOptionBuilders, | ||
} = createMultiInjectionToken<DaffSearchRoutingOptionBuilder>('DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { createConfigInjectionToken } from '@daffodil/core'; | ||
|
||
import { DAFF_SEARCH_STATE_CONFIG_DEFAULT } from './default'; | ||
import { DaffSearchStateConfig } from './interface'; | ||
|
||
/** | ||
* The token used to provide @daffodil/search/state config data. | ||
*/ | ||
export const DAFF_SEARCH_STATE_CONFIG = new InjectionToken<DaffSearchStateConfig>('DAFF_SEARCH_STATE_CONFIG', { | ||
factory: () => DAFF_SEARCH_STATE_CONFIG_DEFAULT, | ||
}); | ||
export const { | ||
/** | ||
* The token used to provide @daffodil/search/state config data. | ||
*/ | ||
token: DAFF_SEARCH_STATE_CONFIG, | ||
provider: provideDaffAuthRoutingConfig, | ||
} = createConfigInjectionToken<DaffSearchStateConfig>(DAFF_SEARCH_STATE_CONFIG_DEFAULT, 'DAFF_SEARCH_STATE_CONFIG'); |
16 changes: 9 additions & 7 deletions
16
libs/search/state/src/injection-tokens/error-matcher.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { createSingleInjectionToken } from '@daffodil/core'; | ||
import { daffTransformErrorToStateError } from '@daffodil/core/state'; | ||
|
||
/** | ||
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state. | ||
* Can be used to further refine Daffodil errors into more specific app errors. | ||
*/ | ||
export const DAFF_SEARCH_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>( | ||
export const { | ||
/** | ||
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state. | ||
* Can be used to further refine Daffodil errors into more specific app errors. | ||
*/ | ||
token: DAFF_SEARCH_ERROR_MATCHER, | ||
provider: daffProvideSearchErrorMatcher, | ||
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>( | ||
'DAFF_SEARCH_ERROR_MATCHER', | ||
{ factory: () => daffTransformErrorToStateError }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { DAFF_SEARCH_ERROR_MATCHER } from './error-matcher.token'; | ||
export * from './error-matcher.token'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,32 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
} from '@angular/core'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
import { createMultiInjectionToken } from '@daffodil/core'; | ||
import { DaffSearchResult } from '@daffodil/search'; | ||
|
||
import { DaffSearchReducersState } from '../reducers.interface'; | ||
|
||
/** | ||
* A token to hold the injectable extra reducers. | ||
* | ||
* Prefer using {@link daffSearchProvideExtraReducers}. | ||
*/ | ||
export const DAFF_SEARCH_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffSearchReducersState>[]>( | ||
export const { | ||
/** | ||
* A token to hold the injectable extra reducers. | ||
* | ||
* Prefer using {@link daffSearchProvideExtraReducers}. | ||
*/ | ||
token: DAFF_SEARCH_EXTRA_REDUCERS, | ||
|
||
/** | ||
* Provides additional reducers that run after the standard Daffodil cart reducers. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffSearchProvideExtraReducers( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffSearchProvideExtraReducers, | ||
} = createMultiInjectionToken<ActionReducer<DaffSearchReducersState>>( | ||
'DAFF_SEARCH_EXTRA_REDUCERS', | ||
{ | ||
factory: () => [], | ||
providedIn: 'any', | ||
}, | ||
{ providedIn: 'any' }, | ||
); | ||
|
||
/** | ||
* Provides additional reducers that run after the standard Daffodil cart reducers. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffSearchProvideExtraReducers( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffSearchProvideExtraReducers<T extends DaffSearchResult = DaffSearchResult>( | ||
...reducers: ActionReducer<DaffSearchReducersState<T>>[] | ||
): Provider[] { | ||
return reducers.map(reducer => ({ | ||
provide: DAFF_SEARCH_EXTRA_REDUCERS, | ||
useValue: reducer, | ||
multi: true, | ||
})); | ||
} |
Oops, something went wrong.