From 9d37bf54ebd3c0440d3c8eaa794ce3b24fdcfd6e Mon Sep 17 00:00:00 2001 From: michaelyons Date: Mon, 24 Jun 2024 12:12:09 -0600 Subject: [PATCH 1/6] Add generic Userinfo to createFusionAuth for vue sdk --- .../sdk-vue/src/createFusionAuth/createFusionAuth.test.ts | 6 +++++- packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts index 7821c05..edda499 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts @@ -35,7 +35,11 @@ describe('createFusionAuth', () => { }); it('Fetches userInfo', async () => { - const user = { given_name: 'JSON', family_name: 'Bourne' }; + const user = { + given_name: 'JSON', + family_name: 'Bourne', + customTrait: 'additional info', + }; const mockUserInfoResponse = new Response(JSON.stringify(user), { status: 200, }); diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts index 8adff80..51cd254 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts @@ -23,12 +23,12 @@ export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => { const isGettingUserInfo = ref(false); const error = ref(null); - async function getUserInfo() { + async function getUserInfo() { isGettingUserInfo.value = true; error.value = null; try { - userInfo.value = await core.fetchUserInfo(); + userInfo.value = await core.fetchUserInfo(); return userInfo.value; } catch (e) { error.value = e as Error; From 78f7a9d22fda6b683ac83802bc77e868f4de6736 Mon Sep 17 00:00:00 2001 From: michaelyons Date: Mon, 24 Jun 2024 14:49:46 -0600 Subject: [PATCH 2/6] Extend generic type into user info --- packages/sdk-vue/src/composables/useFusionAuth.ts | 6 +++--- packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts | 8 +++++--- packages/sdk-vue/src/types.ts | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/sdk-vue/src/composables/useFusionAuth.ts b/packages/sdk-vue/src/composables/useFusionAuth.ts index 7f02189..1528664 100644 --- a/packages/sdk-vue/src/composables/useFusionAuth.ts +++ b/packages/sdk-vue/src/composables/useFusionAuth.ts @@ -1,8 +1,8 @@ import { inject } from 'vue'; import { fusionAuthKey } from '#/injectionSymbols'; -import type { FusionAuth } from '#/types'; +import type { FusionAuth, UserInfo } from '#/types'; -export const useFusionAuth = (): FusionAuth => { +export const useFusionAuth = >(): FusionAuth => { const fusionAuth = inject(fusionAuthKey); if (!fusionAuth) { @@ -10,5 +10,5 @@ export const useFusionAuth = (): FusionAuth => { 'No FusionAuth instance found. Did you forget to call Vue.use(FusionAuthVuePlugin)?', ); } - return fusionAuth; + return fusionAuth as T; }; diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts index 51cd254..9033a2b 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts @@ -1,10 +1,12 @@ -import { ref } from 'vue'; +import { Ref, ref } from 'vue'; import { SDKCore } from '@fusionauth-sdk/core'; import { FusionAuth, FusionAuthConfig, UserInfo } from '#/types'; import { NuxtUseCookieAdapter } from './NuxtUseCookieAdapter'; -export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => { +export const createFusionAuth = ( + config: FusionAuthConfig, +): FusionAuth => { let cookieAdapter; if (config.nuxtUseCookie) { cookieAdapter = new NuxtUseCookieAdapter(config.nuxtUseCookie); @@ -19,7 +21,7 @@ export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => { }); const isLoggedIn = ref(core.isLoggedIn); - const userInfo = ref(null); + const userInfo = ref(null) as Ref; const isGettingUserInfo = ref(false); const error = ref(null); diff --git a/packages/sdk-vue/src/types.ts b/packages/sdk-vue/src/types.ts index 6d9d74c..81ba842 100644 --- a/packages/sdk-vue/src/types.ts +++ b/packages/sdk-vue/src/types.ts @@ -105,7 +105,7 @@ export interface UserInfo { /** * FusionAuth object provided at app-level by FusionAuthVuePlugin */ -export interface FusionAuth { +export interface FusionAuth { /** * Whether the user is logged in. */ @@ -116,12 +116,12 @@ export interface FusionAuth { * Internally updates `isFetchingUser` and `userInfo` refs, as well as `error` if the request fails. * @returns {Promise} */ - getUserInfo: () => Promise; + getUserInfo: () => Promise; /** * Data fetched from the configured 'me' endpoint. */ - userInfo: Ref; + userInfo: Ref; /** * Indicates that the getUserInfo call is unresolved. From 4f40662b459cbf1e85270611ab4d5276dd75aa77 Mon Sep 17 00:00:00 2001 From: michaelyons Date: Mon, 24 Jun 2024 15:25:07 -0600 Subject: [PATCH 3/6] Add back generic to create fusion auth --- packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts index 9033a2b..490a3f5 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts @@ -25,7 +25,7 @@ export const createFusionAuth = ( const isGettingUserInfo = ref(false); const error = ref(null); - async function getUserInfo() { + async function getUserInfo() { isGettingUserInfo.value = true; error.value = null; From 4d19151a6af7ae7d283f7afcb08f4e0d1dd9f525 Mon Sep 17 00:00:00 2001 From: michaelyons Date: Mon, 24 Jun 2024 15:45:26 -0600 Subject: [PATCH 4/6] Remove extends to fix implementation bugs --- packages/sdk-vue/src/composables/useFusionAuth.ts | 4 ++-- packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts | 6 +++--- packages/sdk-vue/src/types.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/sdk-vue/src/composables/useFusionAuth.ts b/packages/sdk-vue/src/composables/useFusionAuth.ts index 1528664..d96d157 100644 --- a/packages/sdk-vue/src/composables/useFusionAuth.ts +++ b/packages/sdk-vue/src/composables/useFusionAuth.ts @@ -2,7 +2,7 @@ import { inject } from 'vue'; import { fusionAuthKey } from '#/injectionSymbols'; import type { FusionAuth, UserInfo } from '#/types'; -export const useFusionAuth = >(): FusionAuth => { +export const useFusionAuth = (): FusionAuth => { const fusionAuth = inject(fusionAuthKey); if (!fusionAuth) { @@ -10,5 +10,5 @@ export const useFusionAuth = >(): FusionAuth => { 'No FusionAuth instance found. Did you forget to call Vue.use(FusionAuthVuePlugin)?', ); } - return fusionAuth as T; + return fusionAuth; }; diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts index 490a3f5..347c956 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts @@ -4,9 +4,9 @@ import { FusionAuth, FusionAuthConfig, UserInfo } from '#/types'; import { NuxtUseCookieAdapter } from './NuxtUseCookieAdapter'; -export const createFusionAuth = ( +export const createFusionAuth = ( config: FusionAuthConfig, -): FusionAuth => { +): FusionAuth => { let cookieAdapter; if (config.nuxtUseCookie) { cookieAdapter = new NuxtUseCookieAdapter(config.nuxtUseCookie); @@ -21,7 +21,7 @@ export const createFusionAuth = ( }); const isLoggedIn = ref(core.isLoggedIn); - const userInfo = ref(null) as Ref; + const userInfo: Ref = ref(null); const isGettingUserInfo = ref(false); const error = ref(null); diff --git a/packages/sdk-vue/src/types.ts b/packages/sdk-vue/src/types.ts index 81ba842..40ae80f 100644 --- a/packages/sdk-vue/src/types.ts +++ b/packages/sdk-vue/src/types.ts @@ -105,7 +105,7 @@ export interface UserInfo { /** * FusionAuth object provided at app-level by FusionAuthVuePlugin */ -export interface FusionAuth { +export interface FusionAuth { /** * Whether the user is logged in. */ From 11c820e6f33f73c81e27169380c88044e49dbd6d Mon Sep 17 00:00:00 2001 From: michaelyons Date: Mon, 24 Jun 2024 16:32:00 -0600 Subject: [PATCH 5/6] Extract injection key type --- packages/sdk-vue/src/FusionAuthVuePlugin.ts | 4 ++-- packages/sdk-vue/src/composables/useFusionAuth.ts | 4 ++-- .../sdk-vue/src/createFusionAuth/createFusionAuth.test.ts | 1 + packages/sdk-vue/src/injectionSymbols.ts | 5 +---- packages/sdk-vue/src/types.ts | 2 +- packages/sdk-vue/src/utilsForTests/index.ts | 4 ++-- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/sdk-vue/src/FusionAuthVuePlugin.ts b/packages/sdk-vue/src/FusionAuthVuePlugin.ts index a5b8ec0..7dfe99c 100644 --- a/packages/sdk-vue/src/FusionAuthVuePlugin.ts +++ b/packages/sdk-vue/src/FusionAuthVuePlugin.ts @@ -1,4 +1,4 @@ -import { App } from 'vue'; +import { App, InjectionKey } from 'vue'; import { FusionAuth, FusionAuthConfig } from './types.ts'; import * as components from './components/index.ts'; import { fusionAuthKey } from './injectionSymbols.ts'; @@ -26,7 +26,7 @@ const FusionAuthVuePlugin = { } // Register the instance - app.provide(fusionAuthKey, fusionAuth); + app.provide(fusionAuthKey as InjectionKey, fusionAuth); // Register the components Object.entries(components).forEach(([key, component]) => { diff --git a/packages/sdk-vue/src/composables/useFusionAuth.ts b/packages/sdk-vue/src/composables/useFusionAuth.ts index d96d157..1f41a7f 100644 --- a/packages/sdk-vue/src/composables/useFusionAuth.ts +++ b/packages/sdk-vue/src/composables/useFusionAuth.ts @@ -1,9 +1,9 @@ -import { inject } from 'vue'; +import { InjectionKey, inject } from 'vue'; import { fusionAuthKey } from '#/injectionSymbols'; import type { FusionAuth, UserInfo } from '#/types'; export const useFusionAuth = (): FusionAuth => { - const fusionAuth = inject(fusionAuthKey); + const fusionAuth = inject(fusionAuthKey as InjectionKey>); if (!fusionAuth) { throw new Error( diff --git a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts index edda499..41eab49 100644 --- a/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts +++ b/packages/sdk-vue/src/createFusionAuth/createFusionAuth.test.ts @@ -49,6 +49,7 @@ describe('createFusionAuth', () => { await fusionAuth.getUserInfo(); expect(fusionAuth.userInfo.value).toEqual(user); + expect(fusionAuth.userInfo.value.customTrait).toEqual(user.customTrait); }); it('Handles a failed user info request', async () => { diff --git a/packages/sdk-vue/src/injectionSymbols.ts b/packages/sdk-vue/src/injectionSymbols.ts index 5cd9057..7a45b59 100644 --- a/packages/sdk-vue/src/injectionSymbols.ts +++ b/packages/sdk-vue/src/injectionSymbols.ts @@ -1,4 +1 @@ -import { InjectionKey } from 'vue'; -import type { FusionAuth } from './types.ts'; - -export const fusionAuthKey = Symbol('fusionAuth') as InjectionKey; +export const fusionAuthKey = Symbol('fusionAuth'); diff --git a/packages/sdk-vue/src/types.ts b/packages/sdk-vue/src/types.ts index 40ae80f..81ba842 100644 --- a/packages/sdk-vue/src/types.ts +++ b/packages/sdk-vue/src/types.ts @@ -105,7 +105,7 @@ export interface UserInfo { /** * FusionAuth object provided at app-level by FusionAuthVuePlugin */ -export interface FusionAuth { +export interface FusionAuth { /** * Whether the user is logged in. */ diff --git a/packages/sdk-vue/src/utilsForTests/index.ts b/packages/sdk-vue/src/utilsForTests/index.ts index 6884d3f..f9cebb6 100644 --- a/packages/sdk-vue/src/utilsForTests/index.ts +++ b/packages/sdk-vue/src/utilsForTests/index.ts @@ -1,4 +1,4 @@ -import { ref } from 'vue'; +import { InjectionKey, ref } from 'vue'; import { vi } from 'vitest'; import type { FusionAuth } from '..'; @@ -13,7 +13,7 @@ export const getMockFusionAuth = ({ userInfo = ref(null), }: Partial) => { return { - key: fusionAuthKey as symbol, + key: fusionAuthKey as InjectionKey, mockedValues: { login, logout, From f45308f6a419d006d88e9ced47953b76363d2c9e Mon Sep 17 00:00:00 2001 From: michaelyons Date: Tue, 25 Jun 2024 09:46:04 -0600 Subject: [PATCH 6/6] Allow for other instance values while instantiating FA instance --- packages/sdk-vue/src/FusionAuthVuePlugin.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sdk-vue/src/FusionAuthVuePlugin.ts b/packages/sdk-vue/src/FusionAuthVuePlugin.ts index 7dfe99c..6410114 100644 --- a/packages/sdk-vue/src/FusionAuthVuePlugin.ts +++ b/packages/sdk-vue/src/FusionAuthVuePlugin.ts @@ -3,8 +3,9 @@ import { FusionAuth, FusionAuthConfig } from './types.ts'; import * as components from './components/index.ts'; import { fusionAuthKey } from './injectionSymbols.ts'; import { createFusionAuth } from './createFusionAuth/index.ts'; +import { UserInfo } from '@fusionauth-sdk/core'; -type FusionAuthInstantiated = { instance: FusionAuth }; +type FusionAuthInstantiated = { instance: FusionAuth }; /** * Installation method for the FusionAuthVuePlugin.