@myparcel-dev/pdk-checkout-common 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -0
- package/dist/__tests__/constants.d.ts +15 -0
- package/dist/__tests__/getMockCheckoutContext.d.ts +2 -0
- package/dist/__tests__/getMockDeliveryOptionsConfig.d.ts +2 -0
- package/dist/__tests__/getMockFormData.d.ts +3 -0
- package/dist/__tests__/getMockPdkCheckoutConfig.d.ts +2 -0
- package/dist/__tests__/index.d.ts +9 -0
- package/dist/__tests__/mockDeliveryOptionsElement.d.ts +1 -0
- package/dist/__tests__/mockPdkCheckout.d.ts +2 -0
- package/dist/__tests__/mockShippingMethod.d.ts +1 -0
- package/dist/__tests__/spies.d.ts +7 -0
- package/dist/constants.d.ts +1 -0
- package/dist/createPdkCheckout.d.ts +2 -0
- package/dist/data/address.d.ts +15 -0
- package/dist/data/events.d.ts +20 -0
- package/dist/data/index.d.ts +4 -0
- package/dist/data/store.d.ts +3 -0
- package/dist/data/utils.d.ts +13 -0
- package/dist/defaults/defaultFormChange.d.ts +1 -0
- package/dist/defaults/defaultGetAddressType.d.ts +2 -0
- package/dist/defaults/defaultGetFormData.d.ts +3 -0
- package/dist/defaults/defaultHasDeliveryOptions.d.ts +1 -0
- package/dist/defaults/index.d.ts +4 -0
- package/dist/globals.d.ts +13 -0
- package/dist/index.d.ts +11 -0
- package/dist/init/addFormListeners.d.ts +1 -0
- package/dist/init/createConfig.d.ts +2 -0
- package/dist/init/index.d.ts +4 -0
- package/dist/init/initializeCheckoutStore.d.ts +1 -0
- package/dist/init/setupGlobals.d.ts +2 -0
- package/dist/listeners/index.d.ts +2 -0
- package/dist/listeners/updateAddressType.d.ts +2 -0
- package/dist/listeners/updateCheckoutForm.d.ts +1 -0
- package/dist/store/createCheckoutStore.d.ts +2 -0
- package/dist/store/index.d.ts +3 -0
- package/dist/store/logStoreDebugInfo.d.ts +3 -0
- package/dist/store/realCreateStore.d.ts +3 -0
- package/dist/types/address.types.d.ts +2 -0
- package/dist/types/checkout.types.d.ts +70 -0
- package/dist/types/endpoints.types.d.ts +20 -0
- package/dist/types/global.types.d.ts +42 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/store.types.d.ts +34 -0
- package/dist/utils/getEnabledShippingMethods.d.ts +1 -0
- package/dist/utils/getFrontendContext.d.ts +2 -0
- package/dist/utils/global/doRequest.d.ts +5 -0
- package/dist/utils/global/fieldsEqual.d.ts +8 -0
- package/dist/utils/global/getAddressField.d.ts +5 -0
- package/dist/utils/global/getAddressFieldValue.d.ts +2 -0
- package/dist/utils/global/getAddressType.d.ts +2 -0
- package/dist/utils/global/getElement.d.ts +1 -0
- package/dist/utils/global/getFieldValue.d.ts +8 -0
- package/dist/utils/global/index.d.ts +9 -0
- package/dist/utils/global/setFieldValue.d.ts +2 -0
- package/dist/utils/global/triggerEvent.d.ts +4 -0
- package/dist/utils/hasAddressType.d.ts +2 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/realUseUtil.d.ts +2 -0
- package/dist/utils/useCheckoutStore.d.ts +1 -0
- package/dist/utils/useConfig.d.ts +1 -0
- package/dist/utils/useEvent.d.ts +3 -0
- package/dist/utils/usePdkCheckout.d.ts +2 -0
- package/dist/utils/useSettings.d.ts +2 -0
- package/dist/utils/useUtil.d.ts +2 -0
- package/package.json +50 -0
- package/src/__tests__/__snapshots__/exports.spec.ts.snap +62 -0
- package/src/__tests__/constants.ts +17 -0
- package/src/__tests__/exports.spec.ts +8 -0
- package/src/__tests__/getMockCheckoutContext.ts +44 -0
- package/src/__tests__/getMockDeliveryOptionsConfig.ts +27 -0
- package/src/__tests__/getMockFormData.ts +29 -0
- package/src/__tests__/getMockPdkCheckoutConfig.ts +46 -0
- package/src/__tests__/index.ts +9 -0
- package/src/__tests__/mockDeliveryOptionsElement.ts +20 -0
- package/src/__tests__/mockPdkCheckout.ts +32 -0
- package/src/__tests__/mockShippingMethod.ts +8 -0
- package/src/__tests__/spies.ts +16 -0
- package/src/constants.ts +1 -0
- package/src/createPdkCheckout.ts +32 -0
- package/src/data/address.ts +17 -0
- package/src/data/events.ts +38 -0
- package/src/data/index.ts +4 -0
- package/src/data/store.ts +3 -0
- package/src/data/utils.ts +13 -0
- package/src/defaults/defaultFormChange.ts +9 -0
- package/src/defaults/defaultGetAddressType.ts +4 -0
- package/src/defaults/defaultGetFormData.ts +11 -0
- package/src/defaults/defaultHasDeliveryOptions.ts +8 -0
- package/src/defaults/index.ts +4 -0
- package/src/globals.ts +18 -0
- package/src/index.ts +11 -0
- package/src/init/addFormListeners.ts +10 -0
- package/src/init/createConfig.ts +15 -0
- package/src/init/index.ts +4 -0
- package/src/init/initializeCheckoutStore.ts +5 -0
- package/src/init/setupGlobals.ts +75 -0
- package/src/listeners/index.ts +2 -0
- package/src/listeners/updateAddressType.ts +13 -0
- package/src/listeners/updateCheckoutForm.ts +38 -0
- package/src/store/createCheckoutStore.ts +27 -0
- package/src/store/index.ts +3 -0
- package/src/store/logStoreDebugInfo.ts +19 -0
- package/src/store/realCreateStore.ts +64 -0
- package/src/types/address.types.ts +3 -0
- package/src/types/checkout.types.ts +89 -0
- package/src/types/endpoints.types.ts +31 -0
- package/src/types/global.types.ts +56 -0
- package/src/types/index.ts +5 -0
- package/src/types/store.types.ts +43 -0
- package/src/utils/getEnabledShippingMethods.ts +9 -0
- package/src/utils/getFrontendContext.ts +22 -0
- package/src/utils/global/doRequest.spec.ts +18 -0
- package/src/utils/global/doRequest.ts +25 -0
- package/src/utils/global/fieldsEqual.spec.ts +83 -0
- package/src/utils/global/fieldsEqual.ts +29 -0
- package/src/utils/global/getAddressField.ts +20 -0
- package/src/utils/global/getAddressFieldValue.ts +6 -0
- package/src/utils/global/getAddressType.ts +10 -0
- package/src/utils/global/getElement.spec.ts +28 -0
- package/src/utils/global/getElement.ts +10 -0
- package/src/utils/global/getFieldValue.ts +24 -0
- package/src/utils/global/index.ts +9 -0
- package/src/utils/global/setFieldValue.ts +24 -0
- package/src/utils/global/triggerEvent.spec.ts +29 -0
- package/src/utils/global/triggerEvent.ts +23 -0
- package/src/utils/hasAddressType.spec.ts +22 -0
- package/src/utils/hasAddressType.ts +8 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/realUseUtil.ts +11 -0
- package/src/utils/useCheckoutStore.ts +2 -0
- package/src/utils/useConfig.ts +2 -0
- package/src/utils/useEvent.ts +9 -0
- package/src/utils/usePdkCheckout.ts +5 -0
- package/src/utils/useSettings.ts +5 -0
- package/src/utils/useUtil.ts +3 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// eslint-disable-next-line max-lines-per-function
|
|
2
|
+
import {isEnumValue, isOfType} from '@myparcel-dev/ts-utils';
|
|
3
|
+
import {
|
|
4
|
+
doRequest,
|
|
5
|
+
fieldsEqual,
|
|
6
|
+
getAddressField,
|
|
7
|
+
getAddressFieldValue,
|
|
8
|
+
getElement,
|
|
9
|
+
getFieldValue,
|
|
10
|
+
realUseUtil,
|
|
11
|
+
setFieldValue,
|
|
12
|
+
triggerEvent,
|
|
13
|
+
} from '../utils';
|
|
14
|
+
import {type PdkCheckoutConfig} from '../types';
|
|
15
|
+
import {realCreateStore} from '../store';
|
|
16
|
+
import {
|
|
17
|
+
createEventName,
|
|
18
|
+
EVENT_CHECKOUT,
|
|
19
|
+
EVENT_STORE,
|
|
20
|
+
EVENT_TYPE_INITIALIZE,
|
|
21
|
+
EVENT_TYPE_INITIALIZED,
|
|
22
|
+
EVENT_TYPE_UPDATE,
|
|
23
|
+
EVENT_TYPE_UPDATED,
|
|
24
|
+
PdkEvent,
|
|
25
|
+
} from '../data';
|
|
26
|
+
import {initializeCheckoutStore} from './initializeCheckoutStore';
|
|
27
|
+
import {addFormListeners} from './addFormListeners';
|
|
28
|
+
|
|
29
|
+
export const setupGlobals = (config: PdkCheckoutConfig): void => {
|
|
30
|
+
window.MyParcelPdk ??= {
|
|
31
|
+
config,
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
events: {
|
|
36
|
+
[PdkEvent.CheckoutUpdate]: createEventName(EVENT_CHECKOUT, EVENT_TYPE_UPDATE),
|
|
37
|
+
[PdkEvent.CheckoutUpdated]: createEventName(EVENT_CHECKOUT, EVENT_TYPE_UPDATED),
|
|
38
|
+
[PdkEvent.StoreInitialize]: createEventName(EVENT_STORE, EVENT_TYPE_INITIALIZE),
|
|
39
|
+
[PdkEvent.StoreInitialized]: createEventName(EVENT_STORE, EVENT_TYPE_INITIALIZED),
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
initializeCallbacks: [initializeCheckoutStore, addFormListeners],
|
|
43
|
+
initialized: false,
|
|
44
|
+
|
|
45
|
+
// @ts-expect-error todo
|
|
46
|
+
instance: null,
|
|
47
|
+
|
|
48
|
+
storedState: {},
|
|
49
|
+
|
|
50
|
+
stores: {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
checkout: null,
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
deliveryOptions: null,
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
useUtil: realUseUtil,
|
|
60
|
+
|
|
61
|
+
utils: {
|
|
62
|
+
createStore: realCreateStore,
|
|
63
|
+
doRequest,
|
|
64
|
+
fieldsEqual,
|
|
65
|
+
getAddressField,
|
|
66
|
+
getAddressFieldValue,
|
|
67
|
+
getElement,
|
|
68
|
+
getFieldValue,
|
|
69
|
+
isEnumValue,
|
|
70
|
+
isOfType,
|
|
71
|
+
setFieldValue,
|
|
72
|
+
triggerEvent,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {useConfig, useUtil} from '../utils';
|
|
2
|
+
import {type CheckoutStoreState, type StoreCallbackUpdate} from '../types';
|
|
3
|
+
import {PdkField, PdkUtil} from '../data';
|
|
4
|
+
|
|
5
|
+
export const updateAddressType: StoreCallbackUpdate<CheckoutStoreState> = (newState, oldState) => {
|
|
6
|
+
const fieldsEqual = useUtil(PdkUtil.FieldsEqual);
|
|
7
|
+
|
|
8
|
+
if (!oldState || fieldsEqual(newState.form, oldState.form, PdkField.AddressType)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
newState.addressType = useConfig().getAddressType(newState.form[PdkField.AddressType]);
|
|
13
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {getAddressType, useCheckoutStore, useConfig} from '../utils';
|
|
2
|
+
import {type PdkCheckoutForm, type PdkFormData} from '../types';
|
|
3
|
+
import {type AddressField, AddressType, type PdkField} from '../data';
|
|
4
|
+
|
|
5
|
+
const getEntry = (data: PdkFormData, value: undefined | string): string => {
|
|
6
|
+
return (data[value as string] as string | undefined) ?? '';
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const updateCheckoutForm = (): void => {
|
|
10
|
+
const checkout = useCheckoutStore();
|
|
11
|
+
const config = useConfig();
|
|
12
|
+
|
|
13
|
+
const data = config.getFormData();
|
|
14
|
+
|
|
15
|
+
const transformedData = {
|
|
16
|
+
[AddressType.Billing]: {},
|
|
17
|
+
[AddressType.Shipping]: {},
|
|
18
|
+
} as PdkCheckoutForm;
|
|
19
|
+
|
|
20
|
+
checkout.state.addressTypes.forEach((addressType) => {
|
|
21
|
+
Object.entries(config.formData[addressType]).forEach(([key, value]) => {
|
|
22
|
+
transformedData[addressType][key as AddressField] = getEntry(data, value);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
Object.entries(config.formData).forEach(([key, value]) => {
|
|
27
|
+
if (checkout.state.addressTypes.includes(key as AddressType)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
transformedData[key as PdkField] = getEntry(data, value as string);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
void checkout.set({
|
|
35
|
+
addressType: getAddressType(),
|
|
36
|
+
form: Object.freeze({...transformedData}),
|
|
37
|
+
});
|
|
38
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {getFrontendContext, hasAddressType, useUtil} from '../utils';
|
|
2
|
+
import {type CheckoutStoreState, type PdkCheckoutForm} from '../types';
|
|
3
|
+
import {updateAddressType} from '../listeners';
|
|
4
|
+
import {AddressType, PdkUtil, StoreListener} from '../data';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
7
|
+
export const createCheckoutStore = () => {
|
|
8
|
+
const createStore = useUtil(PdkUtil.CreateStore);
|
|
9
|
+
|
|
10
|
+
return createStore<CheckoutStoreState>(Symbol('checkout'), () => {
|
|
11
|
+
return {
|
|
12
|
+
state: {
|
|
13
|
+
addressType: AddressType.Shipping,
|
|
14
|
+
addressTypes: ([AddressType.Billing, AddressType.Shipping] as const).filter(hasAddressType),
|
|
15
|
+
context: getFrontendContext(),
|
|
16
|
+
form: {
|
|
17
|
+
[AddressType.Billing]: {},
|
|
18
|
+
[AddressType.Shipping]: {},
|
|
19
|
+
} as PdkCheckoutForm,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
listeners: {
|
|
23
|
+
[StoreListener.Update]: [updateAddressType],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
27
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import {type Keyable} from '@myparcel-dev/pdk-common';
|
|
3
|
+
import {type StoreState} from '../types';
|
|
4
|
+
|
|
5
|
+
export const logStoreDebugInfo = (
|
|
6
|
+
name: Keyable,
|
|
7
|
+
newState: Partial<StoreState>,
|
|
8
|
+
oldState: Record<string, unknown>,
|
|
9
|
+
): void => {
|
|
10
|
+
if (import.meta.env.MODE !== 'development') {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const storeName = name.toString();
|
|
15
|
+
|
|
16
|
+
const color = storeName.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0) % 360;
|
|
17
|
+
|
|
18
|
+
console.log(`%c[UPDATE] ${storeName}`, `color: hsl(${color}, 100%, 60%)`, {newState, oldState});
|
|
19
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {type Keyable} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {objectIsEqual} from '@myparcel-dev/ts-utils';
|
|
3
|
+
import {type InitialStoreData, type StoreData, type StoreListenerObject, type StoreState} from '../types';
|
|
4
|
+
import {StoreListener} from '../data'; // eslint-disable-next-line max-lines-per-function
|
|
5
|
+
import {logStoreDebugInfo} from './logStoreDebugInfo';
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line max-lines-per-function
|
|
8
|
+
export const realCreateStore = <S extends StoreState = StoreState, N extends Keyable = Keyable>(
|
|
9
|
+
name: N,
|
|
10
|
+
initialData: () => InitialStoreData<S>,
|
|
11
|
+
): (() => StoreData<S, N>[N]) => {
|
|
12
|
+
const {storedState} = window.MyParcelPdk;
|
|
13
|
+
|
|
14
|
+
if (!storedState[name]) {
|
|
15
|
+
const {state, ...rest}: InitialStoreData<S> = initialData();
|
|
16
|
+
|
|
17
|
+
storedState[name] = {
|
|
18
|
+
state: {},
|
|
19
|
+
|
|
20
|
+
listeners: (rest.listeners ?? {}) as StoreListenerObject<StoreState>,
|
|
21
|
+
|
|
22
|
+
on(listener, callback) {
|
|
23
|
+
storedState[name].listeners[listener] ??= [];
|
|
24
|
+
storedState[name].listeners[listener]?.push(callback);
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
async set(newState) {
|
|
28
|
+
const oldState = {...storedState[name].state};
|
|
29
|
+
const state = {...oldState, ...newState};
|
|
30
|
+
|
|
31
|
+
if (objectIsEqual(state, oldState)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
logStoreDebugInfo(name, state, oldState);
|
|
36
|
+
|
|
37
|
+
Object.assign(storedState[name].state, newState);
|
|
38
|
+
|
|
39
|
+
const updateListeners = storedState[name].listeners?.[StoreListener.Update];
|
|
40
|
+
|
|
41
|
+
const updates = await Promise.all(
|
|
42
|
+
updateListeners?.map(async (listener) => {
|
|
43
|
+
return listener({...state}, oldState);
|
|
44
|
+
}) ?? [],
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
if (updates.length) {
|
|
48
|
+
const additionalUpdate = updates.reduce((state, update) => {
|
|
49
|
+
return {...state, ...update};
|
|
50
|
+
}, {});
|
|
51
|
+
|
|
52
|
+
Object.assign(storedState[name].state, additionalUpdate);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Trigger the update listeners on initialisation.
|
|
58
|
+
void storedState[name].set(state);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return () => {
|
|
62
|
+
return storedState[name] as StoreData<S, N>[N];
|
|
63
|
+
};
|
|
64
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {type FrontendEndpoint} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {type PromiseOr} from '@myparcel-dev/ts-utils';
|
|
3
|
+
import {type AddressType, type PdkField} from '../data';
|
|
4
|
+
import {type FrontendEndpointData, type FrontendEndpointResponse} from './endpoints.types';
|
|
5
|
+
import {type AddressFields} from './address.types';
|
|
6
|
+
|
|
7
|
+
export type PdkFormData = Record<string, FormDataEntryValue | undefined>;
|
|
8
|
+
|
|
9
|
+
export type PdkCheckoutConfigInput = Omit<
|
|
10
|
+
PdkCheckoutConfig,
|
|
11
|
+
'selectors' | 'formChange' | 'getFormData' | 'getAddressType' | 'hasDeliveryOptions'
|
|
12
|
+
> & {
|
|
13
|
+
formChange?(callback: () => void): void;
|
|
14
|
+
getAddressType?(value: unknown): AddressType;
|
|
15
|
+
getFormData?(): PdkFormData;
|
|
16
|
+
hasDeliveryOptions?(shippingMethod: string): PromiseOr<boolean>;
|
|
17
|
+
selectors: Omit<PdkCheckoutConfig['selectors'], 'deliveryOptions'> & {
|
|
18
|
+
deliveryOptions?: string;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Includes our own address fields, and any additional fields described by optional packages.
|
|
24
|
+
*/
|
|
25
|
+
export type PdkCheckoutForm = Record<PdkField, string> & {
|
|
26
|
+
[AddressType.Billing]: AddressFields & Record<string, string>;
|
|
27
|
+
[AddressType.Shipping]: AddressFields & Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export interface PdkCheckoutConfig {
|
|
31
|
+
fields: PdkCheckoutForm;
|
|
32
|
+
formData: PdkCheckoutForm;
|
|
33
|
+
|
|
34
|
+
selectors: {
|
|
35
|
+
deliveryOptions: string;
|
|
36
|
+
deliveryOptionsWrapper: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Do a request to the backend.
|
|
41
|
+
*/
|
|
42
|
+
doRequest<E extends FrontendEndpoint>(endpoint: FrontendEndpointData<E>): Promise<FrontendEndpointResponse<E>>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The handler for the form change event.
|
|
46
|
+
*/
|
|
47
|
+
formChange(callback: () => void): void;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the address type that is currently active.
|
|
51
|
+
*/
|
|
52
|
+
getAddressType(value: unknown): AddressType;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get the form element.
|
|
56
|
+
*/
|
|
57
|
+
getForm(): HTMLFormElement;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the form data.
|
|
61
|
+
*/
|
|
62
|
+
getFormData(): PdkFormData;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check if the address type is available.
|
|
66
|
+
*/
|
|
67
|
+
hasAddressType(addressType: AddressType): boolean;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if the delivery options are available for the shipping method.
|
|
71
|
+
*/
|
|
72
|
+
hasDeliveryOptions(shippingMethod: string): PromiseOr<boolean>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Callback that is called when the checkout is initialized.
|
|
76
|
+
*/
|
|
77
|
+
initialize(): Promise<void>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Toggle showing/hiding a field.
|
|
81
|
+
*/
|
|
82
|
+
toggleField(field: HTMLInputElement, show: boolean): void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type InitializeCallback = () => void;
|
|
86
|
+
|
|
87
|
+
export interface PdkCheckout {
|
|
88
|
+
onInitialize(callback: InitializeCallback): void;
|
|
89
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EndpointObject,
|
|
3
|
+
type FrontendEndpoint,
|
|
4
|
+
type PdkEndpointDefinition,
|
|
5
|
+
type PdkEndpointParameters,
|
|
6
|
+
type PdkEndpointResponse,
|
|
7
|
+
type Plugin,
|
|
8
|
+
} from '@myparcel-dev/pdk-common';
|
|
9
|
+
|
|
10
|
+
export type FrontendPdkEndpointObject = EndpointObject<FrontendEndpoint>;
|
|
11
|
+
|
|
12
|
+
export type FrontendEndpointData<E extends FrontendEndpoint> = FrontendPdkEndpointObject[E] & {
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type FrontendEndpointResponse<E extends FrontendEndpoint> = PdkEndpointResponse<E, FrontendEndpointDefinition>;
|
|
17
|
+
|
|
18
|
+
export type FrontendEndpointParameters<E extends FrontendEndpoint> = PdkEndpointParameters<
|
|
19
|
+
E,
|
|
20
|
+
FrontendEndpointDefinition
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
interface FetchCheckoutContextDefinition extends PdkEndpointDefinition {
|
|
24
|
+
name: FrontendEndpoint.FetchCheckoutContext;
|
|
25
|
+
parameters: {
|
|
26
|
+
shippingMethod?: string;
|
|
27
|
+
};
|
|
28
|
+
response: {data: {context: [Plugin.ModelContextContextBag]}};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type FrontendEndpointDefinition = FetchCheckoutContextDefinition;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {type isEnumValue, type isOfType, type PromiseOr} from '@myparcel-dev/ts-utils';
|
|
2
|
+
import {
|
|
3
|
+
type doRequest,
|
|
4
|
+
type fieldsEqual,
|
|
5
|
+
type getAddressField,
|
|
6
|
+
type getAddressFieldValue,
|
|
7
|
+
type getElement,
|
|
8
|
+
type getFieldValue,
|
|
9
|
+
type setFieldValue,
|
|
10
|
+
type triggerEvent,
|
|
11
|
+
} from '../utils';
|
|
12
|
+
import {type realCreateStore} from '../store';
|
|
13
|
+
import {type PdkEvent, type PdkUtil} from '../data';
|
|
14
|
+
import {type CheckoutStore, type StoreData} from './store.types';
|
|
15
|
+
import {type PdkCheckout, type PdkCheckoutConfig} from './checkout.types';
|
|
16
|
+
|
|
17
|
+
export interface PdkEvents extends AdditionalEvents {
|
|
18
|
+
[PdkEvent.CheckoutUpdate]: string;
|
|
19
|
+
[PdkEvent.CheckoutUpdated]: string;
|
|
20
|
+
[PdkEvent.StoreInitialize]: string;
|
|
21
|
+
[PdkEvent.StoreInitialized]: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PdkStores extends AdditionalStores {
|
|
25
|
+
checkout: CheckoutStore;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PdkUtils extends AdditionalUtils {
|
|
29
|
+
[PdkUtil.CreateStore]: typeof realCreateStore;
|
|
30
|
+
[PdkUtil.DoRequest]: typeof doRequest;
|
|
31
|
+
[PdkUtil.FieldsEqual]: typeof fieldsEqual;
|
|
32
|
+
[PdkUtil.GetAddressFieldValue]: typeof getAddressFieldValue;
|
|
33
|
+
[PdkUtil.GetAddressField]: typeof getAddressField;
|
|
34
|
+
[PdkUtil.GetElement]: typeof getElement;
|
|
35
|
+
[PdkUtil.GetFieldValue]: typeof getFieldValue;
|
|
36
|
+
[PdkUtil.IsEnumValue]: typeof isEnumValue;
|
|
37
|
+
[PdkUtil.IsOfType]: typeof isOfType;
|
|
38
|
+
[PdkUtil.SetFieldValue]: typeof setFieldValue;
|
|
39
|
+
[PdkUtil.TriggerEvent]: typeof triggerEvent;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface MyParcelPdk {
|
|
43
|
+
config: PdkCheckoutConfig;
|
|
44
|
+
events: PdkEvents;
|
|
45
|
+
initializeCallbacks: (() => PromiseOr<void>)[];
|
|
46
|
+
initialized: boolean;
|
|
47
|
+
instance: PdkCheckout;
|
|
48
|
+
storedState: StoreData;
|
|
49
|
+
stores: PdkStores;
|
|
50
|
+
utils: PdkUtils;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Pull in a util from the window object.
|
|
54
|
+
*/
|
|
55
|
+
useUtil<N extends PdkUtil>(name: N): PdkUtils[N];
|
|
56
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {type Keyable, type Plugin} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {type PromiseOr} from '@myparcel-dev/ts-utils';
|
|
3
|
+
import {type AddressType, type StoreListener} from '../data';
|
|
4
|
+
import {type PdkCheckoutForm} from './checkout.types';
|
|
5
|
+
|
|
6
|
+
type StoreListeners<T extends StoreState> = {
|
|
7
|
+
[StoreListener.Update]: StoreCallbackUpdate<T>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type StoreState = Record<string, unknown>;
|
|
11
|
+
|
|
12
|
+
export type StoreListenerObject<S extends StoreState> = {
|
|
13
|
+
[L in StoreListener]?: StoreListeners<S>[L][];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type StoreCallbackUpdate<T extends StoreState = StoreState> = (newState: T, oldState?: T) => PromiseOr<void | T>;
|
|
17
|
+
|
|
18
|
+
export type StoreData<S extends StoreState = StoreState, N extends Keyable = Keyable> = Record<N, Store<S>>;
|
|
19
|
+
|
|
20
|
+
export type InitialStoreData<S extends StoreState = StoreState> = {
|
|
21
|
+
state: S;
|
|
22
|
+
listeners?: StoreListenerObject<S>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type Store<S extends StoreState = StoreState> = {
|
|
26
|
+
readonly set: (newState: Partial<S>) => PromiseOr<void>;
|
|
27
|
+
readonly state: S;
|
|
28
|
+
readonly listeners: StoreListenerObject<S>;
|
|
29
|
+
readonly on: <L extends StoreListener>(listener: L, callback: StoreListeners<S>[L]) => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type CheckoutStoreState = {
|
|
33
|
+
addressType: AddressType;
|
|
34
|
+
addressTypes: AddressType[];
|
|
35
|
+
context: Plugin.ModelContextCheckoutContext;
|
|
36
|
+
form: PdkCheckoutForm;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type CheckoutStore = Store<CheckoutStoreState>;
|
|
40
|
+
|
|
41
|
+
export interface Stores {
|
|
42
|
+
checkout: CheckoutStore;
|
|
43
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import {useSettings} from './useSettings';
|
|
2
|
+
|
|
3
|
+
export const getEnabledShippingMethods = (): string[] => {
|
|
4
|
+
const settings = useSettings();
|
|
5
|
+
// No need to filter keys, 0 (off) is not included in the object
|
|
6
|
+
const entries = Object.entries(settings.allowedShippingMethods);
|
|
7
|
+
|
|
8
|
+
return entries.flatMap(([, value]) => value);
|
|
9
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {type Plugin} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {ATTRIBUTE_CONTEXT} from '../constants';
|
|
3
|
+
import {useConfig} from './useConfig';
|
|
4
|
+
import {getElement} from './global';
|
|
5
|
+
|
|
6
|
+
export const getFrontendContext = (): Plugin.ModelContextCheckoutContext => {
|
|
7
|
+
const config = useConfig();
|
|
8
|
+
|
|
9
|
+
const wrapper = getElement(config.selectors.deliveryOptionsWrapper, false);
|
|
10
|
+
const context = wrapper?.getAttribute(ATTRIBUTE_CONTEXT);
|
|
11
|
+
|
|
12
|
+
if (!wrapper || !context) {
|
|
13
|
+
throw new Error('No delivery options wrapper or context found.');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
wrapper.removeAttribute(ATTRIBUTE_CONTEXT);
|
|
17
|
+
|
|
18
|
+
const {checkout} = JSON.parse(context) as Plugin.ModelContextContextBag;
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
21
|
+
return checkout!;
|
|
22
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {FrontendEndpoint} from '@myparcel-dev/pdk-common';
|
|
3
|
+
import {doRequestSpy, mockPdkCheckout} from '../../__tests__';
|
|
4
|
+
import {doRequest} from './doRequest';
|
|
5
|
+
|
|
6
|
+
describe('doRequest', () => {
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
await mockPdkCheckout();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should call doRequest', async () => {
|
|
12
|
+
expect.assertions(1);
|
|
13
|
+
|
|
14
|
+
await doRequest(FrontendEndpoint.FetchCheckoutContext, {shippingMethod: 'shipping-method'});
|
|
15
|
+
|
|
16
|
+
expect(doRequestSpy).toHaveBeenCalled();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {type FrontendEndpoint} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {useSettings} from '../useSettings';
|
|
3
|
+
import {useConfig} from '../useConfig';
|
|
4
|
+
import {type FrontendEndpointParameters, type FrontendEndpointResponse} from '../../types';
|
|
5
|
+
|
|
6
|
+
type DoRequest = <E extends FrontendEndpoint>(
|
|
7
|
+
endpoint: E,
|
|
8
|
+
parameters?: FrontendEndpointParameters<E>,
|
|
9
|
+
) => Promise<FrontendEndpointResponse<E>>;
|
|
10
|
+
|
|
11
|
+
export const doRequest: DoRequest = (endpoint, parameters) => {
|
|
12
|
+
const config = useConfig();
|
|
13
|
+
const settings = useSettings();
|
|
14
|
+
|
|
15
|
+
const {baseUrl, endpoints} = settings.actions;
|
|
16
|
+
|
|
17
|
+
return config.doRequest({
|
|
18
|
+
...endpoints[endpoint],
|
|
19
|
+
parameters: {
|
|
20
|
+
...endpoints[endpoint].parameters,
|
|
21
|
+
...parameters,
|
|
22
|
+
},
|
|
23
|
+
baseUrl,
|
|
24
|
+
});
|
|
25
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {AddressField, AddressType, PdkField} from '../../data';
|
|
3
|
+
import {getMockFormData, mockPdkCheckout} from '../../__tests__';
|
|
4
|
+
import {fieldsEqual} from './fieldsEqual';
|
|
5
|
+
|
|
6
|
+
describe('fieldsEqual', () => {
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
await mockPdkCheckout();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should return true if the fields are equal', () => {
|
|
12
|
+
const result = fieldsEqual(
|
|
13
|
+
getMockFormData({[AddressType.Billing]: {[AddressField.Address1]: 'appelboom'}}),
|
|
14
|
+
getMockFormData({[AddressType.Billing]: {[AddressField.Address1]: 'appelboom'}}),
|
|
15
|
+
AddressField.Address1,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
expect(result).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should return false if the fields are not equal', () => {
|
|
22
|
+
const result = fieldsEqual(
|
|
23
|
+
getMockFormData({[AddressType.Billing]: {[AddressField.Address1]: 'appelboom'}}),
|
|
24
|
+
getMockFormData({[AddressType.Billing]: {[AddressField.Address1]: 'perenboom'}}),
|
|
25
|
+
AddressField.Address1,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(result).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should return true if the fields are equal for a specific address type', () => {
|
|
32
|
+
const result = fieldsEqual(
|
|
33
|
+
getMockFormData({
|
|
34
|
+
[AddressType.Billing]: {
|
|
35
|
+
[AddressField.Address1]: 'ruurd',
|
|
36
|
+
},
|
|
37
|
+
[AddressType.Shipping]: {
|
|
38
|
+
[AddressField.Address1]: 'appelboom',
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
getMockFormData({
|
|
42
|
+
[AddressType.Billing]: {
|
|
43
|
+
[AddressField.Address1]: 'jan-piet',
|
|
44
|
+
},
|
|
45
|
+
[AddressType.Shipping]: {
|
|
46
|
+
[AddressField.Address1]: 'appelboom',
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
AddressField.Address1,
|
|
50
|
+
AddressType.Shipping,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(result).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('can check if pdk fields match', () => {
|
|
57
|
+
const result = fieldsEqual(
|
|
58
|
+
getMockFormData({
|
|
59
|
+
[PdkField.ShippingMethod]: 'ruurd',
|
|
60
|
+
}),
|
|
61
|
+
getMockFormData({
|
|
62
|
+
[PdkField.ShippingMethod]: 'ruurd',
|
|
63
|
+
}),
|
|
64
|
+
PdkField.ShippingMethod,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('can check if pdk fields do not match', () => {
|
|
71
|
+
const result = fieldsEqual(
|
|
72
|
+
getMockFormData({
|
|
73
|
+
[PdkField.ShippingMethod]: 'ruurd',
|
|
74
|
+
}),
|
|
75
|
+
getMockFormData({
|
|
76
|
+
[PdkField.ShippingMethod]: 'barend',
|
|
77
|
+
}),
|
|
78
|
+
PdkField.ShippingMethod,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(result).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {isEnumValue} from '@myparcel-dev/ts-utils';
|
|
2
|
+
import {type AddressFields, type PdkCheckoutForm} from '../../types';
|
|
3
|
+
import {type AddressField, type AddressType, PdkField} from '../../data';
|
|
4
|
+
import {getAddressType} from './getAddressType';
|
|
5
|
+
|
|
6
|
+
type FieldsEqual = {
|
|
7
|
+
(
|
|
8
|
+
newFields: PdkCheckoutForm,
|
|
9
|
+
oldFields: PdkCheckoutForm,
|
|
10
|
+
addressFieldName: AddressField | string,
|
|
11
|
+
addressType?: AddressType,
|
|
12
|
+
): boolean;
|
|
13
|
+
(newFields: PdkCheckoutForm, oldFields: PdkCheckoutForm, fieldName: PdkField, arg4?: never): boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const fieldsEqual: FieldsEqual = (newFields, oldFields, arg3, arg4) => {
|
|
17
|
+
newFields ??= {} as PdkCheckoutForm;
|
|
18
|
+
oldFields ??= {} as PdkCheckoutForm;
|
|
19
|
+
|
|
20
|
+
if (isEnumValue(arg3, PdkField)) {
|
|
21
|
+
return newFields[arg3] === oldFields[arg3];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (arg4 === undefined) {
|
|
25
|
+
arg4 = getAddressType();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return newFields[arg4][arg3 as keyof AddressFields] === oldFields[arg4][arg3 as keyof AddressFields];
|
|
29
|
+
};
|