Skip to content

Commit

Permalink
Drop generateGetters higher order function support
Browse files Browse the repository at this point in the history
While this HoF does work and autowires our state as intended, it is unable
to pass typescript compilation checks because typescript expects the
methods to be defined explicitly instead of being generated at runtime.

Signed-off-by: Jeremy Ho <jujaga@gmail.com>
  • Loading branch information
jujaga committed Feb 24, 2023
1 parent 46699b5 commit a174e77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
34 changes: 20 additions & 14 deletions frontend/src/store/authStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { computed, ref, unref } from 'vue';

import { AuthService, ConfigService } from '@/services';
import { generateGetters, isDebugMode } from './utils';
import { isDebugMode } from './utils';

import type { IdTokenClaims, User } from 'oidc-client-ts';
import type { Ref } from 'vue';
import type { IGetterIndex, IStateIndex } from '@/types';
import type { IdentityProvider } from '@/interfaces/IdentityProvider';
import type { IdentityProvider } from '@/interfaces';

export type AuthStateStore = {
accessToken: Ref<string | undefined>,
Expand All @@ -18,7 +17,7 @@ export type AuthStateStore = {
refreshToken: Ref<string | undefined>,
scope: Ref<string | undefined>,
user: Ref<User | null>
} & IStateIndex
}

export const useAuthStore = defineStore('auth', () => {
const authService = new AuthService();
Expand All @@ -38,14 +37,22 @@ export const useAuthStore = defineStore('auth', () => {
};

// Getters
const getters: IGetterIndex = generateGetters(state);

function getIdentityId(): string {
return configService.getConfig().idpList
.map((provider: IdentityProvider) => state.profile.value ?
state.profile.value[provider.identityKey] : undefined)
.filter((item?: string) => item)[0];
}
const getters = {
getAccessToken: computed(() => unref(state.accessToken)),
getExpiresAt: computed(() => unref(state.expiresAt)),
getIdentityId: computed(() => {
return configService.getConfig().idpList
.map((provider: IdentityProvider) => state.profile.value ?
state.profile.value[provider.identityKey] : undefined)
.filter((item?: string) => item)[0];
}),
getIdToken: computed(() => unref(state.idToken)),
getIsAuthenticated: computed(() => unref(state.isAuthenticated)),
getProfile: computed(() => unref(state.profile)),
getRefreshToken: computed(() => unref(state.refreshToken)),
getScope: computed(() => unref(state.scope)),
getUser: computed(() => unref(state.user))
};

// Actions
function _registerEvents() {
Expand Down Expand Up @@ -98,7 +105,6 @@ export const useAuthStore = defineStore('auth', () => {
...getters,
_registerEvents,
_updateState,
getIdentityId,
init,
login,
loginCallback,
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/store/configStore.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { computed, ref, unref } from 'vue';

import { ConfigService } from '@/services';
import { generateGetters, isDebugMode } from './utils';
import { isDebugMode } from './utils';

import type { Ref } from 'vue';
import type { IGetterIndex, IStateIndex } from '@/types';

export type ConfigStateStore = {
config: Ref<any | null>
} & IStateIndex
}

export const useConfigStore = defineStore('config', () => {
const configService = new ConfigService();
Expand All @@ -20,7 +19,9 @@ export const useConfigStore = defineStore('config', () => {
};

// Getters
const getters: IGetterIndex = generateGetters(state);
const getters = {
getConfig: computed(() => unref(state.config))
};

// Actions
async function init(): Promise<void> {
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
import { computed, unref } from 'vue';

import type { IGetterIndex, IStateIndex } from '@/interfaces';

export function generateGetters(state: IStateIndex): IGetterIndex {
return Object.fromEntries(Object.keys(state).map((k: string) => ([
`get${k.charAt(0).toUpperCase() + k.slice(1)}`,
computed(() => unref(state[k]))
])));
}

export const isDebugMode: boolean = import.meta.env.MODE.toUpperCase() === 'DEBUG';

0 comments on commit a174e77

Please sign in to comment.