Skip to content

Commit dd4e9a2

Browse files
committed
chore(clerk-js): Refactor Clerk.#environment modifier to protected to use type assert
1 parent ef23574 commit dd4e9a2

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

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

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ export class Clerk implements ClerkInterface {
150150
public telemetry: TelemetryCollector | undefined;
151151

152152
protected internal_last_error: ClerkAPIError | null = null;
153+
// converted to protected environment to support `updateEnvironment` type assertion
154+
protected environment?: EnvironmentResource | null;
153155

154156
#publishableKey: string = '';
155157
#domain: DomainOrProxyUrl['domain'];
156158
#proxyUrl: DomainOrProxyUrl['proxyUrl'];
157159
#authService: SessionCookieService | null = null;
158160
#broadcastChannel: LocalStorageBroadcastChannel<ClerkCoreBroadcastChannelEvent> | null = null;
159161
#componentControls?: ReturnType<MountComponentRenderer> | null;
160-
#environment?: EnvironmentResource | null;
161162
//@ts-expect-error with being undefined even though it's not possible - related to issue with ts and error thrower
162163
#fapiClient: FapiClient;
163164
#instanceType?: InstanceType;
@@ -328,7 +329,7 @@ export class Clerk implements ClerkInterface {
328329

329330
public openSignIn = (props?: SignInProps): void => {
330331
this.assertComponentsReady(this.#componentControls);
331-
if (sessionExistsAndSingleSessionModeEnabled(this, this.#environment)) {
332+
if (sessionExistsAndSingleSessionModeEnabled(this, this.environment)) {
332333
if (this.#instanceType === 'development') {
333334
throw new ClerkRuntimeError(warnings.cannotOpenSignInOrSignUp, {
334335
code: 'cannot_render_single_session_enabled',
@@ -348,7 +349,7 @@ export class Clerk implements ClerkInterface {
348349

349350
public openSignUp = (props?: SignUpProps): void => {
350351
this.assertComponentsReady(this.#componentControls);
351-
if (sessionExistsAndSingleSessionModeEnabled(this, this.#environment)) {
352+
if (sessionExistsAndSingleSessionModeEnabled(this, this.environment)) {
352353
if (this.#instanceType === 'development') {
353354
throw new ClerkRuntimeError(warnings.cannotOpenSignInOrSignUp, {
354355
code: 'cannot_render_single_session_enabled',
@@ -388,7 +389,7 @@ export class Clerk implements ClerkInterface {
388389

389390
public openOrganizationProfile = (props?: OrganizationProfileProps): void => {
390391
this.assertComponentsReady(this.#componentControls);
391-
if (disabledOrganizationsFeature(this, this.#environment)) {
392+
if (disabledOrganizationsFeature(this, this.environment)) {
392393
if (this.#instanceType === 'development') {
393394
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
394395
code: 'cannot_render_organizations_disabled',
@@ -416,7 +417,7 @@ export class Clerk implements ClerkInterface {
416417

417418
public openCreateOrganization = (props?: CreateOrganizationProps): void => {
418419
this.assertComponentsReady(this.#componentControls);
419-
if (disabledOrganizationsFeature(this, this.#environment)) {
420+
if (disabledOrganizationsFeature(this, this.environment)) {
420421
if (this.#instanceType === 'development') {
421422
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
422423
code: 'cannot_render_organizations_disabled',
@@ -535,7 +536,7 @@ export class Clerk implements ClerkInterface {
535536

536537
public mountOrganizationProfile = (node: HTMLDivElement, props?: OrganizationProfileProps) => {
537538
this.assertComponentsReady(this.#componentControls);
538-
if (disabledOrganizationsFeature(this, this.#environment)) {
539+
if (disabledOrganizationsFeature(this, this.environment)) {
539540
if (this.#instanceType === 'development') {
540541
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
541542
code: 'cannot_render_organizations_disabled',
@@ -575,7 +576,7 @@ export class Clerk implements ClerkInterface {
575576

576577
public mountCreateOrganization = (node: HTMLDivElement, props?: CreateOrganizationProps) => {
577578
this.assertComponentsReady(this.#componentControls);
578-
if (disabledOrganizationsFeature(this, this.#environment)) {
579+
if (disabledOrganizationsFeature(this, this.environment)) {
579580
if (this.#instanceType === 'development') {
580581
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
581582
code: 'cannot_render_organizations_disabled',
@@ -606,7 +607,7 @@ export class Clerk implements ClerkInterface {
606607

607608
public mountOrganizationSwitcher = (node: HTMLDivElement, props?: OrganizationSwitcherProps) => {
608609
this.assertComponentsReady(this.#componentControls);
609-
if (disabledOrganizationsFeature(this, this.#environment)) {
610+
if (disabledOrganizationsFeature(this, this.environment)) {
610611
if (this.#instanceType === 'development') {
611612
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationSwitcher'), {
612613
code: 'cannot_render_organizations_disabled',
@@ -633,7 +634,7 @@ export class Clerk implements ClerkInterface {
633634

634635
public mountOrganizationList = (node: HTMLDivElement, props?: OrganizationListProps) => {
635636
this.assertComponentsReady(this.#componentControls);
636-
if (disabledOrganizationsFeature(this, this.#environment)) {
637+
if (disabledOrganizationsFeature(this, this.environment)) {
637638
if (this.#instanceType === 'development') {
638639
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationList'), {
639640
code: 'cannot_render_organizations_disabled',
@@ -842,17 +843,17 @@ export class Clerk implements ClerkInterface {
842843
}
843844

844845
public buildUserProfileUrl(): string {
845-
if (!this.#environment || !this.#environment.displayConfig) {
846+
if (!this.environment || !this.environment.displayConfig) {
846847
return '';
847848
}
848-
return this.buildUrlWithAuth(this.#environment.displayConfig.userProfileUrl);
849+
return this.buildUrlWithAuth(this.environment.displayConfig.userProfileUrl);
849850
}
850851

851852
public buildHomeUrl(): string {
852-
if (!this.#environment || !this.#environment.displayConfig) {
853+
if (!this.environment || !this.environment.displayConfig) {
853854
return '';
854855
}
855-
return this.buildUrlWithAuth(this.#environment.displayConfig.homeUrl);
856+
return this.buildUrlWithAuth(this.environment.displayConfig.homeUrl);
856857
}
857858

858859
public buildAfterSignInUrl(): string {
@@ -872,17 +873,17 @@ export class Clerk implements ClerkInterface {
872873
}
873874

874875
public buildCreateOrganizationUrl(): string {
875-
if (!this.#environment || !this.#environment.displayConfig) {
876+
if (!this.environment || !this.environment.displayConfig) {
876877
return '';
877878
}
878-
return this.buildUrlWithAuth(this.#environment.displayConfig.createOrganizationUrl);
879+
return this.buildUrlWithAuth(this.environment.displayConfig.createOrganizationUrl);
879880
}
880881

881882
public buildOrganizationProfileUrl(): string {
882-
if (!this.#environment || !this.#environment.displayConfig) {
883+
if (!this.environment || !this.environment.displayConfig) {
883884
return '';
884885
}
885-
return this.buildUrlWithAuth(this.#environment.displayConfig.organizationProfileUrl);
886+
return this.buildUrlWithAuth(this.environment.displayConfig.organizationProfileUrl);
886887
}
887888

888889
#redirectToSatellite = async (): Promise<unknown> => {
@@ -1015,11 +1016,11 @@ export class Clerk implements ClerkInterface {
10151016
params: HandleOAuthCallbackParams = {},
10161017
customNavigate?: (to: string) => Promise<unknown>,
10171018
): Promise<unknown> => {
1018-
if (!this.loaded || !this.#environment || !this.client) {
1019+
if (!this.loaded || !this.environment || !this.client) {
10191020
return;
10201021
}
10211022
const { signIn, signUp } = this.client;
1022-
const { displayConfig } = this.#environment;
1023+
const { displayConfig } = this.environment;
10231024
const { firstFactorVerification } = signIn;
10241025
const { externalAccount } = signUp.verifications;
10251026
const su = {
@@ -1205,7 +1206,7 @@ export class Clerk implements ClerkInterface {
12051206
customNavigate,
12061207
unsafeMetadata,
12071208
}: AuthenticateWithMetamaskParams = {}): Promise<void> => {
1208-
if (!this.client || !this.#environment) {
1209+
if (!this.client || !this.environment) {
12091210
return;
12101211
}
12111212

@@ -1252,7 +1253,7 @@ export class Clerk implements ClerkInterface {
12521253
Organization.get(organizationId);
12531254

12541255
public updateEnvironment(environment: EnvironmentResource): asserts this is { environment: EnvironmentResource } {
1255-
this.#environment = environment;
1256+
this.environment = environment;
12561257
this.#authService?.setEnvironment(environment);
12571258
}
12581259

@@ -1292,16 +1293,16 @@ export class Clerk implements ClerkInterface {
12921293
};
12931294

12941295
get __unstable__environment(): EnvironmentResource | null | undefined {
1295-
return this.#environment;
1296+
return this.environment;
12961297
}
12971298

12981299
// TODO: Fix this properly
12991300
// eslint-disable-next-line @typescript-eslint/require-await
13001301
__unstable__setEnvironment = async (env: EnvironmentJSON) => {
1301-
this.#environment = new Environment(env);
1302+
this.environment = new Environment(env);
13021303

13031304
if (Clerk.mountComponentRenderer) {
1304-
this.#componentControls = Clerk.mountComponentRenderer(this, this.#environment, this.#options);
1305+
this.#componentControls = Clerk.mountComponentRenderer(this, this.environment, this.#options);
13051306
}
13061307
};
13071308

@@ -1485,7 +1486,7 @@ export class Clerk implements ClerkInterface {
14851486
}
14861487

14871488
if (Clerk.mountComponentRenderer) {
1488-
this.#componentControls = Clerk.mountComponentRenderer(this, this.#environment as Environment, this.#options);
1489+
this.#componentControls = Clerk.mountComponentRenderer(this, this.environment as Environment, this.#options);
14891490
}
14901491

14911492
break;
@@ -1523,7 +1524,7 @@ export class Clerk implements ClerkInterface {
15231524
// TODO: Add an auth service also for non standard browsers that will poll for the __session JWT but won't use cookies
15241525

15251526
if (Clerk.mountComponentRenderer) {
1526-
this.#componentControls = Clerk.mountComponentRenderer(this, this.#environment, this.#options);
1527+
this.#componentControls = Clerk.mountComponentRenderer(this, this.environment, this.#options);
15271528
}
15281529

15291530
return true;
@@ -1632,10 +1633,10 @@ export class Clerk implements ClerkInterface {
16321633
};
16331634

16341635
#buildUrl = (key: 'signInUrl' | 'signUpUrl', params?: Record<string, string>): string => {
1635-
if (!key || !this.loaded || !this.#environment || !this.#environment.displayConfig) {
1636+
if (!key || !this.loaded || !this.environment || !this.environment.displayConfig) {
16361637
return '';
16371638
}
1638-
const signInOrUpUrl = this.#options[key] || this.#environment.displayConfig[key];
1639+
const signInOrUpUrl = this.#options[key] || this.environment.displayConfig[key];
16391640
const redirectUrls = new RedirectUrls(this.#options, params);
16401641
return this.buildUrlWithAuth(redirectUrls.appendPreservedPropsToUrl(signInOrUpUrl, params));
16411642
};
@@ -1659,9 +1660,9 @@ export class Clerk implements ClerkInterface {
16591660
}
16601661

16611662
const userSignedIn = this.session;
1662-
const signInUrl = this.#options.signInUrl || this.#environment?.displayConfig.signInUrl;
1663+
const signInUrl = this.#options.signInUrl || this.environment?.displayConfig.signInUrl;
16631664
const referrerIsSignInUrl = signInUrl && window.location.href.startsWith(signInUrl);
1664-
const signUpUrl = this.#options.signUpUrl || this.#environment?.displayConfig.signUpUrl;
1665+
const signUpUrl = this.#options.signUpUrl || this.environment?.displayConfig.signUpUrl;
16651666
const referrerIsSignUpUrl = signUpUrl && window.location.href.startsWith(signUpUrl);
16661667

16671668
// don't redirect if user is not signed in and referrer is sign in/up url

0 commit comments

Comments
 (0)