فهرست منبع

refactor(core): Remove restrictions on config arg types

Michael Bromley 5 سال پیش
والد
کامیت
7f6db2ee8b

+ 0 - 2
packages/common/src/shared-types.ts

@@ -109,8 +109,6 @@ export type CustomFieldType = 'string' | 'localeString' | 'int' | 'float' | 'boo
  */
 export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'ID';
 
-export type ConfigArgSubset<T extends ConfigArgType> = T;
-
 export type CustomFieldsObject = { [key: string]: any };
 
 /**

+ 6 - 7
packages/core/src/common/configurable-operation.ts

@@ -92,15 +92,15 @@ export type ConfigArgDef<T extends ConfigArgType> = T extends 'string'
  * @docsCategory common
  * @docsPage Configurable Operations
  */
-export type ConfigArgs<T extends ConfigArgType> = {
-    [name: string]: ConfigArgDef<T>;
+export type ConfigArgs = {
+    [name: string]: ConfigArgDef<ConfigArgType>;
 };
 
 /**
  * Represents the ConfigArgs once they have been coerced into JavaScript values for use
  * in business logic.
  */
-export type ConfigArgValues<T extends ConfigArgs<any>> = {
+export type ConfigArgValues<T extends ConfigArgs> = {
     [K in keyof T]: T[K] extends ConfigArgListDef<'int' | 'float'>
         ? number[]
         : T[K] extends ConfigArgDef<'int' | 'float'>
@@ -130,8 +130,7 @@ export type ConfigArgValues<T extends ConfigArgs<any>> = {
  * @docsCategory common
  * @docsPage Configurable Operations
  */
-export interface ConfigurableOperationDefOptions<T extends ConfigArgs<ConfigArgType>>
-    extends InjectableStrategy {
+export interface ConfigurableOperationDefOptions<T extends ConfigArgs> extends InjectableStrategy {
     /**
      * @description
      * A unique code used to identify this operation.
@@ -168,7 +167,7 @@ export interface ConfigurableOperationDefOptions<T extends ConfigArgs<ConfigArgT
  * @docsCategory common
  * @docsPage Configurable Operations
  */
-export class ConfigurableOperationDef<T extends ConfigArgs<ConfigArgType> = ConfigArgs<ConfigArgType>> {
+export class ConfigurableOperationDef<T extends ConfigArgs = ConfigArgs> {
     get code(): string {
         return this.options.code;
     }
@@ -268,7 +267,7 @@ function localizeString(stringArray: LocalizedStringArray, languageCode: Languag
     return match.value;
 }
 
-function coerceValueToType<T extends ConfigArgs<any>>(
+function coerceValueToType<T extends ConfigArgs>(
     value: string,
     type: ConfigArgType,
     isList: boolean,

+ 3 - 8
packages/core/src/config/collection/collection-filter.ts

@@ -1,5 +1,4 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset } from '@vendure/common/lib/shared-types';
 import { SelectQueryBuilder } from 'typeorm';
 
 import {
@@ -10,16 +9,12 @@ import {
 } from '../../common/configurable-operation';
 import { ProductVariant } from '../../entity/product-variant/product-variant.entity';
 
-export type CollectionFilterArgType = ConfigArgSubset<'ID' | 'string' | 'boolean'>;
-export type CollectionFilterArgs = ConfigArgs<CollectionFilterArgType>;
-
-export type ApplyCollectionFilterFn<T extends CollectionFilterArgs> = (
+export type ApplyCollectionFilterFn<T extends ConfigArgs> = (
     qb: SelectQueryBuilder<ProductVariant>,
     args: ConfigArgValues<T>,
 ) => SelectQueryBuilder<ProductVariant>;
 
-export interface CollectionFilterConfig<T extends CollectionFilterArgs>
-    extends ConfigurableOperationDefOptions<T> {
+export interface CollectionFilterConfig<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     apply: ApplyCollectionFilterFn<T>;
 }
 // tslint:disable:max-line-length
@@ -34,7 +29,7 @@ export interface CollectionFilterConfig<T extends CollectionFilterArgs>
  *
  * @docsCategory configuration
  */
-export class CollectionFilter<T extends CollectionFilterArgs = {}> extends ConfigurableOperationDef<T> {
+export class CollectionFilter<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
     // tslint:enable:max-line-length
     private readonly applyFn: ApplyCollectionFilterFn<T>;
     constructor(config: CollectionFilterConfig<T>) {

+ 5 - 11
packages/core/src/config/payment-method/payment-method-handler.ts

@@ -1,5 +1,4 @@
 import { ConfigArg, RefundOrderInput } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset } from '@vendure/common/lib/shared-types';
 
 import {
     ConfigArgs,
@@ -16,8 +15,6 @@ import {
 } from '../../service/helpers/payment-state-machine/payment-state';
 import { RefundState } from '../../service/helpers/refund-state-machine/refund-state';
 
-export type PaymentMethodArgType = ConfigArgSubset<'int' | 'string' | 'boolean'>;
-export type PaymentMethodArgs = ConfigArgs<PaymentMethodArgType>;
 export type OnPaymentTransitionStartReturnType = ReturnType<
     Required<StateMachineConfig<any>>['onTransitionStart']
 >;
@@ -85,7 +82,7 @@ export interface SettlePaymentResult {
  * @docsCategory payment
  * @docsPage Payment Method Types
  */
-export type CreatePaymentFn<T extends PaymentMethodArgs> = (
+export type CreatePaymentFn<T extends ConfigArgs> = (
     order: Order,
     args: ConfigArgValues<T>,
     metadata: PaymentMetadata,
@@ -98,7 +95,7 @@ export type CreatePaymentFn<T extends PaymentMethodArgs> = (
  * @docsCategory payment
  * @docsPage Payment Method Types
  */
-export type SettlePaymentFn<T extends PaymentMethodArgs> = (
+export type SettlePaymentFn<T extends ConfigArgs> = (
     order: Order,
     payment: Payment,
     args: ConfigArgValues<T>,
@@ -111,7 +108,7 @@ export type SettlePaymentFn<T extends PaymentMethodArgs> = (
  * @docsCategory payment
  * @docsPage Payment Method Types
  */
-export type CreateRefundFn<T extends PaymentMethodArgs> = (
+export type CreateRefundFn<T extends ConfigArgs> = (
     input: RefundOrderInput,
     total: number,
     order: Order,
@@ -125,8 +122,7 @@ export type CreateRefundFn<T extends PaymentMethodArgs> = (
  *
  * @docsCategory payment
  */
-export interface PaymentMethodConfigOptions<T extends PaymentMethodArgs = PaymentMethodArgs>
-    extends ConfigurableOperationDefOptions<T> {
+export interface PaymentMethodConfigOptions<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     /**
      * @description
      * This function provides the logic for creating a payment. For example,
@@ -211,9 +207,7 @@ export interface PaymentMethodConfigOptions<T extends PaymentMethodArgs = Paymen
  *
  * @docsCategory payment
  */
-export class PaymentMethodHandler<
-    T extends PaymentMethodArgs = PaymentMethodArgs
-> extends ConfigurableOperationDef<T> {
+export class PaymentMethodHandler<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
     private readonly createPaymentFn: CreatePaymentFn<T>;
     private readonly settlePaymentFn: SettlePaymentFn<T>;
     private readonly createRefundFn?: CreateRefundFn<T>;

+ 8 - 15
packages/core/src/config/promotion/promotion-action.ts

@@ -1,5 +1,4 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset } from '@vendure/common/lib/shared-types';
 
 import {
     ConfigArgs,
@@ -13,9 +12,6 @@ import { Order } from '../../entity/order/order.entity';
 
 import { PromotionUtils } from './promotion-condition';
 
-export type PromotionActionArgType = ConfigArgSubset<'int' | 'ID'>;
-export type PromotionActionArgs = ConfigArgs<PromotionActionArgType>;
-
 /**
  * @description
  * The function which is used by a PromotionItemAction to calculate the
@@ -24,7 +20,7 @@ export type PromotionActionArgs = ConfigArgs<PromotionActionArgType>;
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export type ExecutePromotionItemActionFn<T extends PromotionActionArgs> = (
+export type ExecutePromotionItemActionFn<T extends ConfigArgs> = (
     orderItem: OrderItem,
     orderLine: OrderLine,
     args: ConfigArgValues<T>,
@@ -39,14 +35,13 @@ export type ExecutePromotionItemActionFn<T extends PromotionActionArgs> = (
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export type ExecutePromotionOrderActionFn<T extends PromotionActionArgs> = (
+export type ExecutePromotionOrderActionFn<T extends ConfigArgs> = (
     order: Order,
     args: ConfigArgValues<T>,
     utils: PromotionUtils,
 ) => number | Promise<number>;
 
-export interface PromotionActionConfig<T extends PromotionActionArgs>
-    extends ConfigurableOperationDefOptions<T> {
+export interface PromotionActionConfig<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     priorityValue?: number;
 }
 
@@ -56,7 +51,7 @@ export interface PromotionActionConfig<T extends PromotionActionArgs>
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export interface PromotionItemActionConfig<T extends PromotionActionArgs> extends PromotionActionConfig<T> {
+export interface PromotionItemActionConfig<T extends ConfigArgs> extends PromotionActionConfig<T> {
     /**
      * @description
      * The function which contains the promotion calculation logic.
@@ -70,7 +65,7 @@ export interface PromotionItemActionConfig<T extends PromotionActionArgs> extend
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export interface PromotionOrderActionConfig<T extends PromotionActionArgs> extends PromotionActionConfig<T> {
+export interface PromotionOrderActionConfig<T extends ConfigArgs> extends PromotionActionConfig<T> {
     /**
      * @description
      * The function which contains the promotion calculation logic.
@@ -85,9 +80,7 @@ export interface PromotionOrderActionConfig<T extends PromotionActionArgs> exten
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export abstract class PromotionAction<T extends PromotionActionArgs = {}> extends ConfigurableOperationDef<
-    T
-> {
+export abstract class PromotionAction<T extends ConfigArgs = {}> extends ConfigurableOperationDef<T> {
     readonly priorityValue: number;
 
     protected constructor(config: PromotionActionConfig<T>) {
@@ -116,7 +109,7 @@ export abstract class PromotionAction<T extends PromotionActionArgs = {}> extend
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export class PromotionItemAction<T extends PromotionActionArgs = {}> extends PromotionAction<T> {
+export class PromotionItemAction<T extends ConfigArgs = ConfigArgs> extends PromotionAction<T> {
     private readonly executeFn: ExecutePromotionItemActionFn<T>;
     constructor(config: PromotionItemActionConfig<T>) {
         super(config);
@@ -149,7 +142,7 @@ export class PromotionItemAction<T extends PromotionActionArgs = {}> extends Pro
  * @docsCategory promotions
  * @docsPage promotion-action
  */
-export class PromotionOrderAction<T extends PromotionActionArgs = {}> extends PromotionAction<T> {
+export class PromotionOrderAction<T extends ConfigArgs = ConfigArgs> extends PromotionAction<T> {
     private readonly executeFn: ExecutePromotionOrderActionFn<T>;
     constructor(config: PromotionOrderActionConfig<T>) {
         super(config);

+ 4 - 8
packages/core/src/config/promotion/promotion-condition.ts

@@ -1,5 +1,5 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset, ID } from '@vendure/common/lib/shared-types';
+import { ConfigArgType, ID } from '@vendure/common/lib/shared-types';
 
 import {
     ConfigArgs,
@@ -10,9 +10,6 @@ import {
 import { OrderLine } from '../../entity';
 import { Order } from '../../entity/order/order.entity';
 
-export type PromotionConditionArgType = ConfigArgSubset<'int' | 'string' | 'datetime' | 'boolean' | 'ID'>;
-export type PromotionConditionArgs = ConfigArgs<PromotionConditionArgType>;
-
 /**
  * @description
  * An object containing utility methods which may be used in promotion `check` functions
@@ -40,14 +37,13 @@ export interface PromotionUtils {
  * @docsCategory promotions
  * @docsPage promotion-condition
  */
-export type CheckPromotionConditionFn<T extends PromotionConditionArgs> = (
+export type CheckPromotionConditionFn<T extends ConfigArgs> = (
     order: Order,
     args: ConfigArgValues<T>,
     utils: PromotionUtils,
 ) => boolean | Promise<boolean>;
 
-export interface PromotionConditionConfig<T extends PromotionConditionArgs>
-    extends ConfigurableOperationDefOptions<T> {
+export interface PromotionConditionConfig<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     check: CheckPromotionConditionFn<T>;
     priorityValue?: number;
 }
@@ -61,7 +57,7 @@ export interface PromotionConditionConfig<T extends PromotionConditionArgs>
  * @docsCategory promotions
  * @docsPage promotion-condition
  */
-export class PromotionCondition<T extends PromotionConditionArgs = {}> extends ConfigurableOperationDef<T> {
+export class PromotionCondition<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
     readonly priorityValue: number;
     private readonly checkFn: CheckPromotionConditionFn<T>;
 

+ 3 - 8
packages/core/src/config/shipping-method/shipping-calculator.ts

@@ -1,5 +1,4 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset } from '@vendure/common/lib/shared-types';
 
 import {
     ConfigArgs,
@@ -9,11 +8,7 @@ import {
 } from '../../common/configurable-operation';
 import { Order } from '../../entity/order/order.entity';
 
-export type ShippingCalculatorArgType = ConfigArgSubset<'int' | 'float' | 'string' | 'boolean'>;
-export type ShippingCalculatorArgs = ConfigArgs<ShippingCalculatorArgType>;
-
-export interface ShippingCalculatorConfig<T extends ShippingCalculatorArgs>
-    extends ConfigurableOperationDefOptions<T> {
+export interface ShippingCalculatorConfig<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     calculate: CalculateShippingFn<T>;
 }
 
@@ -41,7 +36,7 @@ export interface ShippingCalculatorConfig<T extends ShippingCalculatorArgs>
  * @docsCategory shipping
  * @docsPage ShippingCalculator
  */
-export class ShippingCalculator<T extends ShippingCalculatorArgs = {}> extends ConfigurableOperationDef<T> {
+export class ShippingCalculator<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
     private readonly calculateFn: CalculateShippingFn<T>;
 
     constructor(config: ShippingCalculatorConfig<T>) {
@@ -101,7 +96,7 @@ export type CalculateShippingFnResult =
  * @docsCategory shipping
  * @docsPage ShippingCalculator
  */
-export type CalculateShippingFn<T extends ShippingCalculatorArgs> = (
+export type CalculateShippingFn<T extends ConfigArgs> = (
     order: Order,
     args: ConfigArgValues<T>,
 ) => CalculateShippingFnResult;

+ 5 - 9
packages/core/src/config/shipping-method/shipping-eligibility-checker.ts

@@ -1,5 +1,4 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
-import { ConfigArgSubset } from '@vendure/common/lib/shared-types';
 
 import {
     ConfigArgs,
@@ -9,10 +8,7 @@ import {
 } from '../../common/configurable-operation';
 import { Order } from '../../entity/order/order.entity';
 
-export type ShippingEligibilityCheckerArgType = ConfigArgSubset<'int' | 'float' | 'string' | 'boolean'>;
-export type ShippingEligibilityCheckerArgs = ConfigArgs<ShippingEligibilityCheckerArgType>;
-
-export interface ShippingEligibilityCheckerConfig<T extends ShippingEligibilityCheckerArgs>
+export interface ShippingEligibilityCheckerConfig<T extends ConfigArgs>
     extends ConfigurableOperationDefOptions<T> {
     check: CheckShippingEligibilityCheckerFn<T>;
 }
@@ -38,9 +34,9 @@ export interface ShippingEligibilityCheckerConfig<T extends ShippingEligibilityC
  * @docsCategory shipping
  * @docsPage ShippingEligibilityChecker
  */
-export class ShippingEligibilityChecker<
-    T extends ShippingEligibilityCheckerArgs = {}
-> extends ConfigurableOperationDef<T> {
+export class ShippingEligibilityChecker<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<
+    T
+> {
     private readonly checkFn: CheckShippingEligibilityCheckerFn<T>;
 
     constructor(config: ShippingEligibilityCheckerConfig<T>) {
@@ -67,7 +63,7 @@ export class ShippingEligibilityChecker<
  * @docsCategory shipping
  * @docsPage ShippingEligibilityChecker
  */
-export type CheckShippingEligibilityCheckerFn<T extends ShippingEligibilityCheckerArgs> = (
+export type CheckShippingEligibilityCheckerFn<T extends ConfigArgs> = (
     order: Order,
     args: ConfigArgValues<T>,
 ) => boolean | Promise<boolean>;

+ 1 - 1
packages/core/src/config/vendure-config.ts

@@ -445,7 +445,7 @@ export interface PaymentOptions {
      * @description
      * An array of {@link PaymentMethodHandler}s with which to process payments.
      */
-    paymentMethodHandlers: Array<PaymentMethodHandler<any>>;
+    paymentMethodHandlers: PaymentMethodHandler[];
 }
 
 /**

+ 9 - 9
packages/core/src/service/services/payment-method.service.ts

@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
 import { ConfigArg, RefundOrderInput, UpdatePaymentMethodInput } from '@vendure/common/lib/generated-types';
 import { omit } from '@vendure/common/lib/omit';
-import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
+import { ConfigArgType, ID, PaginatedList } from '@vendure/common/lib/shared-types';
 import { assertNever } from '@vendure/common/lib/shared-utils';
 import { Connection } from 'typeorm';
 
@@ -10,11 +10,7 @@ import { RequestContext } from '../../api/common/request-context';
 import { UserInputError } from '../../common/error/errors';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { ConfigService } from '../../config/config.service';
-import {
-    PaymentMethodArgs,
-    PaymentMethodArgType,
-    PaymentMethodHandler,
-} from '../../config/payment-method/payment-method-handler';
+import { PaymentMethodHandler } from '../../config/payment-method/payment-method-handler';
 import { OrderItem } from '../../entity/order-item/order-item.entity';
 import { Order } from '../../entity/order/order.entity';
 import { PaymentMethod } from '../../entity/payment-method/payment-method.entity';
@@ -167,8 +163,7 @@ export class PaymentMethodService {
     }
 
     private async ensurePaymentMethodsExist() {
-        const paymentMethodHandlers: Array<PaymentMethodHandler<PaymentMethodArgs>> = this.configService
-            .paymentOptions.paymentMethodHandlers;
+        const paymentMethodHandlers = this.configService.paymentOptions.paymentMethodHandlers;
         const existingPaymentMethods = await this.connection.getRepository(PaymentMethod).find();
         const toCreate = paymentMethodHandlers.filter(
             h => !existingPaymentMethods.find(pm => pm.code === h.code),
@@ -221,14 +216,19 @@ export class PaymentMethodService {
         return [...existingConfigArgs, ...configArgs];
     }
 
-    private getDefaultValue(type: PaymentMethodArgType): string {
+    private getDefaultValue(type: ConfigArgType): string {
         switch (type) {
             case 'string':
                 return '';
             case 'boolean':
                 return 'false';
             case 'int':
+            case 'float':
                 return '0';
+            case 'ID':
+                return '';
+            case 'datetime':
+                return new Date().toISOString();
             default:
                 assertNever(type);
                 return '';