Skip to content

Commit

Permalink
feat: validating checkout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeiroguilherme committed Feb 2, 2024
1 parent 811e028 commit 4088a8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
26 changes: 19 additions & 7 deletions packages/lib/src/components/internal/BaseElement/BaseElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import { ComponentChild, render } from 'preact';
import getProp from '../../../utils/getProp';
import uuid from '../../../utils/uuid';
import AdyenCheckoutError from '../../../core/Errors/AdyenCheckoutError';
import { ICore } from '../../../core/types';
import { BaseElementProps, IBaseElement } from './types';
import { PaymentData } from '../../../types/global-types';

import type { ICore } from '../../../core/types';
import type { BaseElementProps, IBaseElement } from './types';
import type { PaymentData } from '../../../types/global-types';

/**
* Verify if the first parameter is instance of Core.
* We do not use 'instanceof' to avoid importing the Core class directly into this class.
* @param checkout
*/
function assertIsCoreInstance(checkout: ICore): checkout is ICore {
if (!checkout) return false;
const isCoreObject = typeof checkout.initialize === 'function' && typeof checkout.createFromAction === 'function';
return isCoreObject;
}

class BaseElement<P extends BaseElementProps> implements IBaseElement {
public readonly _id = `${this.constructor['type']}-${uuid()}`;
Expand All @@ -19,20 +31,20 @@ class BaseElement<P extends BaseElementProps> implements IBaseElement {
protected static defaultProps = {};

constructor(checkout: ICore, props?: P) {
this.core = checkout;
const isCoreInstance = assertIsCoreInstance(checkout);

if (!this.core) {
if (!isCoreInstance) {
throw new AdyenCheckoutError(
'IMPLEMENTATION_ERROR',
`Trying to initialise the component '${this.constructor['type']}' without a reference to an instance of Checkout ('core' prop)`
`Trying to initialise the component '${this.constructor['type']}' without a reference to an instance of AdyenCheckout`
);
}

this.core = checkout;
this.buildElementProps(props);
}

protected buildElementProps(componentProps?: P) {
// const { core, ...rest } = componentProps;
this.props = this.formatProps({ ...this.constructor['defaultProps'], ...componentProps });
}

Expand Down
10 changes: 6 additions & 4 deletions packages/lib/src/components/internal/UIElement/UIElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { getSanitizedResponse, resolveFinalResult } from './utils';
import AdyenCheckoutError from '../../../core/Errors/AdyenCheckoutError';
import { hasOwnProperty } from '../../../utils/hasOwnProperty';
import DropinElement from '../../Dropin';
import { CoreConfiguration, ICore } from '../../../core/types';
import { Resources } from '../../../core/Context/Resources';
import { NewableComponent } from '../../../core/core.registry';

import type { CoreConfiguration, ICore } from '../../../core/types';
import type { NewableComponent } from '../../../core/core.registry';
import type { ComponentMethodsRef, IUIElement, PayButtonFunctionProps, UIElementProps, UIElementStatus } from './types';
import type { PaymentAction, PaymentResponseData, RawPaymentResponse } from '../../../types/global-types';

import './UIElement.scss';
import { ComponentMethodsRef, IUIElement, PayButtonFunctionProps, UIElementProps, UIElementStatus } from './types';
import { PaymentAction, PaymentResponseData, RawPaymentResponse } from '../../../types/global-types';

export abstract class UIElement<P extends UIElementProps = UIElementProps> extends BaseElement<P> implements IUIElement {
protected componentRef: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/core/core.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function assertClassHasType(Class: any): Class is typeof UIElement {
return hasValidType;
}

export type NewableComponent = new (checkout: ICore, props) => IUIElement;
export type NewableComponent = new (checkout: ICore, props?) => IUIElement;

export interface IRegistry {
add(...items: NewableComponent[]): void;
Expand Down

0 comments on commit 4088a8f

Please sign in to comment.