-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(astro,clerk-react,shared,nextjs): Migrate functions that can be…
… reused across JavaScript SDKs (#3849)
- Loading branch information
1 parent
def3a38
commit 7e0ced3
Showing
23 changed files
with
370 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@clerk/astro": patch | ||
"@clerk/shared": patch | ||
"@clerk/clerk-react": patch | ||
"@clerk/nextjs": patch | ||
--- | ||
|
||
Introduce functions that can be reused across front-end SDKs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { loadClerkJsScript, setClerkJsLoadingErrorPackageName } from '@clerk/shared/loadClerkJsScript'; | ||
import type { ClerkOptions } from '@clerk/types'; | ||
|
||
import { $clerk, $csrState } from '../stores/internal'; | ||
import type { AstroClerkIntegrationParams, AstroClerkUpdateOptions } from '../types'; | ||
import type { AstroClerkCreateInstanceParams, AstroClerkUpdateOptions } from '../types'; | ||
import { invokeClerkAstroJSFunctions } from './invoke-clerk-astro-js-functions'; | ||
import { mountAllClerkAstroJSComponents } from './mount-clerk-astro-js-components'; | ||
import { runOnce } from './run-once'; | ||
import { waitForClerkScript } from './utils/loadClerkJSScript'; | ||
|
||
let initOptions: ClerkOptions | undefined; | ||
|
||
// TODO-SHARED: copied from `clerk-js` | ||
export const CLERK_BEFORE_UNLOAD_EVENT = 'clerk:beforeunload'; | ||
|
||
setClerkJsLoadingErrorPackageName(PACKAGE_NAME); | ||
|
||
function windowNavigate(to: URL | string): void { | ||
const toURL = new URL(to, window.location.href); | ||
window.dispatchEvent(new CustomEvent(CLERK_BEFORE_UNLOAD_EVENT)); | ||
|
@@ -35,10 +37,10 @@ function createNavigationHandler( | |
*/ | ||
const createClerkInstance = runOnce(createClerkInstanceInternal); | ||
|
||
async function createClerkInstanceInternal(options?: AstroClerkIntegrationParams) { | ||
async function createClerkInstanceInternal(options?: AstroClerkCreateInstanceParams) { | ||
let clerkJSInstance = window.Clerk; | ||
if (!clerkJSInstance) { | ||
await waitForClerkScript(); | ||
await loadClerkJsScript(options); | ||
|
||
if (!window.Clerk) { | ||
throw new Error('Failed to download latest ClerkJS. Contact [email protected].'); | ||
|
@@ -47,7 +49,6 @@ async function createClerkInstanceInternal(options?: AstroClerkIntegrationParams | |
} | ||
|
||
if (!$clerk.get()) { | ||
// @ts-ignore | ||
$clerk.set(clerkJSInstance); | ||
} | ||
|
||
|
@@ -57,8 +58,7 @@ async function createClerkInstanceInternal(options?: AstroClerkIntegrationParams | |
routerReplace: createNavigationHandler(window.history.replaceState.bind(window.history)), | ||
}; | ||
|
||
// TODO: Update Clerk type from @clerk/types to include this method | ||
return (clerkJSInstance as any) | ||
return clerkJSInstance | ||
.load(initOptions) | ||
.then(() => { | ||
$csrState.setKey('isLoaded', true); | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { InitialState, Resources } from '@clerk/types'; | ||
|
||
import { deriveState } from '../deriveState'; | ||
|
||
describe('deriveState', () => { | ||
const mockInitialState = { | ||
userId: 'user_2U330vGHg3llBga8Oi0fzzeNAaG', | ||
sessionId: 'sess_2j1R7g3AUeKMx9M23dBO0XLEQGY', | ||
orgId: 'org_2U330vGHg3llBga8Oi0fzzeNAaG', | ||
} as InitialState; | ||
|
||
const mockResources = { | ||
client: {}, | ||
user: { id: mockInitialState.userId }, | ||
session: { id: mockInitialState.sessionId }, | ||
organization: { id: mockInitialState.orgId }, | ||
} as Resources; | ||
|
||
it('uses SSR state when !clerkLoaded and initialState is provided', () => { | ||
expect(deriveState(false, {} as Resources, mockInitialState)).toEqual(mockInitialState); | ||
}); | ||
|
||
it('uses CSR state when clerkLoaded', () => { | ||
const result = deriveState(true, mockResources, undefined); | ||
expect(result.userId).toBe(mockInitialState.userId); | ||
expect(result.sessionId).toBe(mockInitialState.sessionId); | ||
expect(result.orgId).toBe(mockInitialState.orgId); | ||
}); | ||
|
||
it('handles !clerkLoaded and undefined initialState', () => { | ||
const result = deriveState(false, {} as Resources, undefined); | ||
expect(result.userId).toBeUndefined(); | ||
expect(result.sessionId).toBeUndefined(); | ||
expect(result.orgId).toBeUndefined(); | ||
}); | ||
}); |
Oops, something went wrong.