Repository navigation
Expand file tree
/
Copy pathexternal.ts
More file actions
149 lines (136 loc) · 5.31 KB
/
Copy pathexternal.ts
File metadata and controls
149 lines (136 loc) · 5.31 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { deriveState } from '@clerk/shared/deriveState';
import { eventMethodCalled } from '@clerk/shared/telemetry';
import { batched, computed, onMount, type Store } from 'nanostores';
import { $clerk, $csrState, $initialState } from './internal';
/**
* A client side store that returns the loaded state of clerk-js.
*
* @example
* A simple example:
*
* $isLoadedStore.subscribe((authloaded => console.log(loaded))
*/
export const $isLoadedStore = computed([$csrState], state => state.isLoaded);
/**
* A client side store that is prepopulated with the authentication context during SSR.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $authStore.subscribe((auth) => console.log(auth.userId))
*/
export const $authStore = batched([$csrState, $initialState], (state, initialState) => {
return deriveState(
state.isLoaded,
{
session: state.session,
user: state.user,
organization: state.organization,
client: state.client!,
},
initialState,
);
});
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns back the authenticated user or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $userStore.subscribe((user) => console.log(user.id))
*/
export const $userStore = computed([$authStore], auth => auth.user);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns the session of the authenticated user or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $sessionStore.subscribe((session) => console.log(session.id))
*/
export const $sessionStore = computed([$authStore], auth => auth.session);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns the active organization of the authenticated user or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $organizationStore.subscribe((org) => console.log(org.id))
*/
export const $organizationStore = computed([$authStore], auth => auth.organization);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns the clerk client or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $clientStore.subscribe((client) => console.log(client.activeSessions))
*/
export const $clientStore = computed([$csrState], csr => csr.client);
/**
* A client side store that is populated after clerk-js is instanciated.
* The store returns the clerk instance or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $clerkStore.subscribe((clerk) => console.log(clerk.publishableKey))
*/
export const $clerkStore = computed([$clerk], clerk => clerk);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns all the sessions of the current clerk client or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $sessionListStore.subscribe((sessionList) => sessionList.map((session) => console.log('Session id:', sessino.id) ))
*/
export const $sessionListStore = computed([$clientStore], client => client?.sessions);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns a `SignInResource` or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $signInStore.subscribe((signIn) => console.log(signIn.status))
*/
export const $signInStore = computed([$clientStore], client => client?.signIn);
/**
* A client side store that is populated after clerk-js has loaded.
* The store returns a `SignUpResource` or `null`.
* It is a nanostore, for instructions on how to use nanostores please review the [documentation](https://github.com/nanostores/nanostores)
*
* @example
* A simple example:
*
* $signUpStore.subscribe((signUp) => console.log(signUp.status))
*/
export const $signUpStore = computed([$clientStore], client => client?.signUp);
/**
* Records a telemetry event when a store is used to match React hooks telemetry.
*
* @param {Store} store - The nanostore instance to monitor.
* @param {string} method - The name of the method associated with the store usage.
*/
const recordTelemetryEvent = (store: Store, method: string) => {
onMount(store, () => {
$clerk.get()?.telemetry?.record(eventMethodCalled(method));
});
};
recordTelemetryEvent($signInStore, '$signInStore');
recordTelemetryEvent($signUpStore, '$signUpStore');
recordTelemetryEvent($organizationStore, '$organizationStore');