Skip to content

Commit

Permalink
chore(shared,types,clerk-react,vue): Share hook return types (#4583)
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano authored Nov 18, 2024
1 parent aba63de commit e47eb58
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 207 deletions.
8 changes: 8 additions & 0 deletions .changeset/mighty-clouds-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@clerk/clerk-react": patch
"@clerk/shared": patch
"@clerk/types": patch
"@clerk/vue": patch
---

Share hook return types
65 changes: 1 addition & 64 deletions packages/react/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { createCheckAuthorization } from '@clerk/shared/authorization';
import type {
ActJWTClaim,
CheckAuthorizationWithCustomPermissions,
GetToken,
OrganizationCustomRoleKey,
SignOut,
} from '@clerk/types';
import type { CheckAuthorizationWithCustomPermissions, GetToken, SignOut, UseAuthReturn } from '@clerk/types';
import { useCallback, useEffect, useState } from 'react';

import { useAuthContext } from '../contexts/AuthContext';
Expand All @@ -15,63 +9,6 @@ import { invalidStateError } from '../errors/messages';
import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider';
import { createGetToken, createSignOut } from './utils';

type CheckAuthorizationSignedOut = undefined;
type CheckAuthorizationWithoutOrgOrUser = (params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;

type UseAuthReturn =
| {
isLoaded: false;
isSignedIn: undefined;
userId: undefined;
sessionId: undefined;
actor: undefined;
orgId: undefined;
orgRole: undefined;
orgSlug: undefined;
has: CheckAuthorizationSignedOut;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: false;
userId: null;
sessionId: null;
actor: null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: string;
orgRole: OrganizationCustomRoleKey;
orgSlug: string | null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
};

type UseAuth = (initialAuthState?: any) => UseAuthReturn;

/**
Expand Down
14 changes: 1 addition & 13 deletions packages/react/src/hooks/useSignIn.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { useClientContext } from '@clerk/shared/react';
import { eventMethodCalled } from '@clerk/shared/telemetry';
import type { SetActive, SignInResource } from '@clerk/types';
import type { UseSignInReturn } from '@clerk/types';

import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider';

type UseSignInReturn =
| {
isLoaded: false;
signIn: undefined;
setActive: undefined;
}
| {
isLoaded: true;
signIn: SignInResource;
setActive: SetActive;
};

type UseSignIn = () => UseSignInReturn;

export const useSignIn: UseSignIn = () => {
Expand Down
14 changes: 1 addition & 13 deletions packages/react/src/hooks/useSignUp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { useClientContext } from '@clerk/shared/react';
import { eventMethodCalled } from '@clerk/shared/telemetry';
import type { SetActive, SignUpResource } from '@clerk/types';
import type { UseSignUpReturn } from '@clerk/types';

import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider';

type UseSignUpReturn =
| {
isLoaded: false;
signUp: undefined;
setActive: undefined;
}
| {
isLoaded: true;
signUp: SignUpResource;
setActive: SetActive;
};

type UseSignUp = () => UseSignUpReturn;

export const useSignUp: UseSignUp = () => {
Expand Down
7 changes: 1 addition & 6 deletions packages/shared/src/react/hooks/useSession.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { ActiveSessionResource } from '@clerk/types';
import type { UseSessionReturn } from '@clerk/types';

import { useAssertWrappedByClerkProvider, useSessionContext } from '../contexts';

type UseSessionReturn =
| { isLoaded: false; isSignedIn: undefined; session: undefined }
| { isLoaded: true; isSignedIn: false; session: null }
| { isLoaded: true; isSignedIn: true; session: ActiveSessionResource };

type UseSession = () => UseSessionReturn;

/**
Expand Down
14 changes: 1 addition & 13 deletions packages/shared/src/react/hooks/useSessionList.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import type { SessionResource, SetActive } from '@clerk/types';
import type { UseSessionListReturn } from '@clerk/types';

import { useAssertWrappedByClerkProvider, useClerkInstanceContext, useClientContext } from '../contexts';

type UseSessionListReturn =
| {
isLoaded: false;
sessions: undefined;
setActive: undefined;
}
| {
isLoaded: true;
sessions: SessionResource[];
setActive: SetActive;
};

type UseSessionList = () => UseSessionListReturn;

export const useSessionList: UseSessionList = () => {
Expand Down
7 changes: 1 addition & 6 deletions packages/shared/src/react/hooks/useUser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { UserResource } from '@clerk/types';
import type { UseUserReturn } from '@clerk/types';

import { useAssertWrappedByClerkProvider, useUserContext } from '../contexts';

type UseUserReturn =
| { isLoaded: false; isSignedIn: undefined; user: undefined }
| { isLoaded: true; isSignedIn: false; user: null }
| { isLoaded: true; isSignedIn: true; user: UserResource };

/**
* Returns the current auth state and if a user is signed in, the user object.
*
Expand Down
116 changes: 116 additions & 0 deletions packages/types/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { OrganizationCustomRoleKey } from 'organizationMembership';
import type { SignInResource } from 'signIn';

import type { SetActive, SignOut } from './clerk';
import type { ActJWTClaim } from './jwt';
import type {
ActiveSessionResource,
CheckAuthorizationWithCustomPermissions,
GetToken,
SessionResource,
} from './session';
import type { SignUpResource } from './signUp';
import type { UserResource } from './user';

type CheckAuthorizationSignedOut = undefined;
type CheckAuthorizationWithoutOrgOrUser = (params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;

export type UseAuthReturn =
| {
isLoaded: false;
isSignedIn: undefined;
userId: undefined;
sessionId: undefined;
actor: undefined;
orgId: undefined;
orgRole: undefined;
orgSlug: undefined;
has: CheckAuthorizationSignedOut;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: false;
userId: null;
sessionId: null;
actor: null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: string;
orgRole: OrganizationCustomRoleKey;
orgSlug: string | null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
};

export type UseSignInReturn =
| {
isLoaded: false;
signIn: undefined;
setActive: undefined;
}
| {
isLoaded: true;
signIn: SignInResource;
setActive: SetActive;
};

export type UseSignUpReturn =
| {
isLoaded: false;
signUp: undefined;
setActive: undefined;
}
| {
isLoaded: true;
signUp: SignUpResource;
setActive: SetActive;
};

export type UseSessionReturn =
| { isLoaded: false; isSignedIn: undefined; session: undefined }
| { isLoaded: true; isSignedIn: false; session: null }
| { isLoaded: true; isSignedIn: true; session: ActiveSessionResource };

export type UseSessionListReturn =
| {
isLoaded: false;
sessions: undefined;
setActive: undefined;
}
| {
isLoaded: true;
sessions: SessionResource[];
setActive: SetActive;
};

export type UseUserReturn =
| { isLoaded: false; isSignedIn: undefined; user: undefined }
| { isLoaded: true; isSignedIn: false; user: null }
| { isLoaded: true; isSignedIn: true; user: UserResource };
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './environment';
export * from './externalAccount';
export * from './enterpriseAccount';
export * from './factors';
export * from './hooks';
export * from './identificationLink';
export * from './identifiers';
export * from './image';
Expand Down
66 changes: 1 addition & 65 deletions packages/vue/src/composables/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import type {
ActJWTClaim,
CheckAuthorizationWithCustomPermissions,
Clerk,
GetToken,
OrganizationCustomRoleKey,
SignOut,
} from '@clerk/types';
import type { CheckAuthorizationWithCustomPermissions, Clerk, GetToken, SignOut, UseAuthReturn } from '@clerk/types';
import { computed, type ShallowRef, watch } from 'vue';

import { errorThrower } from '../errors/errorThrower';
Expand All @@ -14,9 +7,6 @@ import type { ToComputedRefs } from '../utils';
import { toComputedRefs } from '../utils';
import { useClerkContext } from './useClerkContext';

type CheckAuthorizationSignedOut = undefined;
type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;

/**
* @internal
*/
Expand Down Expand Up @@ -55,60 +45,6 @@ function createSignOut(clerk: ShallowRef<Clerk | null>) {
};
}

type UseAuthReturn =
| {
isLoaded: false;
isSignedIn: undefined;
userId: undefined;
sessionId: undefined;
actor: undefined;
orgId: undefined;
orgRole: undefined;
orgSlug: undefined;
has: CheckAuthorizationSignedOut;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: false;
userId: null;
sessionId: null;
actor: null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: string;
orgRole: OrganizationCustomRoleKey;
orgSlug: string | null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
};

type UseAuth = () => ToComputedRefs<UseAuthReturn>;

/**
Expand Down
Loading

0 comments on commit e47eb58

Please sign in to comment.