Skip to content

Commit

Permalink
feat(customer-payment): create injection tokens with factory (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent 31af2df commit 81aecc5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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,
DaffCustomerPaymentRequest,
} from '@daffodil/customer-payment';
import { DaffPersonalAddress } from '@daffodil/geography';

/**
* An injection token for the customer payment driver.
*/
export const DaffCustomerPaymentDriver = new InjectionToken<DaffCustomerPaymentDriverInterface>('DaffCustomerPaymentDriver');
export const {
/**
* An injection token for the customer payment driver.
*/
token: DaffCustomerPaymentDriver,
provider: daffProvideCustomerPaymentDriver,
} = createSingleInjectionToken<DaffCustomerPaymentDriverInterface>('DaffCustomerPaymentDriver');

/**
* The customer payment driver is responsible for loading customers.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DaffCustomerPaymentRequestTransform[]>(
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<DaffCustomerPaymentRequestTransform>(
'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,
}));
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_CUSTOMER_PAYMENT_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_CUSTOMER_PAYMENT_ERROR_MATCHER,
provider: daffProvideCustomerPaymentErrorMatcher,
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
'DAFF_CUSTOMER_PAYMENT_ERROR_MATCHER',
{ factory: () => daffTransformErrorToStateError },
);
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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<Record<DaffCustomerPaymentRequestTransform['kind'], DaffCustomerPaymentRequestTransform>>(
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<Record<DaffCustomerPaymentRequestTransform['kind'], DaffCustomerPaymentRequestTransform>>(
'DAFF_CUSTOMER_PAYMENT_REQUEST_TRANSFORM_COLLECTION',
{
factory: () => {
Expand Down
61 changes: 25 additions & 36 deletions libs/customer-payment/state/src/reducers/token/extra.token.ts
Original file line number Diff line number Diff line change
@@ -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<ActionReducer<DaffCustomerPaymentReducersState>[]>(
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<ActionReducer<DaffCustomerPaymentReducersState>>(
'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<DaffCustomerPaymentReducersState>[]
): Provider[] {
return reducers.map(reducer => ({
provide: DAFF_CUSTOMER_PAYMENT_EXTRA_REDUCERS,
useValue: reducer,
multi: true,
}));
}
23 changes: 12 additions & 11 deletions libs/customer-payment/state/src/reducers/token/reducers.token.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<ActionReducer<DaffCustomerPaymentReducersState>>(
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<ActionReducer<DaffCustomerPaymentReducersState>>(
'DAFF_CUSTOMER_PAYMENT_REDUCERS',
{
providedIn: 'any',
Expand Down

0 comments on commit 81aecc5

Please sign in to comment.