Skip to content

Commit 2fbd482

Browse files
authored
feat(search): create injection tokens with factory (#3259)
1 parent 91cf3e9 commit 2fbd482

File tree

13 files changed

+176
-215
lines changed

13 files changed

+176
-215
lines changed

libs/search/driver/federated/src/config/interface.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { InjectionToken } from '@angular/core';
1+
import { createConfigInjectionToken } from '@daffodil/core';
22

33
import { SEARCH_FEDERATED_CONFIG_DEFAULT } from './default';
44

5-
/**
6-
* The token used to provide @daffodil/search/driver/federated config data.
7-
* Mandatory for the Magento driver.
8-
*/
9-
export const SEARCH_FEDERATED_CONFIG_TOKEN = new InjectionToken<DaffSearchFederatedDriverConfig>('SEARCH_FEDERATED_CONFIG_TOKEN', { factory: () => SEARCH_FEDERATED_CONFIG_DEFAULT });
5+
export const {
6+
/**
7+
* The token used to provide @daffodil/search/driver/federated config data.
8+
* Mandatory for the Magento driver.
9+
*/
10+
token: SEARCH_FEDERATED_CONFIG_TOKEN,
11+
provider: daffProvideAnalyticsConfig,
12+
} = createConfigInjectionToken<DaffSearchFederatedDriverConfig>(
13+
SEARCH_FEDERATED_CONFIG_DEFAULT,
14+
'SEARCH_FEDERATED_CONFIG_TOKEN',
15+
);
1016

1117
/**
1218
* An interface for providing @daffodil/search/driver/federated with necessary config values.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
1-
import {
2-
InjectionToken,
3-
Provider,
4-
Type,
5-
} from '@angular/core';
6-
1+
import { createServicesInjectionToken } from '@daffodil/core';
72
import { DaffSearchResult } from '@daffodil/search';
83
import { DaffSearchDriverKindedInterface } from '@daffodil/search/driver';
94

10-
/**
11-
* A multi-provider injection token for providing feature-specific search platform drivers.
12-
* This allows disparate modules to contribute to the business logic of performing searches while
13-
* preventing tight coupling of said modules.
14-
*/
15-
export const DAFF_SEARCH_FEDERATED_DRIVERS = new InjectionToken<DaffSearchDriverKindedInterface[]>(
16-
'DAFF_SEARCH_FEDERATED_DRIVERS',
17-
{ factory: () => []},
18-
);
5+
export const {
6+
/**
7+
* A multi-provider injection token for providing feature-specific search platform drivers.
8+
* This allows disparate modules to contribute to the business logic of performing searches while
9+
* preventing tight coupling of said modules.
10+
*/
11+
token: DAFF_SEARCH_FEDERATED_DRIVERS,
1912

20-
/**
21-
* Provides feature drivers for the federated search driver.
22-
*
23-
* See {@link DAFF_SEARCH_FEDERATED_DRIVERS}.
24-
*
25-
* ```ts
26-
* providers: [
27-
* ...daffProvideSearchFederatedDrivers(MySearchDriver)
28-
* ]
29-
* ```
30-
*/
31-
export function daffProvideSearchFederatedDrivers(...drivers: Type<DaffSearchDriverKindedInterface>[]): Provider[] {
32-
return drivers.map(driver => ({
33-
provide: DAFF_SEARCH_FEDERATED_DRIVERS,
34-
useExisting: driver,
35-
multi: true,
36-
}));
37-
}
13+
/**
14+
* Provides feature drivers for the federated search driver.
15+
*
16+
* See {@link DAFF_SEARCH_FEDERATED_DRIVERS}.
17+
*
18+
* ```ts
19+
* providers: [
20+
* ...daffProvideSearchFederatedDrivers(MySearchDriver)
21+
* ]
22+
* ```
23+
*/
24+
provider: daffProvideSearchFederatedDrivers,
25+
} = createServicesInjectionToken<DaffSearchDriverKindedInterface>('DAFF_SEARCH_FEDERATED_DRIVERS');

libs/search/driver/federated/src/search.service.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('@daffodil/search/driver/federated | DaffSearchFederatedDriver', () =>
6666
TestBed.configureTestingModule({
6767
providers: [
6868
DaffSearchFederatedDriver,
69-
...daffProvideSearchFederatedDrivers(TestDriver1, TestDriver2),
69+
...daffProvideSearchFederatedDrivers<TestDriver1 | TestDriver2>(TestDriver1, TestDriver2),
7070
],
7171
});
7272

Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import {
2-
InjectionToken,
3-
Provider,
4-
Type,
5-
} from '@angular/core';
1+
import { createServicesInjectionToken } from '@daffodil/core';
62

73
import { DaffSearchInMemoryChildBackend } from '../interfaces/public_api';
84

9-
/**
10-
* A multi-provider injection token for providing feature-specific search in-memory backends.
11-
* Feature/search "join" packages should provide a backend here to interface
12-
* with their feature backend for realistic search behavior.
13-
*/
14-
export const DAFF_SEARCH_IN_MEMORY_BACKENDS = new InjectionToken<DaffSearchInMemoryChildBackend[]>(
15-
'DAFF_SEARCH_IN_MEMORY_BACKENDS',
16-
{ factory: () => []},
17-
);
5+
export const {
6+
/**
7+
* A multi-provider injection token for providing feature-specific search in-memory backends.
8+
* Feature/search "join" packages should provide a backend here to interface
9+
* with their feature backend for realistic search behavior.
10+
*/
11+
token: DAFF_SEARCH_IN_MEMORY_BACKENDS,
1812

19-
/**
20-
* Provides child feature backends for the search in-memory backend.
21-
*
22-
* See {@link DAFF_SEARCH_IN_MEMORY_BACKENDS}.
23-
*
24-
* ```ts
25-
* providers: [
26-
* ...daffProvideSearchInMemoryBackends(MySearchFeatureBackend)
27-
* ]
28-
* ```
29-
*/
30-
export function daffProvideSearchInMemoryBackends(...drivers: Type<DaffSearchInMemoryChildBackend>[]): Provider[] {
31-
return drivers.map(driver => ({
32-
provide: DAFF_SEARCH_IN_MEMORY_BACKENDS,
33-
useExisting: driver,
34-
multi: true,
35-
}));
36-
}
13+
/**
14+
* Provides child feature backends for the search in-memory backend.
15+
*
16+
* See {@link DAFF_SEARCH_IN_MEMORY_BACKENDS}.
17+
*
18+
* ```ts
19+
* providers: [
20+
* ...daffProvideSearchInMemoryBackends(MySearchFeatureBackend)
21+
* ]
22+
* ```
23+
*/
24+
provider: daffProvideSearchInMemoryBackends,
25+
} = createServicesInjectionToken<DaffSearchInMemoryChildBackend>('DAFF_SEARCH_IN_MEMORY_BACKENDS');

libs/search/driver/src/interfaces/search-service.interface.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

3+
import { createSingleInjectionToken } from '@daffodil/core';
44
import {
55
DaffSearchResult,
66
DaffSearchResultCollection,
77
} from '@daffodil/search';
88

99
import type { DaffSearchDriverResponse } from './response.interface';
1010

11-
/**
12-
* An injection token for the search driver.
13-
*/
14-
export const DaffSearchDriver = new InjectionToken<DaffSearchDriverInterface>('DaffSearchDriver');
11+
export const {
12+
/**
13+
* An injection token for the search driver.
14+
*/
15+
token: DaffSearchDriver,
16+
provider: daffProvideSearchDriver,
17+
} = createSingleInjectionToken<DaffSearchDriverInterface>('DaffSearchDriver');
1518

1619
/**
1720
* The options for making a search.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { InjectionToken } from '@angular/core';
1+
import { createSingleInjectionToken } from '@daffodil/core';
22

33
import { DaffSearchRoutingOptionBuilder } from './builders.token';
44

5-
/**
6-
* An internal token to combine the {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS} into a single builder.
7-
*/
8-
export const DAFF_SEARCH_ROUTING_OPTIONS_BUILDER = new InjectionToken<DaffSearchRoutingOptionBuilder>(
9-
'DAFF_SEARCH_ROUTING_OPTIONS_BUILDER',
10-
);
5+
export const {
6+
/**
7+
* An internal token to combine the {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS} into a single builder.
8+
*/
9+
token: DAFF_SEARCH_ROUTING_OPTIONS_BUILDER,
10+
provider: daffProvideSearchRoutingOptionsBuilder,
11+
} = createSingleInjectionToken<DaffSearchRoutingOptionBuilder>('DAFF_SEARCH_ROUTING_OPTIONS_BUILDER');
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
1-
import {
2-
InjectionToken,
3-
ValueProvider,
4-
} from '@angular/core';
51
import { ActivatedRouteSnapshot } from '@angular/router';
62

3+
import { createMultiInjectionToken } from '@daffodil/core';
74
import { DaffSearchDriverOptions } from '@daffodil/search/driver';
85

96
export type DaffSearchRoutingOptionBuilder<T extends DaffSearchDriverOptions = DaffSearchDriverOptions> = (route: ActivatedRouteSnapshot) => T;
107

11-
/**
12-
* A multi-provider injection token for search option builders.
13-
* These builders are called with the requested route during the resolve step
14-
* and return options to pass to the search driver.
15-
*/
16-
export const DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS = new InjectionToken<DaffSearchRoutingOptionBuilder[]>(
17-
'DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS',
18-
{ factory: () => []},
19-
);
8+
export const {
9+
/**
10+
* A multi-provider injection token for search option builders.
11+
* These builders are called with the requested route during the resolve step
12+
* and return options to pass to the search driver.
13+
*/
14+
token: DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS,
2015

21-
/**
22-
* Provides search option builders for the routing layer.
23-
*
24-
* See {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS}.
25-
*
26-
* ```ts
27-
* providers: [
28-
* ...daffProvideSearchRoutingOptionBuilders(
29-
* route => ({
30-
* limit: route.queryParams.limit
31-
* })
32-
* )
33-
* ]
34-
* ```
35-
*/
36-
export function daffProvideSearchRoutingOptionBuilders(...builders: DaffSearchRoutingOptionBuilder[]): ValueProvider[] {
37-
return builders.map(builder => ({
38-
provide: DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS,
39-
useValue: builder,
40-
multi: true,
41-
}));
42-
}
16+
/**
17+
* Provides search option builders for the routing layer.
18+
*
19+
* See {@link DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS}.
20+
*
21+
* ```ts
22+
* providers: [
23+
* ...daffProvideSearchRoutingOptionBuilders(
24+
* route => ({
25+
* limit: route.queryParams.limit
26+
* })
27+
* )
28+
* ]
29+
* ```
30+
*/
31+
provider: daffProvideSearchRoutingOptionBuilders,
32+
} = createMultiInjectionToken<DaffSearchRoutingOptionBuilder>('DAFF_SEARCH_ROUTING_OPTIONS_BUILDERS');

libs/search/state/src/config/token.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { InjectionToken } from '@angular/core';
1+
import { createConfigInjectionToken } from '@daffodil/core';
22

33
import { DAFF_SEARCH_STATE_CONFIG_DEFAULT } from './default';
44
import { DaffSearchStateConfig } from './interface';
55

6-
/**
7-
* The token used to provide @daffodil/search/state config data.
8-
*/
9-
export const DAFF_SEARCH_STATE_CONFIG = new InjectionToken<DaffSearchStateConfig>('DAFF_SEARCH_STATE_CONFIG', {
10-
factory: () => DAFF_SEARCH_STATE_CONFIG_DEFAULT,
11-
});
6+
export const {
7+
/**
8+
* The token used to provide @daffodil/search/state config data.
9+
*/
10+
token: DAFF_SEARCH_STATE_CONFIG,
11+
provider: provideDaffAuthRoutingConfig,
12+
} = createConfigInjectionToken<DaffSearchStateConfig>(DAFF_SEARCH_STATE_CONFIG_DEFAULT, 'DAFF_SEARCH_STATE_CONFIG');
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { InjectionToken } from '@angular/core';
2-
1+
import { createSingleInjectionToken } from '@daffodil/core';
32
import { daffTransformErrorToStateError } from '@daffodil/core/state';
43

5-
/**
6-
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
7-
* Can be used to further refine Daffodil errors into more specific app errors.
8-
*/
9-
export const DAFF_SEARCH_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
4+
export const {
5+
/**
6+
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
7+
* Can be used to further refine Daffodil errors into more specific app errors.
8+
*/
9+
token: DAFF_SEARCH_ERROR_MATCHER,
10+
provider: daffProvideSearchErrorMatcher,
11+
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
1012
'DAFF_SEARCH_ERROR_MATCHER',
1113
{ factory: () => daffTransformErrorToStateError },
1214
);
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { DAFF_SEARCH_ERROR_MATCHER } from './error-matcher.token';
1+
export * from './error-matcher.token';
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
1-
import {
2-
InjectionToken,
3-
Provider,
4-
} from '@angular/core';
51
import { ActionReducer } from '@ngrx/store';
62

3+
import { createMultiInjectionToken } from '@daffodil/core';
74
import { DaffSearchResult } from '@daffodil/search';
85

96
import { DaffSearchReducersState } from '../reducers.interface';
107

11-
/**
12-
* A token to hold the injectable extra reducers.
13-
*
14-
* Prefer using {@link daffSearchProvideExtraReducers}.
15-
*/
16-
export const DAFF_SEARCH_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffSearchReducersState>[]>(
8+
export const {
9+
/**
10+
* A token to hold the injectable extra reducers.
11+
*
12+
* Prefer using {@link daffSearchProvideExtraReducers}.
13+
*/
14+
token: DAFF_SEARCH_EXTRA_REDUCERS,
15+
16+
/**
17+
* Provides additional reducers that run after the standard Daffodil cart reducers.
18+
*
19+
* ```ts
20+
* providers: [
21+
* ...daffSearchProvideExtraReducers(
22+
* myReducer1,
23+
* myReducer2
24+
* )
25+
* ]
26+
* ```
27+
*/
28+
provider: daffSearchProvideExtraReducers,
29+
} = createMultiInjectionToken<ActionReducer<DaffSearchReducersState>>(
1730
'DAFF_SEARCH_EXTRA_REDUCERS',
18-
{
19-
factory: () => [],
20-
providedIn: 'any',
21-
},
31+
{ providedIn: 'any' },
2232
);
23-
24-
/**
25-
* Provides additional reducers that run after the standard Daffodil cart reducers.
26-
*
27-
* ```ts
28-
* providers: [
29-
* ...daffSearchProvideExtraReducers(
30-
* myReducer1,
31-
* myReducer2
32-
* )
33-
* ]
34-
* ```
35-
*/
36-
export function daffSearchProvideExtraReducers<T extends DaffSearchResult = DaffSearchResult>(
37-
...reducers: ActionReducer<DaffSearchReducersState<T>>[]
38-
): Provider[] {
39-
return reducers.map(reducer => ({
40-
provide: DAFF_SEARCH_EXTRA_REDUCERS,
41-
useValue: reducer,
42-
multi: true,
43-
}));
44-
}

0 commit comments

Comments
 (0)