Skip to content

Commit 33c5f52

Browse files
Register package version with Stripe instance (stripe#35)
1 parent e11d171 commit 33c5f52

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
globals: {
1111
// Suppress noise about enabling `esModuleInterop`
1212
'ts-jest': {diagnostics: {ignoreCodes: [151001]}},
13+
_VERSION: true,
1314
},
1415
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"devDependencies": {
3636
"@babel/core": "^7.7.2",
3737
"@babel/preset-env": "^7.7.1",
38+
"@rollup/plugin-replace": "^2.3.1",
3839
"@types/jest": "^24.0.25",
3940
"@typescript-eslint/eslint-plugin": "^2.15.0",
4041
"@typescript-eslint/parser": "^2.15.0",

rollup.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import babel from 'rollup-plugin-babel';
22
import pkg from './package.json';
33
import ts from 'rollup-plugin-typescript2';
4+
import replace from '@rollup/plugin-replace';
5+
import {version} from './package.json';
46

57
export default [
68
{
@@ -16,6 +18,9 @@ export default [
1618
babel({
1719
extensions: ['.ts', '.js', '.tsx', '.jsx'],
1820
}),
21+
replace({
22+
_VERSION: JSON.stringify(version),
23+
}),
1924
],
2025
},
2126
];

src/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
///<reference path='../types/index.d.ts' />
33
import {Stripe as StripeInstance, StripeConstructor} from '@stripe/stripe-js';
44

5+
// `_VERSION` will be rewritten by `@rollup/plugin-replace` as a string literal
6+
// containing the package.json version
7+
declare const _VERSION: string;
8+
59
const V3_URL = 'https://js.stripe.com/v3';
610

711
const injectScript = (): HTMLScriptElement => {
@@ -21,6 +25,14 @@ const injectScript = (): HTMLScriptElement => {
2125
return script;
2226
};
2327

28+
const registerWrapper = (stripe: any): void => {
29+
if (!stripe || !stripe._registerWrapper) {
30+
return;
31+
}
32+
33+
stripe._registerWrapper({name: 'stripe-js', version: _VERSION});
34+
};
35+
2436
// Execute our own script injection after a tick to give users time to
2537
// do their own script injection.
2638
const stripePromise: Promise<StripeConstructor | null> = Promise.resolve().then(
@@ -67,7 +79,13 @@ export const loadStripe = (
6779
): Promise<StripeInstance | null> => {
6880
loadCalled = true;
6981

70-
return stripePromise.then((maybeStripe) =>
71-
maybeStripe ? maybeStripe(...args) : null
72-
);
82+
return stripePromise.then((maybeStripe) => {
83+
if (maybeStripe === null) {
84+
return null;
85+
}
86+
87+
const stripe = maybeStripe(...args);
88+
registerWrapper(stripe);
89+
return stripe;
90+
});
7391
};

yarn.lock

+32
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,21 @@
806806
"@types/istanbul-reports" "^1.1.1"
807807
"@types/yargs" "^13.0.0"
808808

809+
"@rollup/plugin-replace@^2.3.1":
810+
version "2.3.1"
811+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.1.tgz#16fb0563628f9e6c6ef9e05d48d3608916d466f5"
812+
integrity sha512-qDcXj2VOa5+j0iudjb+LiwZHvBRRgWbHPhRmo1qde2KItTjuxDVQO21rp9/jOlzKR5YO0EsgRQoyox7fnL7y/A==
813+
dependencies:
814+
"@rollup/pluginutils" "^3.0.4"
815+
magic-string "^0.25.5"
816+
817+
"@rollup/pluginutils@^3.0.4":
818+
version "3.0.8"
819+
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.8.tgz#4e94d128d94b90699e517ef045422960d18c8fde"
820+
integrity sha512-rYGeAc4sxcZ+kPG/Tw4/fwJODC3IXHYDH4qusdN/b6aLw5LPUbzpecYbEJh4sVQGPFJxd2dBU4kc1H3oy9/bnw==
821+
dependencies:
822+
estree-walker "^1.0.1"
823+
809824
"@types/babel__core@^7.1.0":
810825
version "7.1.3"
811826
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"
@@ -1869,6 +1884,11 @@ estree-walker@^0.6.1:
18691884
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
18701885
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
18711886

1887+
estree-walker@^1.0.1:
1888+
version "1.0.1"
1889+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
1890+
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==
1891+
18721892
esutils@^2.0.2:
18731893
version "2.0.3"
18741894
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
@@ -3196,6 +3216,13 @@ loose-envify@^1.0.0:
31963216
dependencies:
31973217
js-tokens "^3.0.0 || ^4.0.0"
31983218

3219+
magic-string@^0.25.5:
3220+
version "0.25.7"
3221+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
3222+
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
3223+
dependencies:
3224+
sourcemap-codec "^1.4.4"
3225+
31993226
make-dir@^2.1.0:
32003227
version "2.1.0"
32013228
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
@@ -4290,6 +4317,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
42904317
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
42914318
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
42924319

4320+
sourcemap-codec@^1.4.4:
4321+
version "1.4.8"
4322+
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
4323+
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
4324+
42934325
spdx-correct@^3.0.0:
42944326
version "3.1.0"
42954327
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4"

0 commit comments

Comments
 (0)