Skip to content

Commit e47eb58

Browse files
authoredNov 18, 2024··
chore(shared,types,clerk-react,vue): Share hook return types (#4583)
1 parent aba63de commit e47eb58

File tree

15 files changed

+137
-207
lines changed

15 files changed

+137
-207
lines changed
 

‎.changeset/mighty-clouds-explode.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@clerk/clerk-react": patch
3+
"@clerk/shared": patch
4+
"@clerk/types": patch
5+
"@clerk/vue": patch
6+
---
7+
8+
Share hook return types

‎packages/react/src/hooks/useAuth.ts

+1-64
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { createCheckAuthorization } from '@clerk/shared/authorization';
2-
import type {
3-
ActJWTClaim,
4-
CheckAuthorizationWithCustomPermissions,
5-
GetToken,
6-
OrganizationCustomRoleKey,
7-
SignOut,
8-
} from '@clerk/types';
2+
import type { CheckAuthorizationWithCustomPermissions, GetToken, SignOut, UseAuthReturn } from '@clerk/types';
93
import { useCallback, useEffect, useState } from 'react';
104

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

18-
type CheckAuthorizationSignedOut = undefined;
19-
type CheckAuthorizationWithoutOrgOrUser = (params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
20-
21-
type UseAuthReturn =
22-
| {
23-
isLoaded: false;
24-
isSignedIn: undefined;
25-
userId: undefined;
26-
sessionId: undefined;
27-
actor: undefined;
28-
orgId: undefined;
29-
orgRole: undefined;
30-
orgSlug: undefined;
31-
has: CheckAuthorizationSignedOut;
32-
signOut: SignOut;
33-
getToken: GetToken;
34-
}
35-
| {
36-
isLoaded: true;
37-
isSignedIn: false;
38-
userId: null;
39-
sessionId: null;
40-
actor: null;
41-
orgId: null;
42-
orgRole: null;
43-
orgSlug: null;
44-
has: CheckAuthorizationWithoutOrgOrUser;
45-
signOut: SignOut;
46-
getToken: GetToken;
47-
}
48-
| {
49-
isLoaded: true;
50-
isSignedIn: true;
51-
userId: string;
52-
sessionId: string;
53-
actor: ActJWTClaim | null;
54-
orgId: null;
55-
orgRole: null;
56-
orgSlug: null;
57-
has: CheckAuthorizationWithCustomPermissions;
58-
signOut: SignOut;
59-
getToken: GetToken;
60-
}
61-
| {
62-
isLoaded: true;
63-
isSignedIn: true;
64-
userId: string;
65-
sessionId: string;
66-
actor: ActJWTClaim | null;
67-
orgId: string;
68-
orgRole: OrganizationCustomRoleKey;
69-
orgSlug: string | null;
70-
has: CheckAuthorizationWithCustomPermissions;
71-
signOut: SignOut;
72-
getToken: GetToken;
73-
};
74-
7512
type UseAuth = (initialAuthState?: any) => UseAuthReturn;
7613

7714
/**

‎packages/react/src/hooks/useSignIn.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import { useClientContext } from '@clerk/shared/react';
22
import { eventMethodCalled } from '@clerk/shared/telemetry';
3-
import type { SetActive, SignInResource } from '@clerk/types';
3+
import type { UseSignInReturn } from '@clerk/types';
44

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

8-
type UseSignInReturn =
9-
| {
10-
isLoaded: false;
11-
signIn: undefined;
12-
setActive: undefined;
13-
}
14-
| {
15-
isLoaded: true;
16-
signIn: SignInResource;
17-
setActive: SetActive;
18-
};
19-
208
type UseSignIn = () => UseSignInReturn;
219

2210
export const useSignIn: UseSignIn = () => {

‎packages/react/src/hooks/useSignUp.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import { useClientContext } from '@clerk/shared/react';
22
import { eventMethodCalled } from '@clerk/shared/telemetry';
3-
import type { SetActive, SignUpResource } from '@clerk/types';
3+
import type { UseSignUpReturn } from '@clerk/types';
44

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

8-
type UseSignUpReturn =
9-
| {
10-
isLoaded: false;
11-
signUp: undefined;
12-
setActive: undefined;
13-
}
14-
| {
15-
isLoaded: true;
16-
signUp: SignUpResource;
17-
setActive: SetActive;
18-
};
19-
208
type UseSignUp = () => UseSignUpReturn;
219

2210
export const useSignUp: UseSignUp = () => {

‎packages/shared/src/react/hooks/useSession.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import type { ActiveSessionResource } from '@clerk/types';
1+
import type { UseSessionReturn } from '@clerk/types';
22

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

5-
type UseSessionReturn =
6-
| { isLoaded: false; isSignedIn: undefined; session: undefined }
7-
| { isLoaded: true; isSignedIn: false; session: null }
8-
| { isLoaded: true; isSignedIn: true; session: ActiveSessionResource };
9-
105
type UseSession = () => UseSessionReturn;
116

127
/**

‎packages/shared/src/react/hooks/useSessionList.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
import type { SessionResource, SetActive } from '@clerk/types';
1+
import type { UseSessionListReturn } from '@clerk/types';
22

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

5-
type UseSessionListReturn =
6-
| {
7-
isLoaded: false;
8-
sessions: undefined;
9-
setActive: undefined;
10-
}
11-
| {
12-
isLoaded: true;
13-
sessions: SessionResource[];
14-
setActive: SetActive;
15-
};
16-
175
type UseSessionList = () => UseSessionListReturn;
186

197
export const useSessionList: UseSessionList = () => {

‎packages/shared/src/react/hooks/useUser.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import type { UserResource } from '@clerk/types';
1+
import type { UseUserReturn } from '@clerk/types';
22

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

5-
type UseUserReturn =
6-
| { isLoaded: false; isSignedIn: undefined; user: undefined }
7-
| { isLoaded: true; isSignedIn: false; user: null }
8-
| { isLoaded: true; isSignedIn: true; user: UserResource };
9-
105
/**
116
* Returns the current auth state and if a user is signed in, the user object.
127
*

‎packages/types/src/hooks.ts

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import type { OrganizationCustomRoleKey } from 'organizationMembership';
2+
import type { SignInResource } from 'signIn';
3+
4+
import type { SetActive, SignOut } from './clerk';
5+
import type { ActJWTClaim } from './jwt';
6+
import type {
7+
ActiveSessionResource,
8+
CheckAuthorizationWithCustomPermissions,
9+
GetToken,
10+
SessionResource,
11+
} from './session';
12+
import type { SignUpResource } from './signUp';
13+
import type { UserResource } from './user';
14+
15+
type CheckAuthorizationSignedOut = undefined;
16+
type CheckAuthorizationWithoutOrgOrUser = (params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
17+
18+
export type UseAuthReturn =
19+
| {
20+
isLoaded: false;
21+
isSignedIn: undefined;
22+
userId: undefined;
23+
sessionId: undefined;
24+
actor: undefined;
25+
orgId: undefined;
26+
orgRole: undefined;
27+
orgSlug: undefined;
28+
has: CheckAuthorizationSignedOut;
29+
signOut: SignOut;
30+
getToken: GetToken;
31+
}
32+
| {
33+
isLoaded: true;
34+
isSignedIn: false;
35+
userId: null;
36+
sessionId: null;
37+
actor: null;
38+
orgId: null;
39+
orgRole: null;
40+
orgSlug: null;
41+
has: CheckAuthorizationWithoutOrgOrUser;
42+
signOut: SignOut;
43+
getToken: GetToken;
44+
}
45+
| {
46+
isLoaded: true;
47+
isSignedIn: true;
48+
userId: string;
49+
sessionId: string;
50+
actor: ActJWTClaim | null;
51+
orgId: null;
52+
orgRole: null;
53+
orgSlug: null;
54+
has: CheckAuthorizationWithCustomPermissions;
55+
signOut: SignOut;
56+
getToken: GetToken;
57+
}
58+
| {
59+
isLoaded: true;
60+
isSignedIn: true;
61+
userId: string;
62+
sessionId: string;
63+
actor: ActJWTClaim | null;
64+
orgId: string;
65+
orgRole: OrganizationCustomRoleKey;
66+
orgSlug: string | null;
67+
has: CheckAuthorizationWithCustomPermissions;
68+
signOut: SignOut;
69+
getToken: GetToken;
70+
};
71+
72+
export type UseSignInReturn =
73+
| {
74+
isLoaded: false;
75+
signIn: undefined;
76+
setActive: undefined;
77+
}
78+
| {
79+
isLoaded: true;
80+
signIn: SignInResource;
81+
setActive: SetActive;
82+
};
83+
84+
export type UseSignUpReturn =
85+
| {
86+
isLoaded: false;
87+
signUp: undefined;
88+
setActive: undefined;
89+
}
90+
| {
91+
isLoaded: true;
92+
signUp: SignUpResource;
93+
setActive: SetActive;
94+
};
95+
96+
export type UseSessionReturn =
97+
| { isLoaded: false; isSignedIn: undefined; session: undefined }
98+
| { isLoaded: true; isSignedIn: false; session: null }
99+
| { isLoaded: true; isSignedIn: true; session: ActiveSessionResource };
100+
101+
export type UseSessionListReturn =
102+
| {
103+
isLoaded: false;
104+
sessions: undefined;
105+
setActive: undefined;
106+
}
107+
| {
108+
isLoaded: true;
109+
sessions: SessionResource[];
110+
setActive: SetActive;
111+
};
112+
113+
export type UseUserReturn =
114+
| { isLoaded: false; isSignedIn: undefined; user: undefined }
115+
| { isLoaded: true; isSignedIn: false; user: null }
116+
| { isLoaded: true; isSignedIn: true; user: UserResource };

‎packages/types/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from './environment';
1313
export * from './externalAccount';
1414
export * from './enterpriseAccount';
1515
export * from './factors';
16+
export * from './hooks';
1617
export * from './identificationLink';
1718
export * from './identifiers';
1819
export * from './image';

‎packages/vue/src/composables/useAuth.ts

+1-65
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import type {
2-
ActJWTClaim,
3-
CheckAuthorizationWithCustomPermissions,
4-
Clerk,
5-
GetToken,
6-
OrganizationCustomRoleKey,
7-
SignOut,
8-
} from '@clerk/types';
1+
import type { CheckAuthorizationWithCustomPermissions, Clerk, GetToken, SignOut, UseAuthReturn } from '@clerk/types';
92
import { computed, type ShallowRef, watch } from 'vue';
103

114
import { errorThrower } from '../errors/errorThrower';
@@ -14,9 +7,6 @@ import type { ToComputedRefs } from '../utils';
147
import { toComputedRefs } from '../utils';
158
import { useClerkContext } from './useClerkContext';
169

17-
type CheckAuthorizationSignedOut = undefined;
18-
type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
19-
2010
/**
2111
* @internal
2212
*/
@@ -55,60 +45,6 @@ function createSignOut(clerk: ShallowRef<Clerk | null>) {
5545
};
5646
}
5747

58-
type UseAuthReturn =
59-
| {
60-
isLoaded: false;
61-
isSignedIn: undefined;
62-
userId: undefined;
63-
sessionId: undefined;
64-
actor: undefined;
65-
orgId: undefined;
66-
orgRole: undefined;
67-
orgSlug: undefined;
68-
has: CheckAuthorizationSignedOut;
69-
signOut: SignOut;
70-
getToken: GetToken;
71-
}
72-
| {
73-
isLoaded: true;
74-
isSignedIn: false;
75-
userId: null;
76-
sessionId: null;
77-
actor: null;
78-
orgId: null;
79-
orgRole: null;
80-
orgSlug: null;
81-
has: CheckAuthorizationWithoutOrgOrUser;
82-
signOut: SignOut;
83-
getToken: GetToken;
84-
}
85-
| {
86-
isLoaded: true;
87-
isSignedIn: true;
88-
userId: string;
89-
sessionId: string;
90-
actor: ActJWTClaim | null;
91-
orgId: null;
92-
orgRole: null;
93-
orgSlug: null;
94-
has: CheckAuthorizationWithoutOrgOrUser;
95-
signOut: SignOut;
96-
getToken: GetToken;
97-
}
98-
| {
99-
isLoaded: true;
100-
isSignedIn: true;
101-
userId: string;
102-
sessionId: string;
103-
actor: ActJWTClaim | null;
104-
orgId: string;
105-
orgRole: OrganizationCustomRoleKey;
106-
orgSlug: string | null;
107-
has: CheckAuthorizationWithCustomPermissions;
108-
signOut: SignOut;
109-
getToken: GetToken;
110-
};
111-
11248
type UseAuth = () => ToComputedRefs<UseAuthReturn>;
11349

11450
/**

‎packages/vue/src/composables/useSession.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import type { ActiveSessionResource } from '@clerk/types';
1+
import type { UseSessionReturn } from '@clerk/types';
22
import { computed } from 'vue';
33

44
import type { ToComputedRefs } from '../utils';
55
import { toComputedRefs } from '../utils';
66
import { useClerkContext } from './useClerkContext';
77

8-
type UseSessionReturn =
9-
| { isLoaded: false; isSignedIn: undefined; session: undefined }
10-
| { isLoaded: true; isSignedIn: false; session: null }
11-
| { isLoaded: true; isSignedIn: true; session: ActiveSessionResource };
12-
138
type UseSession = () => ToComputedRefs<UseSessionReturn>;
149

1510
/**

‎packages/vue/src/composables/useSessionList.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import type { SessionResource, SetActive } from '@clerk/types';
1+
import type { UseSessionListReturn } from '@clerk/types';
22
import { computed } from 'vue';
33

44
import type { ToComputedRefs } from '../utils';
55
import { toComputedRefs } from '../utils';
66
import { useClerkContext } from './useClerkContext';
77

8-
type UseSessionListReturn =
9-
| { isLoaded: false; sessions: undefined; setActive: undefined }
10-
| { isLoaded: true; sessions: SessionResource[]; setActive: SetActive };
11-
128
type UseSessionList = () => ToComputedRefs<UseSessionListReturn>;
139

1410
/**

‎packages/vue/src/composables/useSignIn.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { eventMethodCalled } from '@clerk/shared/telemetry';
2-
import type { SetActive, SignInResource } from '@clerk/types';
2+
import type { UseSignInReturn } from '@clerk/types';
33
import { computed, watch } from 'vue';
44

55
import type { ToComputedRefs } from '../utils';
66
import { toComputedRefs } from '../utils';
77
import { useClerkContext } from './useClerkContext';
88

9-
type UseSignInReturn =
10-
| { isLoaded: false; signIn: undefined; setActive: undefined }
11-
| { isLoaded: true; signIn: SignInResource; setActive: SetActive };
12-
139
type UseSignIn = () => ToComputedRefs<UseSignInReturn>;
1410

1511
/**

‎packages/vue/src/composables/useSignUp.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { eventMethodCalled } from '@clerk/shared/telemetry';
2-
import type { SetActive, SignUpResource } from '@clerk/types';
2+
import type { UseSignUpReturn } from '@clerk/types';
33
import { computed, watch } from 'vue';
44

55
import type { ToComputedRefs } from '../utils';
66
import { toComputedRefs } from '../utils';
77
import { useClerkContext } from './useClerkContext';
88

9-
type UseSignUpReturn =
10-
| { isLoaded: false; signUp: undefined; setActive: undefined }
11-
| { isLoaded: true; signUp: SignUpResource; setActive: SetActive };
12-
139
type UseSignUp = () => ToComputedRefs<UseSignUpReturn>;
1410

1511
/**

‎packages/vue/src/composables/useUser.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import type { UserResource } from '@clerk/types';
1+
import type { UseUserReturn } from '@clerk/types';
22
import { computed } from 'vue';
33

44
import type { ToComputedRefs } from '../utils';
55
import { toComputedRefs } from '../utils';
66
import { useClerkContext } from './useClerkContext';
77

8-
type UseUserReturn =
9-
| { isLoaded: false; isSignedIn: undefined; user: undefined }
10-
| { isLoaded: true; isSignedIn: false; user: null }
11-
| { isLoaded: true; isSignedIn: true; user: UserResource };
12-
138
type UseUser = () => ToComputedRefs<UseUserReturn>;
149

1510
/**

0 commit comments

Comments
 (0)
Please sign in to comment.