From d5f19f303e48cbff0e9421a7a49f237e652091f8 Mon Sep 17 00:00:00 2001 From: Stefanos Anagnostou Date: Wed, 18 Dec 2024 16:30:38 +0200 Subject: [PATCH] chore(clerk-js): Refactor `FraudProtection` singleton mechanism --- packages/clerk-js/src/core/fraudProtection.test.ts | 1 + packages/clerk-js/src/core/fraudProtection.ts | 12 ++++++------ packages/clerk-js/src/core/resources/Base.ts | 5 +---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/clerk-js/src/core/fraudProtection.test.ts b/packages/clerk-js/src/core/fraudProtection.test.ts index b1b0e51e63..a630671424 100644 --- a/packages/clerk-js/src/core/fraudProtection.test.ts +++ b/packages/clerk-js/src/core/fraudProtection.test.ts @@ -40,6 +40,7 @@ describe('FraudProtectionService', () => { mockClerk = { client: mockClient.getOrCreateInstance() } as any as Clerk; + // @ts-expect-error The contructor is private and should be accessed through getInstance sut = new FraudProtection(mockClient, MockCaptchaChallenge as any); }); diff --git a/packages/clerk-js/src/core/fraudProtection.ts b/packages/clerk-js/src/core/fraudProtection.ts index 7de38d7641..5a15c0b398 100644 --- a/packages/clerk-js/src/core/fraudProtection.ts +++ b/packages/clerk-js/src/core/fraudProtection.ts @@ -1,20 +1,20 @@ -import type { CaptchaChallenge } from '../utils/captcha/CaptchaChallenge'; -import type { Clerk, Client } from './resources/internal'; -import { isClerkAPIResponseError } from './resources/internal'; +import { CaptchaChallenge } from '../utils/captcha/CaptchaChallenge'; +import type { Clerk } from './resources/internal'; +import { Client, isClerkAPIResponseError } from './resources/internal'; export class FraudProtection { private static instance: FraudProtection; private inflightException: Promise | null = null; - public static getInstance(client: typeof Client, CaptchaChallengeImpl: typeof CaptchaChallenge): FraudProtection { + public static getInstance(): FraudProtection { if (!FraudProtection.instance) { - FraudProtection.instance = new FraudProtection(client, CaptchaChallengeImpl); + FraudProtection.instance = new FraudProtection(Client, CaptchaChallenge); } return FraudProtection.instance; } - constructor( + private constructor( private client: typeof Client, private CaptchaChallengeImpl: typeof CaptchaChallenge, ) {} diff --git a/packages/clerk-js/src/core/resources/Base.ts b/packages/clerk-js/src/core/resources/Base.ts index 562f8e0e58..44e93cc191 100644 --- a/packages/clerk-js/src/core/resources/Base.ts +++ b/packages/clerk-js/src/core/resources/Base.ts @@ -2,7 +2,6 @@ import { isValidBrowserOnline } from '@clerk/shared/browser'; import { isProductionFromPublishableKey } from '@clerk/shared/keys'; import type { ClerkAPIErrorJSON, ClerkResourceJSON, ClerkResourceReloadParams, DeletedObjectJSON } from '@clerk/types'; -import { CaptchaChallenge } from '../../utils/captcha/CaptchaChallenge'; import { clerkMissingFapiClientInResources } from '../errors'; import type { FapiClient, FapiRequestInit, FapiResponse, FapiResponseJSON, HTTPMethod } from '../fapiClient'; import { FraudProtection } from '../fraudProtection'; @@ -67,9 +66,7 @@ export abstract class BaseResource { requestInit: FapiRequestInit, opts: BaseFetchOptions = {}, ): Promise | null> { - return FraudProtection.getInstance(Client, CaptchaChallenge).execute(this.clerk, () => - this._baseFetch(requestInit, opts), - ); + return FraudProtection.getInstance().execute(this.clerk, () => this._baseFetch(requestInit, opts)); } protected static async _baseFetch(