From 2a9a32d642e8d69888dceda7b094b22a4ffd01f1 Mon Sep 17 00:00:00 2001 From: griest024 Date: Mon, 14 Oct 2024 14:46:11 -0400 Subject: [PATCH] feat(category): create injection tokens with factory (#3238) --- .../src/interfaces/config.interface.ts | 18 +++-- .../interfaces/category-service.interface.ts | 7 +- .../injection-tokens/request/builder.token.ts | 23 +++---- .../request/builders.token.ts | 65 ++++++++----------- .../injection-tokens/error-matcher.token.ts | 16 +++-- .../state/src/injection-tokens/public_api.ts | 2 +- .../state/src/reducers/token/extra.token.ts | 60 +++++++---------- .../src/reducers/token/reducers.token.ts | 23 +++---- 8 files changed, 101 insertions(+), 113 deletions(-) diff --git a/libs/category/driver/magento/src/interfaces/config.interface.ts b/libs/category/driver/magento/src/interfaces/config.interface.ts index 62acfeb91f..7affa89afa 100644 --- a/libs/category/driver/magento/src/interfaces/config.interface.ts +++ b/libs/category/driver/magento/src/interfaces/config.interface.ts @@ -1,12 +1,18 @@ -import { InjectionToken } from '@angular/core'; +import { createConfigInjectionToken } from '@daffodil/core'; import { MAGENTO_CATEGORY_CONFIG_DEFAULT } from '../config/default'; -/** - * The token used to provide @daffodil/category/driver/magento config data. - * Mandatory for the Magento driver. - */ -export const MAGENTO_CATEGORY_CONFIG_TOKEN = new InjectionToken('MAGENTO_CATEGORY_CONFIG_TOKEN', { factory: () => MAGENTO_CATEGORY_CONFIG_DEFAULT }); +export const { + /** + * The token used to provide @daffodil/category/driver/magento config data. + * Mandatory for the Magento driver. + */ + token: MAGENTO_CATEGORY_CONFIG_TOKEN, + provider: daffProvideAnalyticsConfig, +} = createConfigInjectionToken( + MAGENTO_CATEGORY_CONFIG_DEFAULT, + 'MAGENTO_CATEGORY_CONFIG_TOKEN', +); /** * An interface for providing @daffodil/category/driver/magento with necessary config values. diff --git a/libs/category/driver/src/interfaces/category-service.interface.ts b/libs/category/driver/src/interfaces/category-service.interface.ts index 293a52ec9b..343edf9aa1 100644 --- a/libs/category/driver/src/interfaces/category-service.interface.ts +++ b/libs/category/driver/src/interfaces/category-service.interface.ts @@ -1,4 +1,3 @@ -import { InjectionToken } from '@angular/core'; import { Observable } from 'rxjs'; import { @@ -8,6 +7,7 @@ import { DaffCategoryIdRequest, DaffCategoryUrlRequest, } from '@daffodil/category'; +import { createSingleInjectionToken } from '@daffodil/core'; import { DaffProduct } from '@daffodil/product'; /** @@ -29,4 +29,7 @@ export interface DaffCategoryServiceInterface< //TODO(damienwebdev): This any generic is necessary until we ship Ivy packages, do not change it. //See: https://github.com/ng-packagr/ng-packagr/issues/1844 -export const DaffCategoryDriver = new InjectionToken('DaffCategoryDriver'); +export const { + token: DaffCategoryDriver, + provider: daffProvideCategoryDriver, +} = createSingleInjectionToken('DaffCategoryDriver'); diff --git a/libs/category/routing/src/injection-tokens/request/builder.token.ts b/libs/category/routing/src/injection-tokens/request/builder.token.ts index 54a8878c0f..dc4d83ae78 100644 --- a/libs/category/routing/src/injection-tokens/request/builder.token.ts +++ b/libs/category/routing/src/injection-tokens/request/builder.token.ts @@ -1,18 +1,15 @@ -import { - inject, - InjectionToken, -} from '@angular/core'; -import { ActivatedRouteSnapshot } from '@angular/router'; +import { createSingleInjectionToken } from '@daffodil/core'; -import { - DaffCategoryRoutingRequestBuilder, - DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS, -} from './builders.token'; +import { DaffCategoryRoutingRequestBuilder } from './builders.token'; -/** - * An internal token to combine the {@link DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS} into a single builder. - */ -export const DAFF_CATEGORY_ROUTING_OPTIONS_BUILDER = new InjectionToken( + +export const { + /** + * An internal token to combine the {@link DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS} into a single builder. + */ + token: DAFF_CATEGORY_ROUTING_OPTIONS_BUILDER, + provider: daffProvideCategoryRoutingOptionsBuilder, +} = createSingleInjectionToken( 'DAFF_CATEGORY_ROUTING_OPTIONS_BUILDER', { factory: () => () => ({}), diff --git a/libs/category/routing/src/injection-tokens/request/builders.token.ts b/libs/category/routing/src/injection-tokens/request/builders.token.ts index 1de0655720..aba303c230 100644 --- a/libs/category/routing/src/injection-tokens/request/builders.token.ts +++ b/libs/category/routing/src/injection-tokens/request/builders.token.ts @@ -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 = (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( - '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('DAFF_CATEGORY_ROUTING_OPTIONS_BUILDERS'); diff --git a/libs/category/state/src/injection-tokens/error-matcher.token.ts b/libs/category/state/src/injection-tokens/error-matcher.token.ts index 7dd7d5606e..8416533254 100644 --- a/libs/category/state/src/injection-tokens/error-matcher.token.ts +++ b/libs/category/state/src/injection-tokens/error-matcher.token.ts @@ -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( +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( 'DAFF_CATEGORY_ERROR_MATCHER', { factory: () => daffTransformErrorToStateError }, ); diff --git a/libs/category/state/src/injection-tokens/public_api.ts b/libs/category/state/src/injection-tokens/public_api.ts index 0db7fb0c13..315411d8fd 100644 --- a/libs/category/state/src/injection-tokens/public_api.ts +++ b/libs/category/state/src/injection-tokens/public_api.ts @@ -1 +1 @@ -export { DAFF_CATEGORY_ERROR_MATCHER } from './error-matcher.token'; +export * from './error-matcher.token'; diff --git a/libs/category/state/src/reducers/token/extra.token.ts b/libs/category/state/src/reducers/token/extra.token.ts index 667cffe411..b148f52779 100644 --- a/libs/category/state/src/reducers/token/extra.token.ts +++ b/libs/category/state/src/reducers/token/extra.token.ts @@ -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[]>( +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>( '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[] -): Provider[] { - return reducers.map(reducer => ({ - provide: DAFF_CATEGORY_EXTRA_REDUCERS, - useValue: reducer, - multi: true, - })); -} diff --git a/libs/category/state/src/reducers/token/reducers.token.ts b/libs/category/state/src/reducers/token/reducers.token.ts index 3b16d01a84..940fee2158 100644 --- a/libs/category/state/src/reducers/token/reducers.token.ts +++ b/libs/category/state/src/reducers/token/reducers.token.ts @@ -1,7 +1,4 @@ -import { - inject, - InjectionToken, -} from '@angular/core'; +import { inject } from '@angular/core'; import { ActionReducer, combineReducers, @@ -9,19 +6,23 @@ import { // these unused imports are a workaround import { DaffCategory } from '@daffodil/category'; +import { createSingleInjectionToken } from '@daffodil/core'; import { daffComposeReducers } from '@daffodil/core/state'; import { DAFF_CATEGORY_EXTRA_REDUCERS } from './extra.token'; import { daffCategoryReducers } from '../category-reducers'; import { DaffCategoryReducersState } from '../category-reducers.interface'; -/** - * An internal token to hold the Daffodil category reducers. - * Includes the extra and standard reducers. - * - * @docs-private - */ -export const DAFF_CATEGORY_REDUCERS = new InjectionToken>( +export const { + /** + * An internal token to hold the Daffodil category reducers. + * Includes the extra and standard reducers. + * + * @docs-private + */ + token: DAFF_CATEGORY_REDUCERS, + provider: daffProvideCategoryReducers, +} = createSingleInjectionToken>( 'DAFF_CATEGORY_REDUCERS', { providedIn: 'any',