@tryfinch/react-connect
Advanced tools
Comparing version 3.7.0 to 3.11.0
export type SuccessEvent = { | ||
code: string; | ||
state?: string; | ||
idpRedirectUri?: string; | ||
}; | ||
@@ -9,6 +10,3 @@ export type ErrorEvent = { | ||
export type Sandbox = 'finch' /** This is to enable the new Finch (simulated) Sandbox */ | 'provider' /** This is to enable the new Provider Sandbox */ | boolean /** This is the old sandbox flag retained for backwards compatibility */; | ||
export type ConnectOptions = { | ||
category: string | null; | ||
clientId: string; | ||
manual: boolean; | ||
type BaseConnectOptions = { | ||
state: string | null; | ||
@@ -18,5 +16,2 @@ onSuccess: (e: SuccessEvent) => void; | ||
onClose: () => void; | ||
payrollProvider: string | null; | ||
products: string[]; | ||
sandbox: Sandbox; | ||
zIndex: number; | ||
@@ -28,3 +23,17 @@ apiConfig?: { | ||
}; | ||
type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products' | 'state' | 'payrollProvider'>>) => void; | ||
type ConnectOptionsWithSessionId = BaseConnectOptions & { | ||
sessionId: string; | ||
products?: string[]; | ||
}; | ||
type ConnectOptionsWithClientId = BaseConnectOptions & { | ||
category: string | null; | ||
clientId: string; | ||
manual: boolean; | ||
payrollProvider: string | null; | ||
products: string[]; | ||
clientName?: string; | ||
sandbox: Sandbox; | ||
}; | ||
export type ConnectOptions = ConnectOptionsWithSessionId | ConnectOptionsWithClientId; | ||
type OpenFn = (overrides?: Partial<ConnectOptions>) => void; | ||
export declare const useFinchConnect: (options: Partial<ConnectOptions>) => { | ||
@@ -31,0 +40,0 @@ open: OpenFn; |
@@ -7,13 +7,29 @@ import { useRef, useEffect } from 'react'; | ||
const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message'; | ||
const constructAuthUrl = ({ clientId, payrollProvider, category, products, manual, sandbox, state, apiConfig, }) => { | ||
const constructAuthUrl = (connectOptions) => { | ||
const { state, apiConfig } = connectOptions; | ||
const CONNECT_URL = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.connectUrl) || BASE_FINCH_CONNECT_URI; | ||
const REDIRECT_URL = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.redirectUrl) || DEFAULT_FINCH_REDIRECT_URI; | ||
const authUrl = new URL(`${CONNECT_URL}/authorize`); | ||
if (clientId) | ||
authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) | ||
authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) | ||
authUrl.searchParams.append('category', category); | ||
authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' ')); | ||
if ('sessionId' in connectOptions) { | ||
const { sessionId, products } = connectOptions; | ||
authUrl.searchParams.append('session', sessionId); | ||
if (products) | ||
authUrl.searchParams.append('products', products.join(' ')); | ||
} | ||
else { | ||
const { clientId, payrollProvider, category, products, manual, sandbox, clientName, } = connectOptions; | ||
if (clientId) | ||
authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) | ||
authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) | ||
authUrl.searchParams.append('category', category); | ||
if (clientName) | ||
authUrl.searchParams.append('client_name', clientName); | ||
authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' ')); | ||
if (manual) | ||
authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) | ||
authUrl.searchParams.append('sandbox', String(sandbox)); | ||
} | ||
authUrl.searchParams.append('app_type', 'spa'); | ||
@@ -24,10 +40,6 @@ authUrl.searchParams.append('redirect_uri', REDIRECT_URL); | ||
authUrl.searchParams.append('mode', 'employer'); | ||
if (manual) | ||
authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) | ||
authUrl.searchParams.append('sandbox', String(sandbox)); | ||
if (state) | ||
authUrl.searchParams.append('state', state); | ||
// replace with actual SDK version by rollup | ||
authUrl.searchParams.append('sdk_version', 'react-3.7.0'); | ||
authUrl.searchParams.append('sdk_version', 'react-3.11.0'); | ||
return authUrl.href; | ||
@@ -38,18 +50,19 @@ }; | ||
}; | ||
const DEFAULT_OPTIONS = { | ||
category: null, | ||
manual: false, | ||
const BASE_DEFAULTS = { | ||
onSuccess: noop, | ||
onError: noop, | ||
onClose: noop, | ||
payrollProvider: null, | ||
products: [], | ||
sandbox: false, | ||
state: null, | ||
zIndex: 999, | ||
}; | ||
const DEFAULT_OPTIONS_WITH_CLIENT_ID = Object.assign(Object.assign({}, BASE_DEFAULTS), { clientId: '', category: null, manual: false, payrollProvider: null, products: [], clientName: undefined, sandbox: false }); | ||
const DEFAULT_OPTIONS_WITH_SESSION_ID = Object.assign(Object.assign({}, BASE_DEFAULTS), { sessionId: '' }); | ||
let isUseFinchConnectInitialized = false; | ||
const useFinchConnect = (options) => { | ||
if (!options.clientId) | ||
throw new Error('must specify clientId in options for useFinchConnect'); | ||
if (!('sessionId' in options) && !('clientId' in options)) { | ||
throw new Error('must specify either sessionId or clientId in options for useFinchConnect'); | ||
} | ||
if ('sessionId' in options && 'clientId' in options) { | ||
throw new Error('cannot specify both sessionId and clientId in options for useFinchConnect'); | ||
} | ||
const isHookMounted = useRef(false); | ||
@@ -67,3 +80,4 @@ useEffect(() => { | ||
}, []); | ||
const combinedOptions = Object.assign(Object.assign({ clientId: '' }, DEFAULT_OPTIONS), options); | ||
const combinedOptions = 'sessionId' in options | ||
? Object.assign(Object.assign({}, DEFAULT_OPTIONS_WITH_SESSION_ID), options) : Object.assign(Object.assign({}, DEFAULT_OPTIONS_WITH_CLIENT_ID), options); | ||
const open = (overrides) => { | ||
@@ -117,2 +131,3 @@ const openOptions = Object.assign(Object.assign({}, combinedOptions), overrides); | ||
state: event.data.state, | ||
idpRedirectUri: event.data.idpRedirectUri, | ||
}); | ||
@@ -119,0 +134,0 @@ break; |
@@ -11,13 +11,29 @@ 'use strict'; | ||
const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message'; | ||
const constructAuthUrl = ({ clientId, payrollProvider, category, products, manual, sandbox, state, apiConfig, }) => { | ||
const constructAuthUrl = (connectOptions) => { | ||
const { state, apiConfig } = connectOptions; | ||
const CONNECT_URL = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.connectUrl) || BASE_FINCH_CONNECT_URI; | ||
const REDIRECT_URL = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.redirectUrl) || DEFAULT_FINCH_REDIRECT_URI; | ||
const authUrl = new URL(`${CONNECT_URL}/authorize`); | ||
if (clientId) | ||
authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) | ||
authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) | ||
authUrl.searchParams.append('category', category); | ||
authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' ')); | ||
if ('sessionId' in connectOptions) { | ||
const { sessionId, products } = connectOptions; | ||
authUrl.searchParams.append('session', sessionId); | ||
if (products) | ||
authUrl.searchParams.append('products', products.join(' ')); | ||
} | ||
else { | ||
const { clientId, payrollProvider, category, products, manual, sandbox, clientName, } = connectOptions; | ||
if (clientId) | ||
authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) | ||
authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) | ||
authUrl.searchParams.append('category', category); | ||
if (clientName) | ||
authUrl.searchParams.append('client_name', clientName); | ||
authUrl.searchParams.append('products', (products !== null && products !== void 0 ? products : []).join(' ')); | ||
if (manual) | ||
authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) | ||
authUrl.searchParams.append('sandbox', String(sandbox)); | ||
} | ||
authUrl.searchParams.append('app_type', 'spa'); | ||
@@ -28,10 +44,6 @@ authUrl.searchParams.append('redirect_uri', REDIRECT_URL); | ||
authUrl.searchParams.append('mode', 'employer'); | ||
if (manual) | ||
authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) | ||
authUrl.searchParams.append('sandbox', String(sandbox)); | ||
if (state) | ||
authUrl.searchParams.append('state', state); | ||
// replace with actual SDK version by rollup | ||
authUrl.searchParams.append('sdk_version', 'react-3.7.0'); | ||
authUrl.searchParams.append('sdk_version', 'react-3.11.0'); | ||
return authUrl.href; | ||
@@ -42,18 +54,19 @@ }; | ||
}; | ||
const DEFAULT_OPTIONS = { | ||
category: null, | ||
manual: false, | ||
const BASE_DEFAULTS = { | ||
onSuccess: noop, | ||
onError: noop, | ||
onClose: noop, | ||
payrollProvider: null, | ||
products: [], | ||
sandbox: false, | ||
state: null, | ||
zIndex: 999, | ||
}; | ||
const DEFAULT_OPTIONS_WITH_CLIENT_ID = Object.assign(Object.assign({}, BASE_DEFAULTS), { clientId: '', category: null, manual: false, payrollProvider: null, products: [], clientName: undefined, sandbox: false }); | ||
const DEFAULT_OPTIONS_WITH_SESSION_ID = Object.assign(Object.assign({}, BASE_DEFAULTS), { sessionId: '' }); | ||
let isUseFinchConnectInitialized = false; | ||
const useFinchConnect = (options) => { | ||
if (!options.clientId) | ||
throw new Error('must specify clientId in options for useFinchConnect'); | ||
if (!('sessionId' in options) && !('clientId' in options)) { | ||
throw new Error('must specify either sessionId or clientId in options for useFinchConnect'); | ||
} | ||
if ('sessionId' in options && 'clientId' in options) { | ||
throw new Error('cannot specify both sessionId and clientId in options for useFinchConnect'); | ||
} | ||
const isHookMounted = react.useRef(false); | ||
@@ -71,3 +84,4 @@ react.useEffect(() => { | ||
}, []); | ||
const combinedOptions = Object.assign(Object.assign({ clientId: '' }, DEFAULT_OPTIONS), options); | ||
const combinedOptions = 'sessionId' in options | ||
? Object.assign(Object.assign({}, DEFAULT_OPTIONS_WITH_SESSION_ID), options) : Object.assign(Object.assign({}, DEFAULT_OPTIONS_WITH_CLIENT_ID), options); | ||
const open = (overrides) => { | ||
@@ -121,2 +135,3 @@ const openOptions = Object.assign(Object.assign({}, combinedOptions), overrides); | ||
state: event.data.state, | ||
idpRedirectUri: event.data.idpRedirectUri, | ||
}); | ||
@@ -123,0 +138,0 @@ break; |
{ | ||
"name": "@tryfinch/react-connect", | ||
"version": "3.7.0", | ||
"version": "3.11.0", | ||
"description": "Finch SDK for embedding Finch Connect in API React Single Page Applications (SPA)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
119
src/index.ts
import { useEffect, useRef } from 'react'; | ||
type HasKey<T, K extends PropertyKey> = T extends Record<K, unknown> ? T : never; | ||
export type SuccessEvent = { | ||
code: string; | ||
state?: string; | ||
idpRedirectUri?: string; | ||
}; | ||
@@ -17,6 +20,3 @@ | ||
export type ConnectOptions = { | ||
category: string | null; | ||
clientId: string; | ||
manual: boolean; | ||
type BaseConnectOptions = { | ||
state: string | null; | ||
@@ -26,5 +26,2 @@ onSuccess: (e: SuccessEvent) => void; | ||
onClose: () => void; | ||
payrollProvider: string | null; | ||
products: string[]; | ||
sandbox: Sandbox; | ||
zIndex: number; | ||
@@ -37,6 +34,23 @@ apiConfig?: { | ||
type OpenFn = ( | ||
overrides?: Partial<Pick<ConnectOptions, 'products' | 'state' | 'payrollProvider'>> | ||
) => void; | ||
type ConnectOptionsWithSessionId = BaseConnectOptions & { | ||
// Use this option if you have a Finch Connect sessionID from the IDP redirect flow | ||
sessionId: string; | ||
// Allow for overriding products for the session | ||
products?: string[]; | ||
}; | ||
type ConnectOptionsWithClientId = BaseConnectOptions & { | ||
category: string | null; | ||
clientId: string; | ||
manual: boolean; | ||
payrollProvider: string | null; | ||
products: string[]; | ||
clientName?: string; | ||
sandbox: Sandbox; | ||
}; | ||
export type ConnectOptions = ConnectOptionsWithSessionId | ConnectOptionsWithClientId; | ||
type OpenFn = (overrides?: Partial<ConnectOptions>) => void; | ||
const POST_MESSAGE_NAME = 'finch-auth-message' as const; | ||
@@ -52,2 +66,3 @@ | ||
state?: string; | ||
idpRedirectUri?: string; | ||
} | ||
@@ -71,12 +86,5 @@ | { | ||
const constructAuthUrl = ({ | ||
clientId, | ||
payrollProvider, | ||
category, | ||
products, | ||
manual, | ||
sandbox, | ||
state, | ||
apiConfig, | ||
}: Partial<ConnectOptions>) => { | ||
const constructAuthUrl = (connectOptions: ConnectOptions) => { | ||
const { state, apiConfig } = connectOptions; | ||
const CONNECT_URL = apiConfig?.connectUrl || BASE_FINCH_CONNECT_URI; | ||
@@ -86,6 +94,27 @@ const REDIRECT_URL = apiConfig?.redirectUrl || DEFAULT_FINCH_REDIRECT_URI; | ||
const authUrl = new URL(`${CONNECT_URL}/authorize`); | ||
if (clientId) authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) authUrl.searchParams.append('category', category); | ||
authUrl.searchParams.append('products', (products ?? []).join(' ')); | ||
if ('sessionId' in connectOptions) { | ||
const { sessionId, products } = connectOptions; | ||
authUrl.searchParams.append('session', sessionId); | ||
if (products) authUrl.searchParams.append('products', products.join(' ')); | ||
} else { | ||
const { | ||
clientId, | ||
payrollProvider, | ||
category, | ||
products, | ||
manual, | ||
sandbox, | ||
clientName, | ||
} = connectOptions; | ||
if (clientId) authUrl.searchParams.append('client_id', clientId); | ||
if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider); | ||
if (category) authUrl.searchParams.append('category', category); | ||
if (clientName) authUrl.searchParams.append('client_name', clientName); | ||
authUrl.searchParams.append('products', (products ?? []).join(' ')); | ||
if (manual) authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox)); | ||
} | ||
authUrl.searchParams.append('app_type', 'spa'); | ||
@@ -96,4 +125,2 @@ authUrl.searchParams.append('redirect_uri', REDIRECT_URL); | ||
authUrl.searchParams.append('mode', 'employer'); | ||
if (manual) authUrl.searchParams.append('manual', String(manual)); | ||
if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox)); | ||
if (state) authUrl.searchParams.append('state', state); | ||
@@ -110,19 +137,37 @@ // replace with actual SDK version by rollup | ||
const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = { | ||
category: null, | ||
manual: false, | ||
const BASE_DEFAULTS = { | ||
onSuccess: noop, | ||
onError: noop, | ||
onClose: noop, | ||
state: null, | ||
zIndex: 999, | ||
}; | ||
const DEFAULT_OPTIONS_WITH_CLIENT_ID: HasKey<ConnectOptions, 'clientId'> = { | ||
...BASE_DEFAULTS, | ||
clientId: '', | ||
category: null, | ||
manual: false, | ||
payrollProvider: null, | ||
products: [], | ||
clientName: undefined, | ||
sandbox: false, | ||
state: null, | ||
zIndex: 999, | ||
}; | ||
const DEFAULT_OPTIONS_WITH_SESSION_ID: HasKey<ConnectOptions, 'sessionId'> = { | ||
...BASE_DEFAULTS, | ||
sessionId: '', | ||
}; | ||
let isUseFinchConnectInitialized = false; | ||
export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenFn } => { | ||
if (!options.clientId) throw new Error('must specify clientId in options for useFinchConnect'); | ||
if (!('sessionId' in options) && !('clientId' in options)) { | ||
throw new Error('must specify either sessionId or clientId in options for useFinchConnect'); | ||
} | ||
if ('sessionId' in options && 'clientId' in options) { | ||
throw new Error('cannot specify both sessionId and clientId in options for useFinchConnect'); | ||
} | ||
const isHookMounted = useRef(false); | ||
@@ -144,7 +189,6 @@ | ||
const combinedOptions: ConnectOptions = { | ||
clientId: '', | ||
...DEFAULT_OPTIONS, | ||
...options, | ||
}; | ||
const combinedOptions: ConnectOptions = | ||
'sessionId' in options | ||
? { ...DEFAULT_OPTIONS_WITH_SESSION_ID, ...options } | ||
: { ...DEFAULT_OPTIONS_WITH_CLIENT_ID, ...options }; | ||
@@ -203,2 +247,3 @@ const open: OpenFn = (overrides) => { | ||
state: event.data.state, | ||
idpRedirectUri: event.data.idpRedirectUri, | ||
}); | ||
@@ -205,0 +250,0 @@ break; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
943992
32274