@stripe/connect-js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +81 -0
- package/dist/connect.esm.js +91 -0
- package/dist/connect.js +95 -0
- package/dist/index.d.ts +3 -0
- package/dist/pure.d.ts +2 -0
- package/dist/pure.esm.js +81 -0
- package/dist/pure.js +85 -0
- package/dist/shared.d.ts +5 -0
- package/dist/utils/jestHelpers.d.ts +2 -0
- package/dist/utils/jestSetup.d.ts +1 -0
- package/package.json +69 -0
- package/pure.js +1 -0
- package/src/index.test.ts +14 -0
- package/src/index.ts +21 -0
- package/src/pure.test.ts +18 -0
- package/src/pure.ts +5 -0
- package/src/shared.ts +104 -0
- package/src/utils/jestHelpers.ts +4 -0
- package/src/utils/jestSetup.ts +17 -0
- package/types/.eslintrc.yml +3 -0
- package/types/index.d.ts +4 -0
- package/types/shared.d.ts +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Stripe, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Connect.js ES Module
|
|
2
|
+
|
|
3
|
+
The [Connect.js library](https://stripe.com/docs/connect/get-started-connect-embedded-components) and its supporting API allows you to add connected account dashboard functionality to your website.
|
|
4
|
+
This NPM package contains initialization logic for Connect embedded components along with related types.
|
|
5
|
+
|
|
6
|
+
Note: Connect embedded components is currently still in beta. Please [contact us](https://stripe.com/docs/connect/get-started-connect-embedded-components#access) to request beta access.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Use `npm` to install the Connect.js module:
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @stripe/connect-js
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Documentation
|
|
17
|
+
|
|
18
|
+
- [Connect embedded components](https://stripe.com/docs/connect/get-started-connect-embedded-uis)
|
|
19
|
+
- [Quickstart guide](https://stripe.com/docs/connect/connect-embedded-uis/quickstart)
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### `loadConnect`
|
|
24
|
+
|
|
25
|
+
This function returns a `Promise` that resolves with a newly created `StripeConnect`
|
|
26
|
+
object once Connect.js has loaded. If necessary, it will load Connect.js for you by inserting the Connect.js script tag.
|
|
27
|
+
|
|
28
|
+
The `stripeConnect.initialize` function returns a `ConnectInstance` once you initialize it with a publishable key and a client secret returned from the [Account Session API](https://stripe.com/docs/api/account_sessions/create) call.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { loadConnect } from "@stripe/connect-js";
|
|
32
|
+
|
|
33
|
+
const stripeConnect = await loadConnect();
|
|
34
|
+
const connectInstance = stripeConnect.initialize({
|
|
35
|
+
publishableKey: "{{pk test123}}",
|
|
36
|
+
clientSecret: "{{client secret}}"
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
We’ve placed a random API key in this example. Replace it with your
|
|
41
|
+
[actual publishable API keys](https://dashboard.stripe.com/account/apikeys) to
|
|
42
|
+
test this code through your Connect account.
|
|
43
|
+
|
|
44
|
+
If you have deployed a
|
|
45
|
+
[Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/Security/CSP),
|
|
46
|
+
make sure to
|
|
47
|
+
[include Connect.js in your directives](https://stripe.com/docs/security/guide#content-security-policy).
|
|
48
|
+
|
|
49
|
+
### Import as a side effect
|
|
50
|
+
|
|
51
|
+
Import `@stripe/connect-js` as a side effect in code that will be included
|
|
52
|
+
throughout your site (e.g. your root module). This will make sure the Connect.js
|
|
53
|
+
script tag is inserted immediately upon page load.
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import "@stripe/connect-js";
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Manually include the script tag
|
|
60
|
+
|
|
61
|
+
Manually add the Connect.js script tag to the `<head>` of each page on your site.
|
|
62
|
+
If an existing script tag is already present, this module will not insert a new
|
|
63
|
+
one. When you call `loadConnect`, it will use the existing script tag.
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<!-- Somewhere in your site's <head> -->
|
|
67
|
+
<script src="https://connect-js.stripe.com/v0.1/connect.js" async></script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Importing `loadConnect` without side effects
|
|
71
|
+
|
|
72
|
+
If you would like to use `loadConnect` in your application, but defer loading the
|
|
73
|
+
Connect.js script until `loadConnect` is first called, use the alternative
|
|
74
|
+
`@stripe/connect-js/pure` import path:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { loadConnect } from "@stripe/connect-js/pure";
|
|
78
|
+
|
|
79
|
+
// Connect.js will not be loaded until `loadConnect` is called
|
|
80
|
+
const stripeConnect = await loadConnect();
|
|
81
|
+
```
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const EXISTING_SCRIPT_MESSAGE = "loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
2
|
+
const V0_URL = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
3
|
+
const findScript = () => {
|
|
4
|
+
return document.querySelectorAll(`script[src="${V0_URL}"]`)[0];
|
|
5
|
+
};
|
|
6
|
+
const injectScript = () => {
|
|
7
|
+
const script = document.createElement("script");
|
|
8
|
+
script.src = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
9
|
+
const head = document.head;
|
|
10
|
+
if (!head) {
|
|
11
|
+
throw new Error("Expected document.head not to be null. Connect.js requires a <head> element.");
|
|
12
|
+
}
|
|
13
|
+
document.head.appendChild(script);
|
|
14
|
+
return script;
|
|
15
|
+
};
|
|
16
|
+
let stripePromise = null;
|
|
17
|
+
const loadScript = () => {
|
|
18
|
+
// Ensure that we only attempt to load Connect.js at most once
|
|
19
|
+
if (stripePromise !== null) {
|
|
20
|
+
return stripePromise;
|
|
21
|
+
}
|
|
22
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
23
|
+
if (window.StripeConnect) {
|
|
24
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
25
|
+
}
|
|
26
|
+
if (window.StripeConnect) {
|
|
27
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
28
|
+
resolve(wrapper);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
let script = findScript();
|
|
33
|
+
if (script) {
|
|
34
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
35
|
+
} else if (!script) {
|
|
36
|
+
script = injectScript();
|
|
37
|
+
}
|
|
38
|
+
script.addEventListener("load", () => {
|
|
39
|
+
if (window.StripeConnect) {
|
|
40
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
41
|
+
resolve(wrapper);
|
|
42
|
+
} else {
|
|
43
|
+
reject(new Error("Connect.js did not load the necessary objects"));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
script.addEventListener("error", () => {
|
|
47
|
+
reject(new Error("Failed to load Connect.js"));
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
reject(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return stripePromise;
|
|
54
|
+
};
|
|
55
|
+
const initStripeConnect = stripeConnectPromise => {
|
|
56
|
+
if (stripeConnectPromise === null) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return stripeConnectPromise;
|
|
60
|
+
};
|
|
61
|
+
const createWrapper = stripeConnect => {
|
|
62
|
+
const wrapper = {
|
|
63
|
+
initialize: params => {
|
|
64
|
+
const metaOptions = params.metaOptions ?? {};
|
|
65
|
+
return stripeConnect.init({
|
|
66
|
+
...params,
|
|
67
|
+
metaOptions: {
|
|
68
|
+
...metaOptions,
|
|
69
|
+
sdk: true
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return wrapper;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Execute our own script injection after a tick to give users time to do their
|
|
78
|
+
// own script injection.
|
|
79
|
+
const stripePromise$1 = Promise.resolve().then(() => loadScript());
|
|
80
|
+
let loadCalled = false;
|
|
81
|
+
stripePromise$1.catch(err => {
|
|
82
|
+
if (!loadCalled) {
|
|
83
|
+
console.warn(err);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const loadConnect = () => {
|
|
87
|
+
loadCalled = true;
|
|
88
|
+
return stripePromise$1.then(maybeConnect => initStripeConnect(maybeConnect));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { loadConnect };
|
package/dist/connect.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const EXISTING_SCRIPT_MESSAGE = "loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
6
|
+
const V0_URL = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
7
|
+
const findScript = () => {
|
|
8
|
+
return document.querySelectorAll(`script[src="${V0_URL}"]`)[0];
|
|
9
|
+
};
|
|
10
|
+
const injectScript = () => {
|
|
11
|
+
const script = document.createElement("script");
|
|
12
|
+
script.src = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
13
|
+
const head = document.head;
|
|
14
|
+
if (!head) {
|
|
15
|
+
throw new Error("Expected document.head not to be null. Connect.js requires a <head> element.");
|
|
16
|
+
}
|
|
17
|
+
document.head.appendChild(script);
|
|
18
|
+
return script;
|
|
19
|
+
};
|
|
20
|
+
let stripePromise = null;
|
|
21
|
+
const loadScript = () => {
|
|
22
|
+
// Ensure that we only attempt to load Connect.js at most once
|
|
23
|
+
if (stripePromise !== null) {
|
|
24
|
+
return stripePromise;
|
|
25
|
+
}
|
|
26
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
27
|
+
if (window.StripeConnect) {
|
|
28
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
29
|
+
}
|
|
30
|
+
if (window.StripeConnect) {
|
|
31
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
32
|
+
resolve(wrapper);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
let script = findScript();
|
|
37
|
+
if (script) {
|
|
38
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
39
|
+
} else if (!script) {
|
|
40
|
+
script = injectScript();
|
|
41
|
+
}
|
|
42
|
+
script.addEventListener("load", () => {
|
|
43
|
+
if (window.StripeConnect) {
|
|
44
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
45
|
+
resolve(wrapper);
|
|
46
|
+
} else {
|
|
47
|
+
reject(new Error("Connect.js did not load the necessary objects"));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
script.addEventListener("error", () => {
|
|
51
|
+
reject(new Error("Failed to load Connect.js"));
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
reject(error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return stripePromise;
|
|
58
|
+
};
|
|
59
|
+
const initStripeConnect = stripeConnectPromise => {
|
|
60
|
+
if (stripeConnectPromise === null) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return stripeConnectPromise;
|
|
64
|
+
};
|
|
65
|
+
const createWrapper = stripeConnect => {
|
|
66
|
+
const wrapper = {
|
|
67
|
+
initialize: params => {
|
|
68
|
+
const metaOptions = params.metaOptions ?? {};
|
|
69
|
+
return stripeConnect.init({
|
|
70
|
+
...params,
|
|
71
|
+
metaOptions: {
|
|
72
|
+
...metaOptions,
|
|
73
|
+
sdk: true
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return wrapper;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Execute our own script injection after a tick to give users time to do their
|
|
82
|
+
// own script injection.
|
|
83
|
+
const stripePromise$1 = Promise.resolve().then(() => loadScript());
|
|
84
|
+
let loadCalled = false;
|
|
85
|
+
stripePromise$1.catch(err => {
|
|
86
|
+
if (!loadCalled) {
|
|
87
|
+
console.warn(err);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const loadConnect = () => {
|
|
91
|
+
loadCalled = true;
|
|
92
|
+
return stripePromise$1.then(maybeConnect => initStripeConnect(maybeConnect));
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
exports.loadConnect = loadConnect;
|
package/dist/index.d.ts
ADDED
package/dist/pure.d.ts
ADDED
package/dist/pure.esm.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const EXISTING_SCRIPT_MESSAGE = "loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
2
|
+
const V0_URL = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
3
|
+
const findScript = () => {
|
|
4
|
+
return document.querySelectorAll(`script[src="${V0_URL}"]`)[0];
|
|
5
|
+
};
|
|
6
|
+
const injectScript = () => {
|
|
7
|
+
const script = document.createElement("script");
|
|
8
|
+
script.src = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
9
|
+
const head = document.head;
|
|
10
|
+
if (!head) {
|
|
11
|
+
throw new Error("Expected document.head not to be null. Connect.js requires a <head> element.");
|
|
12
|
+
}
|
|
13
|
+
document.head.appendChild(script);
|
|
14
|
+
return script;
|
|
15
|
+
};
|
|
16
|
+
let stripePromise = null;
|
|
17
|
+
const loadScript = () => {
|
|
18
|
+
// Ensure that we only attempt to load Connect.js at most once
|
|
19
|
+
if (stripePromise !== null) {
|
|
20
|
+
return stripePromise;
|
|
21
|
+
}
|
|
22
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
23
|
+
if (window.StripeConnect) {
|
|
24
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
25
|
+
}
|
|
26
|
+
if (window.StripeConnect) {
|
|
27
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
28
|
+
resolve(wrapper);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
let script = findScript();
|
|
33
|
+
if (script) {
|
|
34
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
35
|
+
} else if (!script) {
|
|
36
|
+
script = injectScript();
|
|
37
|
+
}
|
|
38
|
+
script.addEventListener("load", () => {
|
|
39
|
+
if (window.StripeConnect) {
|
|
40
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
41
|
+
resolve(wrapper);
|
|
42
|
+
} else {
|
|
43
|
+
reject(new Error("Connect.js did not load the necessary objects"));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
script.addEventListener("error", () => {
|
|
47
|
+
reject(new Error("Failed to load Connect.js"));
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
reject(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return stripePromise;
|
|
54
|
+
};
|
|
55
|
+
const initStripeConnect = stripeConnectPromise => {
|
|
56
|
+
if (stripeConnectPromise === null) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return stripeConnectPromise;
|
|
60
|
+
};
|
|
61
|
+
const createWrapper = stripeConnect => {
|
|
62
|
+
const wrapper = {
|
|
63
|
+
initialize: params => {
|
|
64
|
+
const metaOptions = params.metaOptions ?? {};
|
|
65
|
+
return stripeConnect.init({
|
|
66
|
+
...params,
|
|
67
|
+
metaOptions: {
|
|
68
|
+
...metaOptions,
|
|
69
|
+
sdk: true
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return wrapper;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const loadConnect = () => {
|
|
78
|
+
return loadScript().then(maybeStripe => initStripeConnect(maybeStripe));
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export { loadConnect };
|
package/dist/pure.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const EXISTING_SCRIPT_MESSAGE = "loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
6
|
+
const V0_URL = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
7
|
+
const findScript = () => {
|
|
8
|
+
return document.querySelectorAll(`script[src="${V0_URL}"]`)[0];
|
|
9
|
+
};
|
|
10
|
+
const injectScript = () => {
|
|
11
|
+
const script = document.createElement("script");
|
|
12
|
+
script.src = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
13
|
+
const head = document.head;
|
|
14
|
+
if (!head) {
|
|
15
|
+
throw new Error("Expected document.head not to be null. Connect.js requires a <head> element.");
|
|
16
|
+
}
|
|
17
|
+
document.head.appendChild(script);
|
|
18
|
+
return script;
|
|
19
|
+
};
|
|
20
|
+
let stripePromise = null;
|
|
21
|
+
const loadScript = () => {
|
|
22
|
+
// Ensure that we only attempt to load Connect.js at most once
|
|
23
|
+
if (stripePromise !== null) {
|
|
24
|
+
return stripePromise;
|
|
25
|
+
}
|
|
26
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
27
|
+
if (window.StripeConnect) {
|
|
28
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
29
|
+
}
|
|
30
|
+
if (window.StripeConnect) {
|
|
31
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
32
|
+
resolve(wrapper);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
let script = findScript();
|
|
37
|
+
if (script) {
|
|
38
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
39
|
+
} else if (!script) {
|
|
40
|
+
script = injectScript();
|
|
41
|
+
}
|
|
42
|
+
script.addEventListener("load", () => {
|
|
43
|
+
if (window.StripeConnect) {
|
|
44
|
+
const wrapper = createWrapper(window.StripeConnect);
|
|
45
|
+
resolve(wrapper);
|
|
46
|
+
} else {
|
|
47
|
+
reject(new Error("Connect.js did not load the necessary objects"));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
script.addEventListener("error", () => {
|
|
51
|
+
reject(new Error("Failed to load Connect.js"));
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
reject(error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return stripePromise;
|
|
58
|
+
};
|
|
59
|
+
const initStripeConnect = stripeConnectPromise => {
|
|
60
|
+
if (stripeConnectPromise === null) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return stripeConnectPromise;
|
|
64
|
+
};
|
|
65
|
+
const createWrapper = stripeConnect => {
|
|
66
|
+
const wrapper = {
|
|
67
|
+
initialize: params => {
|
|
68
|
+
const metaOptions = params.metaOptions ?? {};
|
|
69
|
+
return stripeConnect.init({
|
|
70
|
+
...params,
|
|
71
|
+
metaOptions: {
|
|
72
|
+
...metaOptions,
|
|
73
|
+
sdk: true
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return wrapper;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const loadConnect = () => {
|
|
82
|
+
return loadScript().then(maybeStripe => initStripeConnect(maybeStripe));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
exports.loadConnect = loadConnect;
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StripeConnectWrapper } from "../types";
|
|
2
|
+
export type LoadConnect = () => Promise<StripeConnectWrapper>;
|
|
3
|
+
export declare const findScript: () => HTMLScriptElement | null;
|
|
4
|
+
export declare const loadScript: () => Promise<StripeConnectWrapper | null>;
|
|
5
|
+
export declare const initStripeConnect: (stripeConnectPromise: StripeConnectWrapper | null) => any | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stripe/connect-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Connect.js loading utility package",
|
|
5
|
+
"main": "dist/connect.js",
|
|
6
|
+
"module": "dist/connect.esm.js",
|
|
7
|
+
"types": "types/index.d.ts",
|
|
8
|
+
"typings": "types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "yarn lint && yarn test:unit --passWithNoTests && yarn typecheck",
|
|
11
|
+
"test:unit": "jest",
|
|
12
|
+
"test:types": "zx ./tests/types/scripts/test.mjs",
|
|
13
|
+
"lint": "eslint '{src,types}/**/*.{ts,js}' && yarn prettier-check",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"build": "yarn clean && rollup -c",
|
|
16
|
+
"validate-change": "yarn run typescript && yarn run lint && yarn run test",
|
|
17
|
+
"clean": "rimraf dist",
|
|
18
|
+
"prettier": "prettier './**/*.{js,ts,md,html,css}' --write",
|
|
19
|
+
"prettier-check": "prettier './**/*.{js,ts,md,html,css}' --check"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/stripe/connect-js.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"Stripe",
|
|
27
|
+
"connect.js"
|
|
28
|
+
],
|
|
29
|
+
"author": "Stripe (https://www.stripe.com)",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stripe/connect-js/issues"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src",
|
|
37
|
+
"types",
|
|
38
|
+
"pure.js",
|
|
39
|
+
"pure.d.ts"
|
|
40
|
+
],
|
|
41
|
+
"homepage": "https://github.com/stripe/connect-js#readme",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@babel/core": "^7.7.2",
|
|
44
|
+
"@babel/preset-env": "^7.7.1",
|
|
45
|
+
"@rollup/plugin-replace": "^2.3.1",
|
|
46
|
+
"@types/jest": "^24.0.25",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^4.15.2",
|
|
48
|
+
"@typescript-eslint/parser": "^4.15.2",
|
|
49
|
+
"babel-eslint": "^10.0.3",
|
|
50
|
+
"babel-jest": "^24.9.0",
|
|
51
|
+
"conditional-type-checks": "^1.0.5",
|
|
52
|
+
"eslint": "^6.8.0",
|
|
53
|
+
"eslint-config-prettier": "^6.8.0",
|
|
54
|
+
"eslint-plugin-import": "^2.18.2",
|
|
55
|
+
"eslint-plugin-jest": "^22.6.3",
|
|
56
|
+
"eslint-plugin-prettier": "^3.1.1",
|
|
57
|
+
"jest": "^29.5.0",
|
|
58
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
59
|
+
"prettier": "^1.19.1",
|
|
60
|
+
"rimraf": "^2.6.2",
|
|
61
|
+
"rollup": "^1.29.0",
|
|
62
|
+
"rollup-plugin-babel": "^4.4.0",
|
|
63
|
+
"rollup-plugin-typescript2": "^0.25.3",
|
|
64
|
+
"ts-jest": "^29.1.0",
|
|
65
|
+
"typescript": "^4.1.2",
|
|
66
|
+
"yalc": "^1.0.0-pre.53",
|
|
67
|
+
"zx": "^4.2.0"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/pure.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./dist/pure.js");
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
import { SCRIPT_SELECTOR } from "./utils/jestHelpers";
|
|
3
|
+
|
|
4
|
+
describe("Stripe module loader", () => {
|
|
5
|
+
jest.spyOn(global, "setTimeout");
|
|
6
|
+
it("injects the Connect.js script as a side effect after a tick", () => {
|
|
7
|
+
require("./index");
|
|
8
|
+
|
|
9
|
+
expect(document.querySelector(SCRIPT_SELECTOR)).toBe(null);
|
|
10
|
+
return Promise.resolve().then(() => {
|
|
11
|
+
expect(document.querySelector(SCRIPT_SELECTOR)).not.toBe(null);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { loadScript, initStripeConnect, LoadConnect } from "./shared";
|
|
2
|
+
|
|
3
|
+
// Execute our own script injection after a tick to give users time to do their
|
|
4
|
+
// own script injection.
|
|
5
|
+
const stripePromise = Promise.resolve().then(() => loadScript());
|
|
6
|
+
|
|
7
|
+
let loadCalled = false;
|
|
8
|
+
|
|
9
|
+
stripePromise.catch((err: Error) => {
|
|
10
|
+
if (!loadCalled) {
|
|
11
|
+
console.warn(err);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const loadConnect: LoadConnect = () => {
|
|
16
|
+
loadCalled = true;
|
|
17
|
+
|
|
18
|
+
return stripePromise.then(maybeConnect => initStripeConnect(maybeConnect));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export {};
|
package/src/pure.test.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
|
|
3
|
+
import { SCRIPT_SELECTOR } from "./utils/jestHelpers";
|
|
4
|
+
|
|
5
|
+
describe("pure module", () => {
|
|
6
|
+
test("does not inject the script if loadConnect is not called", async () => {
|
|
7
|
+
require("./pure");
|
|
8
|
+
|
|
9
|
+
expect(document.querySelector(SCRIPT_SELECTOR)).toBe(null);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("it injects the script if loadConnect is called", async () => {
|
|
13
|
+
const { loadConnect } = require("./pure");
|
|
14
|
+
loadConnect();
|
|
15
|
+
|
|
16
|
+
expect(document.querySelector(SCRIPT_SELECTOR)).not.toBe(null);
|
|
17
|
+
});
|
|
18
|
+
});
|
package/src/pure.ts
ADDED
package/src/shared.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { StripeConnectWrapper, IStripeConnectInitParams } from "../types";
|
|
2
|
+
|
|
3
|
+
export type LoadConnect = () => Promise<StripeConnectWrapper>;
|
|
4
|
+
|
|
5
|
+
const EXISTING_SCRIPT_MESSAGE =
|
|
6
|
+
"loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
7
|
+
const V0_URL = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
8
|
+
|
|
9
|
+
export const findScript = (): HTMLScriptElement | null => {
|
|
10
|
+
return document.querySelectorAll<HTMLScriptElement>(
|
|
11
|
+
`script[src="${V0_URL}"]`
|
|
12
|
+
)[0];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const injectScript = (): HTMLScriptElement => {
|
|
16
|
+
const script = document.createElement("script");
|
|
17
|
+
script.src = "https://connect-js.stripe.com/v0.1/connect.js";
|
|
18
|
+
|
|
19
|
+
const head = document.head;
|
|
20
|
+
|
|
21
|
+
if (!head) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"Expected document.head not to be null. Connect.js requires a <head> element."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
document.head.appendChild(script);
|
|
28
|
+
|
|
29
|
+
return script;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
let stripePromise: Promise<StripeConnectWrapper> | null = null;
|
|
33
|
+
|
|
34
|
+
export const loadScript = (): Promise<StripeConnectWrapper | null> => {
|
|
35
|
+
// Ensure that we only attempt to load Connect.js at most once
|
|
36
|
+
if (stripePromise !== null) {
|
|
37
|
+
return stripePromise;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
41
|
+
if ((window as any).StripeConnect) {
|
|
42
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if ((window as any).StripeConnect) {
|
|
46
|
+
const wrapper = createWrapper((window as any).StripeConnect);
|
|
47
|
+
resolve(wrapper);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
let script = findScript();
|
|
53
|
+
|
|
54
|
+
if (script) {
|
|
55
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
56
|
+
} else if (!script) {
|
|
57
|
+
script = injectScript();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
script.addEventListener("load", () => {
|
|
61
|
+
if ((window as any).StripeConnect) {
|
|
62
|
+
const wrapper = createWrapper((window as any).StripeConnect);
|
|
63
|
+
resolve(wrapper);
|
|
64
|
+
} else {
|
|
65
|
+
reject(new Error("Connect.js did not load the necessary objects"));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
script.addEventListener("error", () => {
|
|
70
|
+
reject(new Error("Failed to load Connect.js"));
|
|
71
|
+
});
|
|
72
|
+
} catch (error) {
|
|
73
|
+
reject(error);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return stripePromise;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const initStripeConnect = (
|
|
81
|
+
stripeConnectPromise: StripeConnectWrapper | null
|
|
82
|
+
): any | null => {
|
|
83
|
+
if (stripeConnectPromise === null) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return stripeConnectPromise;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const createWrapper = (stripeConnect: any) => {
|
|
91
|
+
const wrapper: StripeConnectWrapper = {
|
|
92
|
+
initialize: (params: IStripeConnectInitParams) => {
|
|
93
|
+
const metaOptions = (params as any).metaOptions ?? {};
|
|
94
|
+
return stripeConnect.init({
|
|
95
|
+
...params,
|
|
96
|
+
metaOptions: {
|
|
97
|
+
...metaOptions,
|
|
98
|
+
sdk: true
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return wrapper;
|
|
104
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SCRIPT_SELECTOR } from "./jestHelpers";
|
|
2
|
+
|
|
3
|
+
global.beforeEach(() => {
|
|
4
|
+
jest.spyOn(console, "warn").mockReturnValue();
|
|
5
|
+
jest.useFakeTimers();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
global.afterEach(() => {
|
|
9
|
+
const script = document.querySelector(SCRIPT_SELECTOR);
|
|
10
|
+
if (script && script.parentElement) {
|
|
11
|
+
script.parentElement.removeChild(script);
|
|
12
|
+
}
|
|
13
|
+
jest.resetModules();
|
|
14
|
+
jest.restoreAllMocks();
|
|
15
|
+
jest.runOnlyPendingTimers();
|
|
16
|
+
jest.useRealTimers();
|
|
17
|
+
});
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export declare type LoadConnect = () => Promise<StripeConnectWrapper>;
|
|
2
|
+
|
|
3
|
+
export declare type OverlayOption = "dialog" | "drawer";
|
|
4
|
+
|
|
5
|
+
export declare type UIConfigOptions = {
|
|
6
|
+
overlay?: OverlayOption;
|
|
7
|
+
overlayZIndex?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export declare type AppearanceOptions = {
|
|
11
|
+
colorPrimary?: string;
|
|
12
|
+
fontFamily?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type IStripeConnectUpdateParams = {
|
|
16
|
+
appearance?: AppearanceOptions;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface IStripeConnectInitParams {
|
|
20
|
+
publishableKey: string;
|
|
21
|
+
clientSecret: string;
|
|
22
|
+
appearance?: AppearanceOptions;
|
|
23
|
+
uiConfig?: UIConfigOptions;
|
|
24
|
+
refreshClientSecret?: () => Promise<string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface StripeConnectWrapper {
|
|
28
|
+
initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface StripeConnectInstance {
|
|
32
|
+
create: (tagName: ConnectElementTagName) => HTMLElement | null;
|
|
33
|
+
update: (options: IStripeConnectUpdateParams) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ConnectElementTagName =
|
|
37
|
+
| "stripe-connect-payments"
|
|
38
|
+
| "stripe-connect-payouts"
|
|
39
|
+
| "stripe-connect-account-onboarding"
|
|
40
|
+
| "stripe-connect-account-management"
|
|
41
|
+
| "stripe-connect-instant-payouts"
|
|
42
|
+
| "stripe-connect-payment-details"
|
|
43
|
+
| "stripe-connect-notification-banner";
|
|
44
|
+
|
|
45
|
+
export declare const findScript: () => HTMLScriptElement | null;
|
|
46
|
+
|
|
47
|
+
export declare const loadScript: () => Promise<any | null>;
|
|
48
|
+
|
|
49
|
+
export declare const initStripeConnect: (
|
|
50
|
+
stripeConnectPromise: StripeConnectWrapper | null
|
|
51
|
+
) => any | null;
|