Skip to content

Commit

Permalink
feat(nextjs,shared): Improve CLERK_API_URL default value
Browse files Browse the repository at this point in the history
  • Loading branch information
desiprisg committed Oct 30, 2023
1 parent e5598cf commit feea17f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
16 changes: 9 additions & 7 deletions packages/nextjs/src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { apiUrlFromPublishableKey } from '@clerk/shared';
import { deprecated } from '@clerk/shared/deprecated';

export const PUBLISHABLE_KEY = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY || '';
export const DOMAIN = process.env.NEXT_PUBLIC_CLERK_DOMAIN || '';
export const PROXY_URL = process.env.NEXT_PUBLIC_CLERK_PROXY_URL || '';
export const IS_SATELLITE = process.env.NEXT_PUBLIC_CLERK_IS_SATELLITE === 'true' || false;
export const SIGN_IN_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || '';
export const SIGN_UP_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '';

/**
* @deprecated Use `CLERK_JS_VERSION` instead.
*/
Expand All @@ -9,7 +17,7 @@ if (JS_VERSION) {
}
export const CLERK_JS_VERSION = process.env.NEXT_PUBLIC_CLERK_JS_VERSION || '';
export const CLERK_JS_URL = process.env.NEXT_PUBLIC_CLERK_JS || '';
export const API_URL = process.env.CLERK_API_URL || 'https://api.clerk.com';
export const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);
export const API_VERSION = process.env.CLERK_API_VERSION || 'v1';
/**
* @deprecated Use `CLERK_SECRET_KEY` instead.
Expand All @@ -26,9 +34,3 @@ export const FRONTEND_API = process.env.NEXT_PUBLIC_CLERK_FRONTEND_API || '';
if (FRONTEND_API) {
deprecated('NEXT_PUBLIC_CLERK_FRONTEND_API', 'Use `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` environment variable instead.');
}
export const PUBLISHABLE_KEY = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY || '';
export const DOMAIN = process.env.NEXT_PUBLIC_CLERK_DOMAIN || '';
export const PROXY_URL = process.env.NEXT_PUBLIC_CLERK_PROXY_URL || '';
export const IS_SATELLITE = process.env.NEXT_PUBLIC_CLERK_IS_SATELLITE === 'true' || false;
export const SIGN_IN_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || '';
export const SIGN_UP_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '';
11 changes: 11 additions & 0 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const DEV_SUFFIXES = [
'.lcl.dev',
'.lclstage.dev',
'.dev.lclclerk.com',
'.accounts.lclclerk.com',
'.stg.lclclerk.com',
];

export const STAGING_SUFFIXES = ['.stg.dev', '.stgstage.dev', 'accountsstage.dev', 'accounts.dev'];

export const DEV_OR_STAGING_SUFFIXES = [...DEV_SUFFIXES, ...STAGING_SUFFIXES];
2 changes: 1 addition & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

export * from './utils';

export { createWorkerTimers } from './workerTimers';
export * from './browser';
export { callWithRetry } from './callWithRetry';
export * from './color';
Expand All @@ -27,3 +26,4 @@ export * from './poller';
export * from './proxy';
export * from './underscore';
export * from './url';
export { createWorkerTimers } from './workerTimers';
13 changes: 1 addition & 12 deletions packages/shared/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PublishableKey } from '@clerk/types';

import { DEV_OR_STAGING_SUFFIXES } from './constants';
import { isomorphicAtob } from './isomorphicAtob';

const PUBLISHABLE_KEY_LIVE_PREFIX = 'pk_live_';
Expand Down Expand Up @@ -56,18 +57,6 @@ 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',
'.lclstage.dev',
'.stgstage.dev',
'.dev.lclclerk.com',
'.stg.lclclerk.com',
'.accounts.lclclerk.com',
'accountsstage.dev',
'accounts.dev',
];

const devOrStagingUrlCache = new Map<string, boolean>();

return {
Expand Down
13 changes: 13 additions & 0 deletions packages/shared/src/utils/apiUrlFromPublishableKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DEV_SUFFIXES, STAGING_SUFFIXES } from '../constants';
import { parsePublishableKey } from '../keys';

export const apiUrlFromPublishableKey = (publishableKey: string) => {
const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;
if (DEV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {
return 'https://api.lclclerk.com';
}
if (STAGING_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {
return 'https://api.clerkstage.dev';
}
return 'https://api.clerk.com';
};
5 changes: 3 additions & 2 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { apiUrlFromPublishableKey } from './apiUrlFromPublishableKey';
export * from './createDeferredPromise';
export { isStaging } from './instance';
export { logErrorInDevMode } from './logErrorInDevMode';
export { noop } from './noop';
export * from './runtimeEnvironment';
export * from './runWithExponentialBackOff';
export { logErrorInDevMode } from './logErrorInDevMode';
export * from './runtimeEnvironment';

0 comments on commit feea17f

Please sign in to comment.