From 81aecc5ce4232498bc25967bc0b843b5557a945d Mon Sep 17 00:00:00 2001 From: griest024 Date: Mon, 14 Oct 2024 14:47:38 -0400 Subject: [PATCH] feat(customer-payment): create injection tokens with factory (#3243) --- .../interfaces/payment-service.interface.ts | 17 ++++-- .../available-transforms.token.ts | 39 +++++------- .../injection-tokens/error-matcher.token.ts | 16 ++--- .../state/src/injection-tokens/public_api.ts | 2 +- .../transform-collection.token.ts | 21 ++++--- .../state/src/reducers/token/extra.token.ts | 61 ++++++++----------- .../src/reducers/token/reducers.token.ts | 23 +++---- 7 files changed, 85 insertions(+), 94 deletions(-) diff --git a/libs/customer-payment/driver/src/interfaces/payment-service.interface.ts b/libs/customer-payment/driver/src/interfaces/payment-service.interface.ts index 70be416f94..c6a790284c 100644 --- a/libs/customer-payment/driver/src/interfaces/payment-service.interface.ts +++ b/libs/customer-payment/driver/src/interfaces/payment-service.interface.ts @@ -1,7 +1,9 @@ -import { InjectionToken } from '@angular/core'; import { Observable } from 'rxjs'; -import { DaffIdentifiable } from '@daffodil/core'; +import { + DaffIdentifiable, + createSingleInjectionToken, +} from '@daffodil/core'; import { DaffCustomerAddress } from '@daffodil/customer'; import { DaffCustomerPayment, @@ -9,10 +11,13 @@ import { } from '@daffodil/customer-payment'; import { DaffPersonalAddress } from '@daffodil/geography'; -/** - * An injection token for the customer payment driver. - */ -export const DaffCustomerPaymentDriver = new InjectionToken('DaffCustomerPaymentDriver'); +export const { + /** + * An injection token for the customer payment driver. + */ + token: DaffCustomerPaymentDriver, + provider: daffProvideCustomerPaymentDriver, +} = createSingleInjectionToken('DaffCustomerPaymentDriver'); /** * The customer payment driver is responsible for loading customers. diff --git a/libs/customer-payment/state/src/injection-tokens/available-transforms.token.ts b/libs/customer-payment/state/src/injection-tokens/available-transforms.token.ts index c99420cca4..a8952423f0 100644 --- a/libs/customer-payment/state/src/injection-tokens/available-transforms.token.ts +++ b/libs/customer-payment/state/src/injection-tokens/available-transforms.token.ts @@ -1,29 +1,20 @@ -import { - InjectionToken, - ValueProvider, -} from '@angular/core'; +import { createMultiInjectionToken } from '@daffodil/core'; import { DaffCustomerPaymentRequestTransform } from '../models/public_api'; -/** - * A multi-provider injection token for registering customer payment request transforms. - */ -export const DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORMS = new InjectionToken( +export const { + /** + * A multi-provider injection token for registering customer payment request transforms. + */ + token: DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORMS, + + /** + * Provides a customer payment request transform registration. + * + * See {@link DaffCustomerPaymentRequestTransform}. + */ + provider: daffPaymentProvideAvailableProcessors, +} = createMultiInjectionToken( 'DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORMS', - { - factory: () => [], - providedIn: 'any', - }, + { providedIn: 'any' }, ); - -/** - * Provides a customer payment request transform registration. - * - * See {@link DaffCustomerPaymentRequestTransform}. - */ -export const daffPaymentProvideAvailableProcessors = (...transforms: DaffCustomerPaymentRequestTransform[]): ValueProvider[] => - transforms.map(transform => ({ - provide: DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORMS, - useValue: transform, - multi: true, - })); diff --git a/libs/customer-payment/state/src/injection-tokens/error-matcher.token.ts b/libs/customer-payment/state/src/injection-tokens/error-matcher.token.ts index 4008642c5a..add7287d4e 100644 --- a/libs/customer-payment/state/src/injection-tokens/error-matcher.token.ts +++ b/libs/customer-payment/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_CUSTOMER_PAYMENT_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_CUSTOMER_PAYMENT_ERROR_MATCHER, + provider: daffProvideCustomerPaymentErrorMatcher, +} = createSingleInjectionToken( 'DAFF_CUSTOMER_PAYMENT_ERROR_MATCHER', { factory: () => daffTransformErrorToStateError }, ); diff --git a/libs/customer-payment/state/src/injection-tokens/public_api.ts b/libs/customer-payment/state/src/injection-tokens/public_api.ts index 009a410397..d386afa924 100644 --- a/libs/customer-payment/state/src/injection-tokens/public_api.ts +++ b/libs/customer-payment/state/src/injection-tokens/public_api.ts @@ -3,4 +3,4 @@ export { daffPaymentProvideAvailableProcessors, } from './available-transforms.token'; export { DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORM_COLLECTION } from './transform-collection.token'; -export { DAFF_CUSTOMER_PAYMENT_ERROR_MATCHER } from './error-matcher.token'; +export * from './error-matcher.token'; diff --git a/libs/customer-payment/state/src/injection-tokens/transform-collection.token.ts b/libs/customer-payment/state/src/injection-tokens/transform-collection.token.ts index 44429a5ad1..c4feac72fa 100644 --- a/libs/customer-payment/state/src/injection-tokens/transform-collection.token.ts +++ b/libs/customer-payment/state/src/injection-tokens/transform-collection.token.ts @@ -1,17 +1,20 @@ -import { - inject, - InjectionToken, -} from '@angular/core'; +import { inject } from '@angular/core'; -import { daffArrayToDict } from '@daffodil/core'; +import { + createSingleInjectionToken, + daffArrayToDict, +} from '@daffodil/core'; import { DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORMS } from './available-transforms.token'; import { DaffCustomerPaymentRequestTransform } from '../models/public_api'; -/** - * An internal token to combine the available payment transforms into a single collection. - */ -export const DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORM_COLLECTION = new InjectionToken>( +export const { + /** + * An internal token to combine the available payment transforms into a single collection. + */ + token: DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORM_COLLECTION, + provider: daffProvideCustomerPaymentRequestTransformCollection, +} = createSingleInjectionToken>( 'DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORM_COLLECTION', { factory: () => { diff --git a/libs/customer-payment/state/src/reducers/token/extra.token.ts b/libs/customer-payment/state/src/reducers/token/extra.token.ts index 40c8e6fdb4..c252fd83ce 100644 --- a/libs/customer-payment/state/src/reducers/token/extra.token.ts +++ b/libs/customer-payment/state/src/reducers/token/extra.token.ts @@ -1,42 +1,31 @@ -import { - InjectionToken, - Provider, -} from '@angular/core'; import { ActionReducer } from '@ngrx/store'; +import { createMultiInjectionToken } from '@daffodil/core'; + import { DaffCustomerPaymentReducersState } from '../reducers.interface'; -/** - * A token to hold the injectable extra reducers. - * - * Prefer using {@link daffCustomerPaymentProvideExtraReducers}. - */ -export const DAFF_CUSTOMER_PAYMENT_EXTRA_REDUCERS = new InjectionToken[]>( +export const { + /** + * A token to hold the injectable extra reducers. + * + * Prefer using {@link daffCustomerPaymentProvideExtraReducers}. + */ + token: DAFF_CUSTOMER_PAYMENT_EXTRA_REDUCERS, + + /** + * Provides additional reducers that run after the standard Daffodil customer reducers. + * + * ```ts + * providers: [ + * ...daffCustomerPaymentProvideExtraReducers( + * myReducer1, + * myReducer2 + * ) + * ] + * ``` + */ + provider: daffCustomerPaymentProvideExtraReducers, +} = createMultiInjectionToken>( 'DAFF_CUSTOMER_PAYMENT_EXTRA_REDUCERS', - { - factory: () => [], - providedIn: 'any', - }, + { providedIn: 'any' }, ); - -/** - * Provides additional reducers that run after the standard Daffodil customer reducers. - * - * ```ts - * providers: [ - * ...daffCustomerPaymentProvideExtraReducers( - * myReducer1, - * myReducer2 - * ) - * ] - * ``` - */ -export function daffCustomerPaymentProvideExtraReducers( - ...reducers: ActionReducer[] -): Provider[] { - return reducers.map(reducer => ({ - provide: DAFF_CUSTOMER_PAYMENT_EXTRA_REDUCERS, - useValue: reducer, - multi: true, - })); -} diff --git a/libs/customer-payment/state/src/reducers/token/reducers.token.ts b/libs/customer-payment/state/src/reducers/token/reducers.token.ts index b3996c5427..cfb13a7be9 100644 --- a/libs/customer-payment/state/src/reducers/token/reducers.token.ts +++ b/libs/customer-payment/state/src/reducers/token/reducers.token.ts @@ -1,12 +1,10 @@ -import { - inject, - InjectionToken, -} from '@angular/core'; +import { inject } from '@angular/core'; import { ActionReducer, combineReducers, } from '@ngrx/store'; +import { createSingleInjectionToken } from '@daffodil/core'; import { daffComposeReducers } from '@daffodil/core/state'; // these unused imports are a workaround import { DaffCustomerPayment } from '@daffodil/customer-payment'; @@ -16,13 +14,16 @@ import { daffCustomerPaymentReducer } from '../payment/public_api'; import { daffCustomerPaymentEntitiesReducer } from '../payment-entities/public_api'; import { DaffCustomerPaymentReducersState } from '../reducers.interface'; -/** - * An internal token to hold the Daffodil customer reducers. - * Includes the extra and standard reducers. - * - * @docs-private - */ -export const DAFF_CUSTOMER_PAYMENT_REDUCERS = new InjectionToken>( +export const { + /** + * An internal token to hold the Daffodil customer reducers. + * Includes the extra and standard reducers. + * + * @docs-private + */ + token: DAFF_CUSTOMER_PAYMENT_REDUCERS, + provider: daffProvideCustomerPaymentReducers, +} = createSingleInjectionToken>( 'DAFF_CUSTOMER_PAYMENT_REDUCERS', { providedIn: 'any',