Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert class methods into properties wherever it is relevant #23

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
dist/
docs/
coverage/
.DS_Store
4 changes: 2 additions & 2 deletions packages/auth0-acul-js/interfaces/models/branding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export interface BrandingThemes{

/* @namespace Client */
export interface BrandingMembers {
getSettings(): BrandingSettings | null;
getThemes(): BrandingThemes | null;
settings: BrandingSettings | null;
themes: BrandingThemes | null;
}
2 changes: 1 addition & 1 deletion packages/auth0-acul-js/interfaces/models/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export interface ClientMembers {
name: string;
logoUri: string | null;
description: string | null;
getMetadata(): { [key: string]: string } | null;
metadata: { [key: string]: string } | null;
}
4 changes: 2 additions & 2 deletions packages/auth0-acul-js/interfaces/models/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface OrganizationMembers {
name: string | null;
usage: string | null;
displayName: string | null;
getBranding():
branding:
| {
logoUrl?: string | undefined;
colors?: {
Expand All @@ -29,5 +29,5 @@ export interface OrganizationMembers {
};
}
| null;
getMetadata(): { [key: string]: string } | null;
metadata: { [key: string]: string } | null;
}
8 changes: 4 additions & 4 deletions packages/auth0-acul-js/interfaces/models/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export interface ScreenMembers {
captchaSiteKey: string | null;
captchaProvider: string | null;
isCaptchaAvailable: boolean;
getScreenData(): Record<string, string> | null;
getScreenLinks(): Record<string, string> | null;
getScreenTexts(): Record<string, string> | null;
getCaptchaConfig(): CaptchaContext | null;
data: Record<string, string> | null;
links: Record<string, string> | null;
texts: Record<string, string> | null;
captcha: CaptchaContext | null;
}
4 changes: 2 additions & 2 deletions packages/auth0-acul-js/interfaces/models/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export interface TenantContext {
export interface TenantMembers {
name: string | null;
friendlyName: string | null;
getEnabledLocales(): string[] | null;
getEnabledFactors(): string[] | null;
enabledLocales: string[] | null;
enabledFactors: string[] | null;
}
6 changes: 3 additions & 3 deletions packages/auth0-acul-js/interfaces/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export interface TransactionMembers {
countryPrefix: CountryCode['prefix'] | null;
connectionStrategy: string | null;
hasErrors: boolean;
getErrors(): Error[] | null;
getCurrentConnection(): Connection | null;
getAlternateConnections(): (Connection | EnterpriseConnection)[] | null;
errors: Error[] | null;
currentConnection: Connection | null;
alternateConnections: (Connection | EnterpriseConnection)[] | null;
}
4 changes: 2 additions & 2 deletions packages/auth0-acul-js/interfaces/models/untrusted-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export interface UntrustedDataContext {
}

export interface UntrustedDataMembers {
getSubmittedFormData(): { [key: string]: any } | null;
getAuthParams(): {
submittedFormData: { [key: string]: any } | null;
authParams: {
loginHint: string | undefined;
screenHint: string | undefined;
uiLocales: string | undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/auth0-acul-js/interfaces/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export interface UserMembers {
email: string | null;
picture: string | null;
phoneNumber: string | null;
getEnrolledFactors(): Array<string> | null;
getOrganizations():
enrolledFactors: Array<string> | null;
organizations:
| {
organizationId: string | undefined;
organizationName: string | undefined;
Expand All @@ -35,6 +35,6 @@ export interface UserMembers {
| undefined;
}[]
| null;
getUserMetadata(): { [key: string]: string } | null;
getAppMetadata(): { [key: string]: string } | null;
userMetadata: { [key: string]: string } | null;
appMetadata: { [key: string]: string } | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface EmailChallengeOptions {
}

export interface ScreenMembersOnEmailIdentifierChallenge extends ScreenMembers {
getScreenData(): {
data: {
messageType?: string;
email?: string;
} | null;
Expand Down
6 changes: 3 additions & 3 deletions packages/auth0-acul-js/interfaces/screens/login-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export interface LoginId extends BaseContext {
export interface ScreenMembersOnLoginId extends ScreenMembers {
signupLink: string | null;
resetPasswordLink: string | null;
getPublicKey(): PasskeyRead['public_key'] | null;
publicKey: PasskeyRead['public_key'] | null;
}

export interface TransactionMembersOnLoginId extends TransactionMembers {
isSignupEnabled: boolean;
isPasskeyEnabled: boolean;
isForgotPasswordEnabled: boolean;
isUsernameRequired: boolean;
getUsernamePolicy(): UsernamePolicy | null;
getAllowedIdentifiers(): ('email' | 'username' | 'phone')[] | null;
usernamePolicy: UsernamePolicy | null;
allowedIdentifiers: ('email' | 'username' | 'phone')[] | null;
}

export interface LoginOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ScreenMembersOnLoginPassword extends ScreenMembers {
signupLink: string | null;
resetPasswordLink: string | null;
editIdentifierLink: string | null;
getScreenData(): {
data: {
username: string;
} | null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ScreenMembersOnLoginPasswordlessEmailCode extends ScreenMembers
editIdentifierLink: string | null;
resetPasswordLink: string | null;
signupLink: string | null;
getScreenData(): {
data: {
email?: string;
username?: string;
} | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface ScreenMembersOnLoginPasswordlessSmsOtp extends ScreenMembers {
signupLink: string | null;
resetPasswordLink: string | null;
backLink: string | null;
data: {
username: string;
}
}

export interface TransactionMembersOnLoginPasswordlessSmsOtp extends TransactionMembers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface AbortEnrollmentOptions {
}

export interface ScreenMembersOnPasskeyEnrollmentLocal extends ScreenMembers {
getPublicKey: () => PasskeyCreate['public_key'] | null;
publicKey: PasskeyCreate['public_key'] | null;
}

export interface PasskeyEnrollmentLocalMembers extends BaseMembers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface PasskeyEnrollment extends BaseContext {
export interface ScreenMembersOnPasskeyEnrollment extends ScreenMembers {
backLink: string | null;
loginLink: string | null;
getPublicKey: () => PasskeyCreate['public_key'] | null;
publicKey: PasskeyCreate['public_key'] | null;
}

export interface PasskeyEnrollmentMembers extends BaseMembers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ExtendedScreenContext extends ScreenContext {
}

export interface ScreenMembersOnPhoneIdentifierChallenge extends ScreenMembers {
getScreenData(): {
data: {
messageType?: 'text' | 'voice';
phone?: string;
} | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export interface ScreenDataOptions extends ScreenData {
}

export interface ScreenMembersOnPhoneIdentifierEnrollment extends ScreenMembers {
getScreenData(): {
messageType?: 'text' | 'voice';
data: {
phone?: 'string';
messageType?: 'text' | 'voice';
} | null;
editIdentifierLink: string | null;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/auth0-acul-js/interfaces/screens/signup-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export interface ScreenMembersOnSignupId extends ScreenMembers {

export interface TransactionMembersOnSignupId extends TransactionMembers {
isPasskeyEnabled: boolean;
getUsernamePolicy(): UsernamePolicy | null;
getRequiredIdentifiers(): ('email' | 'username' | 'phone')[] | null;
getOptionalIdentifiers(): ('email' | 'username' | 'phone')[] | null;
usernamePolicy: UsernamePolicy | null;
requiredIdentifiers: ('email' | 'username' | 'phone')[] | null;
optionalIdentifiers: ('email' | 'username' | 'phone')[] | null;
}

export interface SignupId extends BaseContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface SignupPassword extends BaseContext {
export interface ScreenMembersOnSignupPassword extends ScreenMembers {
loginLink: string | null;
editLink: string | null;
getScreenData(): {
data: {
email?: string;
phone?: string;
username?: string;
Expand All @@ -31,9 +31,9 @@ export interface ScreenMembersOnSignupPassword extends ScreenMembers {

export interface TransactionMembersOnSignupPassword extends TransactionMembers {
isPasskeyEnabled: boolean;
getPasswordPolicy(): PasswordPolicy | null;
getRequiredIdentifiers(): ('email' | 'username' | 'phone')[] | null;
getOptionalIdentifiers(): ('email' | 'username' | 'phone')[] | null;
passwordPolicy: PasswordPolicy | null;
requiredIdentifiers: ('email' | 'username' | 'phone')[] | null;
optionalIdentifiers: ('email' | 'username' | 'phone')[] | null;
}

export interface SignupPasswordOptions {
Expand Down
8 changes: 8 additions & 0 deletions packages/auth0-acul-js/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ export default {
],
},
roots: ['<rootDir>/tests/unit'],
collectCoverage: true,
coverageDirectory: '<rootDir>/coverage',
coverageReporters: ['text', 'lcov'],
collectCoverageFrom: [
'**/*.{ts,tsx}',
'!**/node_modules/**',
'!**/tests/**',
],
};
34 changes: 22 additions & 12 deletions packages/auth0-acul-js/src/models/base-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type { BaseContext as UniversalLoginContext, BaseMembers } from '../../in
import { Branding, Client, Prompt, Screen, Organization, User, Transaction, Tenant, UntrustedData } from '../models';

export class BaseContext implements BaseMembers {
private context: UniversalLoginContext;
branding: BrandingMembers;
screen: ScreenMembers;
tenant: TenantMembers;
Expand All @@ -25,21 +24,32 @@ export class BaseContext implements BaseMembers {
user: UserMembers;
untrustedData: UntrustedDataMembers;

private static context: UniversalLoginContext | null = null;

constructor() {
this.context = (window as any).universal_login_context as UniversalLoginContext;
this.branding = new Branding(this.context.branding);
this.screen = new Screen(this.context.screen);
this.tenant = new Tenant(this.context.tenant);
this.prompt = new Prompt(this.context.prompt);
this.organization = new Organization(this.context.organization);
this.client = new Client(this.context.client);
this.transaction = new Transaction(this.context.transaction);
this.user = new User(this.context.user);
this.untrustedData = new UntrustedData(this.context.untrusted_data);
if (!BaseContext.context) {
BaseContext.context = (window as any).universal_login_context as UniversalLoginContext;
}

const context = BaseContext.context;

this.branding = new Branding(context.branding);
this.screen = new Screen(context.screen);
this.tenant = new Tenant(context.tenant);
this.prompt = new Prompt(context.prompt);
this.organization = new Organization(context.organization);
this.client = new Client(context.client);
this.transaction = new Transaction(context.transaction);
this.user = new User(context.user);
this.untrustedData = new UntrustedData(context.untrusted_data);
}

/** @ignore */
getContext<K extends keyof UniversalLoginContext>(model: K): UniversalLoginContext[K] | undefined {
return this.context[model];
if (!BaseContext.context) {
BaseContext.context = (window as any).universal_login_context as UniversalLoginContext;
}

return BaseContext.context[model];
}
}
18 changes: 9 additions & 9 deletions packages/auth0-acul-js/src/models/branding.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { BrandingContext, BrandingMembers } from '../../interfaces/models/branding';

export class Branding implements BrandingMembers {
private branding: BrandingContext | undefined;
settings: BrandingMembers['settings'];
themes: BrandingMembers['themes'];

constructor(client: BrandingContext | undefined) {
this.branding = client;
constructor(branding: BrandingContext | undefined) {
this.settings = Branding.getSettings(branding);
this.themes = Branding.getThemes(branding);
}

getSettings(): ReturnType<BrandingMembers['getSettings']> {
const { branding } = this;

static getSettings(branding: BrandingContext | undefined): BrandingMembers['settings'] {
if (!branding?.settings) return null;

const { colors, favicon_url, logo_url, font } = branding.settings;
Expand All @@ -35,11 +35,11 @@ export class Branding implements BrandingMembers {
};
}

getThemes(): ReturnType<BrandingMembers['getThemes']> {
if (!this.branding || !this.branding?.themes) return null;
static getThemes(branding: BrandingContext | undefined): BrandingMembers['themes'] {
if (!branding || !branding?.themes) return null;

const { default: { borders = {}, colors = {}, displayName = '', fonts = {}, page_background: pageBackground = {}, widget = {} } = {} } =
this.branding.themes;
branding.themes;

return {
default: {
Expand Down
32 changes: 10 additions & 22 deletions packages/auth0-acul-js/src/models/client.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import type { ClientContext, ClientMembers } from '../../interfaces/models/client';

export class Client implements ClientMembers {
protected client: ClientContext;
id: ClientMembers['id'];
name: ClientMembers['name'];
logoUri: ClientMembers['logoUri'];
description: ClientMembers['description'];
metadata: ClientMembers['metadata'];

constructor(client: ClientContext) {
this.client = client;
}

get id(): ClientMembers['id'] {
return this.client.id;
}

get name(): ClientMembers['name'] {
return this.client.name;
}

get logoUri(): ClientMembers['logoUri'] {
return this.client?.logo_uri ?? null;
}

get description(): ClientMembers['description'] {
return this.client?.description ?? null;
}

getMetadata(): ReturnType<ClientMembers['getMetadata']> {
return this.client?.metadata ?? null;
this.id = client.id;
this.name = client.name;
this.logoUri = client?.logo_uri ?? null;
this.description = client?.description ?? null;
this.metadata = client?.metadata ?? null;
}
}
Loading
Loading