Skip to content

Commit

Permalink
feat(clerk-js): Improve DX for headless import
Browse files Browse the repository at this point in the history
"import Clerk from @clerk/clerk-js/headless"

instead of

"import Clerk from @clerk/clerk-js/dist/headless"
  • Loading branch information
SokratisVidros committed Nov 21, 2022
1 parent c225288 commit 8d64310
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/clerk-js/headless/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Clerk from '../dist/types/src/index.headless';

export default Clerk;
export * from '../dist/types/src/index.headless';
1 change: 1 addition & 0 deletions packages/clerk-js/headless/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../dist/clerk.headless');
16 changes: 2 additions & 14 deletions packages/expo/src/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,13 @@ export type ClerkProviderProps = ClerkReactProviderProps & {
};

export function ClerkProvider(props: ClerkProviderProps): JSX.Element {
const { children, tokenCache, hotload, ...rest } = props;
const { children, tokenCache, ...rest } = props;
const frontendApi = props.frontendApi || process.env.CLERK_FRONTEND_API || '';

const clerkRef = React.useRef<ReturnType<typeof buildClerk> | null>(null);

function getClerk() {
if (clerkRef.current === null && !hotload) {
clerkRef.current = buildClerk({
frontendApi,
tokenCache,
});
}
return clerkRef.current;
}

return (
<ClerkReactProvider
{...rest}
Clerk={getClerk()}
Clerk={buildClerk({ frontendApi, tokenCache })}
standardBrowser={!isReactNative()}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions packages/expo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export {
MagicLinkErrorCode,
} from '@clerk/clerk-react';

export { clerk as Clerk } from './singleton';

export * from './ClerkProvider';
13 changes: 6 additions & 7 deletions packages/expo/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
// @ts-ignore
import Clerk from '@clerk/clerk-js/dist/clerk.headless';
import type { FapiRequestInit, FapiResponse } from '@clerk/clerk-js/dist/types/src/core/fapiClient';
import type { ClerkProp } from '@clerk/clerk-react';
import Clerk from '@clerk/clerk-js/headless';
import type { HeadlessBrowserClerk } from '@clerk/clerk-react';

import { getToken as getTokenFromMemory, saveToken as saveTokenInMemory, TokenCache } from './cache';

const KEY = '__clerk_client_jwt';

export let clerk: ClerkProp;
export let clerk: HeadlessBrowserClerk;

type BuildClerkOptions = {
frontendApi: string;
tokenCache?: TokenCache;
};

export function buildClerk({ frontendApi, tokenCache }: BuildClerkOptions): ClerkProp {
const getToken = (tokenCache && tokenCache.getToken) ?? getTokenFromMemory;
const saveToken = (tokenCache && tokenCache.saveToken) ?? saveTokenInMemory;

if (!clerk) {
const getToken = (tokenCache && tokenCache.getToken) ?? getTokenFromMemory;
const saveToken = (tokenCache && tokenCache.saveToken) ?? saveTokenInMemory;

clerk = new Clerk(frontendApi);

if (!tokenCache) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './contexts';
export * from './components';
export * from './hooks';
export type { ClerkProp } from './types';
export type { BrowserClerk, ClerkProp, HeadlessBrowserClerk } from './types';
export { isMagicLinkError, MagicLinkErrorCode } from './errors';
export { useMagicLink } from './hooks/useMagicLink';

Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ export interface MountProps {
props?: any;
}

export interface BrowserClerk extends Clerk {
export interface HeadlessBrowserClerk extends Clerk {
load: (opts?: ClerkOptions) => Promise<void>;
updateClient: (client: ClientResource) => void;
}

export interface BrowserClerk extends HeadlessBrowserClerk {
onComponentsReady: Promise<void>;
components: any;
}
Expand Down

0 comments on commit 8d64310

Please sign in to comment.