|
1 |
| -import { InjectionToken } from '@angular/core'; |
2 | 1 | import { RequestInfo } from 'angular-in-memory-web-api';
|
3 | 2 |
|
4 | 3 | import { DaffCart } from '@daffodil/cart';
|
| 4 | +import { createSingleInjectionToken } from '@daffodil/core'; |
5 | 5 |
|
6 | 6 | export type DaffCartInMemoryExtraAttributesHook = (reqInfo: RequestInfo, cart: DaffCart) => Record<string, any>;
|
7 | 7 |
|
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 | +); |
0 commit comments