-
-
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(category): create injection tokens with factory (#3238)
- Loading branch information
Showing
8 changed files
with
101 additions
and
113 deletions.
There are no files selected for viewing
18 changes: 12 additions & 6 deletions
18
libs/category/driver/magento/src/interfaces/config.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
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
23 changes: 10 additions & 13 deletions
23
libs/category/routing/src/injection-tokens/request/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
65 changes: 28 additions & 37 deletions
65
libs/category/routing/src/injection-tokens/request/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,43 +1,34 @@ | ||
import { | ||
InjectionToken, | ||
ValueProvider, | ||
} from '@angular/core'; | ||
import { ActivatedRouteSnapshot } from '@angular/router'; | ||
|
||
import { DaffCollectionRequest } from '@daffodil/core'; | ||
|
||
import { | ||
DaffCollectionRequest, | ||
createMultiInjectionToken, | ||
} from '@daffodil/core'; | ||
|
||
export type DaffCategoryRoutingRequestBuilder<T extends DaffCollectionRequest = DaffCollectionRequest> = (route: ActivatedRouteSnapshot) => T; | ||
|
||
/** | ||
* A multi-provider injection token for category request builders. | ||
* These builders are called with the requested route during the resolve step | ||
* and return options to pass to the category driver. | ||
*/ | ||
export const DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS = new InjectionToken<DaffCategoryRoutingRequestBuilder[]>( | ||
'DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS', | ||
{ factory: () => []}, | ||
); | ||
export const { | ||
/** | ||
* A multi-provider injection token for category request builders. | ||
* These builders are called with the requested route during the resolve step | ||
* and return options to pass to the category driver. | ||
*/ | ||
token: DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS, | ||
|
||
/** | ||
* Provides category request builders for the routing layer. | ||
* | ||
* See {@link DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideCategoryRoutingRequestBuilders( | ||
* route => ({ | ||
* currentPage: route.queryParams.page | ||
* }) | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffProvideCategoryRoutingRequestBuilders(...builders: DaffCategoryRoutingRequestBuilder[]): ValueProvider[] { | ||
return builders.map(builder => ({ | ||
provide: DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS, | ||
useValue: builder, | ||
multi: true, | ||
})); | ||
} | ||
/** | ||
* Provides category request builders for the routing layer. | ||
* | ||
* See {@link DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideCategoryRoutingRequestBuilders( | ||
* route => ({ | ||
* currentPage: route.queryParams.page | ||
* }) | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffProvideCategoryRoutingRequestBuilders, | ||
} = createMultiInjectionToken<DaffCategoryRoutingRequestBuilder>('DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS'); |
16 changes: 9 additions & 7 deletions
16
libs/category/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_CATEGORY_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_CATEGORY_ERROR_MATCHER, | ||
provider: daffProvideCategoryErrorMatcher, | ||
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>( | ||
'DAFF_CATEGORY_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_CATEGORY_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,42 +1,30 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
} from '@angular/core'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
import { createMultiInjectionToken } from '@daffodil/core'; | ||
|
||
import { DaffCategoryReducersState } from '../category-reducers.interface'; | ||
|
||
/** | ||
* A token to hold the injectable extra reducers. | ||
* | ||
* Prefer using {@link daffCategoryProvideExtraReducers}. | ||
*/ | ||
export const DAFF_CATEGORY_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffCategoryReducersState>[]>( | ||
export const { | ||
/** | ||
* A token to hold the injectable extra reducers. | ||
* | ||
* Prefer using {@link daffCategoryProvideExtraReducers}. | ||
*/ | ||
token: DAFF_CATEGORY_EXTRA_REDUCERS, | ||
/** | ||
* Provides additional reducers that run after the standard Daffodil category reducers. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffCategoryProvideExtraReducers( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffCategoryProvideExtraReducers, | ||
} = createMultiInjectionToken<ActionReducer<DaffCategoryReducersState>>( | ||
'DAFF_CATEGORY_EXTRA_REDUCERS', | ||
{ | ||
factory: () => [], | ||
providedIn: 'any', | ||
}, | ||
{ providedIn: 'any' }, | ||
); | ||
|
||
/** | ||
* Provides additional reducers that run after the standard Daffodil category reducers. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffCategoryProvideExtraReducers( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffCategoryProvideExtraReducers( | ||
...reducers: ActionReducer<DaffCategoryReducersState>[] | ||
): Provider[] { | ||
return reducers.map(reducer => ({ | ||
provide: DAFF_CATEGORY_EXTRA_REDUCERS, | ||
useValue: reducer, | ||
multi: true, | ||
})); | ||
} |
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