forked from clerk/javascript
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclerkClient.ts
28 lines (22 loc) · 910 Bytes
/
clerkClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import type { ClerkClient } from '@clerk/backend';
import { createClerkClient } from '@clerk/backend';
import { loadApiEnv, loadClientEnv } from './utils';
let clerkClientSingleton = {} as unknown as ClerkClient;
export const clerkClient = new Proxy(clerkClientSingleton, {
get(_target, property: keyof ClerkClient) {
if (property in clerkClientSingleton) {
return clerkClientSingleton[property];
}
const env = { ...loadApiEnv(), ...loadClientEnv() };
const client = createClerkClient({ ...env, userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}` });
// if the client is initialized properly, cache it to a singleton instance variable
// in the next invocation the guard at the top will be triggered instead of creating another instance
if (env.secretKey) {
clerkClientSingleton = client;
}
return client[property];
},
set() {
return false;
},
});