Skip to content

Commit

Permalink
extract some utils (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
cweiss-stripe authored Sep 1, 2023
1 parent cc0640c commit aba8948
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
57 changes: 3 additions & 54 deletions src/components/Elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,8 @@ import {
extractAllowedOptionsUpdates,
UnknownOptions,
} from '../utils/extractAllowedOptionsUpdates';
import {isStripe, isPromise} from '../utils/guards';

const INVALID_STRIPE_ERROR =
'Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js`. See https://stripe.com/docs/stripe-js/react#elements-props-stripe for details.';

// We are using types to enforce the `stripe` prop in this lib, but in a real
// integration `stripe` could be anything, so we need to do some sanity
// validation to prevent type errors.
const validateStripe = (maybeStripe: unknown): null | stripeJs.Stripe => {
if (maybeStripe === null || isStripe(maybeStripe)) {
return maybeStripe;
}

throw new Error(INVALID_STRIPE_ERROR);
};

type ParsedStripeProp =
| {tag: 'empty'}
| {tag: 'sync'; stripe: stripeJs.Stripe}
| {tag: 'async'; stripePromise: Promise<stripeJs.Stripe | null>};

const parseStripeProp = (raw: unknown): ParsedStripeProp => {
if (isPromise(raw)) {
return {
tag: 'async',
stripePromise: Promise.resolve(raw).then(validateStripe),
};
}

const stripe = validateStripe(raw);

if (stripe === null) {
return {tag: 'empty'};
}

return {tag: 'sync', stripe};
};
import {parseStripeProp} from '../utils/parseStripeProp';
import {registerWithStripeJs} from '../utils/registerWithStripeJs';

interface ElementsContextValue {
elements: stripeJs.StripeElements | null;
Expand Down Expand Up @@ -220,23 +185,7 @@ export const Elements: FunctionComponent<PropsWithChildren<ElementsProps>> = (({

// Attach react-stripe-js version to stripe.js instance
React.useEffect(() => {
const anyStripe: any = ctx.stripe;

if (
!anyStripe ||
!anyStripe._registerWrapper ||
!anyStripe.registerAppInfo
) {
return;
}

anyStripe._registerWrapper({name: 'react-stripe-js', version: _VERSION});

anyStripe.registerAppInfo({
name: 'react-stripe-js',
version: _VERSION,
url: 'https://stripe.com/docs/stripe-js/react',
});
registerWithStripeJs(ctx.stripe);
}, [ctx.stripe]);

return (
Expand Down
38 changes: 38 additions & 0 deletions src/utils/parseStripeProp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as stripeJs from '@stripe/stripe-js';
import {isStripe, isPromise} from '../utils/guards';

const INVALID_STRIPE_ERROR =
'Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js`. See https://stripe.com/docs/stripe-js/react#elements-props-stripe for details.';

// We are using types to enforce the `stripe` prop in this lib, but in a real
// integration `stripe` could be anything, so we need to do some sanity
// validation to prevent type errors.
const validateStripe = (maybeStripe: unknown): null | stripeJs.Stripe => {
if (maybeStripe === null || isStripe(maybeStripe)) {
return maybeStripe;
}

throw new Error(INVALID_STRIPE_ERROR);
};

type ParsedStripeProp =
| {tag: 'empty'}
| {tag: 'sync'; stripe: stripeJs.Stripe}
| {tag: 'async'; stripePromise: Promise<stripeJs.Stripe | null>};

export const parseStripeProp = (raw: unknown): ParsedStripeProp => {
if (isPromise(raw)) {
return {
tag: 'async',
stripePromise: Promise.resolve(raw).then(validateStripe),
};
}

const stripe = validateStripe(raw);

if (stripe === null) {
return {tag: 'empty'};
}

return {tag: 'sync', stripe};
};
13 changes: 13 additions & 0 deletions src/utils/registerWithStripeJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const registerWithStripeJs = (stripe: any) => {
if (!stripe || !stripe._registerWrapper || !stripe.registerAppInfo) {
return;
}

stripe._registerWrapper({name: 'react-stripe-js', version: _VERSION});

stripe.registerAppInfo({
name: 'react-stripe-js',
version: _VERSION,
url: 'https://stripe.com/docs/stripe-js/react',
});
};

0 comments on commit aba8948

Please sign in to comment.