Skip to content

Commit 7130db0

Browse files
authored
fix(clerk-react): Missing methods and properties from IsomorphicClerk (clerk#2226) (clerk#2233)
1 parent 6cef817 commit 7130db0

5 files changed

Lines changed: 227 additions & 26 deletions

File tree

‎.changeset/mean-poets-bow.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/clerk-react': patch
4+
---
5+
6+
Sync IsomorphicClerk with the clerk singleton and the LoadedClerk interface. IsomorphicClerk now extends from LoadedClerk.

‎packages/clerk-js/src/core/clerk.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ export default class Clerk implements ClerkInterface {
154154
version: __PKG_VERSION__,
155155
};
156156

157-
public client?: ClientResource;
158-
public session?: ActiveSessionResource | null;
159-
public organization?: OrganizationResource | null;
160-
public user?: UserResource | null;
157+
public client: ClientResource | undefined;
158+
public session: ActiveSessionResource | null | undefined;
159+
public organization: OrganizationResource | null | undefined;
160+
public user: UserResource | null | undefined;
161161
public __internal_country?: string | null;
162162
public readonly frontendApi: string;
163-
public readonly publishableKey?: string;
163+
public readonly publishableKey: string | undefined;
164164

165165
protected internal_last_error: ClerkAPIError | null = null;
166166

@@ -191,6 +191,10 @@ export default class Clerk implements ClerkInterface {
191191
return Clerk.version;
192192
}
193193

194+
get sdkMetadata(): SDKMetadata {
195+
return Clerk.sdkMetadata;
196+
}
197+
194198
get loaded(): boolean {
195199
return this.#isReady;
196200
}

‎packages/react/src/errors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const invalidStateError =
3030
'Clerk: Invalid state. Feel free to submit a bug or reach out to support here: https://clerk.com/support';
3131

3232
export const unsupportedNonBrowserDomainOrProxyUrlFunction =
33-
'Clerk: Unsupported usage of domain or proxyUrl. The usage of domain or proxyUrl as function is not supported in non-browser environments.';
33+
'Clerk: Unsupported usage of isSatellite, domain or proxyUrl. The usage of isSatellite, domain or proxyUrl as function is not supported in non-browser environments.';
3434

3535
export const userProfilePageRenderedError =
3636
'Clerk: <UserProfile.Page /> component needs to be a direct child of `<UserProfile />` or `<UserButton />`.';

‎packages/react/src/isomorphicClerk.ts‎

Lines changed: 194 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
ActiveSessionResource,
66
AuthenticateWithMetamaskParams,
77
BeforeEmitCallback,
8+
BuildUrlWithAuthParams,
89
Clerk,
910
ClientResource,
1011
CreateOrganizationParams,
@@ -13,10 +14,16 @@ import type {
1314
HandleEmailLinkVerificationParams,
1415
HandleMagicLinkVerificationParams,
1516
HandleOAuthCallbackParams,
17+
InstanceType,
1618
ListenerCallback,
19+
LoadedClerk,
1720
OrganizationListProps,
1821
OrganizationMembershipResource,
22+
OrganizationProfileProps,
1923
OrganizationResource,
24+
OrganizationSwitcherProps,
25+
RedirectOptions,
26+
SDKMetadata,
2027
SetActiveParams,
2128
SignInProps,
2229
SignInRedirectOptions,
@@ -30,7 +37,6 @@ import type {
3037
UserProfileProps,
3138
UserResource,
3239
} from '@clerk/types';
33-
import type { OrganizationProfileProps, OrganizationSwitcherProps } from '@clerk/types';
3440

3541
import { unsupportedNonBrowserDomainOrProxyUrlFunction } from './errors';
3642
import type {
@@ -57,10 +63,84 @@ type MethodName<T> = {
5763

5864
type MethodCallback = () => Promise<unknown> | unknown;
5965

60-
export default class IsomorphicClerk {
66+
type IsomorphicLoadedClerk = Omit<
67+
LoadedClerk,
68+
/**
69+
* Override ClerkJS methods in order to support premountMethodCalls
70+
*/
71+
| 'buildSignInUrl'
72+
| 'buildSignUpUrl'
73+
| 'buildUserProfileUrl'
74+
| 'buildCreateOrganizationUrl'
75+
| 'buildOrganizationProfileUrl'
76+
| 'buildHomeUrl'
77+
| 'buildUrlWithAuth'
78+
| 'redirectWithAuth'
79+
| 'redirectToSignIn'
80+
| 'redirectToSignUp'
81+
| 'handleRedirectCallback'
82+
| 'handleUnauthenticated'
83+
| 'authenticateWithMetamask'
84+
| 'createOrganization'
85+
| 'getOrganization'
86+
| 'mountUserButton'
87+
| 'mountOrganizationList'
88+
| 'mountOrganizationSwitcher'
89+
| 'mountOrganizationProfile'
90+
| 'mountCreateOrganization'
91+
| 'mountSignUp'
92+
| 'mountSignIn'
93+
| 'mountUserProfile'
94+
| 'client'
95+
| 'getOrganizationMemberships'
96+
> & {
97+
// TODO: Align return type
98+
redirectWithAuth: (...args: Parameters<Clerk['redirectWithAuth']>) => void;
99+
// TODO: Align return type
100+
redirectToSignIn: (options: SignInRedirectOptions) => void;
101+
// TODO: Align return type
102+
redirectToSignUp: (options: SignUpRedirectOptions) => void;
103+
// TODO: Align return type and parms
104+
handleRedirectCallback: (params: HandleOAuthCallbackParams) => void;
105+
handleUnauthenticated: () => void;
106+
// TODO: Align Promise unknown
107+
authenticateWithMetamask: (params: AuthenticateWithMetamaskParams) => Promise<void>;
108+
// TODO: Align return type (maybe not possible or correct)
109+
createOrganization: (params: CreateOrganizationParams) => Promise<OrganizationResource | void>;
110+
// TODO: Align return type (maybe not possible or correct)
111+
getOrganization: (organizationId: string) => Promise<OrganizationResource | void>;
112+
113+
// TODO: Align return type
114+
buildSignInUrl: (opts?: RedirectOptions) => string | void;
115+
// TODO: Align return type
116+
buildSignUpUrl: (opts?: RedirectOptions) => string | void;
117+
// TODO: Align return type
118+
buildUserProfileUrl: () => string | void;
119+
// TODO: Align return type
120+
buildCreateOrganizationUrl: () => string | void;
121+
// TODO: Align return type
122+
buildOrganizationProfileUrl: () => string | void;
123+
// TODO: Align return type
124+
buildHomeUrl: () => string | void;
125+
// TODO: Align return type
126+
buildUrlWithAuth: (to: string, opts?: BuildUrlWithAuthParams | undefined) => string | void;
127+
128+
// TODO: Align optional props
129+
mountUserButton: (node: HTMLDivElement, props: UserButtonProps) => void;
130+
mountOrganizationList: (node: HTMLDivElement, props: OrganizationListProps) => void;
131+
mountOrganizationSwitcher: (node: HTMLDivElement, props: OrganizationSwitcherProps) => void;
132+
mountOrganizationProfile: (node: HTMLDivElement, props: OrganizationProfileProps) => void;
133+
mountCreateOrganization: (node: HTMLDivElement, props: CreateOrganizationProps) => void;
134+
mountSignUp: (node: HTMLDivElement, props: SignUpProps) => void;
135+
mountSignIn: (node: HTMLDivElement, props: SignInProps) => void;
136+
mountUserProfile: (node: HTMLDivElement, props: UserProfileProps) => void;
137+
client: ClientResource | undefined;
138+
139+
getOrganizationMemberships: () => Promise<OrganizationMembershipResource[] | void>;
140+
};
141+
142+
export default class IsomorphicClerk implements IsomorphicLoadedClerk {
61143
private readonly mode: 'browser' | 'server';
62-
private readonly frontendApi?: string;
63-
private readonly publishableKey?: string;
64144
private readonly options: IsomorphicClerkOptions;
65145
private readonly Clerk: ClerkProp;
66146
private clerkjs: BrowserClerk | HeadlessBrowserClerk | null = null;
@@ -83,6 +163,12 @@ export default class IsomorphicClerk {
83163
#loaded = false;
84164
#domain: DomainOrProxyUrl['domain'];
85165
#proxyUrl: DomainOrProxyUrl['proxyUrl'];
166+
#frontendApi: string | undefined;
167+
#publishableKey: string | undefined;
168+
169+
get publishableKey(): string | undefined {
170+
return this.#publishableKey;
171+
}
86172

87173
get loaded(): boolean {
88174
return this.#loaded;
@@ -127,8 +213,8 @@ export default class IsomorphicClerk {
127213

128214
constructor(options: IsomorphicClerkOptions) {
129215
const { Clerk = null, frontendApi, publishableKey } = options || {};
130-
this.frontendApi = frontendApi;
131-
this.publishableKey = publishableKey;
216+
this.#frontendApi = frontendApi;
217+
this.#publishableKey = publishableKey;
132218
this.#proxyUrl = options?.proxyUrl;
133219
this.#domain = options?.domain;
134220
this.options = options;
@@ -137,6 +223,108 @@ export default class IsomorphicClerk {
137223
void this.loadClerkJS();
138224
}
139225

226+
get sdkMetadata(): SDKMetadata | undefined {
227+
return this.clerkjs?.sdkMetadata || this.options.sdkMetadata || undefined;
228+
}
229+
230+
get instanceType(): InstanceType | undefined {
231+
return this.clerkjs?.instanceType;
232+
}
233+
234+
get frontendApi(): string {
235+
return this.clerkjs?.frontendApi || this.#frontendApi || '';
236+
}
237+
238+
get isStandardBrowser(): boolean {
239+
return this.clerkjs?.isStandardBrowser || this.options.standardBrowser || false;
240+
}
241+
242+
get isSatellite(): boolean {
243+
// This getter can run in environments where window is not available.
244+
// In those cases we should expect and use domain as a string
245+
if (typeof window !== 'undefined' && window.location) {
246+
return handleValueOrFn(this.options.isSatellite, new URL(window.location.href), false);
247+
}
248+
if (typeof this.options.isSatellite === 'function') {
249+
throw new Error(unsupportedNonBrowserDomainOrProxyUrlFunction);
250+
}
251+
return false;
252+
}
253+
254+
isReady = (): boolean => Boolean(this.clerkjs?.isReady());
255+
256+
buildSignInUrl = (opts?: RedirectOptions): string | void => {
257+
const callback = () => this.clerkjs?.buildSignInUrl(opts) || '';
258+
if (this.clerkjs && this.#loaded) {
259+
return callback();
260+
} else {
261+
this.premountMethodCalls.set('buildSignInUrl', callback);
262+
}
263+
};
264+
265+
buildSignUpUrl = (opts?: RedirectOptions): string | void => {
266+
const callback = () => this.clerkjs?.buildSignUpUrl(opts) || '';
267+
if (this.clerkjs && this.#loaded) {
268+
return callback();
269+
} else {
270+
this.premountMethodCalls.set('buildSignUpUrl', callback);
271+
}
272+
};
273+
274+
buildUserProfileUrl = (): string | void => {
275+
const callback = () => this.clerkjs?.buildUserProfileUrl() || '';
276+
if (this.clerkjs && this.#loaded) {
277+
return callback();
278+
} else {
279+
this.premountMethodCalls.set('buildUserProfileUrl', callback);
280+
}
281+
};
282+
283+
buildCreateOrganizationUrl = (): string | void => {
284+
const callback = () => this.clerkjs?.buildCreateOrganizationUrl() || '';
285+
if (this.clerkjs && this.#loaded) {
286+
return callback();
287+
} else {
288+
this.premountMethodCalls.set('buildCreateOrganizationUrl', callback);
289+
}
290+
};
291+
292+
buildOrganizationProfileUrl = (): string | void => {
293+
const callback = () => this.clerkjs?.buildOrganizationProfileUrl() || '';
294+
if (this.clerkjs && this.#loaded) {
295+
return callback();
296+
} else {
297+
this.premountMethodCalls.set('buildOrganizationProfileUrl', callback);
298+
}
299+
};
300+
301+
buildHomeUrl = (): string | void => {
302+
const callback = () => this.clerkjs?.buildHomeUrl() || '';
303+
if (this.clerkjs && this.#loaded) {
304+
return callback();
305+
} else {
306+
this.premountMethodCalls.set('buildHomeUrl', callback);
307+
}
308+
};
309+
310+
buildUrlWithAuth = (to: string, opts?: BuildUrlWithAuthParams | undefined): string | void => {
311+
const callback = () => this.clerkjs?.buildUrlWithAuth(to, opts) || '';
312+
if (this.clerkjs && this.#loaded) {
313+
return callback();
314+
} else {
315+
this.premountMethodCalls.set('buildUrlWithAuth', callback);
316+
}
317+
};
318+
319+
handleUnauthenticated = (): void => {
320+
const callback = () => this.clerkjs?.handleUnauthenticated();
321+
if (this.clerkjs && this.#loaded) {
322+
void callback();
323+
} else {
324+
this.premountMethodCalls.set('handleUnauthenticated', callback);
325+
}
326+
};
327+
140328
async loadClerkJS(): Promise<HeadlessBrowserClerk | BrowserClerk | undefined> {
141329
if (this.mode !== 'browser' || this.#loaded) {
142330
return;

‎packages/types/src/clerk.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ export interface Clerk {
6060
/**
6161
* Clerk SDK version number.
6262
*/
63-
version?: string;
63+
version: string | undefined;
6464

6565
/**
6666
* If present, contains information about the SDK that the host application is using.
6767
* For example, if Clerk is loaded through `@clerk/nextjs`, this would be `{ name: '@clerk/nextjs', version: '1.0.0' }`
6868
*/
69-
sdkMetadata?: SDKMetadata;
69+
sdkMetadata: SDKMetadata | undefined;
7070

71+
/**
72+
* If true the bootstrapping of Clerk.load() has completed successfully.
73+
*/
7174
loaded: boolean;
7275

7376
/**
@@ -77,10 +80,10 @@ export interface Clerk {
7780
frontendApi: string;
7881

7982
/** Clerk Publishable Key string. */
80-
publishableKey?: string;
83+
publishableKey: string | undefined;
8184

8285
/** Clerk Proxy url string. */
83-
proxyUrl?: string;
86+
proxyUrl: string | undefined;
8487

8588
/** Clerk Satellite Frontend API string. */
8689
domain: string;
@@ -89,22 +92,22 @@ export interface Clerk {
8992
isSatellite: boolean;
9093

9194
/** Clerk Instance type is defined from the Publishable key */
92-
instanceType?: InstanceType;
95+
instanceType: InstanceType | undefined;
9396

9497
/** Clerk flag for loading Clerk in a standard browser setup */
95-
isStandardBrowser?: boolean;
98+
isStandardBrowser: boolean | undefined;
9699

97100
/** Client handling most Clerk operations. */
98-
client?: ClientResource;
101+
client: ClientResource | undefined;
99102

100103
/** Active Session. */
101-
session?: ActiveSessionResource | null;
104+
session: ActiveSessionResource | null | undefined;
102105

103106
/** Active Organization */
104-
organization?: OrganizationResource | null;
107+
organization: OrganizationResource | null | undefined;
105108

106109
/** Current User. */
107-
user?: UserResource | null;
110+
user: UserResource | null | undefined;
108111

109112
/**
110113
* Signs out the current user on single-session instances, or all users on multi-session instances
@@ -790,8 +793,8 @@ export type UserButtonProps = {
790793
*/
791794
showName?: boolean;
792795
/**
793-
Controls the default state of the UserButton
794-
*/
796+
* Controls the default state of the UserButton
797+
*/
795798
defaultOpen?: boolean;
796799
/**
797800
* Full URL or path to navigate after sign out is complete
@@ -851,8 +854,8 @@ type LooseExtractedParams<T extends string> = `:${T}` | (string & NonNullable<un
851854

852855
export type OrganizationSwitcherProps = {
853856
/**
854-
Controls the default state of the OrganizationSwitcher
855-
*/
857+
* Controls the default state of the OrganizationSwitcher
858+
*/
856859
defaultOpen?: boolean;
857860
/**
858861
* By default, users can switch between organization and their personal account.

0 commit comments

Comments
 (0)