Skip to content

Commit

Permalink
fix(checkout): export more values
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 29, 2023
1 parent f57eedc commit cc6721f
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -12,8 +11,6 @@ exports[`exports > exports from index.ts 1`] = `
"EVENT_UPDATE_DELIVERY_OPTIONS",
"FrontendEndpoint",
"PdkDeliveryOptionsEvent",
"PdkEvent",
"PdkField",
"StoreListener",
"Util",
"createPdkCheckout",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -12,8 +11,6 @@ exports[`exports > exports from index.ts 1`] = `
"EVENT_UPDATE_DELIVERY_OPTIONS",
"FrontendEndpoint",
"PdkDeliveryOptionsEvent",
"PdkEvent",
"PdkField",
"StoreListener",
"Util",
"createPdkCheckout",
Expand Down
2 changes: 1 addition & 1 deletion apps/checkout/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 23 additions & 21 deletions apps/checkout/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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';
25 changes: 25 additions & 0 deletions libs/checkout/common/src/types/address.types.ts
Original file line number Diff line number Diff line change
@@ -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<AddressField, string>;
30 changes: 29 additions & 1 deletion libs/checkout/common/src/types/endpoints.types.ts
Original file line number Diff line number Diff line change
@@ -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<FrontendEndpoint>;

export type FrontendEndpointData<E extends FrontendEndpoint> = FrontendPdkEndpointObject[E] & {
baseUrl: string;
};

export type FrontendEndpointResponse<E extends FrontendEndpoint> = PdkEndpointResponse<E, FrontendEndpointDefinition>;

export type FrontendEndpointParameters<E extends FrontendEndpoint> = PdkEndpointParameters<
E,
FrontendEndpointDefinition
>;

interface FetchCheckoutContextDefinition extends PdkEndpointDefinition {
formattedResponse: Plugin.ModelContextCheckoutContext;
name: FrontendEndpoint.FetchCheckoutContext;
parameters: {
shippingMethod?: string;
};
response: [Plugin.ModelContextCheckoutContext];
}

export type FrontendEndpointDefinition = FetchCheckoutContextDefinition;
1 change: 1 addition & 0 deletions libs/checkout/common/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './address.types';
export * from './endpoints.types';
3 changes: 2 additions & 1 deletion libs/checkout/core/src/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {type PdkEvent} from '@myparcel-pdk/checkout-common';
import {type isOfType, type PromiseOr} from '@myparcel/ts-utils';
import {
type doRequest,
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion libs/checkout/core/src/init/createConfig.ts
Original file line number Diff line number Diff line change
@@ -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 => ({
Expand Down
3 changes: 2 additions & 1 deletion libs/checkout/core/src/init/setupGlobals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {PdkEvent} from '@myparcel-pdk/checkout-common';
import {isOfType} from '@myparcel/ts-utils';
import {
doRequest,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion libs/checkout/core/src/listeners/updateAddressType.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
3 changes: 2 additions & 1 deletion libs/checkout/core/src/listeners/updateCheckoutForm.ts
Original file line number Diff line number Diff line change
@@ -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<string, FormDataEntryValue>, value: undefined | string): string {
return (data[value as string] as string | undefined) ?? '';
Expand Down
3 changes: 2 additions & 1 deletion libs/checkout/core/src/store/createCheckoutStore.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
25 changes: 0 additions & 25 deletions libs/checkout/core/src/types/endpoints.types.ts

This file was deleted.

1 change: 0 additions & 1 deletion libs/checkout/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './endpoints.types';
export * from './types';
41 changes: 10 additions & 31 deletions libs/checkout/core/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -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<AddressField, string>;

export type PdkCheckoutConfigInput = Omit<
PdkCheckoutConfig,
Expand Down Expand Up @@ -60,9 +41,7 @@ export interface PdkCheckoutConfig {
/**
* Do a request to the backend.
*/
doRequest<E extends FrontendEndpoint>(
endpoint: FrontendPdkEndpointObject[E] & {baseUrl: string},
): Promise<FrontendEndpointResponse<E>>;
doRequest<E extends FrontendEndpoint>(endpoint: FrontendEndpointData<E>): Promise<FrontendEndpointResponse<E>>;

/**
* The handler for the form change event.
Expand Down
2 changes: 1 addition & 1 deletion libs/checkout/core/src/utils/getFrontendContext.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
7 changes: 5 additions & 2 deletions libs/checkout/core/src/utils/global/doRequest.ts
Original file line number Diff line number Diff line change
@@ -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 = <E extends FrontendEndpoint>(
Expand Down
10 changes: 2 additions & 8 deletions libs/checkout/core/src/utils/global/fieldsEqual.ts
Original file line number Diff line number Diff line change
@@ -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 = {
(
Expand Down
4 changes: 2 additions & 2 deletions libs/checkout/core/src/utils/global/getAddressField.ts
Original file line number Diff line number Diff line change
@@ -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: "#<billing|shipping>_<name>".
Expand Down
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion libs/checkout/core/src/utils/global/getAddressType.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion libs/checkout/core/src/utils/global/getFieldValue.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit cc6721f

Please sign in to comment.