Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setValue method to elements, other qol improvements #248

Merged
merged 2 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BasisTheory.ts
Expand Up @@ -85,12 +85,12 @@ export class BasisTheory
private _proxies?: Proxies;

public init(
apiKey: string,
apiKey: string | undefined,
options?: BasisTheoryInitOptionsWithoutElements
): Promise<IBasisTheory>;

public init(
apiKey: string,
apiKey: string | undefined,
options: BasisTheoryInitOptionsWithElements
): Promise<IBasisTheory & BasisTheoryElements>;

Expand Down
4 changes: 3 additions & 1 deletion src/elements/services/tokens.ts
Expand Up @@ -49,7 +49,9 @@ const delegateTokens = (
public retrieve(
id: string,
requestOptions?: RequestOptions
): Promise<Token> {
// returned data type as any to avoid casting when trying to retrieve token.data.<prop>
lcschy marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<Token<any>> {
lcschy marked this conversation as resolved.
Show resolved Hide resolved
if (elements !== undefined) {
return elements.tokens.retrieve(id, requestOptions);
}
Expand Down
7 changes: 6 additions & 1 deletion src/tokens/BasisTheoryTokens.ts
Expand Up @@ -39,7 +39,12 @@ export const BasisTheoryTokens = new CrudBuilder(
super(_options);
}

public retrieve(id: string, options: RequestOptions = {}): Promise<Token> {
public retrieve(
id: string,
options: RequestOptions = {}
// returned data type as any to avoid casting when trying to retrieve token.data.<prop>
lcschy marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<Token<any>> {
const url = `/${id}`;

return this.client
Expand Down
20 changes: 16 additions & 4 deletions src/types/elements/elements.ts
Expand Up @@ -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<UpdateOptions, ElementEvents> {
readonly mounted: boolean;
Expand All @@ -36,27 +39,36 @@ interface BaseElement<UpdateOptions, ElementEvents> {
): Subscription;
}

type CardElement = BaseElement<UpdateCardElementOptions, CardElementEvents>;
type CardElement = BaseElement<UpdateCardElementOptions, CardElementEvents> & {
setValue(value: CardElementValue<'reference'>): void;
};

type TextElement = BaseElement<UpdateTextElementOptions, TextElementEvents>;
type TextElement = BaseElement<UpdateTextElementOptions, TextElementEvents> & {
setValue(value: DataElementReference): void;
};

type CardNumberElement = BaseElement<
UpdateCardNumberElementOptions,
CardNumberElementEvents
>;
> & {
setValue(value: DataElementReference): void;
lcschy marked this conversation as resolved.
Show resolved Hide resolved
};

type CardExpirationDateElement = BaseElement<
UpdateCardExpirationDateElementOptions,
CardExpirationDateElementEvents
> & {
month(): ElementWrapper<CardExpirationDateElement>;
year(): ElementWrapper<CardExpirationDateElement>;
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<T extends BaseElement<any, any> = BaseElement<any, any>> = {
Expand Down
10 changes: 5 additions & 5 deletions src/types/elements/options.ts
Expand Up @@ -58,15 +58,15 @@ interface CardElementValue<T extends ElementValueType> {
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<T extends ElementValueType> {
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 & {
Expand Down Expand Up @@ -104,7 +104,7 @@ type CreateCardExpirationDateElementOptions = CustomizableElementOptions &
Pick<ElementOptions, 'placeholder'> &
Required<Pick<ElementOptions, 'targetId'>> & {
'aria-label'?: string;
value?: CardExpirationDateValue<'static'>;
value?: CardExpirationDateValue<'static'> | string;
};

type UpdateCardExpirationDateElementOptions = Omit<
Expand Down
4 changes: 3 additions & 1 deletion src/types/elements/services/tokens.ts
Expand Up @@ -10,7 +10,9 @@ type CreateToken = CreateTokenModel<ElementValue>;
type UpdateToken = UpdateTokenModel<ElementValue>;

type Tokens = Create<Token, CreateToken> &
Retrieve<Token> &
// returned data type as any to avoid casting when trying to retrieve token.data.<prop>
lcschy marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Retrieve<Token<any>> &
Update<Token, UpdateToken>;

export type { Tokens, CreateToken, UpdateToken };
4 changes: 3 additions & 1 deletion src/types/sdk/services/tokens.ts
Expand Up @@ -30,7 +30,9 @@ interface SearchTokensRequest {
interface Tokens
extends Create<Token, CreateToken>,
Update<Token, UpdateToken>,
Retrieve<Token>,
// returned data type as any to avoid casting when trying to retrieve token.data.<prop>
lcschy marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Retrieve<Token<any>>,
Delete,
List<Token, ListTokensQuery> {
createAssociation(
Expand Down