Skip to content

Commit

Permalink
feat(authorizenet): create injection tokens with factory (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent e536798 commit f96294f
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { InjectionToken } from '@angular/core';
import { createSingleInjectionToken } from '@daffodil/core';

export const DaffAuthorizeNetConfigToken = new InjectionToken<DaffAuthorizeNetConfig>('DaffAuthorizeNetConfigToken');
export const {
token: DaffAuthorizeNetConfigToken,
provider: daffProvideAuthorizeNetConfigToken,
} = createSingleInjectionToken<DaffAuthorizeNetConfig>('DaffAuthorizeNetConfigToken');

/**
* An interface for providing @daffodil/authorizenet with needed configurations specific to your authorizenet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { InjectionToken } from '@angular/core';
import { createSingleInjectionToken } from '@daffodil/core';

export const DaffAuthorizeNetPaymentId = new InjectionToken<string>('DaffAuthorizeNetPaymentId');
export const {
token: DaffAuthorizeNetPaymentId,
provider: daffProvideAuthorizeNetPaymentId,
} = createSingleInjectionToken<string>('DaffAuthorizeNetPaymentId');
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import { DaffAuthorizeNetTokenRequest } from '@daffodil/authorizenet';
import { createSingleInjectionToken } from '@daffodil/core';

export const DaffAuthorizeNetDriver = new InjectionToken<DaffAuthorizeNetService>('DaffAuthorizeNetDriver');
export const {
token: DaffAuthorizeNetDriver,
provider: daffProvideAuthorizeNetDriver,
} = createSingleInjectionToken<DaffAuthorizeNetService>('DaffAuthorizeNetDriver');

export interface DaffAuthorizeNetService<T extends DaffAuthorizeNetTokenRequest = DaffAuthorizeNetTokenRequest> {
generateToken(paymentTokenRequest: T): Observable<any>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import {
DaffAuthorizenetPaymentRequest,
DaffAuthorizenetPaymentResponse,
} from '@daffodil/authorizenet';
import { createSingleInjectionToken } from '@daffodil/core';
import { DaffPaymentDriverInterface } from '@daffodil/payment/driver';

/**
* An injection token for the authorize.net payment driver.
*/
export const DaffAuthorizeNetPaymentDriver = new InjectionToken<DaffAuthorizeNetPaymentDriverInterface>('DaffAuthorizeNetPaymentDriver');
export const {
/**
* An injection token for the authorize.net payment driver.
*/
token: DaffAuthorizeNetPaymentDriver,
provider: daffProvideAuthorizeNetPaymentDriver,
} = createSingleInjectionToken<DaffAuthorizeNetPaymentDriverInterface>('DaffAuthorizeNetPaymentDriver');

/**
* @inheritdoc
Expand Down
30 changes: 17 additions & 13 deletions libs/authorizenet/src/services/accept-js-production.token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { InjectionToken } from '@angular/core';
import { createSingleInjectionToken } from '@daffodil/core';

/**
* A token that represents the version to use for Accept.JS calls.
* A "true" value configures the package to use the production AcceptJs endpoint,
* and a "false" value configures it to use the Sandbox AcceptJs endpoint.
* By default, we assume you're using the sandbox (false).
*/
export const DAFF_AUTHORIZENET_ACCEPT_JS_PRODUCTION = new InjectionToken<
boolean
>('DAFF_AUTHORIZENET_ACCEPT_JS_PRODUCTION', {
providedIn: 'root',
factory: () => false,
});
export const {
/**
* A token that represents the version to use for Accept.JS calls.
* A "true" value configures the package to use the production AcceptJs endpoint,
* and a "false" value configures it to use the Sandbox AcceptJs endpoint.
* By default, we assume you're using the sandbox (false).
*/
token: DAFF_AUTHORIZENET_ACCEPT_JS_PRODUCTION,
provider: daffProvideAuthorizenetAcceptJsProduction,
} = createSingleInjectionToken<boolean>(
'DAFF_AUTHORIZENET_ACCEPT_JS_PRODUCTION',
{
providedIn: 'root',
factory: () => false,
},
);
15 changes: 5 additions & 10 deletions libs/authorizenet/state/src/config/token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
InjectionToken,
ValueProvider,
} from '@angular/core';
import { createConfigInjectionToken } from '@daffodil/core';

import { daffAuthorizeNetStateDefaultConfig } from './default';
import { DaffAuthorizeNetStateConfig } from './type';

export const DAFF_AUTHORIZE_NET_STATE_CONFIG = new InjectionToken('DAFF_AUTHORIZE_NET_STATE_CONFIG', { factory: () => daffAuthorizeNetStateDefaultConfig });

export const provideDaffAuthorizeNetStateConfig = (config: DaffAuthorizeNetStateConfig): ValueProvider => ({
provide: DAFF_AUTHORIZE_NET_STATE_CONFIG,
useValue: config,
});
export const {
token: DAFF_AUTHORIZE_NET_STATE_CONFIG,
provider: provideDaffAuthorizeNetStateConfig,
} = createConfigInjectionToken<DaffAuthorizeNetStateConfig>(daffAuthorizeNetStateDefaultConfig, 'DAFF_AUTHORIZE_NET_STATE_CONFIG');
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_AUTHORIZENET_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
export const {
token: DAFF_AUTHORIZENET_ERROR_MATCHER,
provider: daffProvideAuthorizenetErrorMatcher,
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
'DAFF_AUTHORIZENET_ERROR_MATCHER',
{ factory: () => daffTransformErrorToStateError },
);
2 changes: 1 addition & 1 deletion libs/authorizenet/state/src/injection-tokens/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DAFF_AUTHORIZENET_ERROR_MATCHER } from './error-matcher.token';
export * from './error-matcher.token';
61 changes: 25 additions & 36 deletions libs/authorizenet/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 { DaffAuthorizeNetReducersState } from '../authorize-net-reducers.interface';

/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffAuthorizeNetProvideExtraReducers}.
*/
export const DAFF_AUTHORIZE_NET_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffAuthorizeNetReducersState>[]>(
export const {
/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffAuthorizeNetProvideExtraReducers}.
*/
token: DAFF_AUTHORIZE_NET_EXTRA_REDUCERS,

/**
* Provides additional reducers that run after the standard Daffodil authorizenet reducers.
*
* ```ts
* providers: [
* ...daffAuthorizeNetProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
provider: daffAuthorizeNetProvideExtraReducers,
} = createMultiInjectionToken<ActionReducer<DaffAuthorizeNetReducersState>>(
'DAFF_AUTHORIZE_NET_EXTRA_REDUCERS',
{
factory: () => [],
providedIn: 'any',
},
{ providedIn: 'any' },
);

/**
* Provides additional reducers that run after the standard Daffodil authorizenet reducers.
*
* ```ts
* providers: [
* ...daffAuthorizeNetProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
export function daffAuthorizeNetProvideExtraReducers(
...reducers: ActionReducer<DaffAuthorizeNetReducersState>[]
): Provider[] {
return reducers.map(reducer => ({
provide: DAFF_AUTHORIZE_NET_EXTRA_REDUCERS,
useValue: reducer,
multi: true,
}));
}
23 changes: 12 additions & 11 deletions libs/authorizenet/state/src/reducers/token/reducers.token.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
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 { DAFF_AUTHORIZE_NET_EXTRA_REDUCERS } from './extra.token';
import { daffAuthorizeNetReducer } from '../authorize-net/authorize-net.reducer';
import { DaffAuthorizeNetReducersState } from '../authorize-net-reducers.interface';

/**
* An internal token to hold the Daffodil authorizenet reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
export const DAFF_AUTHORIZE_NET_REDUCERS = new InjectionToken<ActionReducer<DaffAuthorizeNetReducersState>>(
export const {
/**
* An internal token to hold the Daffodil authorizenet reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
token: DAFF_AUTHORIZE_NET_REDUCERS,
provider: daffProvideAuthorizeNetReducers,
} = createSingleInjectionToken<ActionReducer<DaffAuthorizeNetReducersState>>(
'DAFF_AUTHORIZE_NET_REDUCERS',
{
providedIn: 'any',
Expand Down

0 comments on commit f96294f

Please sign in to comment.