@sanity/sdk-react 2.3.0 → 2.3.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk-react",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"react-compiler-runtime": "19.1.0-rc.2",
|
|
52
52
|
"react-error-boundary": "^5.0.0",
|
|
53
53
|
"rxjs": "^7.8.2",
|
|
54
|
-
"@sanity/sdk": "2.3.
|
|
54
|
+
"@sanity/sdk": "2.3.1"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@sanity/browserslist-config": "^1.0.5",
|
|
@@ -76,8 +76,8 @@
|
|
|
76
76
|
"vite": "^6.3.4",
|
|
77
77
|
"vitest": "^3.1.2",
|
|
78
78
|
"@repo/config-eslint": "0.0.0",
|
|
79
|
-
"@repo/config-test": "0.0.1",
|
|
80
79
|
"@repo/package.bundle": "3.82.0",
|
|
80
|
+
"@repo/config-test": "0.0.1",
|
|
81
81
|
"@repo/package.config": "0.0.1",
|
|
82
82
|
"@repo/tsconfig": "0.0.1"
|
|
83
83
|
},
|
|
@@ -7,6 +7,7 @@ import {ComlinkTokenRefreshProvider} from '../../context/ComlinkTokenRefresh'
|
|
|
7
7
|
import {useAuthState} from '../../hooks/auth/useAuthState'
|
|
8
8
|
import {useLoginUrl} from '../../hooks/auth/useLoginUrl'
|
|
9
9
|
import {useVerifyOrgProjects} from '../../hooks/auth/useVerifyOrgProjects'
|
|
10
|
+
import {useSanityInstance} from '../../hooks/context/useSanityInstance'
|
|
10
11
|
import {CorsErrorComponent} from '../errors/CorsErrorComponent'
|
|
11
12
|
import {isInIframe} from '../utils'
|
|
12
13
|
import {AuthError} from './AuthError'
|
|
@@ -154,17 +155,21 @@ function AuthSwitch({
|
|
|
154
155
|
...props
|
|
155
156
|
}: AuthSwitchProps) {
|
|
156
157
|
const authState = useAuthState()
|
|
157
|
-
const
|
|
158
|
+
const instance = useSanityInstance()
|
|
159
|
+
const studioModeEnabled = instance.config.studioMode?.enabled
|
|
160
|
+
const disableVerifyOrg =
|
|
161
|
+
!verifyOrganization || studioModeEnabled || authState.type !== AuthStateType.LOGGED_IN
|
|
162
|
+
const orgError = useVerifyOrgProjects(disableVerifyOrg, projectIds)
|
|
158
163
|
|
|
159
164
|
const isLoggedOut = authState.type === AuthStateType.LOGGED_OUT && !authState.isDestroyingSession
|
|
160
165
|
const loginUrl = useLoginUrl()
|
|
161
166
|
|
|
162
167
|
useEffect(() => {
|
|
163
|
-
if (isLoggedOut && !isInIframe()) {
|
|
164
|
-
// We don't want to redirect to login if we're in the Dashboard
|
|
168
|
+
if (isLoggedOut && !isInIframe() && !studioModeEnabled) {
|
|
169
|
+
// We don't want to redirect to login if we're in the Dashboard nor in studio mode
|
|
165
170
|
window.location.href = loginUrl
|
|
166
171
|
}
|
|
167
|
-
}, [isLoggedOut, loginUrl])
|
|
172
|
+
}, [isLoggedOut, loginUrl, studioModeEnabled])
|
|
168
173
|
|
|
169
174
|
// Only check the error if verification is enabled
|
|
170
175
|
if (verifyOrganization && orgError) {
|
|
@@ -5,6 +5,7 @@ import {afterEach, beforeEach, describe, expect, it, type Mock, vi} from 'vitest
|
|
|
5
5
|
|
|
6
6
|
import {useAuthState} from '../hooks/auth/useAuthState'
|
|
7
7
|
import {useWindowConnection} from '../hooks/comlink/useWindowConnection'
|
|
8
|
+
import {useSanityInstance} from '../hooks/context/useSanityInstance'
|
|
8
9
|
import {ComlinkTokenRefreshProvider} from './ComlinkTokenRefresh'
|
|
9
10
|
import {ResourceProvider} from './ResourceProvider'
|
|
10
11
|
|
|
@@ -26,13 +27,24 @@ vi.mock('../hooks/comlink/useWindowConnection', () => ({
|
|
|
26
27
|
useWindowConnection: vi.fn(),
|
|
27
28
|
}))
|
|
28
29
|
|
|
30
|
+
vi.mock('../hooks/context/useSanityInstance', () => ({
|
|
31
|
+
useSanityInstance: vi.fn(),
|
|
32
|
+
}))
|
|
33
|
+
|
|
29
34
|
// Use simpler mock typings
|
|
30
35
|
const mockGetIsInDashboardState = getIsInDashboardState as Mock
|
|
31
36
|
const mockSetAuthToken = setAuthToken as Mock
|
|
32
37
|
const mockUseAuthState = useAuthState as Mock
|
|
33
38
|
const mockUseWindowConnection = useWindowConnection as Mock
|
|
39
|
+
const mockUseSanityInstance = useSanityInstance as unknown as Mock
|
|
34
40
|
|
|
35
41
|
const mockFetch = vi.fn()
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
const mockSanityInstance: any = {
|
|
44
|
+
projectId: 'test',
|
|
45
|
+
dataset: 'test',
|
|
46
|
+
config: {studioMode: {enabled: false}},
|
|
47
|
+
}
|
|
36
48
|
|
|
37
49
|
describe('ComlinkTokenRefresh', () => {
|
|
38
50
|
beforeEach(() => {
|
|
@@ -40,6 +52,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
40
52
|
mockGetIsInDashboardState.mockReturnValue({getCurrent: vi.fn(() => false)})
|
|
41
53
|
mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN})
|
|
42
54
|
mockUseWindowConnection.mockReturnValue({fetch: mockFetch})
|
|
55
|
+
mockUseSanityInstance.mockReturnValue(mockSanityInstance)
|
|
43
56
|
})
|
|
44
57
|
|
|
45
58
|
afterEach(() => {
|
|
@@ -89,7 +102,8 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
89
102
|
mockGetIsInDashboardState.mockReturnValue({getCurrent: () => true})
|
|
90
103
|
})
|
|
91
104
|
|
|
92
|
-
it('should initialize useWindowConnection with correct parameters', () => {
|
|
105
|
+
it('should initialize useWindowConnection with correct parameters when not in studio mode', () => {
|
|
106
|
+
// Simulate studio mode disabled by default
|
|
93
107
|
render(
|
|
94
108
|
<ResourceProvider projectId="test-project" dataset="test-dataset" fallback={null}>
|
|
95
109
|
<ComlinkTokenRefreshProvider>
|
|
@@ -106,7 +120,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
106
120
|
)
|
|
107
121
|
})
|
|
108
122
|
|
|
109
|
-
it('should handle received token', async () => {
|
|
123
|
+
it('should handle received token when not in studio mode', async () => {
|
|
110
124
|
mockUseAuthState.mockReturnValue({
|
|
111
125
|
type: AuthStateType.ERROR,
|
|
112
126
|
error: {statusCode: 401, message: 'Unauthorized'},
|
|
@@ -129,7 +143,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
129
143
|
expect(mockFetch).toHaveBeenCalledTimes(1)
|
|
130
144
|
})
|
|
131
145
|
|
|
132
|
-
it('should not set auth token if received token is null', async () => {
|
|
146
|
+
it('should not set auth token if received token is null when not in studio mode', async () => {
|
|
133
147
|
mockUseAuthState.mockReturnValue({
|
|
134
148
|
type: AuthStateType.ERROR,
|
|
135
149
|
error: {statusCode: 401, message: 'Unauthorized'},
|
|
@@ -151,7 +165,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
151
165
|
expect(mockSetAuthToken).not.toHaveBeenCalled()
|
|
152
166
|
})
|
|
153
167
|
|
|
154
|
-
it('should handle fetch errors gracefully', async () => {
|
|
168
|
+
it('should handle fetch errors gracefully when not in studio mode', async () => {
|
|
155
169
|
mockUseAuthState.mockReturnValue({
|
|
156
170
|
type: AuthStateType.ERROR,
|
|
157
171
|
error: {statusCode: 401, message: 'Unauthorized'},
|
|
@@ -174,7 +188,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
174
188
|
})
|
|
175
189
|
|
|
176
190
|
describe('Automatic token refresh', () => {
|
|
177
|
-
it('should not request new token for non-401 errors', async () => {
|
|
191
|
+
it('should not request new token for non-401 errors when not in studio mode', async () => {
|
|
178
192
|
mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN})
|
|
179
193
|
const {rerender} = render(
|
|
180
194
|
<ResourceProvider fallback={null}>
|
|
@@ -205,7 +219,7 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
205
219
|
expect(mockFetch).not.toHaveBeenCalled()
|
|
206
220
|
})
|
|
207
221
|
|
|
208
|
-
it('should request new token on LOGGED_OUT state', async () => {
|
|
222
|
+
it('should request new token on LOGGED_OUT state when not in studio mode', async () => {
|
|
209
223
|
mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN})
|
|
210
224
|
const {rerender} = render(
|
|
211
225
|
<ResourceProvider fallback={null}>
|
|
@@ -228,6 +242,28 @@ describe('ComlinkTokenRefresh', () => {
|
|
|
228
242
|
|
|
229
243
|
expect(mockFetch).toHaveBeenCalledWith('dashboard/v1/auth/tokens/create')
|
|
230
244
|
})
|
|
245
|
+
|
|
246
|
+
describe('when in studio mode', () => {
|
|
247
|
+
beforeEach(() => {
|
|
248
|
+
// Make the instance report studio mode enabled
|
|
249
|
+
mockUseSanityInstance.mockReturnValue({
|
|
250
|
+
...mockSanityInstance,
|
|
251
|
+
config: {studioMode: {enabled: true}},
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('should not render DashboardTokenRefresh when studio mode enabled', () => {
|
|
256
|
+
render(
|
|
257
|
+
<ComlinkTokenRefreshProvider>
|
|
258
|
+
<div>Test</div>
|
|
259
|
+
</ComlinkTokenRefreshProvider>,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
// In studio mode, provider should return children directly
|
|
263
|
+
// So window connection should not be initialized
|
|
264
|
+
expect(mockUseWindowConnection).not.toHaveBeenCalled()
|
|
265
|
+
})
|
|
266
|
+
})
|
|
231
267
|
})
|
|
232
268
|
})
|
|
233
269
|
})
|
|
@@ -130,8 +130,9 @@ function DashboardTokenRefresh({children}: PropsWithChildren) {
|
|
|
130
130
|
export const ComlinkTokenRefreshProvider: React.FC<PropsWithChildren> = ({children}) => {
|
|
131
131
|
const instance = useSanityInstance()
|
|
132
132
|
const isInDashboard = useMemo(() => getIsInDashboardState(instance).getCurrent(), [instance])
|
|
133
|
+
const studioModeEnabled = !!instance.config.studioMode?.enabled
|
|
133
134
|
|
|
134
|
-
if (isInDashboard) {
|
|
135
|
+
if (isInDashboard && !studioModeEnabled) {
|
|
135
136
|
return <DashboardTokenRefresh>{children}</DashboardTokenRefresh>
|
|
136
137
|
}
|
|
137
138
|
|