@@ -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
3541import { unsupportedNonBrowserDomainOrProxyUrlFunction } from './errors' ;
3642import type {
@@ -57,10 +63,84 @@ type MethodName<T> = {
5763
5864type 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 ;
0 commit comments