Skip to content

Commit

Permalink
fix custom method unavailability issue during connect instance loadin…
Browse files Browse the repository at this point in the history
…g (cherry-pick beta-> master) (#71)

* fix custom method unavailability issue during connect instance loading (cherry-pick)

* run prettier
  • Loading branch information
kaiying-stripe authored Nov 16, 2023
1 parent c0c3356 commit 880767c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
19 changes: 19 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ConnectElementTagName,
ConnectHTMLElementRecord
} from "../types";
import { ConnectElementCustomMethodConfig } from "../types/config";

export type LoadConnectAndInitialize = (
initParams: IStripeConnectInitParams
Expand Down Expand Up @@ -108,6 +109,12 @@ export const loadScript = (): Promise<StripeConnectWrapper> => {
return stripePromise;
};

const hasCustomMethod = (
tagName: ConnectElementTagName
): tagName is keyof typeof ConnectElementCustomMethodConfig => {
return tagName in ConnectElementCustomMethodConfig;
};

export const initStripeConnect = (
stripePromise: Promise<StripeConnectWrapper>,
initParams: IStripeConnectInitParams
Expand All @@ -123,6 +130,18 @@ export const initStripeConnect = (
htmlName = (tagName as unknown) as ConnectElementHTMLName;
}
const element = document.createElement(htmlName);

if (hasCustomMethod(tagName)) {
const methods = ConnectElementCustomMethodConfig[tagName];
for (const method in methods) {
(element as any)[method] = function(value: any) {
stripeConnectInstance.then(() => {
this[`${method}InternalOnly`](value);
});
};
}
}

stripeConnectInstance.then(instance => {
(element as any).setConnector((instance as any).connect);
});
Expand Down
27 changes: 27 additions & 0 deletions types/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ConnectElementTagName } from "./shared.d";

/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */

export const ConnectElementCustomMethodConfig = {
"account-onboarding": {
setFullTermsOfServiceUrl: (
termOfServiceUrl: string | undefined
): void => {},
setRecipientTermsOfServiceUrl: (
recipientTermsOfServiceUrl: string | undefined
): void => {},
setPrivacyPolicyUrl: (privacyPolicyUrl: string | undefined): void => {},
setSkipTermsOfServiceCollection: (
skipTermsOfServiceCollection: boolean | undefined
): void => {},
setOnExit: (listener: (() => void) | undefined): void => {}
}
};

// ensure that keys of ConnectElementCustomMethodConfig are from ConnectElementTagName
type HasType<T, Q extends T> = Q;
type CustomMethodConfigValidation = HasType<
ConnectElementTagName,
keyof typeof ConnectElementCustomMethodConfig
>;
30 changes: 9 additions & 21 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ConnectElementCustomMethodConfig } from "./config";

export declare type LoadConnectAndInitialize = (
initParams: IStripeConnectInitParams
) => StripeConnectInstance;
Expand Down Expand Up @@ -354,28 +356,14 @@ export interface IStripeConnectInitParams {
locale?: string;
}

type ConnectHTMLElementRecordFallback = {
[key in string]: HTMLElement | null;
};
type ConnectHTMLElementRecordBase = {
[tagName in ConnectElementTagName]: HTMLElement;
};
type ConnectElementCustomMethods = typeof ConnectElementCustomMethodConfig;

interface ConnectHTMLElementRecord
extends ConnectHTMLElementRecordBase,
ConnectHTMLElementRecordFallback {
"account-onboarding": HTMLElement & {
setFullTermsOfServiceUrl: (termOfServiceUrl: string | undefined) => void;
setRecipientTermsOfServiceUrl: (
recipientTermsOfServiceUrl: string | undefined
) => void;
setPrivacyPolicyUrl: (privacyPolicyUrl: string | undefined) => void;
setSkipTermsOfServiceCollection: (
skipTermsOfServiceCollection: boolean | undefined
) => void;
setOnExit: (listener: () => void) => void;
};
}
type ConnectHTMLElementRecord = {
[K in keyof ConnectElementCustomMethods]: HTMLElement &
ConnectElementCustomMethods[K];
} & {
[key: string]: HTMLElement;
};

export interface StripeConnectInstance {
/**
Expand Down

0 comments on commit 880767c

Please sign in to comment.