@stripe/connect-js 2.0.5 → 3.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/README.md CHANGED
@@ -3,9 +3,11 @@
3
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
4
  This NPM package contains initialization logic for Connect embedded components along with related types.
5
5
 
6
- Calling `loadConnect` always loads the latest version of Connect.js, regardless of which version of `@stripe/connect-js` you use. Updates for this package only impact tooling around the `loadConnect` helper itself and the TypeScript type definitions provided for Connect.js. Updates do not affect runtime availability of features of Connect.js.
6
+ Calling `loadConnectAndInitialize` always loads the latest version of Connect.js, regardless of which version of `@stripe/connect-js` you use. Updates for this package only impact tooling around the `loadConnectAndInitialize` helper itself and the TypeScript type definitions provided for Connect.js. Updates do not affect runtime availability of features of Connect.js.
7
7
 
8
- 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.
8
+ The embedded onboarding component is generally available now. Please refer to our [documentation](https://stripe.com/docs/connect/get-started-connect-embedded-components#account-onboarding) for more information.
9
+
10
+ Note: Majority of Connect embedded components are currently still in beta. Please [contact us](https://stripe.com/docs/connect/get-started-connect-embedded-components#access) to request beta access.
9
11
 
10
12
  ## Installation
11
13
 
@@ -17,25 +19,25 @@ npm install @stripe/connect-js
17
19
 
18
20
  ## Documentation
19
21
 
20
- - [Connect embedded components](https://stripe.com/docs/connect/get-started-connect-embedded-uis)
21
- - [Quickstart guide](https://stripe.com/docs/connect/connect-embedded-uis/quickstart)
22
+ - [Connect embedded components](https://stripe.com/docs/connect/get-started-connect-embedded-components)
23
+ - [Quickstart guide for GA](https://stripe.com/docs/connect/connect-embedded-components/quickstart)
24
+ - [Quickstart guide for beta](https://stripe.com/docs/connect/connect-embedded-components/beta-quickstart)
22
25
 
23
26
  ## Usage
24
27
 
25
- ### `loadConnect`
26
-
27
- This function returns a `Promise` that resolves with a newly created `StripeConnect`
28
- object once Connect.js has loaded. If necessary, it will load Connect.js for you by inserting the Connect.js script tag.
28
+ ### `loadConnectAndInitialize`
29
29
 
30
- 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.
30
+ This synchronous function takes in a publishable key, a function to retrieve the client secret returned from the [Account Session API](https://stripe.com/docs/api/account_sessions/create), and other [initialization parameters](https://stripe.com/docs/connect/get-started-connect-embedded-components#configuring-connect-js). It returns a `StripeConnectInstance`. If necessary, it will load Connect.js for you by inserting the Connect.js script tag.
31
31
 
32
32
  ```js
33
- import { loadConnect } from "@stripe/connect-js";
33
+ import { loadConnectAndInitialize } from "@stripe/connect-js";
34
+ const fetchClientSecret = async () => {
35
+ // Fetch the AccountSession client secret by making an API call to your service
36
+ };
34
37
 
35
- const stripeConnect = await loadConnect();
36
- const connectInstance = stripeConnect.initialize({
38
+ const instance = loadConnectAndInitialize({
37
39
  publishableKey: "{{pk test123}}",
38
- clientSecret: "{{client secret}}"
40
+ fetchClientSecret: fetchClientSecret
39
41
  });
40
42
  ```
41
43
 
@@ -62,22 +64,25 @@ import "@stripe/connect-js";
62
64
 
63
65
  Manually add the Connect.js script tag to the `<head>` of each page on your site.
64
66
  If an existing script tag is already present, this module will not insert a new
65
- one. When you call `loadConnect`, it will use the existing script tag.
67
+ one. When you call `loadConnectAndInitialize`, it will use the existing script tag.
66
68
 
67
69
  ```html
68
70
  <!-- Somewhere in your site's <head> -->
69
71
  <script src="https://connect-js.stripe.com/v0.1/connect.js" async></script>
70
72
  ```
71
73
 
72
- ### Importing `loadConnect` without side effects
74
+ ### Importing `loadConnectAndInitialize` without side effects
73
75
 
74
- If you would like to use `loadConnect` in your application, but defer loading the
75
- Connect.js script until `loadConnect` is first called, use the alternative
76
+ If you would like to use `loadConnectAndInitialize` in your application, but defer loading the
77
+ Connect.js script until `loadConnectAndInitialize` is first called, use the alternative
76
78
  `@stripe/connect-js/pure` import path:
77
79
 
78
80
  ```js
79
- import { loadConnect } from "@stripe/connect-js/pure";
81
+ import { loadConnectAndInitialize } from "@stripe/connect-js/pure";
80
82
 
81
83
  // Connect.js will not be loaded until `loadConnect` is called
82
- const stripeConnect = await loadConnect();
84
+ const instance = loadConnectAndInitialize({
85
+ publishableKey: "{{pk test123}}",
86
+ fetchClientSecret: fetchClientSecret
87
+ });
83
88
  ```
@@ -59,13 +59,38 @@ const loadScript = () => {
59
59
  });
60
60
  return stripePromise;
61
61
  };
62
- const initStripeConnect = stripeConnectPromise => {
63
- if (stripeConnectPromise === null) {
64
- return null;
65
- }
66
- return stripeConnectPromise;
62
+ const initStripeConnect = (stripePromise, initParams) => {
63
+ const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
64
+ return {
65
+ create: tagName => {
66
+ let htmlName = componentNameMapping[tagName];
67
+ if (!htmlName) {
68
+ htmlName = tagName;
69
+ }
70
+ const element = document.createElement(htmlName);
71
+ stripeConnectInstance.then(instance => {
72
+ element.setConnector(instance.connect);
73
+ });
74
+ return element;
75
+ },
76
+ update: updateOptions => {
77
+ stripeConnectInstance.then(instance => {
78
+ instance.update(updateOptions);
79
+ });
80
+ },
81
+ debugInstance: () => {
82
+ return stripeConnectInstance;
83
+ },
84
+ logout: () => {
85
+ return stripeConnectInstance.then(instance => {
86
+ instance.logout();
87
+ });
88
+ }
89
+ };
67
90
  };
68
91
  const createWrapper = stripeConnect => {
92
+ window.StripeConnect = window.StripeConnect || {};
93
+ window.StripeConnect.optimizedLoading = true;
69
94
  const wrapper = {
70
95
  initialize: params => {
71
96
  var _a;
@@ -75,19 +100,10 @@ const createWrapper = stripeConnect => {
75
100
  sdk: true,
76
101
  sdkOptions: {
77
102
  // This will be replaced by the npm package version when bundling
78
- sdkVersion: "2.0.5"
103
+ sdkVersion: "3.0.0"
79
104
  }
80
105
  })
81
106
  }));
82
- // We wrap create so we can map its different strings to supported components
83
- const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
84
- stripeConnectInstance.create = tagName => {
85
- let htmlName = componentNameMapping[tagName];
86
- if (!htmlName) {
87
- htmlName = tagName;
88
- }
89
- return oldCreate(htmlName);
90
- };
91
107
  return stripeConnectInstance;
92
108
  }
93
109
  };
@@ -103,9 +119,9 @@ stripePromise$1.catch(err => {
103
119
  console.warn(err);
104
120
  }
105
121
  });
106
- const loadConnect = () => {
122
+ const loadConnectAndInitialize = initParams => {
107
123
  loadCalled = true;
108
- return stripePromise$1.then(maybeConnect => initStripeConnect(maybeConnect));
124
+ return initStripeConnect(stripePromise$1, initParams);
109
125
  };
110
126
 
111
- export { loadConnect };
127
+ export { loadConnectAndInitialize };
package/dist/connect.js CHANGED
@@ -63,13 +63,38 @@ const loadScript = () => {
63
63
  });
64
64
  return stripePromise;
65
65
  };
66
- const initStripeConnect = stripeConnectPromise => {
67
- if (stripeConnectPromise === null) {
68
- return null;
69
- }
70
- return stripeConnectPromise;
66
+ const initStripeConnect = (stripePromise, initParams) => {
67
+ const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
68
+ return {
69
+ create: tagName => {
70
+ let htmlName = componentNameMapping[tagName];
71
+ if (!htmlName) {
72
+ htmlName = tagName;
73
+ }
74
+ const element = document.createElement(htmlName);
75
+ stripeConnectInstance.then(instance => {
76
+ element.setConnector(instance.connect);
77
+ });
78
+ return element;
79
+ },
80
+ update: updateOptions => {
81
+ stripeConnectInstance.then(instance => {
82
+ instance.update(updateOptions);
83
+ });
84
+ },
85
+ debugInstance: () => {
86
+ return stripeConnectInstance;
87
+ },
88
+ logout: () => {
89
+ return stripeConnectInstance.then(instance => {
90
+ instance.logout();
91
+ });
92
+ }
93
+ };
71
94
  };
72
95
  const createWrapper = stripeConnect => {
96
+ window.StripeConnect = window.StripeConnect || {};
97
+ window.StripeConnect.optimizedLoading = true;
73
98
  const wrapper = {
74
99
  initialize: params => {
75
100
  var _a;
@@ -79,19 +104,10 @@ const createWrapper = stripeConnect => {
79
104
  sdk: true,
80
105
  sdkOptions: {
81
106
  // This will be replaced by the npm package version when bundling
82
- sdkVersion: "2.0.5"
107
+ sdkVersion: "3.0.0"
83
108
  }
84
109
  })
85
110
  }));
86
- // We wrap create so we can map its different strings to supported components
87
- const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
88
- stripeConnectInstance.create = tagName => {
89
- let htmlName = componentNameMapping[tagName];
90
- if (!htmlName) {
91
- htmlName = tagName;
92
- }
93
- return oldCreate(htmlName);
94
- };
95
111
  return stripeConnectInstance;
96
112
  }
97
113
  };
@@ -107,9 +123,9 @@ stripePromise$1.catch(err => {
107
123
  console.warn(err);
108
124
  }
109
125
  });
110
- const loadConnect = () => {
126
+ const loadConnectAndInitialize = initParams => {
111
127
  loadCalled = true;
112
- return stripePromise$1.then(maybeConnect => initStripeConnect(maybeConnect));
128
+ return initStripeConnect(stripePromise$1, initParams);
113
129
  };
114
130
 
115
- exports.loadConnect = loadConnect;
131
+ exports.loadConnectAndInitialize = loadConnectAndInitialize;
@@ -0,0 +1,2 @@
1
+ import { LoadConnectAndInitialize } from "./shared";
2
+ export declare const loadConnectAndInitialize: LoadConnectAndInitialize;
package/dist/pure.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { LoadConnect } from "./shared";
2
- export declare const loadConnect: LoadConnect;
1
+ import { LoadConnectAndInitialize } from "./shared";
2
+ export declare const loadConnectAndInitialize: LoadConnectAndInitialize;
package/dist/pure.esm.js CHANGED
@@ -59,13 +59,38 @@ const loadScript = () => {
59
59
  });
60
60
  return stripePromise;
61
61
  };
62
- const initStripeConnect = stripeConnectPromise => {
63
- if (stripeConnectPromise === null) {
64
- return null;
65
- }
66
- return stripeConnectPromise;
62
+ const initStripeConnect = (stripePromise, initParams) => {
63
+ const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
64
+ return {
65
+ create: tagName => {
66
+ let htmlName = componentNameMapping[tagName];
67
+ if (!htmlName) {
68
+ htmlName = tagName;
69
+ }
70
+ const element = document.createElement(htmlName);
71
+ stripeConnectInstance.then(instance => {
72
+ element.setConnector(instance.connect);
73
+ });
74
+ return element;
75
+ },
76
+ update: updateOptions => {
77
+ stripeConnectInstance.then(instance => {
78
+ instance.update(updateOptions);
79
+ });
80
+ },
81
+ debugInstance: () => {
82
+ return stripeConnectInstance;
83
+ },
84
+ logout: () => {
85
+ return stripeConnectInstance.then(instance => {
86
+ instance.logout();
87
+ });
88
+ }
89
+ };
67
90
  };
68
91
  const createWrapper = stripeConnect => {
92
+ window.StripeConnect = window.StripeConnect || {};
93
+ window.StripeConnect.optimizedLoading = true;
69
94
  const wrapper = {
70
95
  initialize: params => {
71
96
  var _a;
@@ -75,27 +100,19 @@ const createWrapper = stripeConnect => {
75
100
  sdk: true,
76
101
  sdkOptions: {
77
102
  // This will be replaced by the npm package version when bundling
78
- sdkVersion: "2.0.5"
103
+ sdkVersion: "3.0.0"
79
104
  }
80
105
  })
81
106
  }));
82
- // We wrap create so we can map its different strings to supported components
83
- const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
84
- stripeConnectInstance.create = tagName => {
85
- let htmlName = componentNameMapping[tagName];
86
- if (!htmlName) {
87
- htmlName = tagName;
88
- }
89
- return oldCreate(htmlName);
90
- };
91
107
  return stripeConnectInstance;
92
108
  }
93
109
  };
94
110
  return wrapper;
95
111
  };
96
112
 
97
- const loadConnect = () => {
98
- return loadScript().then(maybeStripe => initStripeConnect(maybeStripe));
113
+ const loadConnectAndInitialize = initParams => {
114
+ const maybeConnect = loadScript();
115
+ return initStripeConnect(maybeConnect, initParams);
99
116
  };
100
117
 
101
- export { loadConnect };
118
+ export { loadConnectAndInitialize };
package/dist/pure.js CHANGED
@@ -63,13 +63,38 @@ const loadScript = () => {
63
63
  });
64
64
  return stripePromise;
65
65
  };
66
- const initStripeConnect = stripeConnectPromise => {
67
- if (stripeConnectPromise === null) {
68
- return null;
69
- }
70
- return stripeConnectPromise;
66
+ const initStripeConnect = (stripePromise, initParams) => {
67
+ const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
68
+ return {
69
+ create: tagName => {
70
+ let htmlName = componentNameMapping[tagName];
71
+ if (!htmlName) {
72
+ htmlName = tagName;
73
+ }
74
+ const element = document.createElement(htmlName);
75
+ stripeConnectInstance.then(instance => {
76
+ element.setConnector(instance.connect);
77
+ });
78
+ return element;
79
+ },
80
+ update: updateOptions => {
81
+ stripeConnectInstance.then(instance => {
82
+ instance.update(updateOptions);
83
+ });
84
+ },
85
+ debugInstance: () => {
86
+ return stripeConnectInstance;
87
+ },
88
+ logout: () => {
89
+ return stripeConnectInstance.then(instance => {
90
+ instance.logout();
91
+ });
92
+ }
93
+ };
71
94
  };
72
95
  const createWrapper = stripeConnect => {
96
+ window.StripeConnect = window.StripeConnect || {};
97
+ window.StripeConnect.optimizedLoading = true;
73
98
  const wrapper = {
74
99
  initialize: params => {
75
100
  var _a;
@@ -79,27 +104,19 @@ const createWrapper = stripeConnect => {
79
104
  sdk: true,
80
105
  sdkOptions: {
81
106
  // This will be replaced by the npm package version when bundling
82
- sdkVersion: "2.0.5"
107
+ sdkVersion: "3.0.0"
83
108
  }
84
109
  })
85
110
  }));
86
- // We wrap create so we can map its different strings to supported components
87
- const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
88
- stripeConnectInstance.create = tagName => {
89
- let htmlName = componentNameMapping[tagName];
90
- if (!htmlName) {
91
- htmlName = tagName;
92
- }
93
- return oldCreate(htmlName);
94
- };
95
111
  return stripeConnectInstance;
96
112
  }
97
113
  };
98
114
  return wrapper;
99
115
  };
100
116
 
101
- const loadConnect = () => {
102
- return loadScript().then(maybeStripe => initStripeConnect(maybeStripe));
117
+ const loadConnectAndInitialize = initParams => {
118
+ const maybeConnect = loadScript();
119
+ return initStripeConnect(maybeConnect, initParams);
103
120
  };
104
121
 
105
- exports.loadConnect = loadConnect;
122
+ exports.loadConnectAndInitialize = loadConnectAndInitialize;
package/dist/shared.d.ts CHANGED
@@ -1,5 +1,14 @@
1
- import { StripeConnectWrapper } from "../types";
2
- export type LoadConnect = () => Promise<StripeConnectWrapper>;
1
+ import { IStripeConnectInitParams, StripeConnectInstance, ConnectElementTagName } from "../types";
2
+ export type LoadConnectAndInitialize = (initParams: IStripeConnectInitParams) => StripeConnectInstance;
3
+ type ConnectElementHTMLName = "stripe-connect-account-onboarding";
4
+ export declare const componentNameMapping: Record<ConnectElementTagName, ConnectElementHTMLName>;
5
+ type StripeConnectInstanceExtended = StripeConnectInstance & {
6
+ debugInstance: () => Promise<StripeConnectInstance>;
7
+ };
8
+ interface StripeConnectWrapper {
9
+ initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
10
+ }
3
11
  export declare const findScript: () => HTMLScriptElement | null;
4
- export declare const loadScript: () => Promise<StripeConnectWrapper | null>;
5
- export declare const initStripeConnect: (stripeConnectPromise: StripeConnectWrapper | null) => any | null;
12
+ export declare const loadScript: () => Promise<StripeConnectWrapper>;
13
+ export declare const initStripeConnect: (stripePromise: Promise<StripeConnectWrapper>, initParams: IStripeConnectInitParams) => StripeConnectInstanceExtended;
14
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stripe/connect-js",
3
- "version": "2.0.5",
3
+ "version": "3.0.0",
4
4
  "description": "Connect.js loading utility package",
5
5
  "main": "dist/connect.js",
6
6
  "module": "dist/connect.esm.js",
package/pure.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LoadConnect } from "./src/shared";
1
+ import { LoadConnectAndInitialize } from "./src/shared";
2
2
 
3
- export declare const loadConnect: LoadConnect;
3
+ export declare const loadConnectAndInitialize: LoadConnectAndInitialize;
4
4
  export * from "./types/shared";
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
- import { loadScript, initStripeConnect, LoadConnect } from "./shared";
1
+ import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
2
+ import {
3
+ loadScript,
4
+ initStripeConnect,
5
+ LoadConnectAndInitialize
6
+ } from "./shared";
2
7
 
3
8
  // Execute our own script injection after a tick to give users time to do their
4
9
  // own script injection.
@@ -12,10 +17,9 @@ stripePromise.catch((err: Error) => {
12
17
  }
13
18
  });
14
19
 
15
- export const loadConnect: LoadConnect = () => {
20
+ export const loadConnectAndInitialize: LoadConnectAndInitialize = (
21
+ initParams: IStripeConnectInitParams
22
+ ): StripeConnectInstance => {
16
23
  loadCalled = true;
17
-
18
- return stripePromise.then(maybeConnect => initStripeConnect(maybeConnect));
24
+ return initStripeConnect(stripePromise, initParams);
19
25
  };
20
-
21
- export {};
package/src/pure.test.ts CHANGED
@@ -1,17 +1,24 @@
1
1
  /* eslint-disable @typescript-eslint/no-var-requires */
2
2
 
3
+ import { IStripeConnectInitParams } from "../types";
3
4
  import { SCRIPT_SELECTOR } from "./utils/jestHelpers";
4
5
 
5
6
  describe("pure module", () => {
6
- test("does not inject the script if loadConnect is not called", async () => {
7
+ test("does not inject the script if loadConnectAndInitialize is not called", async () => {
7
8
  require("./pure");
8
9
 
9
10
  expect(document.querySelector(SCRIPT_SELECTOR)).toBe(null);
10
11
  });
11
12
 
12
- test("it injects the script if loadConnect is called", async () => {
13
- const { loadConnect } = require("./pure");
14
- loadConnect();
13
+ test("it injects the script if loadConnectAndInitialize is called", async () => {
14
+ const { loadConnectAndInitialize } = require("./pure");
15
+ const mockInitParams: IStripeConnectInitParams = {
16
+ publishableKey: "pk_123",
17
+ fetchClientSecret: async () => {
18
+ return "secret_123";
19
+ }
20
+ };
21
+ loadConnectAndInitialize(mockInitParams);
15
22
 
16
23
  expect(document.querySelector(SCRIPT_SELECTOR)).not.toBe(null);
17
24
  });
package/src/pure.ts CHANGED
@@ -1,5 +1,13 @@
1
- import { loadScript, initStripeConnect, LoadConnect } from "./shared";
1
+ import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
2
+ import {
3
+ loadScript,
4
+ initStripeConnect,
5
+ LoadConnectAndInitialize
6
+ } from "./shared";
2
7
 
3
- export const loadConnect: LoadConnect = () => {
4
- return loadScript().then(maybeStripe => initStripeConnect(maybeStripe));
8
+ export const loadConnectAndInitialize: LoadConnectAndInitialize = (
9
+ initParams: IStripeConnectInitParams
10
+ ): StripeConnectInstance => {
11
+ const maybeConnect = loadScript();
12
+ return initStripeConnect(maybeConnect, initParams);
5
13
  };
package/src/shared.ts CHANGED
@@ -1,20 +1,31 @@
1
1
  import {
2
- StripeConnectWrapper,
3
2
  IStripeConnectInitParams,
4
- ConnectElementTagName
3
+ StripeConnectInstance,
4
+ ConnectElementTagName,
5
+ ConnectHTMLElementRecord
5
6
  } from "../types";
6
7
 
7
- export type LoadConnect = () => Promise<StripeConnectWrapper>;
8
+ export type LoadConnectAndInitialize = (
9
+ initParams: IStripeConnectInitParams
10
+ ) => StripeConnectInstance;
8
11
 
9
12
  type ConnectElementHTMLName = "stripe-connect-account-onboarding";
10
13
 
11
- const componentNameMapping: Record<
14
+ export const componentNameMapping: Record<
12
15
  ConnectElementTagName,
13
16
  ConnectElementHTMLName
14
17
  > = {
15
18
  "account-onboarding": "stripe-connect-account-onboarding"
16
19
  };
17
20
 
21
+ type StripeConnectInstanceExtended = StripeConnectInstance & {
22
+ debugInstance: () => Promise<StripeConnectInstance>;
23
+ };
24
+
25
+ interface StripeConnectWrapper {
26
+ initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
27
+ }
28
+
18
29
  const EXISTING_SCRIPT_MESSAGE =
19
30
  "loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
20
31
  const V0_URL = "https://connect-js.stripe.com/v1.0/connect.js";
@@ -44,13 +55,13 @@ const injectScript = (): HTMLScriptElement => {
44
55
 
45
56
  let stripePromise: Promise<StripeConnectWrapper> | null = null;
46
57
 
47
- export const loadScript = (): Promise<StripeConnectWrapper | null> => {
58
+ export const loadScript = (): Promise<StripeConnectWrapper> => {
48
59
  // Ensure that we only attempt to load Connect.js at most once
49
60
  if (stripePromise !== null) {
50
61
  return stripePromise;
51
62
  }
52
63
 
53
- stripePromise = new Promise((resolve, reject) => {
64
+ stripePromise = new Promise<StripeConnectWrapper>((resolve, reject) => {
54
65
  if (typeof window === "undefined") {
55
66
  reject(
56
67
  "ConnectJS won't load when rendering code in the server - it can only be loaded on a browser. This error is expected when loading ConnectJS in SSR environments, like NextJS. It will have no impact in the UI, however if you wish to avoid it, you can switch to the `pure` version of the connect.js loader: https://github.com/stripe/connect-js#importing-loadconnect-without-side-effects."
@@ -98,16 +109,45 @@ export const loadScript = (): Promise<StripeConnectWrapper | null> => {
98
109
  };
99
110
 
100
111
  export const initStripeConnect = (
101
- stripeConnectPromise: StripeConnectWrapper | null
102
- ): any | null => {
103
- if (stripeConnectPromise === null) {
104
- return null;
105
- }
112
+ stripePromise: Promise<StripeConnectWrapper>,
113
+ initParams: IStripeConnectInitParams
114
+ ): StripeConnectInstanceExtended => {
115
+ const stripeConnectInstance = stripePromise.then(wrapper =>
116
+ wrapper.initialize(initParams)
117
+ );
118
+
119
+ return {
120
+ create: tagName => {
121
+ let htmlName = componentNameMapping[tagName];
122
+ if (!htmlName) {
123
+ htmlName = (tagName as unknown) as ConnectElementHTMLName;
124
+ }
125
+ const element = document.createElement(htmlName);
126
+ stripeConnectInstance.then(instance => {
127
+ (element as any).setConnector((instance as any).connect);
128
+ });
106
129
 
107
- return stripeConnectPromise;
130
+ return element as ConnectHTMLElementRecord[typeof tagName];
131
+ },
132
+ update: updateOptions => {
133
+ stripeConnectInstance.then(instance => {
134
+ instance.update(updateOptions);
135
+ });
136
+ },
137
+ debugInstance: () => {
138
+ return stripeConnectInstance;
139
+ },
140
+ logout: () => {
141
+ return stripeConnectInstance.then(instance => {
142
+ instance.logout();
143
+ });
144
+ }
145
+ };
108
146
  };
109
147
 
110
148
  const createWrapper = (stripeConnect: any) => {
149
+ (window as any).StripeConnect = (window as any).StripeConnect || {};
150
+ (window as any).StripeConnect.optimizedLoading = true;
111
151
  const wrapper: StripeConnectWrapper = {
112
152
  initialize: (params: IStripeConnectInitParams) => {
113
153
  const metaOptions = (params as any).metaOptions ?? {};
@@ -122,18 +162,6 @@ const createWrapper = (stripeConnect: any) => {
122
162
  }
123
163
  }
124
164
  });
125
-
126
- // We wrap create so we can map its different strings to supported components
127
- const oldCreate = stripeConnectInstance.create.bind(
128
- stripeConnectInstance
129
- );
130
- stripeConnectInstance.create = (tagName: ConnectElementTagName) => {
131
- let htmlName = componentNameMapping[tagName];
132
- if (!htmlName) {
133
- htmlName = tagName as ConnectElementHTMLName;
134
- }
135
- return oldCreate(htmlName);
136
- };
137
165
  return stripeConnectInstance;
138
166
  }
139
167
  };
package/types/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { LoadConnect } from "../src/shared";
1
+ import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
2
2
 
3
- export declare const loadConnect: LoadConnect;
3
+ export declare const loadConnectAndInitialize: (
4
+ initParams: IStripeConnectInitParams
5
+ ) => StripeConnectInstance;
4
6
  export * from "./shared";
package/types/shared.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- export declare type LoadConnect = () => Promise<StripeConnectWrapper>;
1
+ export declare type LoadConnectAndInitialize = (
2
+ initParams: IStripeConnectInitParams
3
+ ) => StripeConnectInstance;
2
4
 
3
5
  export declare type OverlayOption = "dialog" | "drawer";
4
6
 
@@ -335,9 +337,10 @@ export interface IStripeConnectInitParams {
335
337
  publishableKey: string;
336
338
 
337
339
  /**
338
- * The client secret for the connected account.
340
+ * Function that fetches client secret
341
+ * @returns A promise that resolves with a new client secret.
339
342
  */
340
- clientSecret: string;
343
+ fetchClientSecret: () => Promise<string>;
341
344
 
342
345
  /**
343
346
  * Appearance options for the Connect instance.
@@ -345,24 +348,33 @@ export interface IStripeConnectInitParams {
345
348
  */
346
349
  appearance?: AppearanceOptions;
347
350
 
348
- /**
349
- * Callback function that returns a new client secret. Used to support long running sessions and called on account session expiry.
350
- */
351
- refreshClientSecret?: () => Promise<string>;
352
-
353
351
  /**
354
352
  * The locale to use for the Connect instance.
355
353
  */
356
354
  locale?: string;
357
355
  }
358
356
 
359
- export interface StripeConnectWrapper {
360
- /**
361
- * Initializes a Connect JS instance that can be used to create and update Connect components.
362
- * @param params Initialization parameters for Connect JS. See https://stripe.com/docs/connect/get-started-connect-embedded-components#configuring-connect-js for more details.
363
- * @returns A Connect JS instance.
364
- */
365
- initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
357
+ type ConnectHTMLElementRecordFallback = {
358
+ [key in string]: HTMLElement | null;
359
+ };
360
+ type ConnectHTMLElementRecordBase = {
361
+ [tagName in ConnectElementTagName]: HTMLElement;
362
+ };
363
+
364
+ interface ConnectHTMLElementRecord
365
+ extends ConnectHTMLElementRecordBase,
366
+ ConnectHTMLElementRecordFallback {
367
+ "account-onboarding": HTMLElement & {
368
+ setFullTermsOfServiceUrl: (termOfServiceUrl: string | undefined) => void;
369
+ setRecipientTermsOfServiceUrl: (
370
+ recipientTermsOfServiceUrl: string | undefined
371
+ ) => void;
372
+ setPrivacyPolicyUrl: (privacyPolicyUrl: string | undefined) => void;
373
+ setSkipTermsOfServiceCollection: (
374
+ skipTermsOfServiceCollection: boolean | undefined
375
+ ) => void;
376
+ setOnExit: (listener: () => void) => void;
377
+ };
366
378
  }
367
379
 
368
380
  export interface StripeConnectInstance {
@@ -371,7 +383,9 @@ export interface StripeConnectInstance {
371
383
  * @tagName Name of the Connect component to create.
372
384
  * @returns An HTML component corresponding to that connect component
373
385
  */
374
- create: (tagName: ConnectElementTagName) => HTMLElement | null;
386
+ create: <T extends ConnectElementTagName>(
387
+ tagName: T
388
+ ) => ConnectHTMLElementRecord[T];
375
389
 
376
390
  /**
377
391
  * Updates the Connect instance with new parameters.
@@ -390,11 +404,3 @@ export interface StripeConnectInstance {
390
404
  * Tagnames to be used with the `create` method of the Connect instance.
391
405
  */
392
406
  export type ConnectElementTagName = "account-onboarding";
393
-
394
- export declare const findScript: () => HTMLScriptElement | null;
395
-
396
- export declare const loadScript: () => Promise<any | null>;
397
-
398
- export declare const initStripeConnect: (
399
- stripeConnectPromise: StripeConnectWrapper | null
400
- ) => any | null;
@@ -1,3 +0,0 @@
1
- import { LoadConnect } from "./shared";
2
- export declare const loadConnect: LoadConnect;
3
- export {};