@strivacity/sdk-vue 3.0.2 → 4.0.0-beta.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.
Files changed (61) hide show
  1. package/CHANGELOG.md +20 -18
  2. package/README.md +1402 -419
  3. package/dist/composables.cjs +2 -2
  4. package/dist/composables.cjs.map +1 -1
  5. package/dist/composables.d.ts +23 -8
  6. package/dist/composables.mjs +2 -2
  7. package/dist/composables.mjs.map +1 -1
  8. package/dist/errors.cjs +1 -0
  9. package/dist/errors.d.ts +1 -0
  10. package/dist/errors.mjs +1 -0
  11. package/dist/index.cjs +1 -2
  12. package/dist/index.d.ts +9 -31
  13. package/dist/index.mjs +1 -2
  14. package/dist/plugin.cjs +2 -0
  15. package/dist/plugin.cjs.map +1 -0
  16. package/dist/plugin.d.ts +8 -0
  17. package/dist/plugin.mjs +2 -0
  18. package/dist/plugin.mjs.map +1 -0
  19. package/dist/storages.cjs +1 -0
  20. package/dist/storages.d.ts +1 -0
  21. package/dist/storages.mjs +1 -0
  22. package/dist/types.cjs +1 -2
  23. package/dist/types.d.ts +106 -118
  24. package/dist/types.mjs +1 -2
  25. package/dist/utils.cjs +1 -0
  26. package/dist/utils.d.ts +1 -0
  27. package/dist/utils.mjs +1 -0
  28. package/eslint.config.mjs +3 -0
  29. package/package.json +45 -8
  30. package/project.json +46 -0
  31. package/src/composables.ts +219 -0
  32. package/src/env.d.ts +8 -0
  33. package/src/errors.ts +1 -0
  34. package/src/index.ts +9 -0
  35. package/src/plugin.ts +89 -0
  36. package/src/storages.ts +1 -0
  37. package/src/types.ts +220 -0
  38. package/src/utils.ts +1 -0
  39. package/testing/tests/composables.spec.ts +328 -0
  40. package/testing/tests/errors.spec.ts +10 -0
  41. package/testing/tests/index.spec.ts +105 -0
  42. package/testing/tests/plugin.spec.ts +129 -0
  43. package/testing/tests/storages.spec.ts +10 -0
  44. package/testing/tests/utils.spec.ts +10 -0
  45. package/testing/utils/common.ts +53 -0
  46. package/tsconfig.app.json +4 -0
  47. package/tsconfig.json +3 -0
  48. package/vite.config.mts +53 -0
  49. package/dist/assets/login-renderer.vue_vue_type_script_setup_true_lang.cjs +0 -2
  50. package/dist/assets/login-renderer.vue_vue_type_script_setup_true_lang.cjs.map +0 -1
  51. package/dist/assets/login-renderer.vue_vue_type_script_setup_true_lang.mjs +0 -2
  52. package/dist/assets/login-renderer.vue_vue_type_script_setup_true_lang.mjs.map +0 -1
  53. package/dist/index.cjs.map +0 -1
  54. package/dist/index.mjs.map +0 -1
  55. package/dist/login-renderer.cjs +0 -2
  56. package/dist/login-renderer.cjs.map +0 -1
  57. package/dist/login-renderer.mjs +0 -2
  58. package/dist/login-renderer.mjs.map +0 -1
  59. package/dist/login-renderer.vue.d.ts +0 -37
  60. package/dist/types.cjs.map +0 -1
  61. package/dist/types.mjs.map +0 -1
package/package.json CHANGED
@@ -1,19 +1,14 @@
1
1
  {
2
2
  "name": "@strivacity/sdk-vue",
3
- "version": "3.0.2",
3
+ "version": "4.0.0-beta.0",
4
+ "description": "Strivacity Vue SDK client",
4
5
  "license": "MIT",
5
- "description": "Strivacity Vue.js SDK client",
6
6
  "author": "strivacity <opensource@strivacity.com>",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "https://github.com/Strivacity/sdk-js"
10
10
  },
11
- "dependencies": {
12
- "@strivacity/sdk-core": "3.0.2"
13
- },
14
- "peerDependencies": {
15
- "vue": ">=3"
16
- },
11
+ "sideEffects": false,
17
12
  "main": "./dist/index.cjs",
18
13
  "types": "./dist/index.d.ts",
19
14
  "exports": {
@@ -22,6 +17,48 @@
22
17
  "import": "./dist/index.mjs",
23
18
  "require": "./dist/index.cjs",
24
19
  "default": "./dist/index.mjs"
20
+ },
21
+ "./composables": {
22
+ "types": "./dist/composables.d.ts",
23
+ "import": "./dist/composables.mjs",
24
+ "require": "./dist/composables.cjs",
25
+ "default": "./dist/composables.mjs"
26
+ },
27
+ "./errors": {
28
+ "types": "./dist/errors.d.ts",
29
+ "import": "./dist/errors.mjs",
30
+ "require": "./dist/errors.cjs",
31
+ "default": "./dist/errors.mjs"
32
+ },
33
+ "./plugin": {
34
+ "types": "./dist/plugin.d.ts",
35
+ "import": "./dist/plugin.mjs",
36
+ "require": "./dist/plugin.cjs",
37
+ "default": "./dist/plugin.mjs"
38
+ },
39
+ "./storages": {
40
+ "types": "./dist/storages.d.ts",
41
+ "import": "./dist/storages.mjs",
42
+ "require": "./dist/storages.cjs",
43
+ "default": "./dist/storages.mjs"
44
+ },
45
+ "./types": {
46
+ "types": "./dist/types.d.ts",
47
+ "import": "./dist/types.mjs",
48
+ "require": "./dist/types.cjs",
49
+ "default": "./dist/types.mjs"
50
+ },
51
+ "./utils": {
52
+ "types": "./dist/utils.d.ts",
53
+ "import": "./dist/utils.mjs",
54
+ "require": "./dist/utils.cjs",
55
+ "default": "./dist/utils.mjs"
25
56
  }
57
+ },
58
+ "dependencies": {
59
+ "@strivacity/sdk-core": "4.0.0-beta.0"
60
+ },
61
+ "peerDependencies": {
62
+ "vue": ">=3.3"
26
63
  }
27
64
  }
package/project.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
3
+ "name": "sdk-vue",
4
+ "projectType": "library",
5
+ "sourceRoot": "packages/sdk-vue/src",
6
+ "targets": {
7
+ "type-check": {
8
+ "executor": "nx:run-commands",
9
+ "dependsOn": ["sdk-core:build"],
10
+ "options": {
11
+ "command": "tsc --noEmit --project packages/sdk-vue/tsconfig.app.json"
12
+ }
13
+ },
14
+ "build": {
15
+ "executor": "nx:run-commands",
16
+ "dependsOn": ["type-check"],
17
+ "outputs": ["{workspaceRoot}/packages/sdk-vue/dist"],
18
+ "defaultConfiguration": "development",
19
+ "options": {
20
+ "cwd": "packages/sdk-vue"
21
+ },
22
+ "configurations": {
23
+ "serve": {
24
+ "mode": "development",
25
+ "command": "vite build --watch"
26
+ },
27
+ "development": {
28
+ "mode": "development",
29
+ "command": "vite build"
30
+ },
31
+ "production": {
32
+ "mode": "production",
33
+ "command": "vite build"
34
+ }
35
+ }
36
+ },
37
+ "test": {
38
+ "executor": "nx:run-commands",
39
+ "dependsOn": ["build"],
40
+ "options": {
41
+ "cwd": "packages/sdk-vue",
42
+ "command": "vitest"
43
+ }
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,219 @@
1
+ import type { InjectionKey } from 'vue';
2
+ import type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';
3
+ import type { LoginContext, SDKContext, UseNativeLoginOptions } from './types';
4
+ import { provide, inject, ref, onMounted, onUnmounted } from 'vue';
5
+ import { FallbackError } from '@strivacity/sdk-core/utils/errors';
6
+ import { unflattenObject } from '@strivacity/sdk-core/utils';
7
+
8
+ export const STRIVACITY_SDK: InjectionKey<SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>> = Symbol('strivacity-sdk');
9
+ export const STRIVACITY_LOGIN_CONTEXT: InjectionKey<LoginContext> = Symbol('strivacity-login-context');
10
+
11
+ /**
12
+ * A composable to access the Strivacity SDK context.
13
+ *
14
+ * @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).
15
+ * @returns {SDKContext<T>} - The Strivacity SDK context.
16
+ * @throws {Error} - If the Strivacity SDK context is not found.
17
+ */
18
+ export const useStrivacity = <
19
+ T extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,
20
+ >(): SDKContext<T> => {
21
+ const context = inject<SDKContext<T>>(STRIVACITY_SDK);
22
+
23
+ if (!context) {
24
+ throw new Error('Missing SDK context');
25
+ }
26
+
27
+ return context;
28
+ };
29
+
30
+ /**
31
+ * A composable to access the Strivacity SDK context specifically for native login flow.
32
+ *
33
+ * @returns {LoginContext} - The native flow context.
34
+ * @throws {Error} - If the Strivacity SDK context is not found.
35
+ */
36
+ export const useNativeLoginContext = (): LoginContext => {
37
+ const context = inject<LoginContext>(STRIVACITY_LOGIN_CONTEXT);
38
+
39
+ if (!context) {
40
+ throw new Error('Missing SDK native login context');
41
+ }
42
+
43
+ return context;
44
+ };
45
+
46
+ /**
47
+ * A composable that manages a native login flow session.
48
+ *
49
+ * @param {UseNativeLoginOptions} [options] - Optional options for the login session.
50
+ * @returns {LoginContext} - The native flow context.
51
+ * @throws {Error} - If the Strivacity SDK context is not found.
52
+ */
53
+ export function useNativeLogin(options: UseNativeLoginOptions = {}): ReturnType<typeof useNativeLoginContext> {
54
+ const abortController = new AbortController();
55
+ const { sdk } = useStrivacity<NativeFlow>();
56
+
57
+ const loading = ref<boolean>(true);
58
+ const forms = ref<Record<string, Record<string, unknown>>>({});
59
+ const messages = ref<Record<string, Record<string, NativeFlowMessage>>>({});
60
+ const state = ref<Partial<NativeFlowState>>({});
61
+
62
+ onMounted(async () => {
63
+ await sdk.init();
64
+
65
+ try {
66
+ await startSession(options.params);
67
+ } catch (error) {
68
+ if (error instanceof DOMException && error.name === 'AbortError') {
69
+ return;
70
+ }
71
+
72
+ sdk.logging?.error('Error fetching authorization URI', error);
73
+ void options.onError?.(error);
74
+ loading.value = false;
75
+ }
76
+ });
77
+
78
+ onUnmounted(() => abortController.abort());
79
+
80
+ function handleResponse(nextState: Partial<NativeFlowState>) {
81
+ if (sdk.session) {
82
+ return void options.onLogin?.(sdk.session);
83
+ }
84
+
85
+ const newState: NativeFlowState = {
86
+ hostedUrl: nextState?.hostedUrl ?? state.value.hostedUrl,
87
+ finalizeUrl: nextState?.finalizeUrl ?? state.value.finalizeUrl,
88
+ screen: nextState?.screen ?? state.value.screen,
89
+ forms: nextState?.forms ?? state.value.forms,
90
+ layout: nextState?.layout ?? state.value.layout,
91
+ messages: nextState?.messages ?? {},
92
+ branding: nextState?.branding ?? state.value.branding,
93
+ };
94
+
95
+ if (newState.screen !== state.value.screen) {
96
+ forms.value = {};
97
+ messages.value = {};
98
+
99
+ for (const form of newState.forms ?? []) {
100
+ forms.value[form.id] = {};
101
+ messages.value[form.id] = {};
102
+ }
103
+ } else {
104
+ sdk.logging?.info(`Updating screen: ${newState.screen}`);
105
+ }
106
+
107
+ for (const formId of Object.keys(newState.messages ?? {})) {
108
+ if (formId === 'global') {
109
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
110
+ void options.onGlobalMessage?.(newState.messages?.global!);
111
+ } else {
112
+ messages.value[formId] = newState.messages![formId] ?? {};
113
+ }
114
+ }
115
+
116
+ state.value = newState;
117
+
118
+ if (!newState.finalizeUrl) {
119
+ loading.value = false;
120
+ }
121
+ }
122
+
123
+ async function startSession(loginParams: NativeParams = {}) {
124
+ try {
125
+ loading.value = true;
126
+
127
+ const nextState = await sdk.startSession(loginParams);
128
+
129
+ if (nextState) {
130
+ handleResponse(nextState);
131
+ }
132
+ } catch (error) {
133
+ if (error instanceof FallbackError) {
134
+ sdk.logging?.error('Fallback error occurred', error);
135
+ void options.onFallback?.(error);
136
+ } else {
137
+ sdk.logging?.error('Error starting session', error);
138
+ void options.onError?.(error);
139
+ }
140
+ }
141
+ }
142
+
143
+ function triggerFallback(message?: string) {
144
+ sdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');
145
+
146
+ if (!state.value.hostedUrl) {
147
+ const error = new Error('No hosted URL provided');
148
+ sdk.logging?.error('Fallback error', error);
149
+ throw error;
150
+ }
151
+
152
+ void options.onFallback?.(new FallbackError(new URL(state.value.hostedUrl)));
153
+ }
154
+
155
+ function triggerClose() {
156
+ sdk.logging?.debug('Triggering close');
157
+ void options.onClose?.();
158
+ }
159
+
160
+ async function submitForm(formId: string, customBody?: Record<string, unknown>) {
161
+ try {
162
+ loading.value = true;
163
+
164
+ const nextState = await sdk.submitForm(formId, customBody ?? unflattenObject(forms.value[formId] ?? {}));
165
+
166
+ if (nextState) {
167
+ handleResponse(nextState);
168
+ }
169
+ } catch (error) {
170
+ if (error instanceof FallbackError) {
171
+ sdk.logging?.error('Fallback error occurred', error);
172
+ void options.onFallback?.(error);
173
+ } else {
174
+ sdk.logging?.error('Error submitting form', error);
175
+ void options.onError?.(error);
176
+ }
177
+ }
178
+ }
179
+
180
+ function setFormValue(formId: string, widgetId: string, value: unknown) {
181
+ if (forms.value[formId] === undefined) {
182
+ forms.value[formId] = {};
183
+ }
184
+
185
+ forms.value[formId][widgetId] = value === '' ? null : value;
186
+ }
187
+
188
+ function setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {
189
+ if (messages.value[formId] === undefined) {
190
+ messages.value[formId] = {};
191
+ }
192
+
193
+ messages.value[formId][widgetId] = value;
194
+ }
195
+
196
+ provide<LoginContext>(STRIVACITY_LOGIN_CONTEXT, {
197
+ loading,
198
+ forms,
199
+ messages,
200
+ state,
201
+ triggerFallback,
202
+ triggerClose,
203
+ submitForm,
204
+ setFormValue,
205
+ setMessage,
206
+ });
207
+
208
+ return {
209
+ loading,
210
+ forms,
211
+ messages,
212
+ state,
213
+ triggerFallback,
214
+ triggerClose,
215
+ submitForm,
216
+ setFormValue,
217
+ setMessage,
218
+ };
219
+ }
package/src/env.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /// <reference types="vite/client" />
2
+
3
+ declare module '*.vue' {
4
+ import type { DefineComponent } from 'vue';
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ const component: DefineComponent<object, object, any>;
7
+ export default component;
8
+ }
package/src/errors.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@strivacity/sdk-core/utils/errors';
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from '@strivacity/sdk-core/flows';
2
+ export * from '@strivacity/sdk-core/handlers';
3
+ export * from '@strivacity/sdk-core/utils';
4
+ export * from './composables';
5
+ export * from './errors';
6
+ export * from './plugin';
7
+ export * from './storages';
8
+ export * from './types';
9
+ export * from './utils';
package/src/plugin.ts ADDED
@@ -0,0 +1,89 @@
1
+ import type { IdTokenClaims, SDKInitConfig } from '@strivacity/sdk-core/types';
2
+ import type { SDKInstance } from './types';
3
+ import { type App, type Plugin, ref } from 'vue';
4
+ import { initFlow } from '@strivacity/sdk-core';
5
+ import { STRIVACITY_SDK } from './composables';
6
+
7
+ let sdk: SDKInstance;
8
+
9
+ /**
10
+ * Creates a Strivacity SDK plugin for Vue.
11
+ *
12
+ * @param {SDKOptions} options - The options used to configure the SDK.
13
+ */
14
+ export const createStrivacitySDK = (options: SDKInitConfig): Plugin => {
15
+ const loadingRef = ref<boolean>(true);
16
+ const languageRef = ref<string>(globalThis.navigator?.language ?? 'en-US');
17
+ const isAuthenticatedRef = ref<boolean>(false);
18
+ const idTokenClaimsRef = ref<IdTokenClaims | null>(null);
19
+ const accessTokenRef = ref<string | null>(null);
20
+ const refreshTokenRef = ref<string | null>(null);
21
+ const accessTokenExpiredRef = ref<boolean>(true);
22
+ const accessTokenExpirationDateRef = ref<number | null>(null);
23
+
24
+ const updateSession = async () => {
25
+ const authenticated = await sdk.isAuthenticated;
26
+
27
+ if (loadingRef.value) {
28
+ loadingRef.value = false;
29
+ }
30
+ if (sdk.language !== languageRef.value) {
31
+ languageRef.value = sdk.language;
32
+ }
33
+ if (authenticated !== isAuthenticatedRef.value) {
34
+ isAuthenticatedRef.value = authenticated;
35
+ }
36
+ if (sdk.idTokenClaims !== idTokenClaimsRef.value) {
37
+ idTokenClaimsRef.value = sdk.idTokenClaims || null;
38
+ }
39
+ if (sdk.accessToken !== accessTokenRef.value) {
40
+ accessTokenRef.value = sdk.accessToken || null;
41
+ }
42
+ if (sdk.refreshToken !== refreshTokenRef.value) {
43
+ refreshTokenRef.value = sdk.refreshToken || null;
44
+ }
45
+ if (sdk.accessTokenExpired !== accessTokenExpiredRef.value) {
46
+ accessTokenExpiredRef.value = sdk.accessTokenExpired;
47
+ }
48
+ if (sdk.accessTokenExpirationDate !== accessTokenExpirationDateRef.value) {
49
+ accessTokenExpirationDateRef.value = sdk.accessTokenExpirationDate || null;
50
+ }
51
+ };
52
+
53
+ const plugin = {
54
+ install: (app: App) => {
55
+ sdk = initFlow(options as never);
56
+ const sdkEvents = sdk.subscribeToAllEvents(updateSession);
57
+
58
+ void updateSession();
59
+
60
+ app.onUnmount(() => sdkEvents.dispose());
61
+
62
+ app.provide(STRIVACITY_SDK, {
63
+ sdk,
64
+ loading: loadingRef,
65
+ language: languageRef,
66
+ isAuthenticated: isAuthenticatedRef,
67
+ idTokenClaims: idTokenClaimsRef,
68
+ accessToken: accessTokenRef,
69
+ refreshToken: refreshTokenRef,
70
+ accessTokenExpired: accessTokenExpiredRef,
71
+ accessTokenExpirationDate: accessTokenExpirationDateRef,
72
+ init: () => sdk.init(),
73
+ subscribeToEvent: ((...args: Parameters<typeof sdk.subscribeToEvent>) => sdk.subscribeToEvent(...args)) as typeof sdk.subscribeToEvent,
74
+ subscribeToAllEvents: (...args: Parameters<typeof sdk.subscribeToAllEvents>) => sdk.subscribeToAllEvents(...args),
75
+ checkAuthentication: (...args: Parameters<typeof sdk.checkAuthentication>) => sdk.checkAuthentication(...args),
76
+ tokenExchange: (...args: Parameters<typeof sdk.tokenExchange>) => sdk.tokenExchange(...args),
77
+ handleCallback: (...args: Parameters<typeof sdk.handleCallback>) => sdk.handleCallback(...args),
78
+ refresh: () => sdk.refresh(),
79
+ revoke: () => sdk.revoke(),
80
+ logout: (...args: Parameters<typeof sdk.logout>) => sdk.logout(...args),
81
+ login: (...args: Parameters<typeof sdk.login>) => sdk.login(...args),
82
+ register: (...args: Parameters<typeof sdk.register>) => sdk.register(...args),
83
+ entry: (...args: Parameters<typeof sdk.entry>) => sdk.entry(...args),
84
+ });
85
+ },
86
+ };
87
+
88
+ return plugin;
89
+ };
@@ -0,0 +1 @@
1
+ export * from '@strivacity/sdk-core/storages';
package/src/types.ts ADDED
@@ -0,0 +1,220 @@
1
+ import type { Ref } from 'vue';
2
+ import type {
3
+ IdTokenClaims,
4
+ RedirectFlow,
5
+ PopupFlow,
6
+ NativeFlow,
7
+ EmbeddedFlow,
8
+ initFlow,
9
+ NativeParams,
10
+ NativeFlowState,
11
+ SessionData,
12
+ NativeFlowMessage,
13
+ } from '@strivacity/sdk-core';
14
+ import type { FallbackError } from '@strivacity/sdk-core/utils';
15
+
16
+ export * from '@strivacity/sdk-core/types';
17
+
18
+ export type SDKContext<Flow extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow> = {
19
+ /**
20
+ * The underlying Strivacity SDK flow instance.
21
+ */
22
+ readonly sdk: Flow;
23
+
24
+ /**
25
+ * A boolean indicating whether the SDK is still loading.
26
+ */
27
+ readonly loading: Ref<boolean>;
28
+
29
+ /**
30
+ * BCP 47 language code representing the current language of the SDK.
31
+ */
32
+ readonly language: Ref<string>;
33
+
34
+ /**
35
+ * A boolean ref indicating whether the user is authenticated.
36
+ */
37
+ readonly isAuthenticated: Ref<boolean>;
38
+
39
+ /**
40
+ * The claims from the ID token, if available.
41
+ */
42
+ readonly idTokenClaims: Ref<IdTokenClaims | null>;
43
+
44
+ /**
45
+ * The access token, if available.
46
+ */
47
+ readonly accessToken: Ref<string | null>;
48
+
49
+ /**
50
+ * The refresh token, if available.
51
+ */
52
+ readonly refreshToken: Ref<string | null>;
53
+
54
+ /**
55
+ * A boolean ref indicating whether the access token has expired.
56
+ */
57
+ readonly accessTokenExpired: Ref<boolean>;
58
+
59
+ /**
60
+ * The expiration date of the access token, if available.
61
+ */
62
+ readonly accessTokenExpirationDate: Ref<number | null>;
63
+
64
+ /**
65
+ * Subscribes to a specific SDK event.
66
+ */
67
+ subscribeToEvent: Flow['subscribeToEvent'];
68
+
69
+ /**
70
+ * Subscribes to all SDK events.
71
+ */
72
+ subscribeToAllEvents: Flow['subscribeToAllEvents'];
73
+
74
+ /**
75
+ * Checks whether the user is currently authenticated, optionally triggering a token refresh.
76
+ */
77
+ checkAuthentication: Flow['checkAuthentication'];
78
+
79
+ /**
80
+ * Initializes the SDK, loading metadata and restoring any persisted session.
81
+ */
82
+ init: Flow['init'];
83
+
84
+ /**
85
+ * Performs a token exchange using the provided parameters.
86
+ */
87
+ tokenExchange: Flow['tokenExchange'];
88
+
89
+ /**
90
+ * Handles the authentication callback by parsing the response from the given URL.
91
+ */
92
+ handleCallback: Flow['handleCallback'];
93
+
94
+ /**
95
+ * Refreshes the current access token using the refresh token.
96
+ */
97
+ refresh: Flow['refresh'];
98
+
99
+ /**
100
+ * Revokes the current tokens.
101
+ */
102
+ revoke: Flow['revoke'];
103
+
104
+ /**
105
+ * Logs the user out and optionally redirects to a post-logout URI.
106
+ */
107
+ logout: Flow['logout'];
108
+
109
+ /**
110
+ * Initiates the login flow.
111
+ */
112
+ login: Flow['login'];
113
+
114
+ /**
115
+ * Initiates the registration flow.
116
+ */
117
+ register: Flow['register'];
118
+
119
+ /**
120
+ * Handles the entry point URL of the authentication flow, typically used for embedded or native modes.
121
+ */
122
+ entry: Flow['entry'];
123
+ };
124
+
125
+ export type SDKInstance = ReturnType<typeof initFlow>;
126
+
127
+ export type LoginContext = {
128
+ /**
129
+ * Whether a form submission or session initialization is in progress.
130
+ */
131
+ loading: Ref<boolean>;
132
+
133
+ /**
134
+ * Current form field values, keyed by form ID then widget ID.
135
+ */
136
+ forms: Ref<Record<string, Record<string, unknown>>>;
137
+
138
+ /**
139
+ * Current validation and info messages, keyed by form ID then widget ID.
140
+ */
141
+ messages: Ref<Record<string, Record<string, NativeFlowMessage>>>;
142
+
143
+ /**
144
+ * The current login flow state returned by the backend.
145
+ */
146
+ state: Ref<Partial<NativeFlowState>>;
147
+
148
+ /**
149
+ * Submits the form with the given ID and advances the login flow.
150
+ *
151
+ * @param formId - The ID of the form to submit.
152
+ * @param customBody - Optional custom body to send with the form submission. If not provided, the current form values will be used.
153
+ * @returns A promise that resolves when the form submission is complete.
154
+ */
155
+ submitForm: (formId: string, customBody?: Record<string, unknown>) => Promise<void>;
156
+
157
+ /**
158
+ * Redirects to the hosted login UI.
159
+ *
160
+ * @param message - Optional message to log before redirecting.
161
+ * @returns A promise that resolves when the redirect is initiated.
162
+ */
163
+ triggerFallback: (message?: string) => void;
164
+
165
+ /**
166
+ * Signals that the login flow was closed by the user.
167
+ */
168
+ triggerClose: () => void;
169
+
170
+ /**
171
+ * Updates a single field value within a form before submission.
172
+ *
173
+ * @param formId - The ID of the form containing the field.
174
+ * @param widgetId - The ID of the widget (field) to update.
175
+ * @param value - The new value to set for the field.
176
+ */
177
+ setFormValue: (formId: string, widgetId: string, value: unknown) => void;
178
+
179
+ /**
180
+ * Sets a validation or info message on a specific widget.
181
+ *
182
+ * @param formId - The ID of the form containing the widget.
183
+ * @param widgetId - The ID of the widget to set the message for.
184
+ * @param value - The message to set, which can be a string or a structured NativeFlowMessage.
185
+ */
186
+ setMessage: (formId: string, widgetId: string, value: NativeFlowMessage) => void;
187
+ };
188
+
189
+ export type UseNativeLoginOptions = {
190
+ /**
191
+ * Optional parameters to be passed to the login session request. These parameters will be sent to the `authorizationUri` endpoint.
192
+ */
193
+ params?: NativeParams;
194
+
195
+ /**
196
+ * A render function called when the user successfully completes the login flow.
197
+ * This can be used to perform any necessary actions after a successful login, such as redirecting the user or updating the application state. Defaults to `null`.
198
+ */
199
+ onLogin?: (session: SessionData) => void | Promise<void>;
200
+
201
+ /**
202
+ * Called when the fallback flow is triggered, typically due to an error or unsupported environment. Receives a `FallbackError` object containing the hosted URL to redirect to.
203
+ */
204
+ onFallback?: (error: FallbackError) => void | Promise<void>;
205
+
206
+ /**
207
+ * Called when the login flow is closed by the user. This can be used to perform any necessary cleanup or state updates in the application. Defaults to `null`.
208
+ */
209
+ onClose?: () => void | Promise<void>;
210
+
211
+ /**
212
+ * Called when an error occurs during the login session initialization or flow. Receives an `Error` object describing the issue.
213
+ */
214
+ onError?: (error: Error) => void | Promise<void>;
215
+
216
+ /**
217
+ * Called when a global message is received during the login flow. This can be used to display messages to the user or log them for debugging purposes.
218
+ */
219
+ onGlobalMessage?: (message: NativeFlowMessage) => void | Promise<void>;
220
+ };
package/src/utils.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@strivacity/sdk-core/utils';