-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cart): create injection tokens with factory (#3235)
- Loading branch information
Showing
37 changed files
with
358 additions
and
350 deletions.
There are no files selected for viewing
49 changes: 26 additions & 23 deletions
49
libs/cart/driver/in-memory/src/injection-tokens/extra-attributes-hook.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { RequestInfo } from 'angular-in-memory-web-api'; | ||
|
||
import { DaffCart } from '@daffodil/cart'; | ||
import { createSingleInjectionToken } from '@daffodil/core'; | ||
|
||
export type DaffCartInMemoryExtraAttributesHook = (reqInfo: RequestInfo, cart: DaffCart) => Record<string, any>; | ||
|
||
/** | ||
* Allows an app dev to generate extra fields in the in-memory backend. | ||
* This enables the in-memory drivers to return responses similar to what the | ||
* frontend would expect from the production platform. | ||
* | ||
* The value returned by the hook function will be set to the returned cart's `extra_attributes` field | ||
* for driver calls that return a cart. | ||
* | ||
* The following example demonstrates adding the `numberOfCartItems` field to `extra_attributes`: | ||
* ```ts | ||
* (reqInfo: RequestInfo, cart: DaffCart) => ({ | ||
* numberOfCartItems: cart.items.length | ||
* }) | ||
* ``` | ||
* | ||
* Note that this and any `extra_attributes` features are for advanced users | ||
* and should be used with care. | ||
*/ | ||
export const DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK = | ||
new InjectionToken<DaffCartInMemoryExtraAttributesHook>('DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK', { | ||
factory: () => (reqInfo, cart) => ({}), | ||
}); | ||
export const { | ||
/** | ||
* Allows an app dev to generate extra fields in the in-memory backend. | ||
* This enables the in-memory drivers to return responses similar to what the | ||
* frontend would expect from the production platform. | ||
* | ||
* The value returned by the hook function will be set to the returned cart's `extra_attributes` field | ||
* for driver calls that return a cart. | ||
* | ||
* The following example demonstrates adding the `numberOfCartItems` field to `extra_attributes`: | ||
* ```ts | ||
* (reqInfo: RequestInfo, cart: DaffCart) => ({ | ||
* numberOfCartItems: cart.items.length | ||
* }) | ||
* ``` | ||
* | ||
* Note that this and any `extra_attributes` features are for advanced users | ||
* and should be used with care. | ||
*/ | ||
token: DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, | ||
provider: daffProvideCartInMemoryExtraAttributesHook, | ||
} = createSingleInjectionToken<DaffCartInMemoryExtraAttributesHook>( | ||
'DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK', | ||
{ factory: () => (reqInfo, cart) => ({}) }, | ||
); |
19 changes: 11 additions & 8 deletions
19
libs/cart/driver/magento/src/injection-tokens/cart-mutation-queue.token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { | ||
inject, | ||
InjectionToken, | ||
} from '@angular/core'; | ||
import { inject } from '@angular/core'; | ||
import { Apollo } from 'apollo-angular'; | ||
|
||
import { createSingleInjectionToken } from '@daffodil/core'; | ||
import { DaffQueuedApollo } from '@daffodil/core/graphql'; | ||
|
||
export const DAFF_MAGENTO_CART_MUTATION_QUEUE = new InjectionToken<DaffQueuedApollo>('DAFF_MAGENTO_CART_MUTATION_QUEUE', { | ||
providedIn: 'root', | ||
factory: () => new DaffQueuedApollo(inject(Apollo)), | ||
}); | ||
export const { | ||
token: DAFF_MAGENTO_CART_MUTATION_QUEUE, | ||
provider: daffProvideCartMagentoMutationQueue, | ||
} = createSingleInjectionToken<DaffQueuedApollo>( | ||
'DAFF_MAGENTO_CART_MUTATION_QUEUE', { | ||
providedIn: 'root', | ||
factory: () => new DaffQueuedApollo(inject(Apollo)), | ||
}, | ||
); |
35 changes: 18 additions & 17 deletions
35
libs/cart/driver/magento/src/injection-tokens/fragments/cart.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { DocumentNode } from 'graphql'; | ||
|
||
/** | ||
* An multi-provider injection token for providing extra GraphQL fragments that will be spread into cart queries. | ||
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces. | ||
* The data will appear in DaffCart#extra_attributes. | ||
* | ||
* Fragment structure is platform-specific and this feature should be used with care. | ||
* | ||
* This can increase query complexity above the Magento default of 300. | ||
* Daffodil only guarantees that stock queries don't exceed the limit of 300. | ||
* Apps using this token should therefore increase the query complexity limit | ||
* to accommodate the extra complexity contributed by the provided fragments. | ||
*/ | ||
export const DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS = new InjectionToken<DocumentNode[]>( | ||
'DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS', | ||
{ factory: () => []}, | ||
); | ||
import { createMultiInjectionToken } from '@daffodil/core'; | ||
|
||
export const { | ||
/** | ||
* An multi-provider injection token for providing extra GraphQL fragments that will be spread into cart queries. | ||
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces. | ||
* The data will appear in DaffCart#extra_attributes. | ||
* | ||
* Fragment structure is platform-specific and this feature should be used with care. | ||
* | ||
* This can increase query complexity above the Magento default of 300. | ||
* Daffodil only guarantees that stock queries don't exceed the limit of 300. | ||
* Apps using this token should therefore increase the query complexity limit | ||
* to accommodate the extra complexity contributed by the provided fragments. | ||
*/ | ||
token: DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS, | ||
provider: daffProvideCartMagentoExtraCartFragments, | ||
} = createMultiInjectionToken<DocumentNode>('DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS'); |
55 changes: 22 additions & 33 deletions
55
libs/cart/driver/magento/src/injection-tokens/transforms/cart-item/token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,28 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
} from '@angular/core'; | ||
|
||
// workaround https://github.com/graycoreio/daffodil/issues/1667 | ||
import { DaffCartItem } from '@daffodil/cart'; | ||
import { createMultiInjectionToken } from '@daffodil/core'; | ||
|
||
import { DaffCartMagentoCartItemTransform } from '../../../interfaces/public_api'; | ||
|
||
/** | ||
* A multi-provider injection token for providing cart item transform logic in the Magento driver. | ||
* It is run after the standard transforms and passed both the current transformed Daffodil cart item and the Magento cart item. | ||
*/ | ||
export const DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS = new InjectionToken<DaffCartMagentoCartItemTransform[]>( | ||
'DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS', | ||
{ factory: () => []}, | ||
); | ||
export const { | ||
/** | ||
* A multi-provider injection token for providing cart item transform logic in the Magento driver. | ||
* It is run after the standard transforms and passed both the current transformed Daffodil cart item and the Magento cart item. | ||
*/ | ||
token: DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS, | ||
|
||
/** | ||
* Provides cart item transforms for the Magento cart driver. | ||
* | ||
* See {@link DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideCartMagentoCartItemTransforms( | ||
* myCartItemTransform | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffProvideCartMagentoCartItemTransforms(...transforms: DaffCartMagentoCartItemTransform[]): Provider[] { | ||
return transforms.map(transform => ({ | ||
provide: DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS, | ||
useValue: transform, | ||
multi: true, | ||
})); | ||
} | ||
/** | ||
* Provides cart item transforms for the Magento cart driver. | ||
* | ||
* See {@link DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS}. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffProvideCartMagentoCartItemTransforms( | ||
* myCartItemTransform | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
provider: daffProvideCartMagentoCartItemTransforms, | ||
} = createMultiInjectionToken<DaffCartMagentoCartItemTransform>('DAFF_CART_MAGENTO_CART_ITEM_TRANSFORMS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 14 additions & 23 deletions
37
libs/cart/driver/src/injection-tokens/free-payment-method.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
import { | ||
InjectionToken, | ||
ValueProvider, | ||
} from '@angular/core'; | ||
|
||
import { DaffCartPaymentMethod } from '@daffodil/cart'; | ||
import { createSingleInjectionToken } from '@daffodil/core'; | ||
|
||
/** | ||
* The payment method payload that correspondes to the free payment method. | ||
* This will be different per-platform and can be passed into the update driver call. | ||
*/ | ||
export const DAFF_CART_DRIVER_FREE_PAYMENT_METHOD = new InjectionToken<Partial<DaffCartPaymentMethod>>( | ||
export const { | ||
/** | ||
* The payment method payload that correspondes to the free payment method. | ||
* This will be different per-platform and can be passed into the update driver call. | ||
*/ | ||
token: DAFF_CART_DRIVER_FREE_PAYMENT_METHOD, | ||
/** | ||
* Provides the {@link DAFF_CART_DRIVER_FREE_PAYMENT_METHOD} injection token. | ||
*/ | ||
provider: daffCartDriverProvideFreePaymentMethod, | ||
} = createSingleInjectionToken<Partial<DaffCartPaymentMethod>>( | ||
'DAFF_CART_DRIVER_FREE_PAYMENT_METHOD', | ||
{ | ||
factory: () => ({}), | ||
}); | ||
|
||
/** | ||
* Provides the {@link DAFF_CART_DRIVER_FREE_PAYMENT_METHOD} injection token. | ||
*/ | ||
export function daffCartDriverProvideFreePaymentMethod(method: Partial<DaffCartPaymentMethod>): ValueProvider { | ||
return { | ||
provide: DAFF_CART_DRIVER_FREE_PAYMENT_METHOD, | ||
useValue: method, | ||
}; | ||
} | ||
{ factory: () => ({}) }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.