Skip to content

Commit

Permalink
chore(clerk-js): Refactor FraudProtection singleton mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
anagstef committed Dec 18, 2024
1 parent c9da046 commit d5f19f3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/fraudProtection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
12 changes: 6 additions & 6 deletions packages/clerk-js/src/core/fraudProtection.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> | 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,
) {}
Expand Down
5 changes: 1 addition & 4 deletions packages/clerk-js/src/core/resources/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,9 +66,7 @@ export abstract class BaseResource {
requestInit: FapiRequestInit,
opts: BaseFetchOptions = {},
): Promise<FapiResponseJSON<J> | null> {
return FraudProtection.getInstance(Client, CaptchaChallenge).execute(this.clerk, () =>
this._baseFetch<J>(requestInit, opts),
);
return FraudProtection.getInstance().execute(this.clerk, () => this._baseFetch<J>(requestInit, opts));
}

protected static async _baseFetch<J extends ClerkResourceJSON | DeletedObjectJSON | null>(
Expand Down

0 comments on commit d5f19f3

Please sign in to comment.