Skip to content

Commit e536798

Browse files
authored
feat(cart): create injection tokens with factory (#3235)
1 parent 8f078f5 commit e536798

37 files changed

+358
-350
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
import { InjectionToken } from '@angular/core';
21
import { RequestInfo } from 'angular-in-memory-web-api';
32

43
import { DaffCart } from '@daffodil/cart';
4+
import { createSingleInjectionToken } from '@daffodil/core';
55

66
export type DaffCartInMemoryExtraAttributesHook = (reqInfo: RequestInfo, cart: DaffCart) => Record<string, any>;
77

8-
/**
9-
* Allows an app dev to generate extra fields in the in-memory backend.
10-
* This enables the in-memory drivers to return responses similar to what the
11-
* frontend would expect from the production platform.
12-
*
13-
* The value returned by the hook function will be set to the returned cart's `extra_attributes` field
14-
* for driver calls that return a cart.
15-
*
16-
* The following example demonstrates adding the `numberOfCartItems` field to `extra_attributes`:
17-
* ```ts
18-
* (reqInfo: RequestInfo, cart: DaffCart) => ({
19-
* numberOfCartItems: cart.items.length
20-
* })
21-
* ```
22-
*
23-
* Note that this and any `extra_attributes` features are for advanced users
24-
* and should be used with care.
25-
*/
26-
export const DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK =
27-
new InjectionToken<DaffCartInMemoryExtraAttributesHook>('DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK', {
28-
factory: () => (reqInfo, cart) => ({}),
29-
});
8+
export const {
9+
/**
10+
* Allows an app dev to generate extra fields in the in-memory backend.
11+
* This enables the in-memory drivers to return responses similar to what the
12+
* frontend would expect from the production platform.
13+
*
14+
* The value returned by the hook function will be set to the returned cart's `extra_attributes` field
15+
* for driver calls that return a cart.
16+
*
17+
* The following example demonstrates adding the `numberOfCartItems` field to `extra_attributes`:
18+
* ```ts
19+
* (reqInfo: RequestInfo, cart: DaffCart) => ({
20+
* numberOfCartItems: cart.items.length
21+
* })
22+
* ```
23+
*
24+
* Note that this and any `extra_attributes` features are for advanced users
25+
* and should be used with care.
26+
*/
27+
token: DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK,
28+
provider: daffProvideCartInMemoryExtraAttributesHook,
29+
} = createSingleInjectionToken<DaffCartInMemoryExtraAttributesHook>(
30+
'DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK',
31+
{ factory: () => (reqInfo, cart) => ({}) },
32+
);
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import {
2-
inject,
3-
InjectionToken,
4-
} from '@angular/core';
1+
import { inject } from '@angular/core';
52
import { Apollo } from 'apollo-angular';
63

4+
import { createSingleInjectionToken } from '@daffodil/core';
75
import { DaffQueuedApollo } from '@daffodil/core/graphql';
86

9-
export const DAFF_MAGENTO_CART_MUTATION_QUEUE = new InjectionToken<DaffQueuedApollo>('DAFF_MAGENTO_CART_MUTATION_QUEUE', {
10-
providedIn: 'root',
11-
factory: () => new DaffQueuedApollo(inject(Apollo)),
12-
});
7+
export const {
8+
token: DAFF_MAGENTO_CART_MUTATION_QUEUE,
9+
provider: daffProvideCartMagentoMutationQueue,
10+
} = createSingleInjectionToken<DaffQueuedApollo>(
11+
'DAFF_MAGENTO_CART_MUTATION_QUEUE', {
12+
providedIn: 'root',
13+
factory: () => new DaffQueuedApollo(inject(Apollo)),
14+
},
15+
);
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { InjectionToken } from '@angular/core';
21
import { DocumentNode } from 'graphql';
32

4-
/**
5-
* An multi-provider injection token for providing extra GraphQL fragments that will be spread into cart queries.
6-
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
7-
* The data will appear in DaffCart#extra_attributes.
8-
*
9-
* Fragment structure is platform-specific and this feature should be used with care.
10-
*
11-
* This can increase query complexity above the Magento default of 300.
12-
* Daffodil only guarantees that stock queries don't exceed the limit of 300.
13-
* Apps using this token should therefore increase the query complexity limit
14-
* to accommodate the extra complexity contributed by the provided fragments.
15-
*/
16-
export const DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS = new InjectionToken<DocumentNode[]>(
17-
'DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS',
18-
{ factory: () => []},
19-
);
3+
import { createMultiInjectionToken } from '@daffodil/core';
4+
5+
export const {
6+
/**
7+
* An multi-provider injection token for providing extra GraphQL fragments that will be spread into cart queries.
8+
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
9+
* The data will appear in DaffCart#extra_attributes.
10+
*
11+
* Fragment structure is platform-specific and this feature should be used with care.
12+
*
13+
* This can increase query complexity above the Magento default of 300.
14+
* Daffodil only guarantees that stock queries don't exceed the limit of 300.
15+
* Apps using this token should therefore increase the query complexity limit
16+
* to accommodate the extra complexity contributed by the provided fragments.
17+
*/
18+
token: DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS,
19+
provider: daffProvideCartMagentoExtraCartFragments,
20+
} = createMultiInjectionToken<DocumentNode>('DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS');
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
1-
import {
2-
InjectionToken,
3-
Provider,
4-
} from '@angular/core';
5-
61
// workaround https://github.com/graycoreio/daffodil/issues/1667
72
import { DaffCartItem } from '@daffodil/cart';
3+
import { createMultiInjectionToken } from '@daffodil/core';
84

95
import { DaffCartMagentoCartItemTransform } from '../../../interfaces/public_api';
106

11-
/**
12-
* A multi-provider injection token for providing cart item transform logic in the Magento driver.
13-
* It is run after the standard transforms and passed both the current transformed Daffodil cart item and the Magento cart item.
14-
*/
15-
export const DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS = new InjectionToken<DaffCartMagentoCartItemTransform[]>(
16-
'DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS',
17-
{ factory: () => []},
18-
);
7+
export const {
8+
/**
9+
* A multi-provider injection token for providing cart item transform logic in the Magento driver.
10+
* It is run after the standard transforms and passed both the current transformed Daffodil cart item and the Magento cart item.
11+
*/
12+
token: DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS,
1913

20-
/**
21-
* Provides cart item transforms for the Magento cart driver.
22-
*
23-
* See {@link DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS}.
24-
*
25-
* ```ts
26-
* providers: [
27-
* ...daffProvideCartMagentoCartItemTransforms(
28-
* myCartItemTransform
29-
* )
30-
* ]
31-
* ```
32-
*/
33-
export function daffProvideCartMagentoCartItemTransforms(...transforms: DaffCartMagentoCartItemTransform[]): Provider[] {
34-
return transforms.map(transform => ({
35-
provide: DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS,
36-
useValue: transform,
37-
multi: true,
38-
}));
39-
}
14+
/**
15+
* Provides cart item transforms for the Magento cart driver.
16+
*
17+
* See {@link DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS}.
18+
*
19+
* ```ts
20+
* providers: [
21+
* ...daffProvideCartMagentoCartItemTransforms(
22+
* myCartItemTransform
23+
* )
24+
* ]
25+
* ```
26+
*/
27+
provider: daffProvideCartMagentoCartItemTransforms,
28+
} = createMultiInjectionToken<DaffCartMagentoCartItemTransform>('DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS');

libs/cart/driver/magento/src/injection-tokens/transforms/cart/token.ts

+22-29
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,28 @@ import {
55

66
// workaround https://github.com/graycoreio/daffodil/issues/1667
77
import { DaffCart } from '@daffodil/cart';
8+
import { createMultiInjectionToken } from '@daffodil/core';
89

910
import { DaffCartMagentoCartTransform } from '../../../interfaces/public_api';
1011

11-
/**
12-
* A multi-provider injection token for providing cart transform logic in the Magento driver.
13-
* It is run after the standard transforms and passed both the current transformed Daffodil cart and the Magento cart.
14-
*/
15-
export const DAFF_CART_MAGENTO_CART_TRANSFORMS = new InjectionToken<DaffCartMagentoCartTransform[]>(
16-
'DAFF_CART_MAGENTO_CART_TRANSFORMS',
17-
{ factory: () => []},
18-
);
19-
20-
/**
21-
* Provides cart transforms for the Magento cart driver.
22-
*
23-
* See {@link DAFF_CART_MAGENTO_CART_TRANSFORMS}.
24-
*
25-
* ```ts
26-
* providers: [
27-
* ...daffProvideCartMagentoCartTransforms(
28-
* myCartTransform
29-
* )
30-
* ]
31-
* ```
32-
*/
33-
export function daffProvideCartMagentoCartTransforms(...transforms: DaffCartMagentoCartTransform[]): Provider[] {
34-
return transforms.map(transform => ({
35-
provide: DAFF_CART_MAGENTO_CART_TRANSFORMS,
36-
useValue: transform,
37-
multi: true,
38-
}));
39-
}
12+
export const {
13+
/**
14+
* A multi-provider injection token for providing cart transform logic in the Magento driver.
15+
* It is run after the standard transforms and passed both the current transformed Daffodil cart and the Magento cart.
16+
*/
17+
token: DAFF_CART_MAGENTO_CART_TRANSFORMS,
18+
/**
19+
* Provides cart transforms for the Magento cart driver.
20+
*
21+
* See {@link DAFF_CART_MAGENTO_CART_TRANSFORMS}.
22+
*
23+
* ```ts
24+
* providers: [
25+
* ...daffProvideCartMagentoCartTransforms(
26+
* myCartTransform
27+
* )
28+
* ]
29+
* ```
30+
*/
31+
provider: daffProvideCartMagentoCartTransforms,
32+
} = createMultiInjectionToken<DaffCartMagentoCartTransform>('DAFF_CART_MAGENTO_CART_TRANSFORMS');
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
import {
2-
InjectionToken,
3-
ValueProvider,
4-
} from '@angular/core';
5-
61
import { DaffCartPaymentMethod } from '@daffodil/cart';
2+
import { createSingleInjectionToken } from '@daffodil/core';
73

8-
/**
9-
* The payment method payload that correspondes to the free payment method.
10-
* This will be different per-platform and can be passed into the update driver call.
11-
*/
12-
export const DAFF_CART_DRIVER_FREE_PAYMENT_METHOD = new InjectionToken<Partial<DaffCartPaymentMethod>>(
4+
export const {
5+
/**
6+
* The payment method payload that correspondes to the free payment method.
7+
* This will be different per-platform and can be passed into the update driver call.
8+
*/
9+
token: DAFF_CART_DRIVER_FREE_PAYMENT_METHOD,
10+
/**
11+
* Provides the {@link DAFF_CART_DRIVER_FREE_PAYMENT_METHOD} injection token.
12+
*/
13+
provider: daffCartDriverProvideFreePaymentMethod,
14+
} = createSingleInjectionToken<Partial<DaffCartPaymentMethod>>(
1315
'DAFF_CART_DRIVER_FREE_PAYMENT_METHOD',
14-
{
15-
factory: () => ({}),
16-
});
17-
18-
/**
19-
* Provides the {@link DAFF_CART_DRIVER_FREE_PAYMENT_METHOD} injection token.
20-
*/
21-
export function daffCartDriverProvideFreePaymentMethod(method: Partial<DaffCartPaymentMethod>): ValueProvider {
22-
return {
23-
provide: DAFF_CART_DRIVER_FREE_PAYMENT_METHOD,
24-
useValue: method,
25-
};
26-
}
16+
{ factory: () => ({}) },
17+
);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

4-
import { DaffCart } from '@daffodil/cart';
3+
import {
4+
DaffCartAddress,
5+
DaffCart,
6+
} from '@daffodil/cart';
7+
import { createSingleInjectionToken } from '@daffodil/core';
58

69
/**
710
* The interface responsible for managing the address of a cart.
@@ -13,6 +16,7 @@ export interface DaffCartAddressServiceInterface<T extends DaffCart = DaffCart>
1316
update(cartId: T['id'], address: Partial<T['shipping_address'] | T['billing_address']>): Observable<Partial<T>>;
1417
}
1518

16-
export const DaffCartAddressDriver = new InjectionToken<
17-
DaffCartAddressServiceInterface
18-
>('DaffCartAddressDriver');
19+
export const {
20+
token: DaffCartAddressDriver,
21+
provider: daffProvideCartAddressDriver,
22+
} = createSingleInjectionToken<DaffCartAddressServiceInterface>('DaffCartAddressDriver');

libs/cart/driver/src/interfaces/cart-billing-address-service.interface.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

4-
import { DaffCart } from '@daffodil/cart';
3+
import {
4+
DaffCartAddress,
5+
DaffCart,
6+
} from '@daffodil/cart';
7+
import { createSingleInjectionToken } from '@daffodil/core';
58

69
/**
710
* The interface responsible for managing the billing address of a cart.
@@ -18,6 +21,7 @@ export interface DaffCartBillingAddressServiceInterface<T extends DaffCart = Daf
1821
update(cartId: T['id'], address: Partial<T['billing_address']>): Observable<Partial<T>>;
1922
}
2023

21-
export const DaffCartBillingAddressDriver = new InjectionToken<
22-
DaffCartBillingAddressServiceInterface
23-
>('DaffCartBillingAddressDriver');
24+
export const {
25+
token: DaffCartBillingAddressDriver,
26+
provider: daffProvideCartBillingAddressDriver,
27+
} = createSingleInjectionToken<DaffCartBillingAddressServiceInterface>('DaffCartBillingAddressDriver');

libs/cart/driver/src/interfaces/cart-coupon-service.interface.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

4-
import { DaffCart } from '@daffodil/cart';
3+
import {
4+
DaffCart,
5+
DaffCartCoupon,
6+
} from '@daffodil/cart';
7+
import { createSingleInjectionToken } from '@daffodil/core';
58

69
/**
710
* The interface responsible for applying a coupon to a cart.
@@ -28,6 +31,7 @@ export interface DaffCartCouponServiceInterface<T extends DaffCart = DaffCart> {
2831
removeAll(cartId: T['id']): Observable<Partial<T>>;
2932
}
3033

31-
export const DaffCartCouponDriver = new InjectionToken<DaffCartCouponServiceInterface>(
32-
'DaffCartCouponDriver',
33-
);
34+
export const {
35+
token: DaffCartCouponDriver,
36+
provider: daffProvideCartCouponDriver,
37+
} = createSingleInjectionToken<DaffCartCouponServiceInterface>('DaffCartCouponDriver');

libs/cart/driver/src/interfaces/cart-item-service.interface.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

43
import {
54
DaffCartItem,
65
DaffCartItemInput,
76
DaffCart,
87
} from '@daffodil/cart';
8+
import { createSingleInjectionToken } from '@daffodil/core';
99

1010
/**
1111
* The interface responsible for managing the items of a cart.
@@ -44,6 +44,7 @@ export interface DaffCartItemServiceInterface<
4444
delete(cartId: T['id'], itemId: T['items'][number]['id']): Observable<Partial<T>>;
4545
}
4646

47-
export const DaffCartItemDriver = new InjectionToken<
48-
DaffCartItemServiceInterface
49-
>('DaffCartItemDriver');
47+
export const {
48+
token: DaffCartItemDriver,
49+
provider: daffProvideCartItemDriver,
50+
} = createSingleInjectionToken<DaffCartItemServiceInterface>('DaffCartItemDriver');

libs/cart/driver/src/interfaces/cart-order-service.interface.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

43
import {
54
DaffCart,
65
DaffCartOrderResult,
76
} from '@daffodil/cart';
7+
import { createSingleInjectionToken } from '@daffodil/core';
88

99
/**
1010
* The interface responsible for placing an order for the customer's cart.
@@ -19,6 +19,7 @@ export interface DaffCartOrderServiceInterface<
1919
placeOrder(id: T['id'], payment?: T['payment']): Observable<R>;
2020
}
2121

22-
export const DaffCartOrderDriver = new InjectionToken<DaffCartOrderServiceInterface>(
23-
'DaffCartOrderDriver',
24-
);
22+
export const {
23+
token: DaffCartOrderDriver,
24+
provider: daffProvideCartOrderDriver,
25+
} = createSingleInjectionToken<DaffCartOrderServiceInterface>('DaffCartOrderDriver');

0 commit comments

Comments
 (0)