Skip to content

Commit

Permalink
feat(category): create injection tokens with factory (#3238)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent 0518d60 commit 2a9a32d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 113 deletions.
18 changes: 12 additions & 6 deletions libs/category/driver/magento/src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
@@ -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<DaffCategoryMagentoDriverConfig>('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<DaffCategoryMagentoDriverConfig>(
MAGENTO_CATEGORY_CONFIG_DEFAULT,
'MAGENTO_CATEGORY_CONFIG_TOKEN',
);

/**
* An interface for providing @daffodil/category/driver/magento with necessary config values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import {
Expand All @@ -8,6 +7,7 @@ import {
DaffCategoryIdRequest,
DaffCategoryUrlRequest,
} from '@daffodil/category';
import { createSingleInjectionToken } from '@daffodil/core';
import { DaffProduct } from '@daffodil/product';

/**
Expand All @@ -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<any>('DaffCategoryDriver');
export const {
token: DaffCategoryDriver,
provider: daffProvideCategoryDriver,
} = createSingleInjectionToken<any>('DaffCategoryDriver');
23 changes: 10 additions & 13 deletions libs/category/routing/src/injection-tokens/request/builder.token.ts
Original file line number Diff line number Diff line change
@@ -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<DaffCategoryRoutingRequestBuilder>(

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<DaffCategoryRoutingRequestBuilder>(
'DAFF_CATEGORY_ROUTING_OPTIONS_BUILDER',
{
factory: () => () => ({}),
Expand Down
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 libs/category/state/src/injection-tokens/error-matcher.token.ts
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 },
);
2 changes: 1 addition & 1 deletion libs/category/state/src/injection-tokens/public_api.ts
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';
60 changes: 24 additions & 36 deletions libs/category/state/src/reducers/token/extra.token.ts
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,
}));
}
23 changes: 12 additions & 11 deletions libs/category/state/src/reducers/token/reducers.token.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {
inject,
InjectionToken,
} from '@angular/core';
import { inject } from '@angular/core';
import {
ActionReducer,
combineReducers,
} from '@ngrx/store';

// 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<ActionReducer<DaffCategoryReducersState>>(
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<ActionReducer<DaffCategoryReducersState>>(
'DAFF_CATEGORY_REDUCERS',
{
providedIn: 'any',
Expand Down

0 comments on commit 2a9a32d

Please sign in to comment.