@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.
Files changed (127) hide show
  1. package/dist/index.d.ts +428 -325
  2. package/dist/index.js +1618 -1553
  3. package/dist/index.js.map +1 -1
  4. package/package.json +6 -7
  5. package/src/_exports/index.ts +31 -30
  6. package/src/auth/authStore.test.ts +149 -104
  7. package/src/auth/authStore.ts +51 -100
  8. package/src/auth/handleAuthCallback.test.ts +67 -34
  9. package/src/auth/handleAuthCallback.ts +8 -7
  10. package/src/auth/logout.test.ts +61 -29
  11. package/src/auth/logout.ts +26 -28
  12. package/src/auth/refreshStampedToken.test.ts +9 -9
  13. package/src/auth/refreshStampedToken.ts +62 -56
  14. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
  15. package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
  16. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
  17. package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
  18. package/src/client/clientStore.test.ts +131 -67
  19. package/src/client/clientStore.ts +117 -116
  20. package/src/comlink/controller/actions/destroyController.test.ts +38 -13
  21. package/src/comlink/controller/actions/destroyController.ts +11 -15
  22. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
  23. package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
  24. package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
  25. package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
  26. package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
  27. package/src/comlink/controller/actions/releaseChannel.ts +22 -21
  28. package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
  29. package/src/comlink/controller/comlinkControllerStore.ts +44 -5
  30. package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
  31. package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
  32. package/src/comlink/node/actions/releaseNode.test.ts +75 -55
  33. package/src/comlink/node/actions/releaseNode.ts +19 -21
  34. package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
  35. package/src/comlink/node/comlinkNodeStore.ts +22 -5
  36. package/src/config/authConfig.ts +79 -0
  37. package/src/config/sanityConfig.ts +48 -0
  38. package/src/datasets/datasets.test.ts +2 -2
  39. package/src/datasets/datasets.ts +18 -5
  40. package/src/document/actions.test.ts +22 -10
  41. package/src/document/actions.ts +44 -56
  42. package/src/document/applyDocumentActions.test.ts +96 -36
  43. package/src/document/applyDocumentActions.ts +140 -99
  44. package/src/document/documentStore.test.ts +103 -155
  45. package/src/document/documentStore.ts +247 -237
  46. package/src/document/listen.ts +56 -55
  47. package/src/document/patchOperations.ts +0 -43
  48. package/src/document/permissions.test.ts +25 -12
  49. package/src/document/permissions.ts +11 -4
  50. package/src/document/processActions.test.ts +41 -8
  51. package/src/document/reducers.test.ts +87 -16
  52. package/src/document/reducers.ts +2 -2
  53. package/src/document/sharedListener.test.ts +34 -16
  54. package/src/document/sharedListener.ts +33 -11
  55. package/src/preview/getPreviewState.test.ts +40 -39
  56. package/src/preview/getPreviewState.ts +68 -56
  57. package/src/preview/previewConstants.ts +43 -0
  58. package/src/preview/previewQuery.test.ts +1 -1
  59. package/src/preview/previewQuery.ts +4 -5
  60. package/src/preview/previewStore.test.ts +13 -58
  61. package/src/preview/previewStore.ts +7 -21
  62. package/src/preview/resolvePreview.test.ts +33 -104
  63. package/src/preview/resolvePreview.ts +11 -21
  64. package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
  65. package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
  66. package/src/preview/util.ts +1 -0
  67. package/src/project/project.test.ts +3 -3
  68. package/src/project/project.ts +28 -5
  69. package/src/projection/getProjectionState.test.ts +69 -49
  70. package/src/projection/getProjectionState.ts +42 -50
  71. package/src/projection/projectionQuery.ts +1 -1
  72. package/src/projection/projectionStore.test.ts +13 -51
  73. package/src/projection/projectionStore.ts +6 -18
  74. package/src/projection/resolveProjection.test.ts +32 -127
  75. package/src/projection/resolveProjection.ts +15 -28
  76. package/src/projection/subscribeToStateAndFetchBatches.test.ts +105 -90
  77. package/src/projection/subscribeToStateAndFetchBatches.ts +94 -81
  78. package/src/projection/util.ts +2 -0
  79. package/src/projects/projects.test.ts +13 -4
  80. package/src/projects/projects.ts +6 -1
  81. package/src/query/queryStore.test.ts +10 -47
  82. package/src/query/queryStore.ts +151 -133
  83. package/src/query/queryStoreConstants.ts +2 -0
  84. package/src/store/createActionBinder.test.ts +153 -0
  85. package/src/store/createActionBinder.ts +176 -0
  86. package/src/store/createSanityInstance.test.ts +84 -0
  87. package/src/store/createSanityInstance.ts +124 -0
  88. package/src/store/createStateSourceAction.test.ts +196 -0
  89. package/src/store/createStateSourceAction.ts +260 -0
  90. package/src/store/createStoreInstance.test.ts +81 -0
  91. package/src/store/createStoreInstance.ts +80 -0
  92. package/src/store/createStoreState.test.ts +85 -0
  93. package/src/store/createStoreState.ts +92 -0
  94. package/src/store/defineStore.test.ts +18 -0
  95. package/src/store/defineStore.ts +81 -0
  96. package/src/users/reducers.test.ts +318 -0
  97. package/src/users/reducers.ts +88 -0
  98. package/src/users/types.ts +46 -4
  99. package/src/users/usersConstants.ts +4 -0
  100. package/src/users/usersStore.test.ts +350 -223
  101. package/src/users/usersStore.ts +285 -149
  102. package/src/utils/createFetcherStore.test.ts +6 -7
  103. package/src/utils/createFetcherStore.ts +150 -153
  104. package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
  105. package/src/auth/fetchLoginUrls.test.ts +0 -163
  106. package/src/auth/fetchLoginUrls.ts +0 -74
  107. package/src/common/createLiveEventSubscriber.test.ts +0 -121
  108. package/src/common/createLiveEventSubscriber.ts +0 -55
  109. package/src/common/types.ts +0 -4
  110. package/src/instance/identity.test.ts +0 -46
  111. package/src/instance/identity.ts +0 -29
  112. package/src/instance/sanityInstance.test.ts +0 -77
  113. package/src/instance/sanityInstance.ts +0 -57
  114. package/src/instance/types.ts +0 -37
  115. package/src/preview/getPreviewProjection.ts +0 -45
  116. package/src/resources/README.md +0 -370
  117. package/src/resources/createAction.test.ts +0 -101
  118. package/src/resources/createAction.ts +0 -44
  119. package/src/resources/createResource.test.ts +0 -112
  120. package/src/resources/createResource.ts +0 -102
  121. package/src/resources/createStateSourceAction.test.ts +0 -114
  122. package/src/resources/createStateSourceAction.ts +0 -83
  123. package/src/resources/createStore.test.ts +0 -67
  124. package/src/resources/createStore.ts +0 -46
  125. package/src/store/createStore.test.ts +0 -108
  126. package/src/store/createStore.ts +0 -106
  127. /package/src/{common/util.ts → utils/hashString.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import {createAction} from '../resources/createAction'
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 = createAction(authStore, ({state}) => {
9
+ export const logout = bindActionGlobally(authStore, async ({state}) => {
10
10
  const {clientFactory, apiHost, providedToken, storageArea, storageKey} = state.get().options
11
11
 
12
- return async function () {
13
- // If a token is statically provided, logout does nothing
14
- if (providedToken) return
12
+ // If a token is statically provided, logout does nothing
13
+ if (providedToken) return
15
14
 
16
- const {authState} = state.get()
15
+ const {authState} = state.get()
17
16
 
18
- // If we already have an inflight request, no-op
19
- if (authState.type === AuthStateType.LOGGED_OUT && authState.isDestroyingSession) return
20
- const token = authState.type === AuthStateType.LOGGED_IN && authState.token
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
- try {
23
- if (token) {
24
- state.set('loggingOut', {
25
- authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: true},
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
- await client.request<void>({uri: '/auth/logout', method: 'POST'})
37
- }
38
- } finally {
39
- state.set('logoutSuccess', {
40
- authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
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
- storageArea?.removeItem(storageKey)
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 '../instance/sanityInstance'
5
- import {createResourceState} from '../resources/createResource'
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 = createResourceState(initialState)
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 10 minutes so the interval fires
56
- vi.advanceTimersByTime(10 * 60 * 1000)
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 = createResourceState(initialState)
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(10 * 60 * 1000)
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 = createResourceState(initialState)
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 = createResourceState(initialState)
159
+ const state = createStoreState(initialState)
160
160
 
161
161
  const subscription: Subscription = refreshStampedToken({state, instance})
162
162
 
@@ -1,6 +1,14 @@
1
- import {distinctUntilChanged, filter, interval, map, switchMap, takeWhile} from 'rxjs'
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 ActionContext, createInternalAction} from '../resources/createAction'
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 = createInternalAction(
12
- ({state}: ActionContext<AuthStoreState>) => {
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
- const refreshToken$ = state.observable.pipe(
16
- map(({authState}) => authState),
17
- filter(
18
- (authState): authState is Extract<AuthState, {type: AuthStateType.LOGGED_IN}> =>
19
- authState.type === AuthStateType.LOGGED_IN,
20
- ),
21
- distinctUntilChanged(),
22
- filter((authState) => authState.token.includes('-st')), // Ensure we only try to refresh stamped tokens
23
- switchMap((authState) =>
24
- interval(10 * 60 * 1000).pipe(
25
- takeWhile(() => state.get().authState.type === AuthStateType.LOGGED_IN),
26
- map(() => authState.token),
27
- distinctUntilChanged(),
28
- map((token) =>
29
- clientFactory({
30
- apiVersion: DEFAULT_API_VERSION,
31
- requestTagPrefix: 'sdk.token-refresh',
32
- useProjectHostname: false,
33
- token,
34
- ignoreBrowserTokenWarning: true,
35
- ...(apiHost && {apiHost}),
36
- }),
37
- ),
38
- switchMap((client) =>
39
- client.observable.request<{token: string}>({
40
- uri: 'auth/refresh-token',
41
- method: 'POST',
42
- body: {
43
- token: authState.token,
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
- return function () {
52
- return refreshToken$.subscribe({
53
- next: (response) => {
54
- state.set('setRefreshStampedToken', (prev) => ({
55
- authState:
56
- prev.authState.type === AuthStateType.LOGGED_IN
57
- ? {...prev.authState, token: response.token}
58
- : prev.authState,
59
- }))
60
- storageArea?.setItem(storageKey, JSON.stringify({token: response.token}))
61
- },
62
- error: (error) => {
63
- state.set('setRefreshStampedTokenError', {authState: {type: AuthStateType.ERROR, error}})
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 '../instance/sanityInstance'
6
- import {createResourceState} from '../resources/createResource'
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 = createResourceState(authStore.getInitialState(instance))
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 = createResourceState(authStore.getInitialState(instance))
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 = createResourceState(authStore.getInitialState(instance))
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 ActionContext, createInternalAction} from '../resources/createAction'
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 = createInternalAction(
10
- ({state}: ActionContext<AuthStoreState>) => {
11
- const {clientFactory, apiHost} = state.get().options
9
+ export const subscribeToStateAndFetchCurrentUser = ({
10
+ state,
11
+ }: StoreContext<AuthStoreState>): Subscription => {
12
+ const {clientFactory, apiHost} = state.get().options
12
13
 
13
- const currentUser$ = state.observable
14
- .pipe(
15
- map(({authState}) => authState),
16
- filter(
17
- (authState): authState is Extract<AuthState, {type: AuthStateType.LOGGED_IN}> =>
18
- authState.type === AuthStateType.LOGGED_IN && !authState.currentUser,
19
- ),
20
- map((authState) => authState.token),
21
- distinctUntilChanged(),
22
- )
23
- .pipe(
24
- map((token) =>
25
- clientFactory({
26
- apiVersion: DEFAULT_API_VERSION,
27
- requestTagPrefix: REQUEST_TAG_PREFIX,
28
- token,
29
- ignoreBrowserTokenWarning: true,
30
- useProjectHostname: false,
31
- ...(apiHost && {apiHost}),
32
- }),
33
- ),
34
- switchMap((client) =>
35
- client.observable.request<CurrentUser>({uri: '/users/me', method: 'GET'}),
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
- return function () {
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
- }
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 '../instance/sanityInstance'
5
- import {type SanityInstance} from '../instance/types'
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 = createResourceState(authStore.getInitialState(instance))
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 = createResourceState(authStore.getInitialState(instance))
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 ActionContext, createInternalAction} from '../resources/createAction'
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 = createInternalAction(
9
- ({state}: ActionContext<AuthStoreState>) => {
10
- const {storageArea, storageKey} = state.get().options
8
+ export const subscribeToStorageEventsAndSetToken = ({
9
+ state,
10
+ }: StoreContext<AuthStoreState>): Subscription => {
11
+ const {storageArea, storageKey} = state.get().options
11
12
 
12
- const tokenFromStorage$ = defer(getStorageEvents).pipe(
13
- filter(
14
- (e): e is StorageEvent & {newValue: string} =>
15
- e.storageArea === storageArea && e.key === storageKey,
16
- ),
17
- map(() => getTokenFromStorage(storageArea, storageKey)),
18
- distinctUntilChanged(),
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
- return function () {
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
- }
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
+ }