Skip to content

Commit

Permalink
chore(shared): Move share-able code from backend to shared package
Browse files Browse the repository at this point in the history
This is a preparation step to remove the share-able dependencies
from backend package and import them from shared package
  • Loading branch information
dimkl committed Sep 27, 2023
1 parent 3a4c047 commit e71f408
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/shared/src/utils/callWithRetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function wait(ms: number) {
return new Promise(res => setTimeout(res, ms));
}

const MAX_NUMBER_OF_RETRIES = 5;

export async function callWithRetry<T>(
fn: (...args: unknown[]) => Promise<T>,
attempt = 1,
maxAttempts = MAX_NUMBER_OF_RETRIES,
): Promise<T> {
try {
return await fn();
} catch (e) {
if (attempt >= maxAttempts) {
throw e;
}
await wait(2 ** attempt * 100);

return callWithRetry(fn, attempt + 1);
}
}
2 changes: 2 additions & 0 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export * from './globs';
export * from './loadScript';
export * from './runtimeEnvironment';
export { deprecated, deprecatedProperty } from './deprecated';
export { callWithRetry } from './callWithRetry';
export { isDevelopmentFromApiKey, isProductionFromApiKey, isStaging } from './instance';
16 changes: 16 additions & 0 deletions packages/shared/src/utils/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function isDevelopmentFromApiKey(apiKey: string): boolean {
return apiKey.startsWith('test_') || apiKey.startsWith('sk_test_');
}

export function isProductionFromApiKey(apiKey: string): boolean {
return apiKey.startsWith('live_') || apiKey.startsWith('sk_live_');
}

export function isStaging(frontendApi: string): boolean {
return (
frontendApi.endsWith('.lclstage.dev') ||
frontendApi.endsWith('.stgstage.dev') ||
frontendApi.endsWith('.clerkstage.dev') ||
frontendApi.endsWith('.accountsstage.dev')
);
}
1 change: 1 addition & 0 deletions packages/shared/src/utils/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function isLegacyFrontendApiKey(key: string) {
}

export function createDevOrStagingUrlCache() {
// TODO: Check if we can merge it with `./instance.ts#isStaging()`
const DEV_OR_STAGING_SUFFIXES = [
'.lcl.dev',
'.stg.dev',
Expand Down
27 changes: 27 additions & 0 deletions packages/shared/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isStaging } from './instance';

export function parseSearchParams(queryString = ''): URLSearchParams {
if (queryString.startsWith('?')) {
queryString = queryString.slice(1);
Expand Down Expand Up @@ -25,3 +27,28 @@ export function addClerkPrefix(str: string | undefined) {
const stripped = str.replace(regex, '');
return `clerk.${stripped}`;
}

export const getClerkJsMajorVersionOrTag = (frontendApi: string, pkgVersion?: string) => {
if (!pkgVersion && isStaging(frontendApi)) {
return 'staging';
}

if (!pkgVersion) {
return 'latest';
}

if (pkgVersion.includes('next')) {
return 'next';
}

return pkgVersion.split('.')[0] || 'latest';
};

export const getScriptUrl = (
frontendApi: string,
{ pkgVersion, clerkJSVersion }: { pkgVersion?: string; clerkJSVersion?: string },
) => {
const noSchemeFrontendApi = frontendApi.replace(/http(s)?:\/\//, '');
const major = getClerkJsMajorVersionOrTag(frontendApi, pkgVersion);
return `https://${noSchemeFrontendApi}/npm/@clerk/clerk-js@${clerkJSVersion || major}/dist/clerk.browser.js`;
};

0 comments on commit e71f408

Please sign in to comment.