@tryfinch/react-connect 2.8.1 → 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/.eslintrc.js CHANGED
@@ -1,5 +1,12 @@
1
1
  module.exports = {
2
- extends: ['airbnb', 'plugin:prettier/recommended', 'prettier/react'],
2
+ extends: [
3
+ 'airbnb',
4
+ 'plugin:prettier/recommended',
5
+ 'prettier/react',
6
+ 'plugin:@typescript-eslint/recommended',
7
+ ],
8
+ parser: '@typescript-eslint/parser',
9
+ plugins: ['prettier', '@typescript-eslint'],
3
10
  env: {
4
11
  browser: true,
5
12
  commonjs: true,
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @tryfinch/react-connect
2
2
 
3
- [![NPM](https://img.shields.io/npm/v/@tryfinch/react-connect)](https://www.npmjs.com/package/@tryfinch/react-connect) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
3
+ [![NPM](https://img.shields.io/npm/v/@tryfinch/react-connect)](https://www.npmjs.com/package/@tryfinch/react-connect)
4
4
 
5
5
  ## Install
6
6
 
package/dist/index.d.ts CHANGED
@@ -1,25 +1,24 @@
1
- declare module 'react-finch-connect' {
2
- export type SuccessEvent = {
1
+ export type SuccessEvent = {
3
2
  code: string;
4
- };
5
-
6
- export type ErrorEvent = {
3
+ };
4
+ export type ErrorEvent = {
7
5
  errorMessage: string;
8
- };
9
-
10
- export type ConnectOptions = {
6
+ };
7
+ export type ConnectOptions = {
8
+ category: string | null;
11
9
  clientId: string;
12
- products?: string[];
13
- mode?: string;
14
- manual?: boolean;
15
- payrollProvider?: string;
16
- sandbox?: boolean;
17
- category?: string;
18
- onSuccess?: (e: SuccessEvent) => void;
19
- onError?: (e: ErrorEvent) => void;
20
- onClose?: () => void;
21
- zIndex?: bigint | string;
22
- };
23
-
24
- export function useFinchConnect(opts: ConnectOptions): { open: () => void };
25
- }
10
+ manual: boolean;
11
+ onSuccess: (e: SuccessEvent) => void;
12
+ onError: (e: ErrorEvent) => void;
13
+ onClose: () => void;
14
+ payrollProvider: string | null;
15
+ products: string[];
16
+ sandbox: boolean;
17
+ zIndex: number;
18
+ };
19
+ type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
20
+ export declare const useFinchConnect: (options: Partial<ConnectOptions>) => {
21
+ open: OpenFn;
22
+ };
23
+ export {};
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,CAAC,EAAE,YAAY,KAAK,IAAI,CAAC;IACrC,OAAO,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IACjC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,MAAM,GAAG,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC,KAAK,IAAI,CAAC;AA0D9E,eAAO,MAAM,eAAe,YAAa,QAAQ,cAAc,CAAC;UAAW,MAAM;CAkEhF,CAAC"}
package/dist/index.es.js CHANGED
@@ -1,119 +1,99 @@
1
1
  import { useEffect } from 'react';
2
2
 
3
- var BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
4
- var DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
5
- var FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
6
- var FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
7
-
8
- var noop = function noop() {}; // eslint-disable-next-line import/prefer-default-export
9
-
10
-
11
- var useFinchConnect = function useFinchConnect() {
12
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
13
- var clientId = options.clientId,
14
- _options$products = options.products,
15
- products = _options$products === void 0 ? [] : _options$products,
16
- _options$mode = options.mode,
17
- mode = _options$mode === void 0 ? 'employer' : _options$mode,
18
- category = options.category,
19
- _options$manual = options.manual,
20
- manual = _options$manual === void 0 ? false : _options$manual,
21
- _options$payrollProvi = options.payrollProvider,
22
- payrollProvider = _options$payrollProvi === void 0 ? null : _options$payrollProvi,
23
- _options$sandbox = options.sandbox,
24
- sandbox = _options$sandbox === void 0 ? false : _options$sandbox,
25
- _options$onSuccess = options.onSuccess,
26
- onSuccess = _options$onSuccess === void 0 ? noop : _options$onSuccess,
27
- _options$onError = options.onError,
28
- onError = _options$onError === void 0 ? noop : _options$onError,
29
- _options$onClose = options.onClose,
30
- onClose = _options$onClose === void 0 ? noop : _options$onClose,
31
- _options$zIndex = options.zIndex,
32
- zIndex = _options$zIndex === void 0 ? 999 : _options$zIndex;
33
-
34
- var _constructAuthUrl = function _constructAuthUrl(clientId, products) {
35
- var authUrl = new URL("".concat(BASE_FINCH_CONNECT_URI, "/authorize"));
36
- if (clientId) authUrl.searchParams.append('client_id', clientId);
37
- if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
38
- if (category) authUrl.searchParams.append('category', category);
39
- authUrl.searchParams.append('products', products.join(' '));
3
+ const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
4
+ const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
5
+ const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
6
+ const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
7
+ const constructAuthUrl = ({ clientId, payrollProvider, category, products, manual, sandbox, }) => {
8
+ const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
9
+ if (clientId)
10
+ authUrl.searchParams.append('client_id', clientId);
11
+ if (payrollProvider)
12
+ authUrl.searchParams.append('payroll_provider', payrollProvider);
13
+ if (category)
14
+ authUrl.searchParams.append('category', category);
15
+ authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' '));
40
16
  authUrl.searchParams.append('app_type', 'spa');
41
17
  authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
42
- authUrl.searchParams.append('mode', mode);
43
- if (manual) authUrl.searchParams.append('manual', manual);
44
- if (sandbox) authUrl.searchParams.append('sandbox', sandbox);
45
- /* global "2.8.1" */
46
-
47
- authUrl.searchParams.append('sdk_version', "react-".concat("2.8.1"));
18
+ authUrl.searchParams.append('mode', 'employer');
19
+ if (manual)
20
+ authUrl.searchParams.append('manual', String(manual));
21
+ if (sandbox)
22
+ authUrl.searchParams.append('sandbox', String(sandbox));
23
+ // replace with actual SDK version by rollup
24
+ authUrl.searchParams.append('sdk_version', 'react-3.0.0');
48
25
  return authUrl.href;
49
- };
50
-
51
- var open = function open() {
52
- if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
53
- return null;
54
- }
55
-
56
- var iframe = document.createElement('iframe');
57
- iframe.src = _constructAuthUrl(clientId, products);
58
- iframe.frameBorder = '0';
59
- iframe.id = FINCH_CONNECT_IFRAME_ID;
60
- iframe.style.position = 'fixed';
61
- iframe.style.zIndex = zIndex.toString();
62
- iframe.style.height = '100%';
63
- iframe.style.width = '100%';
64
- iframe.style.top = '0';
65
- iframe.style.backgroundColor = 'none transparent';
66
- iframe.style.border = 'none';
67
- document.body.prepend(iframe);
68
- document.body.style.overflow = 'hidden';
69
- };
70
-
71
- var close = function close() {
72
- var frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
73
-
74
- if (frameToRemove) {
75
- frameToRemove.parentNode.removeChild(frameToRemove);
76
- document.body.style.overflow = 'inherit';
77
- }
78
- };
79
-
80
- useEffect(function () {
81
- function handleFinchAuth(event) {
82
- var handleFinchAuthSuccess = function handleFinchAuthSuccess(code) {
83
- return onSuccess({
84
- code: code
85
- });
86
- };
87
-
88
- var handleFinchAuthError = function handleFinchAuthError(error) {
89
- return onError({
90
- errorMessage: error
91
- });
92
- };
93
-
94
- var handleFinchAuthClose = function handleFinchAuthClose() {
95
- return onClose();
96
- };
97
-
98
- if (!event.data) return;
99
- if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
100
- if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
101
- var _event$data = event.data,
102
- code = _event$data.code,
103
- error = _event$data.error,
104
- closed = _event$data.closed;
105
- close();
106
- if (code) handleFinchAuthSuccess(code);else if (error) handleFinchAuthError(error);else if (closed) handleFinchAuthClose();
107
- }
108
-
109
- window.addEventListener('message', handleFinchAuth);
110
- return function () {
111
- return window.removeEventListener('message', handleFinchAuth);
26
+ };
27
+ const noop = () => {
28
+ // intentionally empty
29
+ };
30
+ const DEFAULT_OPTIONS = {
31
+ category: null,
32
+ manual: false,
33
+ onSuccess: noop,
34
+ onError: noop,
35
+ onClose: noop,
36
+ payrollProvider: null,
37
+ products: [],
38
+ sandbox: false,
39
+ zIndex: 999,
40
+ };
41
+ const useFinchConnect = (options) => {
42
+ if (!options.clientId)
43
+ throw new Error('must specify clientId in options for useFinchConnect');
44
+ const combinedOptions = Object.assign(Object.assign({ clientId: '' }, DEFAULT_OPTIONS), options);
45
+ const open = (overrides) => {
46
+ const openOptions = Object.assign(Object.assign({}, combinedOptions), overrides);
47
+ if (!document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
48
+ const iframe = document.createElement('iframe');
49
+ iframe.src = constructAuthUrl(openOptions);
50
+ iframe.frameBorder = '0';
51
+ iframe.id = FINCH_CONNECT_IFRAME_ID;
52
+ iframe.style.position = 'fixed';
53
+ iframe.style.zIndex = openOptions.zIndex.toString();
54
+ iframe.style.height = '100%';
55
+ iframe.style.width = '100%';
56
+ iframe.style.top = '0';
57
+ iframe.style.backgroundColor = 'none transparent';
58
+ iframe.style.border = 'none';
59
+ document.body.prepend(iframe);
60
+ document.body.style.overflow = 'hidden';
61
+ }
62
+ };
63
+ const close = () => {
64
+ var _a;
65
+ const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
66
+ if (frameToRemove) {
67
+ (_a = frameToRemove.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(frameToRemove);
68
+ document.body.style.overflow = 'inherit';
69
+ }
70
+ };
71
+ useEffect(() => {
72
+ function handleFinchAuth(event) {
73
+ const handleFinchAuthSuccess = (code) => combinedOptions.onSuccess({ code });
74
+ const handleFinchAuthError = (error) => combinedOptions.onError({ errorMessage: error });
75
+ const handleFinchAuthClose = () => combinedOptions.onClose();
76
+ if (!event.data)
77
+ return;
78
+ if (event.data.name !== FINCH_AUTH_MESSAGE_NAME)
79
+ return;
80
+ if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI))
81
+ return;
82
+ const { code, error, closed } = event.data;
83
+ close();
84
+ if (code)
85
+ handleFinchAuthSuccess(code);
86
+ else if (error)
87
+ handleFinchAuthError(error);
88
+ else if (closed)
89
+ handleFinchAuthClose();
90
+ }
91
+ window.addEventListener('message', handleFinchAuth);
92
+ return () => window.removeEventListener('message', handleFinchAuth);
93
+ }, [combinedOptions.onClose, combinedOptions.onError, combinedOptions.onSuccess]);
94
+ return {
95
+ open,
112
96
  };
113
- }, [onClose, onError, onSuccess]);
114
- return {
115
- open: open
116
- };
117
97
  };
118
98
 
119
99
  export { useFinchConnect };
@@ -1 +1 @@
1
- {"version":3,"file":"index.es.js","sources":["../src/index.js"],"sourcesContent":["import { useEffect } from 'react';\n\nconst BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';\nconst DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';\nconst FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';\nconst FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';\n\nconst noop = () => {};\n\n// eslint-disable-next-line import/prefer-default-export\nexport const useFinchConnect = (options = {}) => {\n const {\n clientId,\n products = [],\n mode = 'employer',\n category,\n manual = false,\n payrollProvider = null,\n sandbox = false,\n onSuccess = noop,\n onError = noop,\n onClose = noop,\n zIndex = 999,\n } = options;\n\n const _constructAuthUrl = (clientId, products) => {\n const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);\n\n if (clientId) authUrl.searchParams.append('client_id', clientId);\n if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);\n if (category) authUrl.searchParams.append('category', category);\n authUrl.searchParams.append('products', products.join(' '));\n authUrl.searchParams.append('app_type', 'spa');\n authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);\n authUrl.searchParams.append('mode', mode);\n if (manual) authUrl.searchParams.append('manual', manual);\n if (sandbox) authUrl.searchParams.append('sandbox', sandbox);\n /* global SDK_VERSION */\n if (SDK_VERSION) authUrl.searchParams.append('sdk_version', `react-${SDK_VERSION}`);\n\n return authUrl.href;\n };\n\n const open = () => {\n if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {\n return null;\n }\n\n const iframe = document.createElement('iframe');\n iframe.src = _constructAuthUrl(clientId, products);\n iframe.frameBorder = '0';\n iframe.id = FINCH_CONNECT_IFRAME_ID;\n iframe.style.position = 'fixed';\n iframe.style.zIndex = zIndex.toString();\n iframe.style.height = '100%';\n iframe.style.width = '100%';\n iframe.style.top = '0';\n iframe.style.backgroundColor = 'none transparent';\n iframe.style.border = 'none';\n document.body.prepend(iframe);\n document.body.style.overflow = 'hidden';\n };\n\n const close = () => {\n const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);\n if (frameToRemove) {\n frameToRemove.parentNode.removeChild(frameToRemove);\n document.body.style.overflow = 'inherit';\n }\n };\n\n useEffect(() => {\n function handleFinchAuth(event) {\n const handleFinchAuthSuccess = (code) => onSuccess({ code });\n const handleFinchAuthError = (error) => onError({ errorMessage: error });\n const handleFinchAuthClose = () => onClose();\n\n if (!event.data) return;\n if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;\n if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;\n\n const { code, error, closed } = event.data;\n\n close();\n if (code) handleFinchAuthSuccess(code);\n else if (error) handleFinchAuthError(error);\n else if (closed) handleFinchAuthClose();\n }\n\n window.addEventListener('message', handleFinchAuth);\n return () => window.removeEventListener('message', handleFinchAuth);\n }, [onClose, onError, onSuccess]);\n\n return {\n open,\n };\n};\n"],"names":["BASE_FINCH_CONNECT_URI","DEFAULT_FINCH_REDIRECT_URI","FINCH_CONNECT_IFRAME_ID","FINCH_AUTH_MESSAGE_NAME","noop","useFinchConnect","options","clientId","products","mode","category","manual","payrollProvider","sandbox","onSuccess","onError","onClose","zIndex","_constructAuthUrl","authUrl","URL","searchParams","append","join","SDK_VERSION","href","open","document","getElementById","iframe","createElement","src","frameBorder","id","style","position","toString","height","width","top","backgroundColor","border","body","prepend","overflow","close","frameToRemove","parentNode","removeChild","useEffect","handleFinchAuth","event","handleFinchAuthSuccess","code","handleFinchAuthError","error","errorMessage","handleFinchAuthClose","data","name","origin","startsWith","closed","window","addEventListener","removeEventListener"],"mappings":";;AAEA,IAAMA,sBAAsB,GAAG,8BAA/B,CAAA;AACA,IAAMC,0BAA0B,GAAG,sBAAnC,CAAA;AACA,IAAMC,uBAAuB,GAAG,sBAAhC,CAAA;AACA,IAAMC,uBAAuB,GAAG,oBAAhC,CAAA;;AAEA,IAAMC,IAAI,GAAG,SAAPA,IAAO,GAAM,EAAnB;;;AAGaC,IAAAA,eAAe,GAAG,SAAlBA,eAAkB,GAAkB;EAAA,IAAjBC,OAAiB,uEAAP,EAAO,CAAA;AAC/C,EAAA,IACEC,QADF,GAYID,OAZJ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AAC,EAAE,IAAI,EAAE,CAAC,CAAC;AACrF,YAAA,MAAM,oBAAoB,GAAG,CAAC,KAAa,KACzC,eAAe,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC;YACnD,MAAM,oBAAoB,GAAG,MAAM,eAAe,CAAC,OAAO,EAAE,CAAC;YAE7D,IAAI,CAAC,KAAK,CAAC,IAAI;gBAAE,OAAO;AACxB,YAAA,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,uBAAuB;gBAAE,OAAO;YACxD,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,sBAAsB,CAAC;gBAAE,OAAO;YAE7D,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC;AAE3C,YAAA,KAAK,EAAE,CAAC;AACR,YAAA,IAAI,IAAI;gBAAE,sBAAsB,CAAC,IAAI,CAAC,CAAC;AAClC,iBAAA,IAAI,KAAK;gBAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;AACvC,iBAAA,IAAI,MAAM;AAAE,gBAAA,oBAAoB,EAAE,CAAC;SACzC;AAED,QAAA,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QACpD,OAAO,MAAM,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;AACtE,KAAC,EAAE,CAAC,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC,CAAC;IAElF,OAAO;QACL,IAAI;KACL,CAAC;AACJ;;;;"}
package/dist/index.js CHANGED
@@ -4,120 +4,100 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var react = require('react');
6
6
 
7
- var BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
8
- var DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
9
- var FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
10
- var FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
11
-
12
- var noop = function noop() {}; // eslint-disable-next-line import/prefer-default-export
13
-
14
-
15
- var useFinchConnect = function useFinchConnect() {
16
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
17
- var clientId = options.clientId,
18
- _options$products = options.products,
19
- products = _options$products === void 0 ? [] : _options$products,
20
- _options$mode = options.mode,
21
- mode = _options$mode === void 0 ? 'employer' : _options$mode,
22
- category = options.category,
23
- _options$manual = options.manual,
24
- manual = _options$manual === void 0 ? false : _options$manual,
25
- _options$payrollProvi = options.payrollProvider,
26
- payrollProvider = _options$payrollProvi === void 0 ? null : _options$payrollProvi,
27
- _options$sandbox = options.sandbox,
28
- sandbox = _options$sandbox === void 0 ? false : _options$sandbox,
29
- _options$onSuccess = options.onSuccess,
30
- onSuccess = _options$onSuccess === void 0 ? noop : _options$onSuccess,
31
- _options$onError = options.onError,
32
- onError = _options$onError === void 0 ? noop : _options$onError,
33
- _options$onClose = options.onClose,
34
- onClose = _options$onClose === void 0 ? noop : _options$onClose,
35
- _options$zIndex = options.zIndex,
36
- zIndex = _options$zIndex === void 0 ? 999 : _options$zIndex;
37
-
38
- var _constructAuthUrl = function _constructAuthUrl(clientId, products) {
39
- var authUrl = new URL("".concat(BASE_FINCH_CONNECT_URI, "/authorize"));
40
- if (clientId) authUrl.searchParams.append('client_id', clientId);
41
- if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
42
- if (category) authUrl.searchParams.append('category', category);
43
- authUrl.searchParams.append('products', products.join(' '));
7
+ const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
8
+ const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
9
+ const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
10
+ const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
11
+ const constructAuthUrl = ({ clientId, payrollProvider, category, products, manual, sandbox, }) => {
12
+ const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
13
+ if (clientId)
14
+ authUrl.searchParams.append('client_id', clientId);
15
+ if (payrollProvider)
16
+ authUrl.searchParams.append('payroll_provider', payrollProvider);
17
+ if (category)
18
+ authUrl.searchParams.append('category', category);
19
+ authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' '));
44
20
  authUrl.searchParams.append('app_type', 'spa');
45
21
  authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
46
- authUrl.searchParams.append('mode', mode);
47
- if (manual) authUrl.searchParams.append('manual', manual);
48
- if (sandbox) authUrl.searchParams.append('sandbox', sandbox);
49
- /* global "2.8.1" */
50
-
51
- authUrl.searchParams.append('sdk_version', "react-".concat("2.8.1"));
22
+ authUrl.searchParams.append('mode', 'employer');
23
+ if (manual)
24
+ authUrl.searchParams.append('manual', String(manual));
25
+ if (sandbox)
26
+ authUrl.searchParams.append('sandbox', String(sandbox));
27
+ // replace with actual SDK version by rollup
28
+ authUrl.searchParams.append('sdk_version', 'react-3.0.0');
52
29
  return authUrl.href;
53
- };
54
-
55
- var open = function open() {
56
- if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
57
- return null;
58
- }
59
-
60
- var iframe = document.createElement('iframe');
61
- iframe.src = _constructAuthUrl(clientId, products);
62
- iframe.frameBorder = '0';
63
- iframe.id = FINCH_CONNECT_IFRAME_ID;
64
- iframe.style.position = 'fixed';
65
- iframe.style.zIndex = zIndex.toString();
66
- iframe.style.height = '100%';
67
- iframe.style.width = '100%';
68
- iframe.style.top = '0';
69
- iframe.style.backgroundColor = 'none transparent';
70
- iframe.style.border = 'none';
71
- document.body.prepend(iframe);
72
- document.body.style.overflow = 'hidden';
73
- };
74
-
75
- var close = function close() {
76
- var frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
77
-
78
- if (frameToRemove) {
79
- frameToRemove.parentNode.removeChild(frameToRemove);
80
- document.body.style.overflow = 'inherit';
81
- }
82
- };
83
-
84
- react.useEffect(function () {
85
- function handleFinchAuth(event) {
86
- var handleFinchAuthSuccess = function handleFinchAuthSuccess(code) {
87
- return onSuccess({
88
- code: code
89
- });
90
- };
91
-
92
- var handleFinchAuthError = function handleFinchAuthError(error) {
93
- return onError({
94
- errorMessage: error
95
- });
96
- };
97
-
98
- var handleFinchAuthClose = function handleFinchAuthClose() {
99
- return onClose();
100
- };
101
-
102
- if (!event.data) return;
103
- if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
104
- if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
105
- var _event$data = event.data,
106
- code = _event$data.code,
107
- error = _event$data.error,
108
- closed = _event$data.closed;
109
- close();
110
- if (code) handleFinchAuthSuccess(code);else if (error) handleFinchAuthError(error);else if (closed) handleFinchAuthClose();
111
- }
112
-
113
- window.addEventListener('message', handleFinchAuth);
114
- return function () {
115
- return window.removeEventListener('message', handleFinchAuth);
30
+ };
31
+ const noop = () => {
32
+ // intentionally empty
33
+ };
34
+ const DEFAULT_OPTIONS = {
35
+ category: null,
36
+ manual: false,
37
+ onSuccess: noop,
38
+ onError: noop,
39
+ onClose: noop,
40
+ payrollProvider: null,
41
+ products: [],
42
+ sandbox: false,
43
+ zIndex: 999,
44
+ };
45
+ const useFinchConnect = (options) => {
46
+ if (!options.clientId)
47
+ throw new Error('must specify clientId in options for useFinchConnect');
48
+ const combinedOptions = Object.assign(Object.assign({ clientId: '' }, DEFAULT_OPTIONS), options);
49
+ const open = (overrides) => {
50
+ const openOptions = Object.assign(Object.assign({}, combinedOptions), overrides);
51
+ if (!document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
52
+ const iframe = document.createElement('iframe');
53
+ iframe.src = constructAuthUrl(openOptions);
54
+ iframe.frameBorder = '0';
55
+ iframe.id = FINCH_CONNECT_IFRAME_ID;
56
+ iframe.style.position = 'fixed';
57
+ iframe.style.zIndex = openOptions.zIndex.toString();
58
+ iframe.style.height = '100%';
59
+ iframe.style.width = '100%';
60
+ iframe.style.top = '0';
61
+ iframe.style.backgroundColor = 'none transparent';
62
+ iframe.style.border = 'none';
63
+ document.body.prepend(iframe);
64
+ document.body.style.overflow = 'hidden';
65
+ }
66
+ };
67
+ const close = () => {
68
+ var _a;
69
+ const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
70
+ if (frameToRemove) {
71
+ (_a = frameToRemove.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(frameToRemove);
72
+ document.body.style.overflow = 'inherit';
73
+ }
74
+ };
75
+ react.useEffect(() => {
76
+ function handleFinchAuth(event) {
77
+ const handleFinchAuthSuccess = (code) => combinedOptions.onSuccess({ code });
78
+ const handleFinchAuthError = (error) => combinedOptions.onError({ errorMessage: error });
79
+ const handleFinchAuthClose = () => combinedOptions.onClose();
80
+ if (!event.data)
81
+ return;
82
+ if (event.data.name !== FINCH_AUTH_MESSAGE_NAME)
83
+ return;
84
+ if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI))
85
+ return;
86
+ const { code, error, closed } = event.data;
87
+ close();
88
+ if (code)
89
+ handleFinchAuthSuccess(code);
90
+ else if (error)
91
+ handleFinchAuthError(error);
92
+ else if (closed)
93
+ handleFinchAuthClose();
94
+ }
95
+ window.addEventListener('message', handleFinchAuth);
96
+ return () => window.removeEventListener('message', handleFinchAuth);
97
+ }, [combinedOptions.onClose, combinedOptions.onError, combinedOptions.onSuccess]);
98
+ return {
99
+ open,
116
100
  };
117
- }, [onClose, onError, onSuccess]);
118
- return {
119
- open: open
120
- };
121
101
  };
122
102
 
123
103
  exports.useFinchConnect = useFinchConnect;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["import { useEffect } from 'react';\n\nconst BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';\nconst DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';\nconst FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';\nconst FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';\n\nconst noop = () => {};\n\n// eslint-disable-next-line import/prefer-default-export\nexport const useFinchConnect = (options = {}) => {\n const {\n clientId,\n products = [],\n mode = 'employer',\n category,\n manual = false,\n payrollProvider = null,\n sandbox = false,\n onSuccess = noop,\n onError = noop,\n onClose = noop,\n zIndex = 999,\n } = options;\n\n const _constructAuthUrl = (clientId, products) => {\n const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);\n\n if (clientId) authUrl.searchParams.append('client_id', clientId);\n if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);\n if (category) authUrl.searchParams.append('category', category);\n authUrl.searchParams.append('products', products.join(' '));\n authUrl.searchParams.append('app_type', 'spa');\n authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);\n authUrl.searchParams.append('mode', mode);\n if (manual) authUrl.searchParams.append('manual', manual);\n if (sandbox) authUrl.searchParams.append('sandbox', sandbox);\n /* global SDK_VERSION */\n if (SDK_VERSION) authUrl.searchParams.append('sdk_version', `react-${SDK_VERSION}`);\n\n return authUrl.href;\n };\n\n const open = () => {\n if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {\n return null;\n }\n\n const iframe = document.createElement('iframe');\n iframe.src = _constructAuthUrl(clientId, products);\n iframe.frameBorder = '0';\n iframe.id = FINCH_CONNECT_IFRAME_ID;\n iframe.style.position = 'fixed';\n iframe.style.zIndex = zIndex.toString();\n iframe.style.height = '100%';\n iframe.style.width = '100%';\n iframe.style.top = '0';\n iframe.style.backgroundColor = 'none transparent';\n iframe.style.border = 'none';\n document.body.prepend(iframe);\n document.body.style.overflow = 'hidden';\n };\n\n const close = () => {\n const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);\n if (frameToRemove) {\n frameToRemove.parentNode.removeChild(frameToRemove);\n document.body.style.overflow = 'inherit';\n }\n };\n\n useEffect(() => {\n function handleFinchAuth(event) {\n const handleFinchAuthSuccess = (code) => onSuccess({ code });\n const handleFinchAuthError = (error) => onError({ errorMessage: error });\n const handleFinchAuthClose = () => onClose();\n\n if (!event.data) return;\n if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;\n if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;\n\n const { code, error, closed } = event.data;\n\n close();\n if (code) handleFinchAuthSuccess(code);\n else if (error) handleFinchAuthError(error);\n else if (closed) handleFinchAuthClose();\n }\n\n window.addEventListener('message', handleFinchAuth);\n return () => window.removeEventListener('message', handleFinchAuth);\n }, [onClose, onError, onSuccess]);\n\n return {\n open,\n };\n};\n"],"names":["BASE_FINCH_CONNECT_URI","DEFAULT_FINCH_REDIRECT_URI","FINCH_CONNECT_IFRAME_ID","FINCH_AUTH_MESSAGE_NAME","noop","useFinchConnect","options","clientId","products","mode","category","manual","payrollProvider","sandbox","onSuccess","onError","onClose","zIndex","_constructAuthUrl","authUrl","URL","searchParams","append","join","SDK_VERSION","href","open","document","getElementById","iframe","createElement","src","frameBorder","id","style","position","toString","height","width","top","backgroundColor","border","body","prepend","overflow","close","frameToRemove","parentNode","removeChild","useEffect","handleFinchAuth","event","handleFinchAuthSuccess","code","handleFinchAuthError","error","errorMessage","handleFinchAuthClose","data","name","origin","startsWith","closed","window","addEventListener","removeEventListener"],"mappings":";;;;;;AAEA,IAAMA,sBAAsB,GAAG,8BAA/B,CAAA;AACA,IAAMC,0BAA0B,GAAG,sBAAnC,CAAA;AACA,IAAMC,uBAAuB,GAAG,sBAAhC,CAAA;AACA,IAAMC,uBAAuB,GAAG,oBAAhC,CAAA;;AAEA,IAAMC,IAAI,GAAG,SAAPA,IAAO,GAAM,EAAnB;;;AAGaC,IAAAA,eAAe,GAAG,SAAlBA,eAAkB,GAAkB;EAAA,IAAjBC,OAAiB,uEAAP,EAAO,CAAA;AAC/C,EAAA,IACEC,QADF,GAYID,OAZJ,CACEC,QADF;MAYID,iBAAAA,GAAAA,OAZJ,CAEEE,QAFF;MAEEA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,oBAAoB,GAAG,CAAC,KAAa,KACzC,eAAe,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC;YACnD,MAAM,oBAAoB,GAAG,MAAM,eAAe,CAAC,OAAO,EAAE,CAAC;YAE7D,IAAI,CAAC,KAAK,CAAC,IAAI;gBAAE,OAAO;AACxB,YAAA,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,uBAAuB;gBAAE,OAAO;YACxD,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,sBAAsB,CAAC;gBAAE,OAAO;YAE7D,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC;AAE3C,YAAA,KAAK,EAAE,CAAC;AACR,YAAA,IAAI,IAAI;gBAAE,sBAAsB,CAAC,IAAI,CAAC,CAAC;AAClC,iBAAA,IAAI,KAAK;gBAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;AACvC,iBAAA,IAAI,MAAM;AAAE,gBAAA,oBAAoB,EAAE,CAAC;SACzC;AAED,QAAA,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QACpD,OAAO,MAAM,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;AACtE,KAAC,EAAE,CAAC,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC,CAAC;IAElF,OAAO;QACL,IAAI;KACL,CAAC;AACJ;;;;"}
package/package.json CHANGED
@@ -1,10 +1,18 @@
1
1
  {
2
2
  "name": "@tryfinch/react-connect",
3
- "version": "2.8.1",
3
+ "version": "3.0.0",
4
4
  "description": "Finch SDK for embedding Finch Connect in API React Single Page Applications (SPA)",
5
+ "keywords": [
6
+ "finch",
7
+ "sdk",
8
+ "payroll",
9
+ "react",
10
+ "oauth"
11
+ ],
12
+ "homepage": "https://tryfinch.com/",
5
13
  "author": "",
6
14
  "license": "MIT",
7
- "repository": "Finch-API/react-finch-connect",
15
+ "repository": "Finch-API/react-connect",
8
16
  "main": "dist/index.js",
9
17
  "types": "dist/index.d.ts",
10
18
  "module": "dist/index.es.js",
@@ -14,9 +22,8 @@
14
22
  "npm": ">=5"
15
23
  },
16
24
  "scripts": {
17
- "test": "cross-env CI=1 react-scripts test --env=jsdom",
18
- "test:watch": "react-scripts test --env=jsdom",
19
25
  "build": "rollup -c",
26
+ "lint": "eslint --ext .js,.jsx,.ts,.tsx src",
20
27
  "start": "rollup -c -w",
21
28
  "prepare": "npm run build",
22
29
  "predeploy": "cd example && npm install && npm run build"
@@ -25,54 +32,18 @@
25
32
  "react": ">=16.9.0"
26
33
  },
27
34
  "devDependencies": {
28
- "@babel/core": "^7.0.0",
29
- "@babel/plugin-external-helpers": "^7.0.0",
30
- "@babel/plugin-proposal-class-properties": "^7.0.0",
31
- "@babel/plugin-proposal-decorators": "^7.0.0",
32
- "@babel/plugin-proposal-do-expressions": "^7.0.0",
33
- "@babel/plugin-proposal-export-default-from": "^7.0.0",
34
- "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
35
- "@babel/plugin-proposal-function-bind": "^7.0.0",
36
- "@babel/plugin-proposal-function-sent": "^7.0.0",
37
- "@babel/plugin-proposal-json-strings": "^7.0.0",
38
- "@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
39
- "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
40
- "@babel/plugin-proposal-numeric-separator": "^7.0.0",
41
- "@babel/plugin-proposal-optional-chaining": "^7.0.0",
42
- "@babel/plugin-proposal-pipeline-operator": "^7.0.0",
43
- "@babel/plugin-proposal-throw-expressions": "^7.0.0",
44
- "@babel/plugin-syntax-dynamic-import": "^7.0.0",
45
- "@babel/plugin-syntax-import-meta": "^7.0.0",
46
- "@babel/preset-env": "^7.0.0",
47
- "@babel/preset-react": "^7.0.0",
48
35
  "@rollup/plugin-replace": "^4.0.0",
49
- "@testing-library/react-hooks": "^3.2.1",
50
- "babel-eslint": "^10.0.1",
51
- "cross-env": "^5.2.0",
36
+ "@rollup/plugin-typescript": "^11.1.0",
37
+ "@types/react": "^16.14.40",
38
+ "@typescript-eslint/eslint-plugin": "^5.59.0",
39
+ "@typescript-eslint/parser": "^5.59.0",
52
40
  "eslint": "^7.32.0",
53
41
  "eslint-config-airbnb": "^18.2.0",
54
42
  "eslint-config-prettier": "^6.11.0",
55
- "eslint-config-standard": "^11.0.0",
56
- "eslint-config-standard-react": "^6.0.0",
57
- "eslint-plugin-import": "^2.22.0",
58
- "eslint-plugin-jsx-a11y": "^6.3.1",
59
- "eslint-plugin-node": "^7.0.1",
60
43
  "eslint-plugin-prettier": "^3.1.4",
61
- "eslint-plugin-promise": "^4.0.0",
62
- "eslint-plugin-react": "^7.20.3",
63
- "eslint-plugin-react-hooks": "^4.0.8",
64
- "eslint-plugin-standard": "^3.1.0",
65
- "gh-pages": "^2.0.1",
66
- "prettier": "^2.0.5",
67
44
  "react": "^16.9.0",
68
- "react-scripts": "^5.0.1",
69
- "react-test-renderer": "^16.9.0",
70
45
  "rollup": "^2.75.7",
71
- "rollup-plugin-babel": "^4.3.2",
72
- "rollup-plugin-commonjs": "^9.2.0",
73
- "rollup-plugin-dts": "^4.0.1",
74
- "rollup-plugin-node-resolve": "^4.0.0",
75
46
  "rollup-plugin-peer-deps-external": "^2.2.0",
76
- "rollup-plugin-url": "^2.1.0"
47
+ "typescript": "^4.9.5"
77
48
  }
78
49
  }
package/rollup.config.js CHANGED
@@ -1,49 +1,20 @@
1
- import babel from 'rollup-plugin-babel';
2
- import commonjs from 'rollup-plugin-commonjs';
3
- import dts from 'rollup-plugin-dts';
4
1
  import external from 'rollup-plugin-peer-deps-external';
5
- import resolve from 'rollup-plugin-node-resolve';
6
- import url from 'rollup-plugin-url';
7
2
  import replace from '@rollup/plugin-replace';
3
+ import typescript from '@rollup/plugin-typescript';
8
4
 
9
5
  import pkg from './package.json';
10
6
 
7
+ const plugins = [external(), replace({ SDK_VERSION: pkg.version }), typescript()];
8
+
11
9
  export default [
12
10
  {
13
- input: 'src/index.js',
14
- output: [
15
- {
16
- file: pkg.main,
17
- format: 'cjs',
18
- sourcemap: true,
19
- },
20
- {
21
- file: pkg.module,
22
- format: 'es',
23
- sourcemap: true,
24
- },
25
- ],
26
- plugins: [
27
- external(),
28
- url({ exclude: ['**/*.svg'] }),
29
- babel({
30
- exclude: 'node_modules/**',
31
- }),
32
- resolve(),
33
- commonjs(),
34
- replace({
35
- SDK_VERSION: JSON.stringify(pkg.version), // has to be stringified somehow
36
- }),
37
- ],
11
+ input: 'src/index.ts',
12
+ output: [{ file: pkg.main, format: 'cjs', sourcemap: true }],
13
+ plugins,
38
14
  },
39
15
  {
40
- input: 'src/index.d.ts',
41
- output: [
42
- {
43
- file: pkg.types,
44
- format: 'es',
45
- },
46
- ],
47
- plugins: [dts()],
16
+ input: 'src/index.ts',
17
+ output: [{ file: pkg.module, format: 'es', sourcemap: true }],
18
+ plugins,
48
19
  },
49
20
  ];
package/src/index.ts ADDED
@@ -0,0 +1,148 @@
1
+ import { useEffect } from 'react';
2
+
3
+ export type SuccessEvent = {
4
+ code: string;
5
+ };
6
+
7
+ export type ErrorEvent = {
8
+ errorMessage: string;
9
+ };
10
+
11
+ export type ConnectOptions = {
12
+ category: string | null;
13
+ clientId: string;
14
+ manual: boolean;
15
+ onSuccess: (e: SuccessEvent) => void;
16
+ onError: (e: ErrorEvent) => void;
17
+ onClose: () => void;
18
+ payrollProvider: string | null;
19
+ products: string[];
20
+ sandbox: boolean;
21
+ zIndex: number;
22
+ };
23
+
24
+ type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
25
+
26
+ interface FinchConnectPostMessage {
27
+ data: {
28
+ name: string;
29
+ code?: string;
30
+ error?: string;
31
+ closed?: boolean;
32
+ };
33
+ origin: string;
34
+ }
35
+
36
+ const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
37
+ const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
38
+ const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
39
+ const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
40
+
41
+ const constructAuthUrl = ({
42
+ clientId,
43
+ payrollProvider,
44
+ category,
45
+ products,
46
+ manual,
47
+ sandbox,
48
+ }: Partial<ConnectOptions>) => {
49
+ const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
50
+
51
+ if (clientId) authUrl.searchParams.append('client_id', clientId);
52
+ if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
53
+ if (category) authUrl.searchParams.append('category', category);
54
+ authUrl.searchParams.append('products', (products ?? []).join(' '));
55
+ authUrl.searchParams.append('app_type', 'spa');
56
+ authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
57
+ authUrl.searchParams.append('mode', 'employer');
58
+ if (manual) authUrl.searchParams.append('manual', String(manual));
59
+ if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox));
60
+ // replace with actual SDK version by rollup
61
+ authUrl.searchParams.append('sdk_version', 'react-SDK_VERSION');
62
+
63
+ return authUrl.href;
64
+ };
65
+
66
+ const noop = () => {
67
+ // intentionally empty
68
+ };
69
+
70
+ const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = {
71
+ category: null,
72
+ manual: false,
73
+ onSuccess: noop,
74
+ onError: noop,
75
+ onClose: noop,
76
+ payrollProvider: null,
77
+ products: [],
78
+ sandbox: false,
79
+ zIndex: 999,
80
+ };
81
+
82
+ export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenFn } => {
83
+ if (!options.clientId) throw new Error('must specify clientId in options for useFinchConnect');
84
+
85
+ const combinedOptions: ConnectOptions = {
86
+ clientId: '',
87
+ ...DEFAULT_OPTIONS,
88
+ ...options,
89
+ };
90
+
91
+ const open: OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => {
92
+ const openOptions: ConnectOptions = {
93
+ ...combinedOptions,
94
+ ...overrides,
95
+ };
96
+
97
+ if (!document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
98
+ const iframe = document.createElement('iframe');
99
+ iframe.src = constructAuthUrl(openOptions);
100
+ iframe.frameBorder = '0';
101
+ iframe.id = FINCH_CONNECT_IFRAME_ID;
102
+ iframe.style.position = 'fixed';
103
+ iframe.style.zIndex = openOptions.zIndex.toString();
104
+ iframe.style.height = '100%';
105
+ iframe.style.width = '100%';
106
+ iframe.style.top = '0';
107
+ iframe.style.backgroundColor = 'none transparent';
108
+ iframe.style.border = 'none';
109
+ document.body.prepend(iframe);
110
+ document.body.style.overflow = 'hidden';
111
+ }
112
+ };
113
+
114
+ const close = () => {
115
+ const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
116
+ if (frameToRemove) {
117
+ frameToRemove.parentNode?.removeChild(frameToRemove);
118
+ document.body.style.overflow = 'inherit';
119
+ }
120
+ };
121
+
122
+ useEffect(() => {
123
+ function handleFinchAuth(event: FinchConnectPostMessage) {
124
+ const handleFinchAuthSuccess = (code: string) => combinedOptions.onSuccess({ code });
125
+ const handleFinchAuthError = (error: string) =>
126
+ combinedOptions.onError({ errorMessage: error });
127
+ const handleFinchAuthClose = () => combinedOptions.onClose();
128
+
129
+ if (!event.data) return;
130
+ if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
131
+ if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
132
+
133
+ const { code, error, closed } = event.data;
134
+
135
+ close();
136
+ if (code) handleFinchAuthSuccess(code);
137
+ else if (error) handleFinchAuthError(error);
138
+ else if (closed) handleFinchAuthClose();
139
+ }
140
+
141
+ window.addEventListener('message', handleFinchAuth);
142
+ return () => window.removeEventListener('message', handleFinchAuth);
143
+ }, [combinedOptions.onClose, combinedOptions.onError, combinedOptions.onSuccess]);
144
+
145
+ return {
146
+ open,
147
+ };
148
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2016",
4
+ "module": "esnext",
5
+ "declaration": true,
6
+ "declarationMap": true,
7
+ "sourceMap": true,
8
+ "outDir": "./",
9
+ "esModuleInterop": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "strict": true
12
+ },
13
+ "include": ["src/**/*"],
14
+ "exclude": ["node_modules", "example/**/*"]
15
+ }
package/.babelrc DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "presets": [
3
- ["@babel/preset-env", {
4
- "modules": false
5
- }],
6
- "@babel/preset-react"
7
- ],
8
- "plugins": ["@babel/plugin-proposal-class-properties"]
9
- }
package/src/index.d.ts DELETED
@@ -1,25 +0,0 @@
1
- declare module 'react-finch-connect' {
2
- export type SuccessEvent = {
3
- code: string;
4
- };
5
-
6
- export type ErrorEvent = {
7
- errorMessage: string;
8
- };
9
-
10
- export type ConnectOptions = {
11
- clientId: string;
12
- products?: string[];
13
- mode?: string;
14
- manual?: boolean;
15
- payrollProvider?: string;
16
- sandbox?: boolean;
17
- category?: string;
18
- onSuccess?: (e: SuccessEvent) => void;
19
- onError?: (e: ErrorEvent) => void;
20
- onClose?: () => void;
21
- zIndex?: bigint | string;
22
- };
23
-
24
- export function useFinchConnect(opts: ConnectOptions): { open: () => void };
25
- }
package/src/index.js DELETED
@@ -1,97 +0,0 @@
1
- import { useEffect } from 'react';
2
-
3
- const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
4
- const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
5
- const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
6
- const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
7
-
8
- const noop = () => {};
9
-
10
- // eslint-disable-next-line import/prefer-default-export
11
- export const useFinchConnect = (options = {}) => {
12
- const {
13
- clientId,
14
- products = [],
15
- mode = 'employer',
16
- category,
17
- manual = false,
18
- payrollProvider = null,
19
- sandbox = false,
20
- onSuccess = noop,
21
- onError = noop,
22
- onClose = noop,
23
- zIndex = 999,
24
- } = options;
25
-
26
- const _constructAuthUrl = (clientId, products) => {
27
- const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
28
-
29
- if (clientId) authUrl.searchParams.append('client_id', clientId);
30
- if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
31
- if (category) authUrl.searchParams.append('category', category);
32
- authUrl.searchParams.append('products', products.join(' '));
33
- authUrl.searchParams.append('app_type', 'spa');
34
- authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
35
- authUrl.searchParams.append('mode', mode);
36
- if (manual) authUrl.searchParams.append('manual', manual);
37
- if (sandbox) authUrl.searchParams.append('sandbox', sandbox);
38
- /* global SDK_VERSION */
39
- if (SDK_VERSION) authUrl.searchParams.append('sdk_version', `react-${SDK_VERSION}`);
40
-
41
- return authUrl.href;
42
- };
43
-
44
- const open = () => {
45
- if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
46
- return null;
47
- }
48
-
49
- const iframe = document.createElement('iframe');
50
- iframe.src = _constructAuthUrl(clientId, products);
51
- iframe.frameBorder = '0';
52
- iframe.id = FINCH_CONNECT_IFRAME_ID;
53
- iframe.style.position = 'fixed';
54
- iframe.style.zIndex = zIndex.toString();
55
- iframe.style.height = '100%';
56
- iframe.style.width = '100%';
57
- iframe.style.top = '0';
58
- iframe.style.backgroundColor = 'none transparent';
59
- iframe.style.border = 'none';
60
- document.body.prepend(iframe);
61
- document.body.style.overflow = 'hidden';
62
- };
63
-
64
- const close = () => {
65
- const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
66
- if (frameToRemove) {
67
- frameToRemove.parentNode.removeChild(frameToRemove);
68
- document.body.style.overflow = 'inherit';
69
- }
70
- };
71
-
72
- useEffect(() => {
73
- function handleFinchAuth(event) {
74
- const handleFinchAuthSuccess = (code) => onSuccess({ code });
75
- const handleFinchAuthError = (error) => onError({ errorMessage: error });
76
- const handleFinchAuthClose = () => onClose();
77
-
78
- if (!event.data) return;
79
- if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
80
- if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
81
-
82
- const { code, error, closed } = event.data;
83
-
84
- close();
85
- if (code) handleFinchAuthSuccess(code);
86
- else if (error) handleFinchAuthError(error);
87
- else if (closed) handleFinchAuthClose();
88
- }
89
-
90
- window.addEventListener('message', handleFinchAuth);
91
- return () => window.removeEventListener('message', handleFinchAuth);
92
- }, [onClose, onError, onSuccess]);
93
-
94
- return {
95
- open,
96
- };
97
- };