@strivacity/sdk-vue 4.0.0-beta.0 → 4.0.0-beta.1
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/dist/composables.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("vue"),t=require("@strivacity/sdk-core/utils/errors"),n=require("@strivacity/sdk-core/utils");var r=Symbol(`strivacity-sdk`),i=Symbol(`strivacity-login-context`),a=()=>{let t=(0,e.inject)(r);if(!t)throw Error(`Missing SDK context`);return t},o=()=>{let t=(0,e.inject)(i);if(!t)throw Error(`Missing SDK native login context`);return t};function s(r={}){let o=new AbortController,{sdk:s}=a(),c=(0,e.ref)(!0),l=(0,e.ref)({}),u=(0,e.ref)({}),d=(0,e.ref)({});(0,e.onMounted)(async()=>{await s.init();try{await p(r.params)}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;s.logging?.error(`Error fetching authorization URI`,e),r.onError?.(e),c.value=!1}}),(0,e.onUnmounted)(()=>o.abort());function f(e){if(s.session){r.onLogin?.(s.session);return}let t={hostedUrl:e?.hostedUrl??d.value.hostedUrl,finalizeUrl:e?.finalizeUrl??d.value.finalizeUrl,screen:e?.screen??d.value.screen,forms:e?.forms??d.value.forms,layout:e?.layout??d.value.layout,messages:e?.messages??{},branding:e?.branding??d.value.branding};if(t.screen!==d.value.screen){l.value={},u.value={};for(let e of t.forms??[])l.value[e.id]={},u.value[e.id]={}}else s.logging?.info(`Updating screen: ${t.screen}`);for(let e of Object.keys(t.messages??{}))e===`global`?r.onGlobalMessage?.(t.messages?.global):u.value[e]=t.messages[e]??{};d.value=t,t.finalizeUrl||(c.value=!1)}async function p(e={}){try{c.value=!0;let t=await s.startSession(e);t&&f(t)}catch(e){e instanceof t.FallbackError?(s.logging?.error(`Fallback error occurred`,e),r.onFallback?.(e)):(s.logging?.error(`Error starting session`,e),r.onError?.(e))}}function m(e){if(s.logging?.warn(e?`Triggering fallback due to: ${e}`:`Triggering fallback`),!d.value.hostedUrl){let e=Error(`No hosted URL provided`);throw s.logging?.error(`Fallback error`,e),e}r.onFallback?.(new t.FallbackError(new URL(d.value.hostedUrl)))}function h(){s.logging?.debug(`Triggering close`),r.onClose?.()}async function g(e,i){try{c.value=!0;let t=await s.submitForm(e,i??(0,n.unflattenObject)(l.value[e]??{}));t&&f(t)}catch(e){e instanceof t.FallbackError?(s.logging?.error(`Fallback error occurred`,e),r.onFallback?.(e)):(s.logging?.error(`Error submitting form`,e),r.onError?.(e))}}function _(e,t,n){l.value[e]===void 0&&(l.value[e]={}),l.value[e][t]=n===``?null:n}function v(e,t,n){u.value[e]===void 0&&(u.value[e]={}),u.value[e][t]=n}return(0,e.provide)(i,{loading:c,forms:l,messages:u,state:d,triggerFallback:m,triggerClose:h,submitForm:g,setFormValue:_,setMessage:v}),{loading:c,forms:l,messages:u,state:d,triggerFallback:m,triggerClose:h,submitForm:g,setFormValue:_,setMessage:v}}exports.STRIVACITY_LOGIN_CONTEXT=i,exports.STRIVACITY_SDK=r,exports.useNativeLogin=s,exports.useNativeLoginContext=o,exports.useStrivacity=a;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("vue"),t=require("@strivacity/sdk-core/utils/errors"),n=require("@strivacity/sdk-core/utils");var r=Symbol(`strivacity-sdk`),i=Symbol(`strivacity-login-context`),a=()=>{let t=(0,e.inject)(r);if(!t)throw Error(`Missing SDK context`);return t},o=()=>{let t=(0,e.inject)(i);if(!t)throw Error(`Missing SDK native login context`);return t};function s(r={}){let o=new AbortController,{sdk:s}=a(),c=(0,e.ref)(!0),l=(0,e.ref)({}),u=(0,e.ref)({}),d=(0,e.ref)({});(0,e.onMounted)(async()=>{await s.init();try{await p(r.params)}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;s.logging?.error(`Error fetching authorization URI`,e),r.onError?.(e),c.value=!1}}),(0,e.onUnmounted)(()=>o.abort());function f(e){if(s.session&&!(0,n.isSessionExpired)(s.session)){r.onLogin?.(s.session);return}let t={hostedUrl:e?.hostedUrl??d.value.hostedUrl,finalizeUrl:e?.finalizeUrl??d.value.finalizeUrl,screen:e?.screen??d.value.screen,forms:e?.forms??d.value.forms,layout:e?.layout??d.value.layout,messages:e?.messages??{},branding:e?.branding??d.value.branding};if(t.screen!==d.value.screen){l.value={},u.value={};for(let e of t.forms??[])l.value[e.id]={},u.value[e.id]={}}else s.logging?.info(`Updating screen: ${t.screen}`);for(let e of Object.keys(t.messages??{}))e===`global`?r.onGlobalMessage?.(t.messages?.global):u.value[e]=t.messages[e]??{};d.value=t,t.finalizeUrl||(c.value=!1)}async function p(e={}){try{c.value=!0;let t=await s.startSession(e);t&&f(t)}catch(e){e instanceof t.FallbackError?(s.logging?.error(`Fallback error occurred`,e),r.onFallback?.(e)):(s.logging?.error(`Error starting session`,e),r.onError?.(e))}}function m(e){if(s.logging?.warn(e?`Triggering fallback due to: ${e}`:`Triggering fallback`),!d.value.hostedUrl){let e=Error(`No hosted URL provided`);throw s.logging?.error(`Fallback error`,e),e}r.onFallback?.(new t.FallbackError(new URL(d.value.hostedUrl)))}function h(){s.logging?.debug(`Triggering close`),r.onClose?.()}async function g(e,i){try{c.value=!0;let t=await s.submitForm(e,i??(0,n.unflattenObject)(l.value[e]??{}));t&&f(t)}catch(e){e instanceof t.FallbackError?(s.logging?.error(`Fallback error occurred`,e),r.onFallback?.(e)):(s.logging?.error(`Error submitting form`,e),r.onError?.(e))}}function _(e,t,n){l.value[e]===void 0&&(l.value[e]={}),l.value[e][t]=n===``?null:n}function v(e,t,n){u.value[e]===void 0&&(u.value[e]={}),u.value[e][t]=n}return(0,e.provide)(i,{loading:c,forms:l,messages:u,state:d,triggerFallback:m,triggerClose:h,submitForm:g,setFormValue:_,setMessage:v}),{loading:c,forms:l,messages:u,state:d,triggerFallback:m,triggerClose:h,submitForm:g,setFormValue:_,setMessage:v}}exports.STRIVACITY_LOGIN_CONTEXT=i,exports.STRIVACITY_SDK=r,exports.useNativeLogin=s,exports.useNativeLoginContext=o,exports.useStrivacity=a;
|
|
2
2
|
//# sourceMappingURL=composables.cjs.map
|
package/dist/composables.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"composables.cjs","names":[],"sources":["../src/composables.ts"],"sourcesContent":["import type { InjectionKey } from 'vue';\nimport type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';\nimport type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';\nimport { provide, inject, ref, onMounted, onUnmounted } from 'vue';\nimport { FallbackError } from '@strivacity/sdk-core/utils/errors';\nimport { unflattenObject } from '@strivacity/sdk-core/utils';\n\nexport const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');\nexport const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');\n\n/**\n * A composable to access the Strivacity SDK context.\n *\n * @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).\n * @returns {SDKContext<T>} - The Strivacity SDK context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useStrivacity = <\n\tT extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,\n>(): SDKContext<T> => {\n\tconst context = inject<SDKContext<T>>(STRIVACITY_SDK);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable to access the Strivacity SDK context specifically for native login flow.\n *\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useNativeLoginContext = (): LoginContext => {\n\tconst context = inject<LoginContext>(STRIVACITY_LOGIN_CONTEXT);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK native login context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable that manages a native login flow session.\n *\n * @param {UseNativeLoginOptions} [options] - Optional options for the login session.\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<typeof useNativeLoginContext> {\n\tconst abortController = new AbortController();\n\tconst { sdk } = useStrivacity<NativeFlow>();\n\n\tconst loading = ref<boolean>(true);\n\tconst forms = ref<Record<string, Record<string, unknown>>>({});\n\tconst messages = ref<Record<string, Record<string, NativeFlowMessage>>>({});\n\tconst state = ref<Partial<NativeFlowState>>({});\n\n\tonMounted(async () => {\n\t\tawait sdk.init();\n\n\t\ttry {\n\t\t\tawait startSession(options.params);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DOMException && error.name === 'AbortError') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsdk.logging?.error('Error fetching authorization URI', error);\n\t\t\tvoid options.onError?.(error);\n\t\t\tloading.value = false;\n\t\t}\n\t});\n\n\tonUnmounted(() => abortController.abort());\n\n\tfunction handleResponse(nextState: Partial<NativeFlowState>) {\n\t\tif (sdk.session) {\n\t\t\treturn void options.onLogin?.(sdk.session);\n\t\t}\n\n\t\tconst newState: NativeFlowState = {\n\t\t\thostedUrl: nextState?.hostedUrl ?? state.value.hostedUrl,\n\t\t\tfinalizeUrl: nextState?.finalizeUrl ?? state.value.finalizeUrl,\n\t\t\tscreen: nextState?.screen ?? state.value.screen,\n\t\t\tforms: nextState?.forms ?? state.value.forms,\n\t\t\tlayout: nextState?.layout ?? state.value.layout,\n\t\t\tmessages: nextState?.messages ?? {},\n\t\t\tbranding: nextState?.branding ?? state.value.branding,\n\t\t};\n\n\t\tif (newState.screen !== state.value.screen) {\n\t\t\tforms.value = {};\n\t\t\tmessages.value = {};\n\n\t\t\tfor (const form of newState.forms ?? []) {\n\t\t\t\tforms.value[form.id] = {};\n\t\t\t\tmessages.value[form.id] = {};\n\t\t\t}\n\t\t} else {\n\t\t\tsdk.logging?.info(`Updating screen: ${newState.screen}`);\n\t\t}\n\n\t\tfor (const formId of Object.keys(newState.messages ?? {})) {\n\t\t\tif (formId === 'global') {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n\t\t\t\tvoid options.onGlobalMessage?.(newState.messages?.global!);\n\t\t\t} else {\n\t\t\t\tmessages.value[formId] = newState.messages![formId] ?? {};\n\t\t\t}\n\t\t}\n\n\t\tstate.value = newState;\n\n\t\tif (!newState.finalizeUrl) {\n\t\t\tloading.value = false;\n\t\t}\n\t}\n\n\tasync function startSession(loginParams: NativeParams = {}) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.startSession(loginParams);\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error starting session', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction triggerFallback(message?: string) {\n\t\tsdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');\n\n\t\tif (!state.value.hostedUrl) {\n\t\t\tconst error = new Error('No hosted URL provided');\n\t\t\tsdk.logging?.error('Fallback error', error);\n\t\t\tthrow error;\n\t\t}\n\n\t\tvoid options.onFallback?.(new FallbackError(new URL(state.value.hostedUrl)));\n\t}\n\n\tfunction triggerClose() {\n\t\tsdk.logging?.debug('Triggering close');\n\t\tvoid options.onClose?.();\n\t}\n\n\tasync function submitForm(formId: string, customBody?: Record<string, unknown>) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.submitForm(formId, customBody ?? unflattenObject(forms.value[formId] ?? {}));\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error submitting form', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction setFormValue(formId: string, widgetId: string, value: unknown) {\n\t\tif (forms.value[formId] === undefined) {\n\t\t\tforms.value[formId] = {};\n\t\t}\n\n\t\tforms.value[formId][widgetId] = value === '' ? null : value;\n\t}\n\n\tfunction setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {\n\t\tif (messages.value[formId] === undefined) {\n\t\t\tmessages.value[formId] = {};\n\t\t}\n\n\t\tmessages.value[formId][widgetId] = value;\n\t}\n\n\tprovide<LoginContext>(STRIVACITY_LOGIN_CONTEXT, {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t});\n\n\treturn {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t};\n}\n"],"mappings":"+KAOA,IAAa,EAAiG,OAAO,gBAAgB,EACxH,EAAuD,OAAO,0BAA0B,EASxF,MAES,CACrB,IAAM,GAAA,EAAU,EAAA,OAAA,CAAsB,CAAc,EAEpD,GAAI,CAAC,EACJ,MAAU,MAAM,qBAAqB,EAGtC,OAAO,CACR,EAQa,MAA4C,CACxD,IAAM,GAAA,EAAU,EAAA,OAAA,CAAqB,CAAwB,EAE7D,GAAI,CAAC,EACJ,MAAU,MAAM,kCAAkC,EAGnD,OAAO,CACR,EASA,SAAgB,EAAe,EAAiC,CAAC,EAA6C,CAC7G,IAAM,EAAkB,IAAI,gBACtB,CAAE,OAAQ,EAA0B,EAEpC,GAAA,EAAU,EAAA,IAAA,CAAa,EAAI,EAC3B,GAAA,EAAQ,EAAA,IAAA,CAA6C,CAAC,CAAC,EACvD,GAAA,EAAW,EAAA,IAAA,CAAuD,CAAC,CAAC,EACpE,GAAA,EAAQ,EAAA,IAAA,CAA8B,CAAC,CAAC,GAE9C,EAAA,EAAA,UAAA,CAAU,SAAY,CACrB,MAAM,EAAI,KAAK,EAEf,GAAI,CACH,MAAM,EAAa,EAAQ,MAAM,CAClC,OAAS,EAAO,CACf,GAAI,aAAiB,cAAgB,EAAM,OAAS,aACnD,OAGD,EAAI,SAAS,MAAM,mCAAoC,CAAK,EAC5D,EAAa,UAAU,CAAK,EAC5B,EAAQ,MAAQ,EACjB,CACD,CAAC,GAED,EAAA,EAAA,YAAA,KAAkB,EAAgB,MAAM,CAAC,EAEzC,SAAS,EAAe,EAAqC,CAC5D,GAAI,EAAI,
|
|
1
|
+
{"version":3,"file":"composables.cjs","names":[],"sources":["../src/composables.ts"],"sourcesContent":["import type { InjectionKey } from 'vue';\nimport type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';\nimport type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';\nimport { provide, inject, ref, onMounted, onUnmounted } from 'vue';\nimport { FallbackError } from '@strivacity/sdk-core/utils/errors';\nimport { isSessionExpired, unflattenObject } from '@strivacity/sdk-core/utils';\n\nexport const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');\nexport const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');\n\n/**\n * A composable to access the Strivacity SDK context.\n *\n * @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).\n * @returns {SDKContext<T>} - The Strivacity SDK context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useStrivacity = <\n\tT extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,\n>(): SDKContext<T> => {\n\tconst context = inject<SDKContext<T>>(STRIVACITY_SDK);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable to access the Strivacity SDK context specifically for native login flow.\n *\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useNativeLoginContext = (): LoginContext => {\n\tconst context = inject<LoginContext>(STRIVACITY_LOGIN_CONTEXT);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK native login context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable that manages a native login flow session.\n *\n * @param {UseNativeLoginOptions} [options] - Optional options for the login session.\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<typeof useNativeLoginContext> {\n\tconst abortController = new AbortController();\n\tconst { sdk } = useStrivacity<NativeFlow>();\n\n\tconst loading = ref<boolean>(true);\n\tconst forms = ref<Record<string, Record<string, unknown>>>({});\n\tconst messages = ref<Record<string, Record<string, NativeFlowMessage>>>({});\n\tconst state = ref<Partial<NativeFlowState>>({});\n\n\tonMounted(async () => {\n\t\tawait sdk.init();\n\n\t\ttry {\n\t\t\tawait startSession(options.params);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DOMException && error.name === 'AbortError') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsdk.logging?.error('Error fetching authorization URI', error);\n\t\t\tvoid options.onError?.(error);\n\t\t\tloading.value = false;\n\t\t}\n\t});\n\n\tonUnmounted(() => abortController.abort());\n\n\tfunction handleResponse(nextState: Partial<NativeFlowState>) {\n\t\tif (sdk.session && !isSessionExpired(sdk.session)) {\n\t\t\treturn void options.onLogin?.(sdk.session);\n\t\t}\n\n\t\tconst newState: NativeFlowState = {\n\t\t\thostedUrl: nextState?.hostedUrl ?? state.value.hostedUrl,\n\t\t\tfinalizeUrl: nextState?.finalizeUrl ?? state.value.finalizeUrl,\n\t\t\tscreen: nextState?.screen ?? state.value.screen,\n\t\t\tforms: nextState?.forms ?? state.value.forms,\n\t\t\tlayout: nextState?.layout ?? state.value.layout,\n\t\t\tmessages: nextState?.messages ?? {},\n\t\t\tbranding: nextState?.branding ?? state.value.branding,\n\t\t};\n\n\t\tif (newState.screen !== state.value.screen) {\n\t\t\tforms.value = {};\n\t\t\tmessages.value = {};\n\n\t\t\tfor (const form of newState.forms ?? []) {\n\t\t\t\tforms.value[form.id] = {};\n\t\t\t\tmessages.value[form.id] = {};\n\t\t\t}\n\t\t} else {\n\t\t\tsdk.logging?.info(`Updating screen: ${newState.screen}`);\n\t\t}\n\n\t\tfor (const formId of Object.keys(newState.messages ?? {})) {\n\t\t\tif (formId === 'global') {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n\t\t\t\tvoid options.onGlobalMessage?.(newState.messages?.global!);\n\t\t\t} else {\n\t\t\t\tmessages.value[formId] = newState.messages![formId] ?? {};\n\t\t\t}\n\t\t}\n\n\t\tstate.value = newState;\n\n\t\tif (!newState.finalizeUrl) {\n\t\t\tloading.value = false;\n\t\t}\n\t}\n\n\tasync function startSession(loginParams: NativeParams = {}) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.startSession(loginParams);\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error starting session', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction triggerFallback(message?: string) {\n\t\tsdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');\n\n\t\tif (!state.value.hostedUrl) {\n\t\t\tconst error = new Error('No hosted URL provided');\n\t\t\tsdk.logging?.error('Fallback error', error);\n\t\t\tthrow error;\n\t\t}\n\n\t\tvoid options.onFallback?.(new FallbackError(new URL(state.value.hostedUrl)));\n\t}\n\n\tfunction triggerClose() {\n\t\tsdk.logging?.debug('Triggering close');\n\t\tvoid options.onClose?.();\n\t}\n\n\tasync function submitForm(formId: string, customBody?: Record<string, unknown>) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.submitForm(formId, customBody ?? unflattenObject(forms.value[formId] ?? {}));\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error submitting form', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction setFormValue(formId: string, widgetId: string, value: unknown) {\n\t\tif (forms.value[formId] === undefined) {\n\t\t\tforms.value[formId] = {};\n\t\t}\n\n\t\tforms.value[formId][widgetId] = value === '' ? null : value;\n\t}\n\n\tfunction setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {\n\t\tif (messages.value[formId] === undefined) {\n\t\t\tmessages.value[formId] = {};\n\t\t}\n\n\t\tmessages.value[formId][widgetId] = value;\n\t}\n\n\tprovide<LoginContext>(STRIVACITY_LOGIN_CONTEXT, {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t});\n\n\treturn {\n\t\tloading,\n\t\tforms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EA,EAAa,aAAa,IAAI,EAAA,cAAc,IAAI,IAAI,EAAM,MAAM,SAAS,CAAC,CAAC,CAC5E,CAEA,SAAS,GAAe,CACvB,EAAI,SAAS,MAAM,kBAAkB,EACrC,EAAa,UAAU,CACxB,CAEA,eAAe,EAAW,EAAgB,EAAsC,CAC/E,GAAI,CACH,EAAQ,MAAQ,GAEhB,IAAM,EAAY,MAAM,EAAI,WAAW,EAAQ,IAAA,EAAc,EAAA,gBAAA,CAAgB,EAAM,MAAM,IAAW,CAAC,CAAC,CAAC,EAEnG,GACH,EAAe,CAAS,CAE1B,OAAS,EAAO,CACX,aAAiB,EAAA,eACpB,EAAI,SAAS,MAAM,0BAA2B,CAAK,EACnD,EAAa,aAAa,CAAK,IAE/B,EAAI,SAAS,MAAM,wBAAyB,CAAK,EACjD,EAAa,UAAU,CAAK,EAE9B,CACD,CAEA,SAAS,EAAa,EAAgB,EAAkB,EAAgB,CACnE,EAAM,MAAM,KAAY,IAAA,KAC3B,EAAM,MAAM,GAAU,CAAC,GAGxB,EAAM,MAAM,EAAO,CAAC,GAAY,IAAU,GAAK,KAAO,CACvD,CAEA,SAAS,EAAW,EAAgB,EAAkB,EAA0B,CAC3E,EAAS,MAAM,KAAY,IAAA,KAC9B,EAAS,MAAM,GAAU,CAAC,GAG3B,EAAS,MAAM,EAAO,CAAC,GAAY,CACpC,CAcA,OAZA,EAAA,EAAA,QAAA,CAAsB,EAA0B,CAC/C,UACA,QACA,WACA,QACA,kBACA,eACA,aACA,eACA,YACD,CAAC,EAEM,CACN,UACA,QACA,WACA,QACA,kBACA,eACA,aACA,eACA,YACD,CACD"}
|
package/dist/composables.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{inject as e,onMounted as t,onUnmounted as n,provide as r,ref as i}from"vue";import{FallbackError as a}from"@strivacity/sdk-core/utils/errors";import{unflattenObject as
|
|
1
|
+
import{inject as e,onMounted as t,onUnmounted as n,provide as r,ref as i}from"vue";import{FallbackError as a}from"@strivacity/sdk-core/utils/errors";import{isSessionExpired as o,unflattenObject as s}from"@strivacity/sdk-core/utils";var c=Symbol(`strivacity-sdk`),l=Symbol(`strivacity-login-context`),u=()=>{let t=e(c);if(!t)throw Error(`Missing SDK context`);return t},d=()=>{let t=e(l);if(!t)throw Error(`Missing SDK native login context`);return t};function f(e={}){let c=new AbortController,{sdk:d}=u(),f=i(!0),p=i({}),m=i({}),h=i({});t(async()=>{await d.init();try{await _(e.params)}catch(t){if(t instanceof DOMException&&t.name===`AbortError`)return;d.logging?.error(`Error fetching authorization URI`,t),e.onError?.(t),f.value=!1}}),n(()=>c.abort());function g(t){if(d.session&&!o(d.session)){e.onLogin?.(d.session);return}let n={hostedUrl:t?.hostedUrl??h.value.hostedUrl,finalizeUrl:t?.finalizeUrl??h.value.finalizeUrl,screen:t?.screen??h.value.screen,forms:t?.forms??h.value.forms,layout:t?.layout??h.value.layout,messages:t?.messages??{},branding:t?.branding??h.value.branding};if(n.screen!==h.value.screen){p.value={},m.value={};for(let e of n.forms??[])p.value[e.id]={},m.value[e.id]={}}else d.logging?.info(`Updating screen: ${n.screen}`);for(let t of Object.keys(n.messages??{}))t===`global`?e.onGlobalMessage?.(n.messages?.global):m.value[t]=n.messages[t]??{};h.value=n,n.finalizeUrl||(f.value=!1)}async function _(t={}){try{f.value=!0;let e=await d.startSession(t);e&&g(e)}catch(t){t instanceof a?(d.logging?.error(`Fallback error occurred`,t),e.onFallback?.(t)):(d.logging?.error(`Error starting session`,t),e.onError?.(t))}}function v(t){if(d.logging?.warn(t?`Triggering fallback due to: ${t}`:`Triggering fallback`),!h.value.hostedUrl){let e=Error(`No hosted URL provided`);throw d.logging?.error(`Fallback error`,e),e}e.onFallback?.(new a(new URL(h.value.hostedUrl)))}function y(){d.logging?.debug(`Triggering close`),e.onClose?.()}async function b(t,n){try{f.value=!0;let e=await d.submitForm(t,n??s(p.value[t]??{}));e&&g(e)}catch(t){t instanceof a?(d.logging?.error(`Fallback error occurred`,t),e.onFallback?.(t)):(d.logging?.error(`Error submitting form`,t),e.onError?.(t))}}function x(e,t,n){p.value[e]===void 0&&(p.value[e]={}),p.value[e][t]=n===``?null:n}function S(e,t,n){m.value[e]===void 0&&(m.value[e]={}),m.value[e][t]=n}return r(l,{loading:f,forms:p,messages:m,state:h,triggerFallback:v,triggerClose:y,submitForm:b,setFormValue:x,setMessage:S}),{loading:f,forms:p,messages:m,state:h,triggerFallback:v,triggerClose:y,submitForm:b,setFormValue:x,setMessage:S}}export{l as STRIVACITY_LOGIN_CONTEXT,c as STRIVACITY_SDK,f as useNativeLogin,d as useNativeLoginContext,u as useStrivacity};
|
|
2
2
|
//# sourceMappingURL=composables.mjs.map
|
package/dist/composables.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"composables.mjs","names":[],"sources":["../src/composables.ts"],"sourcesContent":["import type { InjectionKey } from 'vue';\nimport type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';\nimport type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';\nimport { provide, inject, ref, onMounted, onUnmounted } from 'vue';\nimport { FallbackError } from '@strivacity/sdk-core/utils/errors';\nimport { unflattenObject } from '@strivacity/sdk-core/utils';\n\nexport const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');\nexport const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');\n\n/**\n * A composable to access the Strivacity SDK context.\n *\n * @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).\n * @returns {SDKContext<T>} - The Strivacity SDK context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useStrivacity = <\n\tT extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,\n>(): SDKContext<T> => {\n\tconst context = inject<SDKContext<T>>(STRIVACITY_SDK);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable to access the Strivacity SDK context specifically for native login flow.\n *\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useNativeLoginContext = (): LoginContext => {\n\tconst context = inject<LoginContext>(STRIVACITY_LOGIN_CONTEXT);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK native login context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable that manages a native login flow session.\n *\n * @param {UseNativeLoginOptions} [options] - Optional options for the login session.\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<typeof useNativeLoginContext> {\n\tconst abortController = new AbortController();\n\tconst { sdk } = useStrivacity<NativeFlow>();\n\n\tconst loading = ref<boolean>(true);\n\tconst forms = ref<Record<string, Record<string, unknown>>>({});\n\tconst messages = ref<Record<string, Record<string, NativeFlowMessage>>>({});\n\tconst state = ref<Partial<NativeFlowState>>({});\n\n\tonMounted(async () => {\n\t\tawait sdk.init();\n\n\t\ttry {\n\t\t\tawait startSession(options.params);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DOMException && error.name === 'AbortError') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsdk.logging?.error('Error fetching authorization URI', error);\n\t\t\tvoid options.onError?.(error);\n\t\t\tloading.value = false;\n\t\t}\n\t});\n\n\tonUnmounted(() => abortController.abort());\n\n\tfunction handleResponse(nextState: Partial<NativeFlowState>) {\n\t\tif (sdk.session) {\n\t\t\treturn void options.onLogin?.(sdk.session);\n\t\t}\n\n\t\tconst newState: NativeFlowState = {\n\t\t\thostedUrl: nextState?.hostedUrl ?? state.value.hostedUrl,\n\t\t\tfinalizeUrl: nextState?.finalizeUrl ?? state.value.finalizeUrl,\n\t\t\tscreen: nextState?.screen ?? state.value.screen,\n\t\t\tforms: nextState?.forms ?? state.value.forms,\n\t\t\tlayout: nextState?.layout ?? state.value.layout,\n\t\t\tmessages: nextState?.messages ?? {},\n\t\t\tbranding: nextState?.branding ?? state.value.branding,\n\t\t};\n\n\t\tif (newState.screen !== state.value.screen) {\n\t\t\tforms.value = {};\n\t\t\tmessages.value = {};\n\n\t\t\tfor (const form of newState.forms ?? []) {\n\t\t\t\tforms.value[form.id] = {};\n\t\t\t\tmessages.value[form.id] = {};\n\t\t\t}\n\t\t} else {\n\t\t\tsdk.logging?.info(`Updating screen: ${newState.screen}`);\n\t\t}\n\n\t\tfor (const formId of Object.keys(newState.messages ?? {})) {\n\t\t\tif (formId === 'global') {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n\t\t\t\tvoid options.onGlobalMessage?.(newState.messages?.global!);\n\t\t\t} else {\n\t\t\t\tmessages.value[formId] = newState.messages![formId] ?? {};\n\t\t\t}\n\t\t}\n\n\t\tstate.value = newState;\n\n\t\tif (!newState.finalizeUrl) {\n\t\t\tloading.value = false;\n\t\t}\n\t}\n\n\tasync function startSession(loginParams: NativeParams = {}) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.startSession(loginParams);\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error starting session', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction triggerFallback(message?: string) {\n\t\tsdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');\n\n\t\tif (!state.value.hostedUrl) {\n\t\t\tconst error = new Error('No hosted URL provided');\n\t\t\tsdk.logging?.error('Fallback error', error);\n\t\t\tthrow error;\n\t\t}\n\n\t\tvoid options.onFallback?.(new FallbackError(new URL(state.value.hostedUrl)));\n\t}\n\n\tfunction triggerClose() {\n\t\tsdk.logging?.debug('Triggering close');\n\t\tvoid options.onClose?.();\n\t}\n\n\tasync function submitForm(formId: string, customBody?: Record<string, unknown>) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.submitForm(formId, customBody ?? unflattenObject(forms.value[formId] ?? {}));\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error submitting form', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction setFormValue(formId: string, widgetId: string, value: unknown) {\n\t\tif (forms.value[formId] === undefined) {\n\t\t\tforms.value[formId] = {};\n\t\t}\n\n\t\tforms.value[formId][widgetId] = value === '' ? null : value;\n\t}\n\n\tfunction setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {\n\t\tif (messages.value[formId] === undefined) {\n\t\t\tmessages.value[formId] = {};\n\t\t}\n\n\t\tmessages.value[formId][widgetId] = value;\n\t}\n\n\tprovide<LoginContext>(STRIVACITY_LOGIN_CONTEXT, {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t});\n\n\treturn {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t};\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"composables.mjs","names":[],"sources":["../src/composables.ts"],"sourcesContent":["import type { InjectionKey } from 'vue';\nimport type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';\nimport type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';\nimport { provide, inject, ref, onMounted, onUnmounted } from 'vue';\nimport { FallbackError } from '@strivacity/sdk-core/utils/errors';\nimport { isSessionExpired, unflattenObject } from '@strivacity/sdk-core/utils';\n\nexport const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');\nexport const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');\n\n/**\n * A composable to access the Strivacity SDK context.\n *\n * @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).\n * @returns {SDKContext<T>} - The Strivacity SDK context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useStrivacity = <\n\tT extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,\n>(): SDKContext<T> => {\n\tconst context = inject<SDKContext<T>>(STRIVACITY_SDK);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable to access the Strivacity SDK context specifically for native login flow.\n *\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport const useNativeLoginContext = (): LoginContext => {\n\tconst context = inject<LoginContext>(STRIVACITY_LOGIN_CONTEXT);\n\n\tif (!context) {\n\t\tthrow new Error('Missing SDK native login context');\n\t}\n\n\treturn context;\n};\n\n/**\n * A composable that manages a native login flow session.\n *\n * @param {UseNativeLoginOptions} [options] - Optional options for the login session.\n * @returns {LoginContext} - The native flow context.\n * @throws {Error} - If the Strivacity SDK context is not found.\n */\nexport function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<typeof useNativeLoginContext> {\n\tconst abortController = new AbortController();\n\tconst { sdk } = useStrivacity<NativeFlow>();\n\n\tconst loading = ref<boolean>(true);\n\tconst forms = ref<Record<string, Record<string, unknown>>>({});\n\tconst messages = ref<Record<string, Record<string, NativeFlowMessage>>>({});\n\tconst state = ref<Partial<NativeFlowState>>({});\n\n\tonMounted(async () => {\n\t\tawait sdk.init();\n\n\t\ttry {\n\t\t\tawait startSession(options.params);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DOMException && error.name === 'AbortError') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsdk.logging?.error('Error fetching authorization URI', error);\n\t\t\tvoid options.onError?.(error);\n\t\t\tloading.value = false;\n\t\t}\n\t});\n\n\tonUnmounted(() => abortController.abort());\n\n\tfunction handleResponse(nextState: Partial<NativeFlowState>) {\n\t\tif (sdk.session && !isSessionExpired(sdk.session)) {\n\t\t\treturn void options.onLogin?.(sdk.session);\n\t\t}\n\n\t\tconst newState: NativeFlowState = {\n\t\t\thostedUrl: nextState?.hostedUrl ?? state.value.hostedUrl,\n\t\t\tfinalizeUrl: nextState?.finalizeUrl ?? state.value.finalizeUrl,\n\t\t\tscreen: nextState?.screen ?? state.value.screen,\n\t\t\tforms: nextState?.forms ?? state.value.forms,\n\t\t\tlayout: nextState?.layout ?? state.value.layout,\n\t\t\tmessages: nextState?.messages ?? {},\n\t\t\tbranding: nextState?.branding ?? state.value.branding,\n\t\t};\n\n\t\tif (newState.screen !== state.value.screen) {\n\t\t\tforms.value = {};\n\t\t\tmessages.value = {};\n\n\t\t\tfor (const form of newState.forms ?? []) {\n\t\t\t\tforms.value[form.id] = {};\n\t\t\t\tmessages.value[form.id] = {};\n\t\t\t}\n\t\t} else {\n\t\t\tsdk.logging?.info(`Updating screen: ${newState.screen}`);\n\t\t}\n\n\t\tfor (const formId of Object.keys(newState.messages ?? {})) {\n\t\t\tif (formId === 'global') {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n\t\t\t\tvoid options.onGlobalMessage?.(newState.messages?.global!);\n\t\t\t} else {\n\t\t\t\tmessages.value[formId] = newState.messages![formId] ?? {};\n\t\t\t}\n\t\t}\n\n\t\tstate.value = newState;\n\n\t\tif (!newState.finalizeUrl) {\n\t\t\tloading.value = false;\n\t\t}\n\t}\n\n\tasync function startSession(loginParams: NativeParams = {}) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.startSession(loginParams);\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error starting session', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction triggerFallback(message?: string) {\n\t\tsdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');\n\n\t\tif (!state.value.hostedUrl) {\n\t\t\tconst error = new Error('No hosted URL provided');\n\t\t\tsdk.logging?.error('Fallback error', error);\n\t\t\tthrow error;\n\t\t}\n\n\t\tvoid options.onFallback?.(new FallbackError(new URL(state.value.hostedUrl)));\n\t}\n\n\tfunction triggerClose() {\n\t\tsdk.logging?.debug('Triggering close');\n\t\tvoid options.onClose?.();\n\t}\n\n\tasync function submitForm(formId: string, customBody?: Record<string, unknown>) {\n\t\ttry {\n\t\t\tloading.value = true;\n\n\t\t\tconst nextState = await sdk.submitForm(formId, customBody ?? unflattenObject(forms.value[formId] ?? {}));\n\n\t\t\tif (nextState) {\n\t\t\t\thandleResponse(nextState);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (error instanceof FallbackError) {\n\t\t\t\tsdk.logging?.error('Fallback error occurred', error);\n\t\t\t\tvoid options.onFallback?.(error);\n\t\t\t} else {\n\t\t\t\tsdk.logging?.error('Error submitting form', error);\n\t\t\t\tvoid options.onError?.(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction setFormValue(formId: string, widgetId: string, value: unknown) {\n\t\tif (forms.value[formId] === undefined) {\n\t\t\tforms.value[formId] = {};\n\t\t}\n\n\t\tforms.value[formId][widgetId] = value === '' ? null : value;\n\t}\n\n\tfunction setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {\n\t\tif (messages.value[formId] === undefined) {\n\t\t\tmessages.value[formId] = {};\n\t\t}\n\n\t\tmessages.value[formId][widgetId] = value;\n\t}\n\n\tprovide<LoginContext>(STRIVACITY_LOGIN_CONTEXT, {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t});\n\n\treturn {\n\t\tloading,\n\t\tforms,\n\t\tmessages,\n\t\tstate,\n\t\ttriggerFallback,\n\t\ttriggerClose,\n\t\tsubmitForm,\n\t\tsetFormValue,\n\t\tsetMessage,\n\t};\n}\n"],"mappings":"wOAOA,IAAa,EAAiG,OAAO,gBAAgB,EACxH,EAAuD,OAAO,0BAA0B,EASxF,MAES,CACrB,IAAM,EAAU,EAAsB,CAAc,EAEpD,GAAI,CAAC,EACJ,MAAU,MAAM,qBAAqB,EAGtC,OAAO,CACR,EAQa,MAA4C,CACxD,IAAM,EAAU,EAAqB,CAAwB,EAE7D,GAAI,CAAC,EACJ,MAAU,MAAM,kCAAkC,EAGnD,OAAO,CACR,EASA,SAAgB,EAAe,EAAiC,CAAC,EAA6C,CAC7G,IAAM,EAAkB,IAAI,gBACtB,CAAE,OAAQ,EAA0B,EAEpC,EAAU,EAAa,EAAI,EAC3B,EAAQ,EAA6C,CAAC,CAAC,EACvD,EAAW,EAAuD,CAAC,CAAC,EACpE,EAAQ,EAA8B,CAAC,CAAC,EAE9C,EAAU,SAAY,CACrB,MAAM,E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AZA,EAAsB,EAA0B,CAC/C,UACA,QACA,WACA,QACA,kBACA,eACA,aACA,eACA,YACD,CAAC,EAEM,CACN,UACA,QACA,WACA,QACA,kBACA,eACA,aACA,eACA,YACD,CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strivacity/sdk-vue",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.1",
|
|
4
4
|
"description": "Strivacity Vue SDK client",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "strivacity <opensource@strivacity.com>",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@strivacity/sdk-core": "4.0.0-beta.
|
|
59
|
+
"@strivacity/sdk-core": "4.0.0-beta.1"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"vue": ">=3.3"
|
package/src/composables.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, N
|
|
|
3
3
|
import type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';
|
|
4
4
|
import { provide, inject, ref, onMounted, onUnmounted } from 'vue';
|
|
5
5
|
import { FallbackError } from '@strivacity/sdk-core/utils/errors';
|
|
6
|
-
import { unflattenObject } from '@strivacity/sdk-core/utils';
|
|
6
|
+
import { isSessionExpired, unflattenObject } from '@strivacity/sdk-core/utils';
|
|
7
7
|
|
|
8
8
|
export const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');
|
|
9
9
|
export const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');
|
|
@@ -78,7 +78,7 @@ export function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<
|
|
|
78
78
|
onUnmounted(() => abortController.abort());
|
|
79
79
|
|
|
80
80
|
function handleResponse(nextState: Partial<NativeFlowState>) {
|
|
81
|
-
if (sdk.session) {
|
|
81
|
+
if (sdk.session && !isSessionExpired(sdk.session)) {
|
|
82
82
|
return void options.onLogin?.(sdk.session);
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -156,7 +156,7 @@ describe('useNativeLogin', () => {
|
|
|
156
156
|
});
|
|
157
157
|
|
|
158
158
|
test('calls onLogin and leaves the tracked state untouched when the sdk already has a session', async () => {
|
|
159
|
-
const session = { access_token: 'access-token' } as unknown as SessionData;
|
|
159
|
+
const session = { access_token: 'access-token', expires_at: Math.floor(Date.now() / 1000) + 3600 } as unknown as SessionData;
|
|
160
160
|
const sdk = createMockFlow<NativeFlow>({ session });
|
|
161
161
|
vi.mocked(sdk.startSession).mockResolvedValue({ screen: 'identifier' });
|
|
162
162
|
const onLogin = vi.fn();
|