Skip to content

Commit dd9d259

Browse files
committed
feat: make source required in core
This modifies the `bindActionByDataset` function to _require_ source to be passed and _only_ supports that. This now completes the work of: 1. Decoupling these methods from SanityInstance. These now don't look at the SanityInstance config to determine what to do. 2. Opening up for supporting other types of sources now that everything is encapsulated in DocumentSource.
1 parent d94d519 commit dd9d259

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+77
-206
lines changed

packages/core/src/auth/authStore.test.ts

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ describe('authStore', () => {
7979
const storageArea = {} as Storage
8080

8181
instance = createSanityInstance({
82-
projectId: 'p',
83-
dataset: 'd',
8482
auth: {
8583
apiHost,
8684
callbackUrl,
@@ -109,8 +107,6 @@ describe('authStore', () => {
109107
const context = {mode: 'test', env: 'staging', orgId: 'abc'}
110108
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
111109
instance = createSanityInstance({
112-
projectId: 'p',
113-
dataset: 'd',
114110
auth: {initialLocationHref},
115111
})
116112

@@ -124,8 +120,6 @@ describe('authStore', () => {
124120
const expectedContext = {mode: 'test', env: 'staging', orgId: 'abc'}
125121
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
126122
instance = createSanityInstance({
127-
projectId: 'p',
128-
dataset: 'd',
129123
auth: {initialLocationHref},
130124
})
131125

@@ -138,8 +132,6 @@ describe('authStore', () => {
138132
const initialLocationHref = `https://example.com/?_context=invalid-json`
139133
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
140134
instance = createSanityInstance({
141-
projectId: 'p',
142-
dataset: 'd',
143135
auth: {initialLocationHref},
144136
})
145137

@@ -158,8 +150,6 @@ describe('authStore', () => {
158150
const context = {mode: 'dashboard'}
159151
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
160152
instance = createSanityInstance({
161-
projectId: 'p',
162-
dataset: 'd',
163153
auth: {
164154
token,
165155
initialLocationHref,
@@ -175,8 +165,6 @@ describe('authStore', () => {
175165
const context = {orgId: 'org1'}
176166
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
177167
instance = createSanityInstance({
178-
projectId: 'p',
179-
dataset: 'd',
180168
auth: {initialLocationHref},
181169
})
182170

@@ -189,10 +177,7 @@ describe('authStore', () => {
189177

190178
it('sets to logged in from storage token when NOT in dashboard', () => {
191179
const storageToken = 'storage-token'
192-
instance = createSanityInstance({
193-
projectId: 'p',
194-
dataset: 'd',
195-
})
180+
instance = createSanityInstance()
196181

197182
vi.mocked(getAuthCode).mockReturnValue(null)
198183
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)
@@ -207,8 +192,6 @@ describe('authStore', () => {
207192
const context = {mode: 'dashboard'}
208193
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
209194
instance = createSanityInstance({
210-
projectId: 'p',
211-
dataset: 'd',
212195
auth: {initialLocationHref},
213196
})
214197

@@ -221,10 +204,7 @@ describe('authStore', () => {
221204
})
222205

223206
it('sets the state to logged out when no token, code, or context', () => {
224-
instance = createSanityInstance({
225-
projectId: 'p',
226-
dataset: 'd',
227-
})
207+
instance = createSanityInstance()
228208

229209
vi.mocked(getAuthCode).mockReturnValue(null)
230210
vi.mocked(getTokenFromStorage).mockReturnValue(null)
@@ -292,10 +272,7 @@ describe('authStore', () => {
292272
const storageToken = 'regular-storage-token'
293273
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)
294274

295-
instance = createSanityInstance({
296-
projectId: 'p',
297-
dataset: 'd',
298-
})
275+
instance = createSanityInstance()
299276

300277
const {authState, options} = authStore.getInitialState(instance, null)
301278
expect(getStudioTokenFromLocalStorage).not.toHaveBeenCalled()
@@ -307,8 +284,6 @@ describe('authStore', () => {
307284
it('sets to logging in if getTokenFromLocation returns a token', () => {
308285
const initialLocationHref = 'https://example.com/#token=hash-token'
309286
instance = createSanityInstance({
310-
projectId: 'p',
311-
dataset: 'd',
312287
auth: {initialLocationHref},
313288
})
314289

@@ -363,8 +338,6 @@ describe('authStore', () => {
363338

364339
it('subscribes to state and storage events and unsubscribes on dispose', () => {
365340
instance = createSanityInstance({
366-
projectId: 'p',
367-
dataset: 'd',
368341
auth: {storageArea: mockLocalStorage},
369342
})
370343

@@ -389,8 +362,6 @@ describe('authStore', () => {
389362

390363
it('does not subscribe to storage events when not using storage area', () => {
391364
instance = createSanityInstance({
392-
projectId: 'p',
393-
dataset: 'd',
394365
auth: {storageArea: undefined},
395366
})
396367

@@ -431,7 +402,7 @@ describe('authStore', () => {
431402
return NEVER.subscribe()
432403
})
433404

434-
instance = createSanityInstance({projectId: 'p', dataset: 'd'})
405+
instance = createSanityInstance()
435406

436407
const {getCurrent} = getCurrentUserState(instance)
437408

@@ -440,7 +411,7 @@ describe('authStore', () => {
440411
})
441412

442413
it('returns null otherwise', () => {
443-
instance = createSanityInstance({projectId: 'p', dataset: 'd'})
414+
instance = createSanityInstance()
444415
const {getCurrent} = getCurrentUserState(instance)
445416
expect(getCurrent()).toBe(null)
446417
})
@@ -456,8 +427,6 @@ describe('authStore', () => {
456427
it('returns the token if logged in', () => {
457428
const token = 'hard-coded-token'
458429
instance = createSanityInstance({
459-
projectId: 'p',
460-
dataset: 'd',
461430
auth: {token},
462431
})
463432
const tokenState = getTokenState(instance)
@@ -468,7 +437,7 @@ describe('authStore', () => {
468437
})
469438

470439
it('returns null otherwise', () => {
471-
instance = createSanityInstance({projectId: 'p', dataset: 'd'})
440+
instance = createSanityInstance()
472441

473442
const tokenState = getTokenState(instance)
474443
expect(tokenState.getCurrent()).toBe(null)
@@ -486,7 +455,7 @@ describe('authStore', () => {
486455
})
487456

488457
it('returns the default login url', () => {
489-
instance = createSanityInstance({projectId: 'p', dataset: 'd'})
458+
instance = createSanityInstance()
490459

491460
const loginUrlState = getLoginUrlState(instance)
492461
expect(loginUrlState.getCurrent()).toBe(
@@ -504,8 +473,6 @@ describe('authStore', () => {
504473

505474
it('returns the current state in `authState`', () => {
506475
instance = createSanityInstance({
507-
projectId: 'p',
508-
dataset: 'd',
509476
auth: {token: 'hard-coded-token'},
510477
})
511478

@@ -536,8 +503,6 @@ describe('authStore', () => {
536503
const context = {orgId: 'initial-org-id'}
537504
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
538505
instance = createSanityInstance({
539-
projectId: 'p',
540-
dataset: 'd',
541506
auth: {initialLocationHref},
542507
})
543508

@@ -554,8 +519,6 @@ describe('authStore', () => {
554519
const authCode = 'test-auth-code'
555520

556521
instance = createSanityInstance({
557-
projectId: 'p',
558-
dataset: 'd',
559522
auth: {
560523
clientFactory: () => mockClient as unknown as SanityClient,
561524
initialLocationHref, // Set initial context
@@ -591,8 +554,6 @@ describe('authStore', () => {
591554
const context = {mode: 'test'} // No orgId
592555
const initialLocationHref = `https://example.com/?_context=${encodeURIComponent(JSON.stringify(context))}`
593556
instance = createSanityInstance({
594-
projectId: 'p',
595-
dataset: 'd',
596557
auth: {initialLocationHref},
597558
})
598559

packages/core/src/auth/handleAuthCallback.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ describe('handleCallback', () => {
4444
vi.mocked(getAuthCode).mockReturnValue(authCode)
4545

4646
instance = createSanityInstance({
47-
projectId: 'p',
48-
dataset: 'd',
4947
auth: {
5048
clientFactory,
5149
storageArea: {setItem} as unknown as Storage,
@@ -89,8 +87,6 @@ describe('handleCallback', () => {
8987
vi.mocked(getAuthCode).mockReturnValue(authCode)
9088

9189
instance = createSanityInstance({
92-
projectId: 'p',
93-
dataset: 'd',
9490
auth: {
9591
clientFactory,
9692
storageArea: {setItem} as unknown as Storage,
@@ -119,8 +115,6 @@ describe('handleCallback', () => {
119115
const setItem = vi.fn()
120116

121117
instance = createSanityInstance({
122-
projectId: 'p',
123-
dataset: 'd',
124118
auth: {
125119
clientFactory,
126120
storageArea: {setItem: setItem as Storage['setItem']} as Storage,
@@ -161,8 +155,6 @@ describe('handleCallback', () => {
161155
const setItem = vi.fn()
162156

163157
instance = createSanityInstance({
164-
projectId: 'p',
165-
dataset: 'd',
166158
auth: {
167159
clientFactory,
168160
storageArea: {setItem} as unknown as Storage,
@@ -190,8 +182,6 @@ describe('handleCallback', () => {
190182
vi.mocked(getAuthCode).mockReturnValue(authCode)
191183

192184
instance = createSanityInstance({
193-
projectId: 'p',
194-
dataset: 'd',
195185
auth: {
196186
clientFactory,
197187
storageArea: {setItem} as unknown as Storage,
@@ -229,8 +219,6 @@ describe('handleCallback', () => {
229219
vi.mocked(getTokenFromLocation).mockReturnValue('hash-token')
230220

231221
instance = createSanityInstance({
232-
projectId: 'p',
233-
dataset: 'd',
234222
auth: {
235223
clientFactory,
236224
storageArea: {setItem} as unknown as Storage,

packages/core/src/auth/logout.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ describe('logout', () => {
3636
const removeItem = vi.fn() as Storage['removeItem']
3737

3838
instance = createSanityInstance({
39-
projectId: 'p',
40-
dataset: 'd',
4139
auth: {
4240
clientFactory,
4341
storageArea: {removeItem} as Storage,
@@ -67,8 +65,6 @@ describe('logout', () => {
6765
const clientFactory = vi.fn()
6866
const removeItem = vi.fn() as Storage['removeItem']
6967
instance = createSanityInstance({
70-
projectId: 'p',
71-
dataset: 'd',
7268
auth: {
7369
token: 'provided-token',
7470
clientFactory,
@@ -93,8 +89,6 @@ describe('logout', () => {
9389
vi.mocked(getTokenFromStorage).mockReturnValue('token')
9490

9591
instance = createSanityInstance({
96-
projectId: 'p',
97-
dataset: 'd',
9892
auth: {
9993
clientFactory,
10094
storageArea: {removeItem} as Storage,

packages/core/src/auth/refreshStampedToken.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ describe('refreshStampedToken', () => {
118118
}
119119
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
120120
const instance = createSanityInstance({
121-
projectId: 'p',
122-
dataset: 'd',
123121
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
124122
})
125123
const initialState = authStore.getInitialState(instance, null)
@@ -164,8 +162,6 @@ describe('refreshStampedToken', () => {
164162
}
165163
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
166164
const instance = createSanityInstance({
167-
projectId: 'p',
168-
dataset: 'd',
169165
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
170166
})
171167
const initialState = authStore.getInitialState(instance, null)
@@ -198,8 +194,6 @@ describe('refreshStampedToken', () => {
198194
const mockClient = {observable: {request: vi.fn()}}
199195
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
200196
const instance = createSanityInstance({
201-
projectId: 'p',
202-
dataset: 'd',
203197
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
204198
})
205199
const initialState = authStore.getInitialState(instance, null)
@@ -249,8 +243,6 @@ describe('refreshStampedToken', () => {
249243
}
250244
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
251245
const instance = createSanityInstance({
252-
projectId: 'p',
253-
dataset: 'd',
254246
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
255247
})
256248
const initialState = authStore.getInitialState(instance, null)
@@ -299,8 +291,6 @@ describe('refreshStampedToken', () => {
299291
}
300292
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
301293
const instance = createSanityInstance({
302-
projectId: 'p',
303-
dataset: 'd',
304294
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
305295
})
306296
const initialState = authStore.getInitialState(instance, null)
@@ -345,8 +335,6 @@ describe('refreshStampedToken', () => {
345335
const mockClient = {observable: {request: vi.fn(() => throwError(() => error))}}
346336
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
347337
const instance = createSanityInstance({
348-
projectId: 'p',
349-
dataset: 'd',
350338
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
351339
})
352340
const initialState = authStore.getInitialState(instance, null)
@@ -374,8 +362,6 @@ describe('refreshStampedToken', () => {
374362
it('does nothing if user is not logged in', async () => {
375363
const mockClientFactory = vi.fn()
376364
const instance = createSanityInstance({
377-
projectId: 'p',
378-
dataset: 'd',
379365
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
380366
})
381367
const initialState = authStore.getInitialState(instance, null)
@@ -400,8 +386,6 @@ describe('refreshStampedToken', () => {
400386
const mockClient = {observable: {request: vi.fn()}}
401387
const mockClientFactory = vi.fn().mockReturnValue(mockClient)
402388
const instance = createSanityInstance({
403-
projectId: 'p',
404-
dataset: 'd',
405389
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
406390
})
407391
const initialState = authStore.getInitialState(instance, null)

packages/core/src/auth/subscribeToStateAndFetchCurrentUser.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
1818
const mockRequest = vi.fn().mockReturnValue(of(mockUser))
1919
const mockClient = {observable: {request: mockRequest}}
2020
const clientFactory = vi.fn().mockReturnValue(mockClient)
21-
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
21+
const instance = createSanityInstance({auth: {clientFactory}})
2222

2323
const state = createStoreState(authStore.getInitialState(instance, null))
2424
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})
@@ -50,7 +50,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
5050
const mockRequest = vi.fn().mockReturnValue(of(mockUser).pipe(delay(0)))
5151
const mockClient = {observable: {request: mockRequest}}
5252
const clientFactory = vi.fn().mockReturnValue(mockClient)
53-
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
53+
const instance = createSanityInstance({auth: {clientFactory}})
5454

5555
const state = createStoreState(authStore.getInitialState(instance, null))
5656
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})
@@ -86,7 +86,7 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
8686
const mockRequest = vi.fn().mockReturnValue(throwError(() => error))
8787
const mockClient = {observable: {request: mockRequest}}
8888
const clientFactory = vi.fn().mockReturnValue(mockClient)
89-
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
89+
const instance = createSanityInstance({auth: {clientFactory}})
9090

9191
const state = createStoreState(authStore.getInitialState(instance, null))
9292
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})

packages/core/src/auth/subscribeToStorageEventsAndSetToken.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ describe('subscribeToStorageEventsAndSetToken', () => {
2323
vi.clearAllMocks()
2424

2525
instance = createSanityInstance({
26-
projectId: 'p',
27-
dataset: 'd',
2826
auth: {storageArea: storageArea},
2927
})
3028

0 commit comments

Comments
 (0)