diff --git a/.changeset/mighty-clouds-explode.md b/.changeset/mighty-clouds-explode.md new file mode 100644 index 0000000000..002d0bd6c3 --- /dev/null +++ b/.changeset/mighty-clouds-explode.md @@ -0,0 +1,8 @@ +--- +"@clerk/clerk-react": patch +"@clerk/shared": patch +"@clerk/types": patch +"@clerk/vue": patch +--- + +Share hook return types diff --git a/packages/react/src/hooks/useAuth.ts b/packages/react/src/hooks/useAuth.ts index cb356b4ff9..bb883908c7 100644 --- a/packages/react/src/hooks/useAuth.ts +++ b/packages/react/src/hooks/useAuth.ts @@ -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'; @@ -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[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; /** diff --git a/packages/react/src/hooks/useSignIn.ts b/packages/react/src/hooks/useSignIn.ts index 6c259b936f..d9ebf42d48 100644 --- a/packages/react/src/hooks/useSignIn.ts +++ b/packages/react/src/hooks/useSignIn.ts @@ -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 = () => { diff --git a/packages/react/src/hooks/useSignUp.ts b/packages/react/src/hooks/useSignUp.ts index f8f6fdbb0b..0d05a177a8 100644 --- a/packages/react/src/hooks/useSignUp.ts +++ b/packages/react/src/hooks/useSignUp.ts @@ -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 = () => { diff --git a/packages/shared/src/react/hooks/useSession.ts b/packages/shared/src/react/hooks/useSession.ts index 6f9544483e..838c61128a 100644 --- a/packages/shared/src/react/hooks/useSession.ts +++ b/packages/shared/src/react/hooks/useSession.ts @@ -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; /** diff --git a/packages/shared/src/react/hooks/useSessionList.ts b/packages/shared/src/react/hooks/useSessionList.ts index 32e9c3718d..f3c1ac7e28 100644 --- a/packages/shared/src/react/hooks/useSessionList.ts +++ b/packages/shared/src/react/hooks/useSessionList.ts @@ -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 = () => { diff --git a/packages/shared/src/react/hooks/useUser.ts b/packages/shared/src/react/hooks/useUser.ts index a1bc368673..f85a7c919a 100644 --- a/packages/shared/src/react/hooks/useUser.ts +++ b/packages/shared/src/react/hooks/useUser.ts @@ -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. * diff --git a/packages/types/src/hooks.ts b/packages/types/src/hooks.ts new file mode 100644 index 0000000000..1b5d4ab0b0 --- /dev/null +++ b/packages/types/src/hooks.ts @@ -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[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 }; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 108545cecc..8812702f13 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -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'; diff --git a/packages/vue/src/composables/useAuth.ts b/packages/vue/src/composables/useAuth.ts index e6d0fb07ee..3a39de0734 100644 --- a/packages/vue/src/composables/useAuth.ts +++ b/packages/vue/src/composables/useAuth.ts @@ -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'; @@ -14,9 +7,6 @@ import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type CheckAuthorizationSignedOut = undefined; -type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters[0]) => false; - /** * @internal */ @@ -55,60 +45,6 @@ function createSignOut(clerk: ShallowRef) { }; } -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; /** diff --git a/packages/vue/src/composables/useSession.ts b/packages/vue/src/composables/useSession.ts index b0fec7e536..4485b76624 100644 --- a/packages/vue/src/composables/useSession.ts +++ b/packages/vue/src/composables/useSession.ts @@ -1,15 +1,10 @@ -import type { ActiveSessionResource } from '@clerk/types'; +import type { UseSessionReturn } from '@clerk/types'; import { computed } from 'vue'; import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type UseSessionReturn = - | { isLoaded: false; isSignedIn: undefined; session: undefined } - | { isLoaded: true; isSignedIn: false; session: null } - | { isLoaded: true; isSignedIn: true; session: ActiveSessionResource }; - type UseSession = () => ToComputedRefs; /** diff --git a/packages/vue/src/composables/useSessionList.ts b/packages/vue/src/composables/useSessionList.ts index e51bdb783c..91068c7618 100644 --- a/packages/vue/src/composables/useSessionList.ts +++ b/packages/vue/src/composables/useSessionList.ts @@ -1,14 +1,10 @@ -import type { SessionResource, SetActive } from '@clerk/types'; +import type { UseSessionListReturn } from '@clerk/types'; import { computed } from 'vue'; import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type UseSessionListReturn = - | { isLoaded: false; sessions: undefined; setActive: undefined } - | { isLoaded: true; sessions: SessionResource[]; setActive: SetActive }; - type UseSessionList = () => ToComputedRefs; /** diff --git a/packages/vue/src/composables/useSignIn.ts b/packages/vue/src/composables/useSignIn.ts index fb1e660abd..05ea8425c0 100644 --- a/packages/vue/src/composables/useSignIn.ts +++ b/packages/vue/src/composables/useSignIn.ts @@ -1,15 +1,11 @@ import { eventMethodCalled } from '@clerk/shared/telemetry'; -import type { SetActive, SignInResource } from '@clerk/types'; +import type { UseSignInReturn } from '@clerk/types'; import { computed, watch } from 'vue'; import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type UseSignInReturn = - | { isLoaded: false; signIn: undefined; setActive: undefined } - | { isLoaded: true; signIn: SignInResource; setActive: SetActive }; - type UseSignIn = () => ToComputedRefs; /** diff --git a/packages/vue/src/composables/useSignUp.ts b/packages/vue/src/composables/useSignUp.ts index 83550d30b0..b4c93a6869 100644 --- a/packages/vue/src/composables/useSignUp.ts +++ b/packages/vue/src/composables/useSignUp.ts @@ -1,15 +1,11 @@ import { eventMethodCalled } from '@clerk/shared/telemetry'; -import type { SetActive, SignUpResource } from '@clerk/types'; +import type { UseSignUpReturn } from '@clerk/types'; import { computed, watch } from 'vue'; import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type UseSignUpReturn = - | { isLoaded: false; signUp: undefined; setActive: undefined } - | { isLoaded: true; signUp: SignUpResource; setActive: SetActive }; - type UseSignUp = () => ToComputedRefs; /** diff --git a/packages/vue/src/composables/useUser.ts b/packages/vue/src/composables/useUser.ts index 66e916e2fc..7c3cf9e25c 100644 --- a/packages/vue/src/composables/useUser.ts +++ b/packages/vue/src/composables/useUser.ts @@ -1,15 +1,10 @@ -import type { UserResource } from '@clerk/types'; +import type { UseUserReturn } from '@clerk/types'; import { computed } from 'vue'; import type { ToComputedRefs } from '../utils'; import { toComputedRefs } from '../utils'; import { useClerkContext } from './useClerkContext'; -type UseUserReturn = - | { isLoaded: false; isSignedIn: undefined; user: undefined } - | { isLoaded: true; isSignedIn: false; user: null } - | { isLoaded: true; isSignedIn: true; user: UserResource }; - type UseUser = () => ToComputedRefs; /**