From cac105a323ff88fec11519beaca67fda8cb98c6a Mon Sep 17 00:00:00 2001 From: Lucas Chociay Date: Wed, 16 Nov 2022 18:14:52 -0300 Subject: [PATCH] feat: add setValue method to elements, other qol improvements (#248) --- src/BasisTheory.ts | 4 ++-- src/elements/services/tokens.ts | 4 +++- src/tokens/BasisTheoryTokens.ts | 7 ++++++- src/types/elements/elements.ts | 20 ++++++++++++++++---- src/types/elements/options.ts | 10 +++++----- src/types/elements/services/tokens.ts | 4 +++- src/types/sdk/services/tokens.ts | 4 +++- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/BasisTheory.ts b/src/BasisTheory.ts index b02898c9..dce13751 100644 --- a/src/BasisTheory.ts +++ b/src/BasisTheory.ts @@ -85,12 +85,12 @@ export class BasisTheory private _proxies?: Proxies; public init( - apiKey: string, + apiKey: string | undefined, options?: BasisTheoryInitOptionsWithoutElements ): Promise; public init( - apiKey: string, + apiKey: string | undefined, options: BasisTheoryInitOptionsWithElements ): Promise; diff --git a/src/elements/services/tokens.ts b/src/elements/services/tokens.ts index ba05414d..0973fea3 100644 --- a/src/elements/services/tokens.ts +++ b/src/elements/services/tokens.ts @@ -49,7 +49,9 @@ const delegateTokens = ( public retrieve( id: string, requestOptions?: RequestOptions - ): Promise { + // avoid casting when accessing token data props + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ): Promise> { if (elements !== undefined) { return elements.tokens.retrieve(id, requestOptions); } diff --git a/src/tokens/BasisTheoryTokens.ts b/src/tokens/BasisTheoryTokens.ts index b26ff4d5..a0514e94 100644 --- a/src/tokens/BasisTheoryTokens.ts +++ b/src/tokens/BasisTheoryTokens.ts @@ -39,7 +39,12 @@ export const BasisTheoryTokens = new CrudBuilder( super(_options); } - public retrieve(id: string, options: RequestOptions = {}): Promise { + public retrieve( + id: string, + options: RequestOptions = {} + // avoid casting when accessing token data props + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ): Promise> { const url = `/${id}`; return this.client diff --git a/src/types/elements/elements.ts b/src/types/elements/elements.ts index f5e93229..fedf533d 100644 --- a/src/types/elements/elements.ts +++ b/src/types/elements/elements.ts @@ -19,8 +19,11 @@ import type { UpdateCardExpirationDateElementOptions, UpdateCardVerificationCodeElementOptions, UpdateTextElementOptions, + CardElementValue, + CardExpirationDateValue, } from './options'; import type { Tokenize, Tokens } from './services'; +import { DataElementReference } from './shared'; interface BaseElement { readonly mounted: boolean; @@ -36,14 +39,20 @@ interface BaseElement { ): Subscription; } -type CardElement = BaseElement; +type CardElement = BaseElement & { + setValue(value: CardElementValue<'reference'>): void; +}; -type TextElement = BaseElement; +type TextElement = BaseElement & { + setValue(value: DataElementReference): void; +}; type CardNumberElement = BaseElement< UpdateCardNumberElementOptions, CardNumberElementEvents ->; +> & { + setValue(value: DataElementReference): void; +}; type CardExpirationDateElement = BaseElement< UpdateCardExpirationDateElementOptions, @@ -51,12 +60,15 @@ type CardExpirationDateElement = BaseElement< > & { month(): ElementWrapper; year(): ElementWrapper; + setValue(value: CardExpirationDateValue<'reference'>): void; }; type CardVerificationCodeElement = BaseElement< UpdateCardVerificationCodeElementOptions, CardVerificationCodeElementEvents ->; +> & { + setValue(value: DataElementReference): void; +}; // eslint-disable-next-line @typescript-eslint/no-explicit-any type ElementWrapper = BaseElement> = { diff --git a/src/types/elements/options.ts b/src/types/elements/options.ts index 9e047d57..6e7d87a5 100644 --- a/src/types/elements/options.ts +++ b/src/types/elements/options.ts @@ -58,15 +58,15 @@ interface CardElementValue { number?: T extends 'reference' ? DataElementReference : string; // disabling camecalse so that the element value matches the API data /* eslint-disable camelcase */ - expiration_month?: T extends 'reference' ? DataElementReference : string; - expiration_year?: T extends 'reference' ? DataElementReference : string; + expiration_month?: T extends 'reference' ? DataElementReference : number; + expiration_year?: T extends 'reference' ? DataElementReference : number; /* eslint-enable camelcase */ cvc?: T extends 'reference' ? DataElementReference : string; } interface CardExpirationDateValue { - month: T extends 'reference' ? DataElementReference : string; - year: T extends 'reference' ? DataElementReference : string; + month: T extends 'reference' ? DataElementReference : number; + year: T extends 'reference' ? DataElementReference : number; } type CreateCardElementOptions = CustomizableElementOptions & { @@ -104,7 +104,7 @@ type CreateCardExpirationDateElementOptions = CustomizableElementOptions & Pick & Required> & { 'aria-label'?: string; - value?: CardExpirationDateValue<'static'>; + value?: CardExpirationDateValue<'static'> | string; }; type UpdateCardExpirationDateElementOptions = Omit< diff --git a/src/types/elements/services/tokens.ts b/src/types/elements/services/tokens.ts index 7ccca766..3721b349 100644 --- a/src/types/elements/services/tokens.ts +++ b/src/types/elements/services/tokens.ts @@ -10,7 +10,9 @@ type CreateToken = CreateTokenModel; type UpdateToken = UpdateTokenModel; type Tokens = Create & - Retrieve & + // avoid casting when accessing token data props + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Retrieve> & Update; export type { Tokens, CreateToken, UpdateToken }; diff --git a/src/types/sdk/services/tokens.ts b/src/types/sdk/services/tokens.ts index 0509d6e6..e0f83b3a 100644 --- a/src/types/sdk/services/tokens.ts +++ b/src/types/sdk/services/tokens.ts @@ -30,7 +30,9 @@ interface SearchTokensRequest { interface Tokens extends Create, Update, - Retrieve, + // avoid casting when accessing token data props + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Retrieve>, Delete, List { createAssociation(