Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
fix(shared, nextjs): Support importing @clerk/shared in @clerk/backend#1769
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8a1a2fc68d06a71b1dadb4c4ae658f52000File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/shared': minor | ||
| --- | ||
| Introduce `getClerkJsMajorVersionOrTag()`, `getScriptUrl()`, `callWithRetry()` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/backend': minor | ||
| --- | ||
| Replace utilities with @clerk/shared exports |
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| export {}; | ||
| declare global { | ||
| const PACKAGE_NAME: string; | ||
| const PACKAGE_VERSION: string; | ||
| const __DEV__: boolean; | ||
| } | ||
| export {}; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -75,7 +75,7 @@ export default (QUnit: QUnit) => { | ||
| assert.raises( | ||
| () => redirectToSignIn({ returnBackUrl }), | ||
| new Error( | ||
| 'Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.', | ||
| '@clerk/backend: Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.', | ||
| ), | ||
| ); | ||
| }); | ||
| @@ -221,7 +221,7 @@ export default (QUnit: QUnit) => { | ||
| assert.raises( | ||
| () => redirectToSignUp({ returnBackUrl }), | ||
| new Error( | ||
| 'Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.', | ||
| '@clerk/backend: Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.', | ||
anagstef marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ), | ||
| ); | ||
| }); | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export { | ||
| addClerkPrefix, | ||
| callWithRetry, | ||
| getClerkJsMajorVersionOrTag, | ||
| getScriptUrl, | ||
| isDevelopmentFromApiKey, | ||
| isProductionFromApiKey, | ||
| parsePublishableKey, | ||
| } from '@clerk/shared'; | ||
| import { buildErrorThrower } from '@clerk/shared'; | ||
| // TODO: replace packageName with `${PACKAGE_NAME}@${PACKAGE_VERSION}` from tsup.config.ts | ||
| export const errorThrower = buildErrorThrower({ packageName: '@clerk/backend' }); | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will fix this in another PR that will update how we build and run tests in backend package (currently tsup is not used in tests) | ||
| import { createDevOrStagingUrlCache } from '@clerk/shared'; | ||
| export const { isDevOrStagingUrl } = createDevOrStagingUrlCache(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,7 +4,12 @@ function wait(ms: number) { | ||
| const MAX_NUMBER_OF_RETRIES = 5; | ||
| // TODO: Move this to @clerk/shared and reuse it with @clerk/clerk-js | ||
| /** | ||
| * Retry callback function every few hundred ms (with an exponential backoff | ||
| * based on the current attempt) until the maximum attempts has reached or | ||
| * the callback is executed successfully. The default number of maximum | ||
| * attempts is 5 and retries are triggered when callback throws an error. | ||
| */ | ||
| export async function callWithRetry<T>( | ||
dimkl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| fn: (...args: unknown[]) => Promise<T>, | ||
| attempt = 1, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 = [ | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will cater this in another iteration where we will evaluate the exports of the | ||
| '.lcl.dev', | ||
| '.stg.dev', | ||
| 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); | ||
| @@ -25,3 +27,40 @@ export function addClerkPrefix(str: string | undefined) { | ||
| const stripped = str.replace(regex, ''); | ||
| return `clerk.${stripped}`; | ||
| } | ||
| /** | ||
| * | ||
| * Retrieve the clerk-js major tag using the major version from the pkgVersion | ||
| * param or use the frontendApi to determine if the staging tag should be used. | ||
| * The default tag is `latest` and a `next` pkgVersion also exists to retrieve | ||
| * the next canary release. | ||
| */ | ||
| export const getClerkJsMajorVersionOrTag = (frontendApi: string, pkgVersion?: string) => { | ||
dimkl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (!pkgVersion && isStaging(frontendApi)) { | ||
| return 'staging'; | ||
| } | ||
| if (!pkgVersion) { | ||
| return 'latest'; | ||
| } | ||
| if (pkgVersion.includes('next')) { | ||
| return 'next'; | ||
| } | ||
| return pkgVersion.split('.')[0] || 'latest'; | ||
| }; | ||
| /** | ||
| * | ||
| * Retrieve the clerk-js script url from the frontendApi and the major tag | ||
| * using the {@link getClerkJsMajorVersionOrTag} or a provided clerkJSVersion tag. | ||
| */ | ||
| export const getScriptUrl = ( | ||
dimkl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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`; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.