Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix permission types and userOrg default value #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/config/index.js → src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {version} from '../utils/version';
import {removeTrailingSlash} from '../utils/removeTrailingSlash';
import {KindeState} from '../../types';

/**
* @type {import('../../types').KindeState}
*/
const initialState = {
const initialState: KindeState = {
accessToken: null,
idToken: null,
isAuthenticated: false,
isLoading: true,
organization: null,
permissions: [],
user: null,
userOrganizations: [],
userOrganizations: null,
getAccessToken: () => null,
getBooleanFlag: () => null,
getClaim: () => null,
Expand All @@ -26,7 +24,13 @@ const initialState = {
getToken: () => null,
getUser: () => null,
getUserOrganizations: () => null,
refreshData: () => null
refreshData: () => null,
accessTokenEncoded: null,
accessTokenRaw: null,
idTokenRaw: null,
idTokenEncoded: null,
getAccessTokenRaw: () => null,
getIdTokenRaw: () => null
};

const SESSION_PREFIX = 'pkce-verifier';
Expand Down
4 changes: 2 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export type KindeState = {
isAuthenticated: boolean | null;
isLoading: boolean | null;
organization: KindeOrganization;
permissions: KindePermissions;
permissions: KindePermissions | [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consider using a more specific union type for permissions.

The current type KindePermissions | [] could lead to runtime errors when code expects the permissions and orgCode properties. Consider using a more type-safe alternative:

-  permissions: KindePermissions | [];
+  permissions: KindePermissions | { permissions: []; orgCode: null };

This ensures that even when empty, the permissions object maintains a consistent structure, preventing potential runtime errors.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
permissions: KindePermissions | [];
permissions: KindePermissions | { permissions: []; orgCode: null };

user: {
id: string;
email: string | null;
Expand Down Expand Up @@ -337,7 +337,7 @@ export type KindeState = {
getPermission: (
key: string
) => {isGranted: boolean; orgCode: string | null} | null;
getPermissions: () => KindePermissions;
getPermissions: () => KindePermissions | [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent typing between permissions property and getPermissions method.

The return type of getPermissions should match the type of the permissions property. If we update the property type as suggested above, we should also update this method:

-  getPermissions: () => KindePermissions | [];
+  getPermissions: () => KindePermissions | { permissions: []; orgCode: null };

This maintains type consistency and prevents potential type mismatches between the property and its getter method.

Committable suggestion was skipped due to low confidence.

getStringFlag: (
code: string,
defaultValue: string
Expand Down