@stripe/stripe-js
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -0,1 +1,21 @@ | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
} | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
} | ||
var V3_URL = 'https://js.stripe.com/v3'; | ||
@@ -25,19 +45,25 @@ | ||
}); | ||
}; // Execute our own script injection after a tick to give users time to | ||
// do their own script injection. | ||
}; | ||
var stripePromise = Promise.resolve().then(function () { | ||
if (typeof window === 'undefined') { | ||
// Resolve to null when imported server side. This makes the module | ||
// safe to import in an isomorphic code base. | ||
return null; | ||
var stripePromise = null; | ||
var loadScript = function loadScript() { | ||
// Ensure that we only attempt to load Stripe.js at most once | ||
if (stripePromise !== null) { | ||
return stripePromise; | ||
} | ||
if (window.Stripe) { | ||
return window.Stripe; | ||
} | ||
stripePromise = new Promise(function (resolve, reject) { | ||
if (typeof window === 'undefined') { | ||
// Resolve to null when imported server side. This makes the module | ||
// safe to import in an isomorphic code base. | ||
resolve(null); | ||
return; | ||
} | ||
var script = document.querySelector("script[src=\"".concat(V3_URL, "\"], script[src=\"").concat(V3_URL, "/\"]")) || injectScript(); | ||
return new Promise(function (resolve, reject) { | ||
if (window.Stripe) { | ||
resolve(window.Stripe); | ||
return; | ||
} | ||
var script = document.querySelector("script[src=\"".concat(V3_URL, "\"], script[src=\"").concat(V3_URL, "/\"]")) || injectScript(); | ||
script.addEventListener('load', function () { | ||
@@ -54,6 +80,22 @@ if (window.Stripe) { | ||
}); | ||
}); | ||
return stripePromise; | ||
}; | ||
var initStripe = function initStripe(maybeStripe, args) { | ||
if (maybeStripe === null) { | ||
return null; | ||
} | ||
var stripe = maybeStripe.apply(void 0, _toConsumableArray(args)); | ||
registerWrapper(stripe); | ||
return stripe; | ||
}; | ||
// own script injection. | ||
var stripePromise$1 = Promise.resolve().then(loadScript); | ||
var loadCalled = false; | ||
stripePromise["catch"](function (err) { | ||
if (!loadCalled) console.warn(err); | ||
stripePromise$1["catch"](function (err) { | ||
if (!loadCalled) { | ||
console.warn(err); | ||
} | ||
}); | ||
@@ -66,10 +108,4 @@ var loadStripe = function loadStripe() { | ||
loadCalled = true; | ||
return stripePromise.then(function (maybeStripe) { | ||
if (maybeStripe === null) { | ||
return null; | ||
} | ||
var stripe = maybeStripe.apply(void 0, args); | ||
registerWrapper(stripe); | ||
return stripe; | ||
return stripePromise$1.then(function (maybeStripe) { | ||
return initStripe(maybeStripe, args); | ||
}); | ||
@@ -76,0 +112,0 @@ }; |
@@ -5,2 +5,22 @@ 'use strict'; | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
} | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
} | ||
var V3_URL = 'https://js.stripe.com/v3'; | ||
@@ -30,19 +50,25 @@ | ||
}); | ||
}; // Execute our own script injection after a tick to give users time to | ||
// do their own script injection. | ||
}; | ||
var stripePromise = Promise.resolve().then(function () { | ||
if (typeof window === 'undefined') { | ||
// Resolve to null when imported server side. This makes the module | ||
// safe to import in an isomorphic code base. | ||
return null; | ||
var stripePromise = null; | ||
var loadScript = function loadScript() { | ||
// Ensure that we only attempt to load Stripe.js at most once | ||
if (stripePromise !== null) { | ||
return stripePromise; | ||
} | ||
if (window.Stripe) { | ||
return window.Stripe; | ||
} | ||
stripePromise = new Promise(function (resolve, reject) { | ||
if (typeof window === 'undefined') { | ||
// Resolve to null when imported server side. This makes the module | ||
// safe to import in an isomorphic code base. | ||
resolve(null); | ||
return; | ||
} | ||
var script = document.querySelector("script[src=\"".concat(V3_URL, "\"], script[src=\"").concat(V3_URL, "/\"]")) || injectScript(); | ||
return new Promise(function (resolve, reject) { | ||
if (window.Stripe) { | ||
resolve(window.Stripe); | ||
return; | ||
} | ||
var script = document.querySelector("script[src=\"".concat(V3_URL, "\"], script[src=\"").concat(V3_URL, "/\"]")) || injectScript(); | ||
script.addEventListener('load', function () { | ||
@@ -59,6 +85,22 @@ if (window.Stripe) { | ||
}); | ||
}); | ||
return stripePromise; | ||
}; | ||
var initStripe = function initStripe(maybeStripe, args) { | ||
if (maybeStripe === null) { | ||
return null; | ||
} | ||
var stripe = maybeStripe.apply(void 0, _toConsumableArray(args)); | ||
registerWrapper(stripe); | ||
return stripe; | ||
}; | ||
// own script injection. | ||
var stripePromise$1 = Promise.resolve().then(loadScript); | ||
var loadCalled = false; | ||
stripePromise["catch"](function (err) { | ||
if (!loadCalled) console.warn(err); | ||
stripePromise$1["catch"](function (err) { | ||
if (!loadCalled) { | ||
console.warn(err); | ||
} | ||
}); | ||
@@ -71,10 +113,4 @@ var loadStripe = function loadStripe() { | ||
loadCalled = true; | ||
return stripePromise.then(function (maybeStripe) { | ||
if (maybeStripe === null) { | ||
return null; | ||
} | ||
var stripe = maybeStripe.apply(void 0, args); | ||
registerWrapper(stripe); | ||
return stripe; | ||
return stripePromise$1.then(function (maybeStripe) { | ||
return initStripe(maybeStripe, args); | ||
}); | ||
@@ -81,0 +117,0 @@ }; |
{ | ||
"name": "@stripe/stripe-js", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Stripe.js loading utility", | ||
@@ -18,3 +18,3 @@ "main": "dist/stripe.js", | ||
"ci": "yarn lint && yarn test && yarn test:versions && yarn test:types && yarn typecheck && yarn build", | ||
"prepublishOnly": "yarn ci", | ||
"prepublishOnly": "echo \"\nPlease use ./scripts/publish instead\n\" && exit 1", | ||
"prettier": "prettier './**/*.{js,ts,md,html,css}' --write", | ||
@@ -34,3 +34,5 @@ "prettier-list-different": "prettier './**/*.{js,ts,md,html,css}' --list-different" | ||
"src", | ||
"types" | ||
"types", | ||
"pure.js", | ||
"pure.d.ts" | ||
], | ||
@@ -37,0 +39,0 @@ "devDependencies": { |
@@ -98,2 +98,14 @@ # Stripe.js ES Module | ||
### Importing `loadStripe` without side effects | ||
If you would like to use `loadStripe` in your application, but defer loading the | ||
Stripe.js script until `loadStripe` is first called, use the alternative | ||
`@stripe/stripe-js/pure` import path: | ||
``` | ||
import {loadStripe} from '@stripe/stripe-js/pure'; | ||
// Stripe.js will not be loaded until `loadStripe` is called | ||
``` | ||
## Stripe.js Documentation | ||
@@ -100,0 +112,0 @@ |
@@ -87,4 +87,5 @@ /* eslint-disable @typescript-eslint/no-var-requires */ | ||
describe('loadStripe', () => { | ||
describe.each(['./index', './pure'])('loadStripe (%s.ts)', (requirePath) => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
jest.spyOn(console, 'warn').mockReturnValue(); | ||
@@ -94,3 +95,3 @@ }); | ||
it('resolves loadStripe with Stripe object', async () => { | ||
const {loadStripe} = require('./index'); | ||
const {loadStripe} = require(requirePath); | ||
const stripePromise = loadStripe('pk_test_foo'); | ||
@@ -106,3 +107,3 @@ | ||
it('rejects when the script fails', async () => { | ||
const {loadStripe} = require('./index'); | ||
const {loadStripe} = require(requirePath); | ||
const stripePromise = loadStripe('pk_test_foo'); | ||
@@ -120,2 +121,16 @@ | ||
it('rejects when Stripe is not added to the window for some reason', async () => { | ||
const {loadStripe} = require(requirePath); | ||
const stripePromise = loadStripe('pk_test_foo'); | ||
await Promise.resolve(); | ||
dispatchScriptEvent('load'); | ||
return expect(stripePromise).rejects.toEqual( | ||
new Error('Stripe.js not available') | ||
); | ||
}); | ||
}); | ||
describe('loadStripe (index.ts)', () => { | ||
it('does not cause unhandled rejects when the script fails', async () => { | ||
@@ -134,15 +149,3 @@ require('./index'); | ||
}); | ||
it('rejects when Stripe is not added to the window for some reason', async () => { | ||
const {loadStripe} = require('./index'); | ||
const stripePromise = loadStripe('pk_test_foo'); | ||
await Promise.resolve(); | ||
dispatchScriptEvent('load'); | ||
return expect(stripePromise).rejects.toEqual( | ||
new Error('Stripe.js not available') | ||
); | ||
}); | ||
}); | ||
}); |
@@ -1,75 +0,15 @@ | ||
// eslint-disable-next-line @typescript-eslint/triple-slash-reference | ||
///<reference path='../types/index.d.ts' /> | ||
import {Stripe as StripeInstance, StripeConstructor} from '@stripe/stripe-js'; | ||
import {loadScript, initStripe} from './shared'; | ||
// `_VERSION` will be rewritten by `@rollup/plugin-replace` as a string literal | ||
// containing the package.json version | ||
declare const _VERSION: string; | ||
// Execute our own script injection after a tick to give users time to do their | ||
// own script injection. | ||
const stripePromise = Promise.resolve().then(loadScript); | ||
const V3_URL = 'https://js.stripe.com/v3'; | ||
const injectScript = (): HTMLScriptElement => { | ||
const script = document.createElement('script'); | ||
script.src = V3_URL; | ||
const headOrBody = document.head || document.body; | ||
if (!headOrBody) { | ||
throw new Error( | ||
'Expected document.body not to be null. Stripe.js requires a <body> element.' | ||
); | ||
} | ||
headOrBody.appendChild(script); | ||
return script; | ||
}; | ||
const registerWrapper = (stripe: any): void => { | ||
if (!stripe || !stripe._registerWrapper) { | ||
return; | ||
} | ||
stripe._registerWrapper({name: 'stripe-js', version: _VERSION}); | ||
}; | ||
// Execute our own script injection after a tick to give users time to | ||
// do their own script injection. | ||
const stripePromise: Promise<StripeConstructor | null> = Promise.resolve().then( | ||
() => { | ||
if (typeof window === 'undefined') { | ||
// Resolve to null when imported server side. This makes the module | ||
// safe to import in an isomorphic code base. | ||
return null; | ||
} | ||
if (window.Stripe) { | ||
return window.Stripe; | ||
} | ||
const script: HTMLScriptElement = | ||
document.querySelector( | ||
`script[src="${V3_URL}"], script[src="${V3_URL}/"]` | ||
) || injectScript(); | ||
return new Promise((resolve, reject) => { | ||
script.addEventListener('load', () => { | ||
if (window.Stripe) { | ||
resolve(window.Stripe); | ||
} else { | ||
reject(new Error('Stripe.js not available')); | ||
} | ||
}); | ||
script.addEventListener('error', () => { | ||
reject(new Error('Failed to load Stripe.js')); | ||
}); | ||
}); | ||
} | ||
); | ||
let loadCalled = false; | ||
stripePromise.catch((err) => { | ||
if (!loadCalled) console.warn(err); | ||
if (!loadCalled) { | ||
console.warn(err); | ||
} | ||
}); | ||
@@ -82,11 +22,3 @@ | ||
return stripePromise.then((maybeStripe) => { | ||
if (maybeStripe === null) { | ||
return null; | ||
} | ||
const stripe = maybeStripe(...args); | ||
registerWrapper(stripe); | ||
return stripe; | ||
}); | ||
return stripePromise.then((maybeStripe) => initStripe(maybeStripe, args)); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
180409
41
4534
120