From cc6721fb9a6f3552c0ab406959860c313de1adde Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Fri, 29 Sep 2023 09:47:49 +0200 Subject: [PATCH] fix(checkout): export more values --- .../__snapshots__/exports.spec.ts.snap | 5 +-- .../__snapshots__/exports.spec.ts.snap | 5 +-- apps/checkout/src/globals.ts | 2 +- apps/checkout/src/index.ts | 44 ++++++++++--------- .../common/src/types/address.types.ts | 25 +++++++++++ .../common/src/types/endpoints.types.ts | 30 ++++++++++++- libs/checkout/common/src/types/index.ts | 1 + libs/checkout/core/src/globals.ts | 3 +- libs/checkout/core/src/init/createConfig.ts | 3 +- libs/checkout/core/src/init/setupGlobals.ts | 3 +- .../core/src/listeners/updateAddressType.ts | 2 +- .../core/src/listeners/updateCheckoutForm.ts | 3 +- .../core/src/store/createCheckoutStore.ts | 3 +- .../core/src/types/endpoints.types.ts | 25 ----------- libs/checkout/core/src/types/index.ts | 1 - libs/checkout/core/src/types/types.ts | 41 +++++------------ .../core/src/utils/getFrontendContext.ts | 2 +- .../core/src/utils/global/doRequest.ts | 7 ++- .../core/src/utils/global/fieldsEqual.ts | 10 +---- .../core/src/utils/global/getAddressField.ts | 4 +- .../src/utils/global/getAddressFieldValue.ts | 2 +- .../core/src/utils/global/getAddressType.ts | 2 +- .../core/src/utils/global/getFieldValue.ts | 3 +- .../core/src/utils/global/setFieldValue.ts | 2 +- .../checkout/core/src/utils/hasAddressType.ts | 2 +- libs/checkout/core/src/utils/useEvent.ts | 2 +- 26 files changed, 119 insertions(+), 113 deletions(-) create mode 100644 libs/checkout/common/src/types/address.types.ts delete mode 100644 libs/checkout/core/src/types/endpoints.types.ts diff --git a/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap b/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap index 836131131..6e2b23e58 100644 --- a/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -2,8 +2,7 @@ exports[`exports > exports from index.ts 1`] = ` [ - "AddressField", - "AddressType", + "DeliveryOptionsMode", "EVENT_HIDE_DELIVERY_OPTIONS", "EVENT_SHOW_DELIVERY_OPTIONS", "EVENT_UPDATED_ADDRESS", @@ -12,8 +11,6 @@ exports[`exports > exports from index.ts 1`] = ` "EVENT_UPDATE_DELIVERY_OPTIONS", "FrontendEndpoint", "PdkDeliveryOptionsEvent", - "PdkEvent", - "PdkField", "StoreListener", "Util", "createPdkCheckout", diff --git a/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap b/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap index 836131131..6e2b23e58 100644 --- a/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -2,8 +2,7 @@ exports[`exports > exports from index.ts 1`] = ` [ - "AddressField", - "AddressType", + "DeliveryOptionsMode", "EVENT_HIDE_DELIVERY_OPTIONS", "EVENT_SHOW_DELIVERY_OPTIONS", "EVENT_UPDATED_ADDRESS", @@ -12,8 +11,6 @@ exports[`exports > exports from index.ts 1`] = ` "EVENT_UPDATE_DELIVERY_OPTIONS", "FrontendEndpoint", "PdkDeliveryOptionsEvent", - "PdkEvent", - "PdkField", "StoreListener", "Util", "createPdkCheckout", diff --git a/apps/checkout/src/globals.ts b/apps/checkout/src/globals.ts index 972dd6116..f8ef384c1 100644 --- a/apps/checkout/src/globals.ts +++ b/apps/checkout/src/globals.ts @@ -8,13 +8,13 @@ import { type getFieldValue, type PdkCheckout, type PdkCheckoutConfig, - type PdkEvent, type realCreateStore, type setFieldValue, type StoreData, type triggerEvent, type Util, } from '@myparcel-pdk/checkout-core'; +import {type PdkEvent} from '@myparcel-pdk/checkout-common'; import {type isOfType, type PromiseOr} from '@myparcel/ts-utils'; declare global { diff --git a/apps/checkout/src/index.ts b/apps/checkout/src/index.ts index 873aa9620..78e60f50b 100644 --- a/apps/checkout/src/index.ts +++ b/apps/checkout/src/index.ts @@ -2,14 +2,12 @@ import './globals'; export type { AddressFields, - CheckoutStoreState, + FrontendEndpointData, + FrontendEndpointDefinition, FrontendEndpointParameters, FrontendEndpointResponse, - PdkCheckoutConfig, - PdkCheckoutConfigInput, - PdkCheckoutForm, - StoreCallbackUpdate, -} from '@myparcel-pdk/checkout-core'; + FrontendPdkEndpointObject, +} from '@myparcel-pdk/checkout-common'; export type { CheckoutDeliveryOptionsSettings, @@ -19,23 +17,16 @@ export type { DeliveryOptionsStoreState, } from '@myparcel-pdk/checkout-delivery-options'; -export type {FrontendPdkEndpointObject} from '@myparcel-pdk/checkout-common'; - -export { - AddressField, - AddressType, - PdkEvent, - PdkField, - StoreListener, - Util, - createPdkCheckout, - useCheckoutStore, - useEvent, - usePdkCheckout, - useSettings, - useUtil, +export type { + CheckoutStoreState, + PdkCheckoutConfig, + PdkCheckoutConfigInput, + PdkCheckoutForm, + StoreCallbackUpdate, } from '@myparcel-pdk/checkout-core'; +export {DeliveryOptionsMode} from '@myparcel-pdk/checkout-delivery-options'; + export { EVENT_HIDE_DELIVERY_OPTIONS, EVENT_SHOW_DELIVERY_OPTIONS, @@ -53,6 +44,17 @@ export { useDeliveryOptionsStore, } from '@myparcel-pdk/checkout-delivery-options'; +export { + StoreListener, + Util, + createPdkCheckout, + useCheckoutStore, + useEvent, + usePdkCheckout, + useSettings, + useUtil, +} from '@myparcel-pdk/checkout-core'; + export {initializeCheckoutSeparateAddressFields} from '@myparcel-pdk/checkout-separate-address-fields'; export {initializeCheckoutTaxFields} from '@myparcel-pdk/checkout-tax-fields'; diff --git a/libs/checkout/common/src/types/address.types.ts b/libs/checkout/common/src/types/address.types.ts new file mode 100644 index 000000000..676dddb43 --- /dev/null +++ b/libs/checkout/common/src/types/address.types.ts @@ -0,0 +1,25 @@ +export enum AddressType { + Billing = 'billing', + Shipping = 'shipping', +} + +export enum PdkEvent { + CheckoutUpdate = 'checkoutUpdate', + CheckoutUpdated = 'checkoutUpdated', + StoreInitialize = 'storeInitialize', + StoreInitialized = 'storeInitialized', +} + +export enum AddressField { + Address1 = 'address1', + City = 'city', + Country = 'country', + PostalCode = 'postalCode', +} + +export enum PdkField { + AddressType = 'addressType', + ShippingMethod = 'shippingMethod', +} + +export type AddressFields = Record; diff --git a/libs/checkout/common/src/types/endpoints.types.ts b/libs/checkout/common/src/types/endpoints.types.ts index 781b152c6..a140a2fd7 100644 --- a/libs/checkout/common/src/types/endpoints.types.ts +++ b/libs/checkout/common/src/types/endpoints.types.ts @@ -1,4 +1,32 @@ -import {type EndpointObject} from '@myparcel-pdk/common'; +import { + type EndpointObject, + type PdkEndpointDefinition, + type PdkEndpointParameters, + type PdkEndpointResponse, + type Plugin, +} from '@myparcel-pdk/common'; import {type FrontendEndpoint} from '../endpoints'; export type FrontendPdkEndpointObject = EndpointObject; + +export type FrontendEndpointData = FrontendPdkEndpointObject[E] & { + baseUrl: string; +}; + +export type FrontendEndpointResponse = PdkEndpointResponse; + +export type FrontendEndpointParameters = PdkEndpointParameters< + E, + FrontendEndpointDefinition +>; + +interface FetchCheckoutContextDefinition extends PdkEndpointDefinition { + formattedResponse: Plugin.ModelContextCheckoutContext; + name: FrontendEndpoint.FetchCheckoutContext; + parameters: { + shippingMethod?: string; + }; + response: [Plugin.ModelContextCheckoutContext]; +} + +export type FrontendEndpointDefinition = FetchCheckoutContextDefinition; diff --git a/libs/checkout/common/src/types/index.ts b/libs/checkout/common/src/types/index.ts index 809177725..164c9fc77 100644 --- a/libs/checkout/common/src/types/index.ts +++ b/libs/checkout/common/src/types/index.ts @@ -1 +1,2 @@ +export * from './address.types'; export * from './endpoints.types'; diff --git a/libs/checkout/core/src/globals.ts b/libs/checkout/core/src/globals.ts index 527066499..15d0201a9 100644 --- a/libs/checkout/core/src/globals.ts +++ b/libs/checkout/core/src/globals.ts @@ -1,3 +1,4 @@ +import {type PdkEvent} from '@myparcel-pdk/checkout-common'; import {type isOfType, type PromiseOr} from '@myparcel/ts-utils'; import { type doRequest, @@ -10,7 +11,7 @@ import { type triggerEvent, type Util, } from './utils'; -import {type PdkCheckout, type PdkCheckoutConfig, type PdkEvent} from './types'; +import {type PdkCheckout, type PdkCheckoutConfig} from './types'; import {type createCheckoutStore, type realCreateStore, type StoreData} from './store'; declare global { diff --git a/libs/checkout/core/src/init/createConfig.ts b/libs/checkout/core/src/init/createConfig.ts index 190f1489f..50a0b6760 100644 --- a/libs/checkout/core/src/init/createConfig.ts +++ b/libs/checkout/core/src/init/createConfig.ts @@ -1,5 +1,6 @@ +import {type AddressType} from '@myparcel-pdk/checkout-common'; import {useSettings} from '../utils'; -import {type AddressType, type PdkCheckoutConfig, type PdkCheckoutConfigInput} from '../types'; +import {type PdkCheckoutConfig, type PdkCheckoutConfigInput} from '../types'; import {useConfig} from '../config'; export const createConfig = (config: PdkCheckoutConfigInput): PdkCheckoutConfig => ({ diff --git a/libs/checkout/core/src/init/setupGlobals.ts b/libs/checkout/core/src/init/setupGlobals.ts index bf6548533..97e7cffb7 100644 --- a/libs/checkout/core/src/init/setupGlobals.ts +++ b/libs/checkout/core/src/init/setupGlobals.ts @@ -1,3 +1,4 @@ +import {PdkEvent} from '@myparcel-pdk/checkout-common'; import {isOfType} from '@myparcel/ts-utils'; import { doRequest, @@ -10,7 +11,7 @@ import { setFieldValue, triggerEvent, } from '../utils'; -import {type PdkCheckoutConfig, PdkEvent} from '../types'; +import {type PdkCheckoutConfig} from '../types'; import {realCreateStore} from '../store'; import { createEventName, diff --git a/libs/checkout/core/src/listeners/updateAddressType.ts b/libs/checkout/core/src/listeners/updateAddressType.ts index 6eafdb60e..00ad19397 100644 --- a/libs/checkout/core/src/listeners/updateAddressType.ts +++ b/libs/checkout/core/src/listeners/updateAddressType.ts @@ -1,5 +1,5 @@ +import {PdkField} from '@myparcel-pdk/checkout-common'; import {useUtil, Util} from '../utils'; -import {PdkField} from '../types'; import {type CheckoutStoreState, type StoreCallbackUpdate} from '../store'; import {useConfig} from '../config'; diff --git a/libs/checkout/core/src/listeners/updateCheckoutForm.ts b/libs/checkout/core/src/listeners/updateCheckoutForm.ts index 8f1ea5d58..64829d71d 100644 --- a/libs/checkout/core/src/listeners/updateCheckoutForm.ts +++ b/libs/checkout/core/src/listeners/updateCheckoutForm.ts @@ -1,5 +1,6 @@ +import {type AddressField, AddressType, type PdkField} from '@myparcel-pdk/checkout-common'; import {useCheckoutStore} from '../utils'; -import {type AddressField, AddressType, getAddressType, type PdkCheckoutForm, type PdkField, useConfig} from '../index'; +import {getAddressType, type PdkCheckoutForm, useConfig} from '../index'; function getEntry(data: Record, value: undefined | string): string { return (data[value as string] as string | undefined) ?? ''; diff --git a/libs/checkout/core/src/store/createCheckoutStore.ts b/libs/checkout/core/src/store/createCheckoutStore.ts index 721639243..e2fe6df7c 100644 --- a/libs/checkout/core/src/store/createCheckoutStore.ts +++ b/libs/checkout/core/src/store/createCheckoutStore.ts @@ -1,5 +1,6 @@ +import {AddressType} from '@myparcel-pdk/checkout-common'; import {getFrontendContext, hasAddressType, useUtil, Util} from '../utils'; -import {AddressType, type CheckoutAppContext, type PdkCheckoutForm} from '../types'; +import {type CheckoutAppContext, type PdkCheckoutForm} from '../types'; import {updateAddressType} from '../listeners'; import {StoreListener} from './realCreateStore'; diff --git a/libs/checkout/core/src/types/endpoints.types.ts b/libs/checkout/core/src/types/endpoints.types.ts deleted file mode 100644 index 58bf80498..000000000 --- a/libs/checkout/core/src/types/endpoints.types.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { - type PdkEndpointDefinition, - type PdkEndpointParameters, - type PdkEndpointResponse, - type Plugin, -} from '@myparcel-pdk/common'; -import {type FrontendEndpoint} from '@myparcel-pdk/checkout-common'; - -export type FrontendEndpointResponse = PdkEndpointResponse; - -export type FrontendEndpointParameters = PdkEndpointParameters< - E, - FrontendEndpointDefinition ->; - -interface FetchCheckoutContextDefinition extends PdkEndpointDefinition { - formattedResponse: Plugin.ModelContextCheckoutContext; - name: FrontendEndpoint.FetchCheckoutContext; - parameters: { - shippingMethod?: string; - }; - response: [Plugin.ModelContextCheckoutContext]; -} - -export type FrontendEndpointDefinition = FetchCheckoutContextDefinition; diff --git a/libs/checkout/core/src/types/index.ts b/libs/checkout/core/src/types/index.ts index d134626e0..fcb073fef 100644 --- a/libs/checkout/core/src/types/index.ts +++ b/libs/checkout/core/src/types/index.ts @@ -1,2 +1 @@ -export * from './endpoints.types'; export * from './types'; diff --git a/libs/checkout/core/src/types/types.ts b/libs/checkout/core/src/types/types.ts index 2a4e71454..d012927c5 100644 --- a/libs/checkout/core/src/types/types.ts +++ b/libs/checkout/core/src/types/types.ts @@ -1,34 +1,15 @@ -import {type FrontendEndpoint, type FrontendPdkEndpointObject} from '@myparcel-pdk/checkout-common'; +import { + type AddressFields, + type AddressType, + type FrontendEndpoint, + type FrontendEndpointData, + type FrontendEndpointResponse, + type FrontendPdkEndpointObject, + type PdkField, +} from '@myparcel-pdk/checkout-common'; import {type PromiseOr} from '@myparcel/ts-utils'; import {type MyParcelDeliveryOptions} from '@myparcel/delivery-options'; import {type CarrierName} from '@myparcel/constants'; -import {type FrontendEndpointResponse} from './endpoints.types'; - -export enum AddressType { - Billing = 'billing', - Shipping = 'shipping', -} - -export enum PdkEvent { - CheckoutUpdate = 'checkoutUpdate', - CheckoutUpdated = 'checkoutUpdated', - StoreInitialize = 'storeInitialize', - StoreInitialized = 'storeInitialized', -} - -export enum AddressField { - Address1 = 'address1', - City = 'city', - Country = 'country', - PostalCode = 'postalCode', -} - -export enum PdkField { - AddressType = 'addressType', - ShippingMethod = 'shippingMethod', -} - -export type AddressFields = Record; export type PdkCheckoutConfigInput = Omit< PdkCheckoutConfig, @@ -60,9 +41,7 @@ export interface PdkCheckoutConfig { /** * Do a request to the backend. */ - doRequest( - endpoint: FrontendPdkEndpointObject[E] & {baseUrl: string}, - ): Promise>; + doRequest(endpoint: FrontendEndpointData): Promise>; /** * The handler for the form change event. diff --git a/libs/checkout/core/src/utils/getFrontendContext.ts b/libs/checkout/core/src/utils/getFrontendContext.ts index fd9ee7bfb..6434cbc46 100644 --- a/libs/checkout/core/src/utils/getFrontendContext.ts +++ b/libs/checkout/core/src/utils/getFrontendContext.ts @@ -1,6 +1,6 @@ import {type CheckoutAppContext} from '../types'; import {useConfig} from '../config'; -import {getElement} from './global/getElement'; +import {getElement} from './global'; const ATTRIBUTE_CONTEXT = 'data-context'; diff --git a/libs/checkout/core/src/utils/global/doRequest.ts b/libs/checkout/core/src/utils/global/doRequest.ts index 25442c2da..19e0f955a 100644 --- a/libs/checkout/core/src/utils/global/doRequest.ts +++ b/libs/checkout/core/src/utils/global/doRequest.ts @@ -1,6 +1,9 @@ -import {type FrontendEndpoint} from '@myparcel-pdk/checkout-common'; +import { + type FrontendEndpoint, + type FrontendEndpointParameters, + type FrontendEndpointResponse, +} from '@myparcel-pdk/checkout-common'; import {useSettings} from '../useSettings'; -import {type FrontendEndpointParameters, type FrontendEndpointResponse} from '../../types/endpoints.types'; import {useConfig} from '../../config'; type DoRequest = ( diff --git a/libs/checkout/core/src/utils/global/fieldsEqual.ts b/libs/checkout/core/src/utils/global/fieldsEqual.ts index dfc67a8aa..d689ac31f 100644 --- a/libs/checkout/core/src/utils/global/fieldsEqual.ts +++ b/libs/checkout/core/src/utils/global/fieldsEqual.ts @@ -1,12 +1,6 @@ +import {type AddressField, type AddressFields, type AddressType, PdkField} from '@myparcel-pdk/checkout-common'; import {isEnumValue} from '@myparcel/ts-utils'; -import { - type AddressField, - type AddressFields, - type AddressType, - getAddressType, - type PdkCheckoutForm, - PdkField, -} from '../../index'; +import {getAddressType, type PdkCheckoutForm} from '../../index'; type FieldsEqual = { ( diff --git a/libs/checkout/core/src/utils/global/getAddressField.ts b/libs/checkout/core/src/utils/global/getAddressField.ts index 4f89466fd..41486847a 100644 --- a/libs/checkout/core/src/utils/global/getAddressField.ts +++ b/libs/checkout/core/src/utils/global/getAddressField.ts @@ -1,6 +1,6 @@ +import {type AddressField, type AddressType} from '@myparcel-pdk/checkout-common'; import {useCheckoutStore} from '../useCheckoutStore'; -import {type AddressType} from '../../types'; -import {type AddressField, useConfig, useUtil, Util} from '../../index'; +import {useConfig, useUtil, Util} from '../../index'; /** * Get field by name. Will return element with selector: "#_". diff --git a/libs/checkout/core/src/utils/global/getAddressFieldValue.ts b/libs/checkout/core/src/utils/global/getAddressFieldValue.ts index 138fdfe82..79d98b66e 100644 --- a/libs/checkout/core/src/utils/global/getAddressFieldValue.ts +++ b/libs/checkout/core/src/utils/global/getAddressFieldValue.ts @@ -1,4 +1,4 @@ -import {type AddressField, type AddressType} from '../../types'; +import {type AddressField, type AddressType} from '@myparcel-pdk/checkout-common'; import {getFieldValue} from './getFieldValue'; export const getAddressFieldValue = (name: AddressField | string, addressType?: AddressType): undefined | string => { diff --git a/libs/checkout/core/src/utils/global/getAddressType.ts b/libs/checkout/core/src/utils/global/getAddressType.ts index 38d768e3f..d8148975a 100644 --- a/libs/checkout/core/src/utils/global/getAddressType.ts +++ b/libs/checkout/core/src/utils/global/getAddressType.ts @@ -1,5 +1,5 @@ +import {type AddressType, PdkField} from '@myparcel-pdk/checkout-common'; import {useUtil, Util} from '../useUtil'; -import {type AddressType, PdkField} from '../../types'; import {useConfig} from '../../config'; export const getAddressType = (): AddressType => { diff --git a/libs/checkout/core/src/utils/global/getFieldValue.ts b/libs/checkout/core/src/utils/global/getFieldValue.ts index f5f4a2a70..4f774d0b7 100644 --- a/libs/checkout/core/src/utils/global/getFieldValue.ts +++ b/libs/checkout/core/src/utils/global/getFieldValue.ts @@ -1,6 +1,7 @@ +import {type AddressField, type AddressType, PdkField} from '@myparcel-pdk/checkout-common'; import {isEnumValue} from '@myparcel/ts-utils'; import {useCheckoutStore} from '../useCheckoutStore'; -import {type AddressField, type AddressType, type PdkCheckoutForm, PdkField} from '../../index'; +import {type PdkCheckoutForm} from '../../index'; type GetFieldValue = { (field: AddressField | string, addressType?: AddressType, fields?: PdkCheckoutForm): undefined | string; diff --git a/libs/checkout/core/src/utils/global/setFieldValue.ts b/libs/checkout/core/src/utils/global/setFieldValue.ts index cbb396c4a..1d4c0ecf5 100644 --- a/libs/checkout/core/src/utils/global/setFieldValue.ts +++ b/libs/checkout/core/src/utils/global/setFieldValue.ts @@ -1,5 +1,5 @@ +import {type AddressField, type AddressType} from '@myparcel-pdk/checkout-common'; import {useUtil, Util} from '../useUtil'; -import {type AddressField, type AddressType} from '../../types'; import {useConfig} from '../../config'; import {getAddressField} from './getAddressField'; diff --git a/libs/checkout/core/src/utils/hasAddressType.ts b/libs/checkout/core/src/utils/hasAddressType.ts index 703e0187a..8ed0e011a 100644 --- a/libs/checkout/core/src/utils/hasAddressType.ts +++ b/libs/checkout/core/src/utils/hasAddressType.ts @@ -1,4 +1,4 @@ -import {type AddressType} from '../types'; +import {type AddressType} from '@myparcel-pdk/checkout-common'; import {useConfig} from '../config'; export const hasAddressType = (addressType: AddressType): boolean => { diff --git a/libs/checkout/core/src/utils/useEvent.ts b/libs/checkout/core/src/utils/useEvent.ts index db9eeb43c..c1c8f2c0d 100644 --- a/libs/checkout/core/src/utils/useEvent.ts +++ b/libs/checkout/core/src/utils/useEvent.ts @@ -1,4 +1,4 @@ -import {type PdkEvent} from '../types'; +import {type PdkEvent} from '@myparcel-pdk/checkout-common'; export const useEvent = (event: E): E extends PdkEvent ? MyParcelPdkEvents[E] : string => { // @ts-expect-error todo