@sanity/sdk 0.0.0-alpha.21 → 0.0.0-alpha.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +428 -325
- package/dist/index.js +1618 -1553
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/_exports/index.ts +31 -30
- package/src/auth/authStore.test.ts +149 -104
- package/src/auth/authStore.ts +51 -100
- package/src/auth/handleAuthCallback.test.ts +67 -34
- package/src/auth/handleAuthCallback.ts +8 -7
- package/src/auth/logout.test.ts +61 -29
- package/src/auth/logout.ts +26 -28
- package/src/auth/refreshStampedToken.test.ts +9 -9
- package/src/auth/refreshStampedToken.ts +62 -56
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
- package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
- package/src/client/clientStore.test.ts +131 -67
- package/src/client/clientStore.ts +117 -116
- package/src/comlink/controller/actions/destroyController.test.ts +38 -13
- package/src/comlink/controller/actions/destroyController.ts +11 -15
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
- package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
- package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
- package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
- package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
- package/src/comlink/controller/actions/releaseChannel.ts +22 -21
- package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
- package/src/comlink/controller/comlinkControllerStore.ts +44 -5
- package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
- package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
- package/src/comlink/node/actions/releaseNode.test.ts +75 -55
- package/src/comlink/node/actions/releaseNode.ts +19 -21
- package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
- package/src/comlink/node/comlinkNodeStore.ts +22 -5
- package/src/config/authConfig.ts +79 -0
- package/src/config/sanityConfig.ts +48 -0
- package/src/datasets/datasets.test.ts +2 -2
- package/src/datasets/datasets.ts +18 -5
- package/src/document/actions.test.ts +22 -10
- package/src/document/actions.ts +44 -56
- package/src/document/applyDocumentActions.test.ts +96 -36
- package/src/document/applyDocumentActions.ts +140 -99
- package/src/document/documentStore.test.ts +103 -155
- package/src/document/documentStore.ts +247 -237
- package/src/document/listen.ts +56 -55
- package/src/document/patchOperations.ts +0 -43
- package/src/document/permissions.test.ts +25 -12
- package/src/document/permissions.ts +11 -4
- package/src/document/processActions.test.ts +41 -8
- package/src/document/reducers.test.ts +87 -16
- package/src/document/reducers.ts +2 -2
- package/src/document/sharedListener.test.ts +34 -16
- package/src/document/sharedListener.ts +33 -11
- package/src/preview/getPreviewState.test.ts +40 -39
- package/src/preview/getPreviewState.ts +68 -56
- package/src/preview/previewConstants.ts +43 -0
- package/src/preview/previewQuery.test.ts +1 -1
- package/src/preview/previewQuery.ts +4 -5
- package/src/preview/previewStore.test.ts +13 -58
- package/src/preview/previewStore.ts +7 -21
- package/src/preview/resolvePreview.test.ts +33 -104
- package/src/preview/resolvePreview.ts +11 -21
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
- package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
- package/src/preview/util.ts +1 -0
- package/src/project/project.test.ts +3 -3
- package/src/project/project.ts +28 -5
- package/src/projection/getProjectionState.test.ts +69 -49
- package/src/projection/getProjectionState.ts +42 -50
- package/src/projection/projectionQuery.ts +1 -1
- package/src/projection/projectionStore.test.ts +13 -51
- package/src/projection/projectionStore.ts +6 -18
- package/src/projection/resolveProjection.test.ts +32 -127
- package/src/projection/resolveProjection.ts +15 -28
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +105 -90
- package/src/projection/subscribeToStateAndFetchBatches.ts +94 -81
- package/src/projection/util.ts +2 -0
- package/src/projects/projects.test.ts +13 -4
- package/src/projects/projects.ts +6 -1
- package/src/query/queryStore.test.ts +10 -47
- package/src/query/queryStore.ts +151 -133
- package/src/query/queryStoreConstants.ts +2 -0
- package/src/store/createActionBinder.test.ts +153 -0
- package/src/store/createActionBinder.ts +176 -0
- package/src/store/createSanityInstance.test.ts +84 -0
- package/src/store/createSanityInstance.ts +124 -0
- package/src/store/createStateSourceAction.test.ts +196 -0
- package/src/store/createStateSourceAction.ts +260 -0
- package/src/store/createStoreInstance.test.ts +81 -0
- package/src/store/createStoreInstance.ts +80 -0
- package/src/store/createStoreState.test.ts +85 -0
- package/src/store/createStoreState.ts +92 -0
- package/src/store/defineStore.test.ts +18 -0
- package/src/store/defineStore.ts +81 -0
- package/src/users/reducers.test.ts +318 -0
- package/src/users/reducers.ts +88 -0
- package/src/users/types.ts +46 -4
- package/src/users/usersConstants.ts +4 -0
- package/src/users/usersStore.test.ts +350 -223
- package/src/users/usersStore.ts +285 -149
- package/src/utils/createFetcherStore.test.ts +6 -7
- package/src/utils/createFetcherStore.ts +150 -153
- package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
- package/src/auth/fetchLoginUrls.test.ts +0 -163
- package/src/auth/fetchLoginUrls.ts +0 -74
- package/src/common/createLiveEventSubscriber.test.ts +0 -121
- package/src/common/createLiveEventSubscriber.ts +0 -55
- package/src/common/types.ts +0 -4
- package/src/instance/identity.test.ts +0 -46
- package/src/instance/identity.ts +0 -29
- package/src/instance/sanityInstance.test.ts +0 -77
- package/src/instance/sanityInstance.ts +0 -57
- package/src/instance/types.ts +0 -37
- package/src/preview/getPreviewProjection.ts +0 -45
- package/src/resources/README.md +0 -370
- package/src/resources/createAction.test.ts +0 -101
- package/src/resources/createAction.ts +0 -44
- package/src/resources/createResource.test.ts +0 -112
- package/src/resources/createResource.ts +0 -102
- package/src/resources/createStateSourceAction.test.ts +0 -114
- package/src/resources/createStateSourceAction.ts +0 -83
- package/src/resources/createStore.test.ts +0 -67
- package/src/resources/createStore.ts +0 -46
- package/src/store/createStore.test.ts +0 -108
- package/src/store/createStore.ts +0 -106
- /package/src/{common/util.ts → utils/hashString.ts} +0 -0
package/src/auth/logout.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {bindActionGlobally} from '../store/createActionBinder'
|
|
2
2
|
import {DEFAULT_API_VERSION, REQUEST_TAG_PREFIX} from './authConstants'
|
|
3
3
|
import {AuthStateType} from './authStateType'
|
|
4
4
|
import {authStore} from './authStore'
|
|
@@ -6,40 +6,38 @@ import {authStore} from './authStore'
|
|
|
6
6
|
/**
|
|
7
7
|
* @public
|
|
8
8
|
*/
|
|
9
|
-
export const logout =
|
|
9
|
+
export const logout = bindActionGlobally(authStore, async ({state}) => {
|
|
10
10
|
const {clientFactory, apiHost, providedToken, storageArea, storageKey} = state.get().options
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (providedToken) return
|
|
12
|
+
// If a token is statically provided, logout does nothing
|
|
13
|
+
if (providedToken) return
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
const {authState} = state.get()
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
// If we already have an inflight request, no-op
|
|
18
|
+
if (authState.type === AuthStateType.LOGGED_OUT && authState.isDestroyingSession) return
|
|
19
|
+
const token = authState.type === AuthStateType.LOGGED_IN && authState.token
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const client = clientFactory({
|
|
29
|
-
token,
|
|
30
|
-
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
31
|
-
apiVersion: DEFAULT_API_VERSION,
|
|
32
|
-
...(apiHost && {apiHost}),
|
|
33
|
-
useProjectHostname: false,
|
|
34
|
-
})
|
|
21
|
+
try {
|
|
22
|
+
if (token) {
|
|
23
|
+
state.set('loggingOut', {
|
|
24
|
+
authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: true},
|
|
25
|
+
})
|
|
35
26
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
const client = clientFactory({
|
|
28
|
+
token,
|
|
29
|
+
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
30
|
+
apiVersion: DEFAULT_API_VERSION,
|
|
31
|
+
...(apiHost && {apiHost}),
|
|
32
|
+
useProjectHostname: false,
|
|
41
33
|
})
|
|
42
|
-
|
|
34
|
+
|
|
35
|
+
await client.request<void>({uri: '/auth/logout', method: 'POST'})
|
|
43
36
|
}
|
|
37
|
+
} finally {
|
|
38
|
+
state.set('logoutSuccess', {
|
|
39
|
+
authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
|
|
40
|
+
})
|
|
41
|
+
storageArea?.removeItem(storageKey)
|
|
44
42
|
}
|
|
45
43
|
})
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {of, Subscription, throwError} from 'rxjs'
|
|
2
2
|
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
|
|
3
3
|
|
|
4
|
-
import {createSanityInstance} from '../
|
|
5
|
-
import {
|
|
4
|
+
import {createSanityInstance} from '../store/createSanityInstance'
|
|
5
|
+
import {createStoreState} from '../store/createStoreState'
|
|
6
6
|
import {AuthStateType} from './authStateType'
|
|
7
7
|
import {type AuthState, authStore} from './authStore'
|
|
8
8
|
import {refreshStampedToken} from './refreshStampedToken'
|
|
@@ -46,14 +46,14 @@ describe('refreshStampedToken', () => {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// Create the resource state
|
|
49
|
-
const state =
|
|
49
|
+
const state = createStoreState(initialState)
|
|
50
50
|
|
|
51
51
|
// Call refreshStampedToken ONCE
|
|
52
52
|
// Because TParams is [], createInternalAction will call the inner function for us
|
|
53
53
|
const subscription: Subscription = refreshStampedToken({state, instance})
|
|
54
54
|
|
|
55
|
-
// Advance time by
|
|
56
|
-
vi.advanceTimersByTime(
|
|
55
|
+
// Advance time by 12 hours so the interval fires
|
|
56
|
+
vi.advanceTimersByTime(12 * 60 * 60 * 1000)
|
|
57
57
|
|
|
58
58
|
// The client request should have been called once
|
|
59
59
|
expect(mockClient.observable.request).toHaveBeenCalledTimes(1)
|
|
@@ -89,12 +89,12 @@ describe('refreshStampedToken', () => {
|
|
|
89
89
|
token: 'sk-initial-token-st123',
|
|
90
90
|
currentUser: null,
|
|
91
91
|
}
|
|
92
|
-
const state =
|
|
92
|
+
const state = createStoreState(initialState)
|
|
93
93
|
|
|
94
94
|
const subscription: Subscription = refreshStampedToken({state, instance})
|
|
95
95
|
|
|
96
96
|
// Move time forward to trigger the interval
|
|
97
|
-
vi.advanceTimersByTime(
|
|
97
|
+
vi.advanceTimersByTime(12 * 60 * 60 * 1000)
|
|
98
98
|
|
|
99
99
|
// Should have set an error state
|
|
100
100
|
expect(state.get().authState).toEqual({
|
|
@@ -119,7 +119,7 @@ describe('refreshStampedToken', () => {
|
|
|
119
119
|
type: AuthStateType.LOGGED_OUT,
|
|
120
120
|
isDestroyingSession: false,
|
|
121
121
|
} as AuthState
|
|
122
|
-
const state =
|
|
122
|
+
const state = createStoreState(initialState)
|
|
123
123
|
|
|
124
124
|
const subscription: Subscription = refreshStampedToken({state, instance})
|
|
125
125
|
|
|
@@ -156,7 +156,7 @@ describe('refreshStampedToken', () => {
|
|
|
156
156
|
token: 'sk-nonstamped-token2', // Note: doesn't include '-st'
|
|
157
157
|
currentUser: null,
|
|
158
158
|
}
|
|
159
|
-
const state =
|
|
159
|
+
const state = createStoreState(initialState)
|
|
160
160
|
|
|
161
161
|
const subscription: Subscription = refreshStampedToken({state, instance})
|
|
162
162
|
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
distinctUntilChanged,
|
|
3
|
+
filter,
|
|
4
|
+
interval,
|
|
5
|
+
map,
|
|
6
|
+
type Subscription,
|
|
7
|
+
switchMap,
|
|
8
|
+
takeWhile,
|
|
9
|
+
} from 'rxjs'
|
|
2
10
|
|
|
3
|
-
import {type
|
|
11
|
+
import {type StoreContext} from '../store/defineStore'
|
|
4
12
|
import {DEFAULT_API_VERSION} from './authConstants'
|
|
5
13
|
import {AuthStateType} from './authStateType'
|
|
6
14
|
import {type AuthState, type AuthStoreState} from './authStore'
|
|
@@ -8,61 +16,59 @@ import {type AuthState, type AuthStoreState} from './authStore'
|
|
|
8
16
|
/**
|
|
9
17
|
* @internal
|
|
10
18
|
*/
|
|
11
|
-
export const refreshStampedToken =
|
|
12
|
-
|
|
13
|
-
const {clientFactory, apiHost, storageArea, storageKey} = state.get().options
|
|
19
|
+
export const refreshStampedToken = ({state}: StoreContext<AuthStoreState>): Subscription => {
|
|
20
|
+
const {clientFactory, apiHost, storageArea, storageKey} = state.get().options
|
|
14
21
|
|
|
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
|
-
|
|
22
|
+
const refreshInterval = 12 * 60 * 60 * 1000 // refresh the token every 12 hours
|
|
23
|
+
|
|
24
|
+
const refreshToken$ = state.observable.pipe(
|
|
25
|
+
map(({authState}) => authState),
|
|
26
|
+
filter(
|
|
27
|
+
(authState): authState is Extract<AuthState, {type: AuthStateType.LOGGED_IN}> =>
|
|
28
|
+
authState.type === AuthStateType.LOGGED_IN,
|
|
29
|
+
),
|
|
30
|
+
distinctUntilChanged(),
|
|
31
|
+
filter((authState) => authState.token.includes('-st')), // Ensure we only try to refresh stamped tokens
|
|
32
|
+
switchMap((authState) =>
|
|
33
|
+
interval(refreshInterval).pipe(
|
|
34
|
+
takeWhile(() => state.get().authState.type === AuthStateType.LOGGED_IN),
|
|
35
|
+
map(() => authState.token),
|
|
36
|
+
distinctUntilChanged(),
|
|
37
|
+
map((token) =>
|
|
38
|
+
clientFactory({
|
|
39
|
+
apiVersion: DEFAULT_API_VERSION,
|
|
40
|
+
requestTagPrefix: 'sdk.token-refresh',
|
|
41
|
+
useProjectHostname: false,
|
|
42
|
+
token,
|
|
43
|
+
ignoreBrowserTokenWarning: true,
|
|
44
|
+
...(apiHost && {apiHost}),
|
|
45
|
+
}),
|
|
46
|
+
),
|
|
47
|
+
switchMap((client) =>
|
|
48
|
+
client.observable.request<{token: string}>({
|
|
49
|
+
uri: 'auth/refresh-token',
|
|
50
|
+
method: 'POST',
|
|
51
|
+
body: {
|
|
52
|
+
token: authState.token,
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
47
55
|
),
|
|
48
56
|
),
|
|
49
|
-
)
|
|
57
|
+
),
|
|
58
|
+
)
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
)
|
|
60
|
+
return refreshToken$.subscribe({
|
|
61
|
+
next: (response) => {
|
|
62
|
+
state.set('setRefreshStampedToken', (prev) => ({
|
|
63
|
+
authState:
|
|
64
|
+
prev.authState.type === AuthStateType.LOGGED_IN
|
|
65
|
+
? {...prev.authState, token: response.token}
|
|
66
|
+
: prev.authState,
|
|
67
|
+
}))
|
|
68
|
+
storageArea?.setItem(storageKey, JSON.stringify({token: response.token}))
|
|
69
|
+
},
|
|
70
|
+
error: (error) => {
|
|
71
|
+
state.set('setRefreshStampedTokenError', {authState: {type: AuthStateType.ERROR, error}})
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
}
|
|
@@ -2,8 +2,8 @@ import {type CurrentUser} from '@sanity/types'
|
|
|
2
2
|
import {delay, of, throwError} from 'rxjs'
|
|
3
3
|
import {beforeEach, describe, it} from 'vitest'
|
|
4
4
|
|
|
5
|
-
import {createSanityInstance} from '../
|
|
6
|
-
import {
|
|
5
|
+
import {createSanityInstance} from '../store/createSanityInstance'
|
|
6
|
+
import {createStoreState} from '../store/createStoreState'
|
|
7
7
|
import {AuthStateType} from './authStateType'
|
|
8
8
|
import {authStore} from './authStore'
|
|
9
9
|
import {subscribeToStateAndFetchCurrentUser} from './subscribeToStateAndFetchCurrentUser'
|
|
@@ -20,7 +20,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
|
|
|
20
20
|
const clientFactory = vi.fn().mockReturnValue(mockClient)
|
|
21
21
|
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
|
|
22
22
|
|
|
23
|
-
const state =
|
|
23
|
+
const state = createStoreState(authStore.getInitialState(instance))
|
|
24
24
|
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
25
25
|
|
|
26
26
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
@@ -51,7 +51,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
|
|
|
51
51
|
const clientFactory = vi.fn().mockReturnValue(mockClient)
|
|
52
52
|
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
|
|
53
53
|
|
|
54
|
-
const state =
|
|
54
|
+
const state = createStoreState(authStore.getInitialState(instance))
|
|
55
55
|
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
56
56
|
|
|
57
57
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
@@ -86,7 +86,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
|
|
|
86
86
|
const clientFactory = vi.fn().mockReturnValue(mockClient)
|
|
87
87
|
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
|
|
88
88
|
|
|
89
|
-
const state =
|
|
89
|
+
const state = createStoreState(authStore.getInitialState(instance))
|
|
90
90
|
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
91
91
|
|
|
92
92
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
@@ -1,55 +1,53 @@
|
|
|
1
1
|
import {type CurrentUser} from '@sanity/types'
|
|
2
|
-
import {distinctUntilChanged, filter, map, switchMap} from 'rxjs'
|
|
2
|
+
import {distinctUntilChanged, filter, map, type Subscription, switchMap} from 'rxjs'
|
|
3
3
|
|
|
4
|
-
import {type
|
|
4
|
+
import {type StoreContext} from '../store/defineStore'
|
|
5
5
|
import {DEFAULT_API_VERSION, REQUEST_TAG_PREFIX} from './authConstants'
|
|
6
6
|
import {AuthStateType} from './authStateType'
|
|
7
7
|
import {type AuthState, type AuthStoreState} from './authStore'
|
|
8
8
|
|
|
9
|
-
export const subscribeToStateAndFetchCurrentUser =
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
export const subscribeToStateAndFetchCurrentUser = ({
|
|
10
|
+
state,
|
|
11
|
+
}: StoreContext<AuthStoreState>): Subscription => {
|
|
12
|
+
const {clientFactory, apiHost} = state.get().options
|
|
12
13
|
|
|
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
|
-
|
|
14
|
+
const currentUser$ = state.observable
|
|
15
|
+
.pipe(
|
|
16
|
+
map(({authState}) => authState),
|
|
17
|
+
filter(
|
|
18
|
+
(authState): authState is Extract<AuthState, {type: AuthStateType.LOGGED_IN}> =>
|
|
19
|
+
authState.type === AuthStateType.LOGGED_IN && !authState.currentUser,
|
|
20
|
+
),
|
|
21
|
+
map((authState) => authState.token),
|
|
22
|
+
distinctUntilChanged(),
|
|
23
|
+
)
|
|
24
|
+
.pipe(
|
|
25
|
+
map((token) =>
|
|
26
|
+
clientFactory({
|
|
27
|
+
apiVersion: DEFAULT_API_VERSION,
|
|
28
|
+
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
29
|
+
token,
|
|
30
|
+
ignoreBrowserTokenWarning: true,
|
|
31
|
+
useProjectHostname: false,
|
|
32
|
+
...(apiHost && {apiHost}),
|
|
33
|
+
}),
|
|
34
|
+
),
|
|
35
|
+
switchMap((client) =>
|
|
36
|
+
client.observable.request<CurrentUser>({uri: '/users/me', method: 'GET'}),
|
|
37
|
+
),
|
|
38
|
+
)
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
)
|
|
40
|
+
return currentUser$.subscribe({
|
|
41
|
+
next: (currentUser) => {
|
|
42
|
+
state.set('setCurrentUser', (prev) => ({
|
|
43
|
+
authState:
|
|
44
|
+
prev.authState.type === AuthStateType.LOGGED_IN
|
|
45
|
+
? {...prev.authState, currentUser}
|
|
46
|
+
: prev.authState,
|
|
47
|
+
}))
|
|
48
|
+
},
|
|
49
|
+
error: (error) => {
|
|
50
|
+
state.set('setError', {authState: {type: AuthStateType.ERROR, error}})
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import {Observable, Subject} from 'rxjs'
|
|
2
2
|
import {beforeEach, describe, it} from 'vitest'
|
|
3
3
|
|
|
4
|
-
import {createSanityInstance} from '../
|
|
5
|
-
import {
|
|
6
|
-
import {createResourceState} from '../resources/createResource'
|
|
4
|
+
import {createSanityInstance, type SanityInstance} from '../store/createSanityInstance'
|
|
5
|
+
import {createStoreState} from '../store/createStoreState'
|
|
7
6
|
import {AuthStateType} from './authStateType'
|
|
8
7
|
import {authStore} from './authStore'
|
|
9
8
|
import {subscribeToStorageEventsAndSetToken} from './subscribeToStorageEventsAndSetToken'
|
|
@@ -35,7 +34,7 @@ describe('subscribeToStorageEventsAndSetToken', () => {
|
|
|
35
34
|
})
|
|
36
35
|
|
|
37
36
|
it('sets the state to logged in when a matching storage event returns a token', () => {
|
|
38
|
-
const state =
|
|
37
|
+
const state = createStoreState(authStore.getInitialState(instance))
|
|
39
38
|
const {storageKey} = state.get().options
|
|
40
39
|
const subscription = subscribeToStorageEventsAndSetToken({state, instance})
|
|
41
40
|
|
|
@@ -55,7 +54,7 @@ describe('subscribeToStorageEventsAndSetToken', () => {
|
|
|
55
54
|
|
|
56
55
|
it('sets the state to logged in when a matching storage event returns null', () => {
|
|
57
56
|
vi.mocked(getTokenFromStorage).mockReturnValue('existing-token')
|
|
58
|
-
const state =
|
|
57
|
+
const state = createStoreState(authStore.getInitialState(instance))
|
|
59
58
|
const {storageKey} = state.get().options
|
|
60
59
|
|
|
61
60
|
const subscription = subscribeToStorageEventsAndSetToken({state, instance})
|
|
@@ -1,31 +1,29 @@
|
|
|
1
|
-
import {defer, distinctUntilChanged, filter, map} from 'rxjs'
|
|
1
|
+
import {defer, distinctUntilChanged, filter, map, type Subscription} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {type
|
|
3
|
+
import {type StoreContext} from '../store/defineStore'
|
|
4
4
|
import {AuthStateType} from './authStateType'
|
|
5
5
|
import {type AuthStoreState} from './authStore'
|
|
6
6
|
import {getStorageEvents, getTokenFromStorage} from './utils'
|
|
7
7
|
|
|
8
|
-
export const subscribeToStorageEventsAndSetToken =
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export const subscribeToStorageEventsAndSetToken = ({
|
|
9
|
+
state,
|
|
10
|
+
}: StoreContext<AuthStoreState>): Subscription => {
|
|
11
|
+
const {storageArea, storageKey} = state.get().options
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const tokenFromStorage$ = defer(getStorageEvents).pipe(
|
|
14
|
+
filter(
|
|
15
|
+
(e): e is StorageEvent & {newValue: string} =>
|
|
16
|
+
e.storageArea === storageArea && e.key === storageKey,
|
|
17
|
+
),
|
|
18
|
+
map(() => getTokenFromStorage(storageArea, storageKey)),
|
|
19
|
+
distinctUntilChanged(),
|
|
20
|
+
)
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
)
|
|
22
|
+
return tokenFromStorage$.subscribe((token) => {
|
|
23
|
+
state.set('updateTokenFromStorageEvent', {
|
|
24
|
+
authState: token
|
|
25
|
+
? {type: AuthStateType.LOGGED_IN, token, currentUser: null}
|
|
26
|
+
: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
}
|