@sanity/sdk 2.3.1 → 2.5.0
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 +173 -105
- package/dist/index.js +354 -122
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/src/_exports/index.ts +30 -0
- package/src/agent/agentActions.test.ts +81 -0
- package/src/agent/agentActions.ts +139 -0
- package/src/auth/authStore.test.ts +13 -13
- package/src/auth/refreshStampedToken.test.ts +16 -16
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
- package/src/auth/utils.ts +36 -0
- package/src/client/clientStore.test.ts +151 -0
- package/src/client/clientStore.ts +39 -1
- package/src/comlink/controller/actions/destroyController.test.ts +2 -2
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
- package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
- package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
- package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
- package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
- package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
- package/src/comlink/node/actions/releaseNode.test.ts +2 -2
- package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
- package/src/config/sanityConfig.ts +49 -3
- package/src/document/actions.test.ts +34 -0
- package/src/document/actions.ts +31 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +148 -107
- package/src/document/documentStore.ts +40 -10
- package/src/document/permissions.test.ts +9 -9
- package/src/document/permissions.ts +17 -7
- package/src/document/processActions.test.ts +345 -0
- package/src/document/processActions.ts +185 -2
- package/src/document/reducers.ts +13 -6
- package/src/presence/presenceStore.ts +13 -7
- package/src/preview/previewStore.test.ts +10 -2
- package/src/preview/previewStore.ts +2 -1
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
- package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/projection/projectionStore.test.ts +18 -2
- package/src/projection/projectionStore.ts +2 -1
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
- package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/query/queryStore.ts +7 -4
- package/src/releases/getPerspectiveState.ts +2 -2
- package/src/releases/releasesStore.ts +10 -4
- package/src/store/createActionBinder.test.ts +8 -6
- package/src/store/createActionBinder.ts +50 -14
- package/src/store/createStateSourceAction.test.ts +12 -11
- package/src/store/createStateSourceAction.ts +6 -6
- package/src/store/createStoreInstance.test.ts +29 -16
- package/src/store/createStoreInstance.ts +6 -5
- package/src/store/defineStore.test.ts +1 -1
- package/src/store/defineStore.ts +12 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK",
|
|
6
6
|
"keywords": [
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "git+
|
|
21
|
+
"url": "git+https://github.com/sanity-io/sdk.git",
|
|
22
|
+
"directory": "packages/core"
|
|
22
23
|
},
|
|
23
24
|
"license": "MIT",
|
|
24
25
|
"author": "Sanity <developers@sanity.io>",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"@sanity/diff-match-patch": "^3.2.0",
|
|
49
50
|
"@sanity/diff-patch": "^6.0.0",
|
|
50
51
|
"@sanity/json-match": "^1.0.5",
|
|
51
|
-
"@sanity/message-protocol": "^0.
|
|
52
|
+
"@sanity/message-protocol": "^0.18.0",
|
|
52
53
|
"@sanity/mutate": "^0.12.4",
|
|
53
54
|
"@sanity/types": "^3.83.0",
|
|
54
55
|
"groq": "3.88.1-typegen-experimental.0",
|
|
@@ -60,24 +61,24 @@
|
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@sanity/browserslist-config": "^1.0.5",
|
|
63
|
-
"@sanity/pkg-utils": "^
|
|
64
|
-
"@sanity/prettier-config": "^1.0.
|
|
64
|
+
"@sanity/pkg-utils": "^8.1.29",
|
|
65
|
+
"@sanity/prettier-config": "^1.0.6",
|
|
65
66
|
"@types/lodash-es": "^4.17.12",
|
|
66
|
-
"@vitest/coverage-v8": "3.
|
|
67
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
67
68
|
"eslint": "^9.22.0",
|
|
68
|
-
"prettier": "^3.
|
|
69
|
+
"prettier": "^3.7.3",
|
|
69
70
|
"rollup-plugin-visualizer": "^5.14.0",
|
|
70
71
|
"typescript": "^5.8.3",
|
|
71
72
|
"vite": "^6.3.4",
|
|
72
|
-
"vitest": "^3.
|
|
73
|
+
"vitest": "^3.2.4",
|
|
73
74
|
"@repo/config-eslint": "0.0.0",
|
|
75
|
+
"@repo/config-test": "0.0.1",
|
|
74
76
|
"@repo/package.bundle": "3.82.0",
|
|
75
77
|
"@repo/package.config": "0.0.1",
|
|
76
|
-
"@repo/tsconfig": "0.0.1"
|
|
77
|
-
"@repo/config-test": "0.0.1"
|
|
78
|
+
"@repo/tsconfig": "0.0.1"
|
|
78
79
|
},
|
|
79
80
|
"engines": {
|
|
80
|
-
"node": ">=20.
|
|
81
|
+
"node": ">=20.19"
|
|
81
82
|
},
|
|
82
83
|
"publishConfig": {
|
|
83
84
|
"access": "public"
|
package/src/_exports/index.ts
CHANGED
|
@@ -5,6 +5,25 @@ import {type SanityProject as _SanityProject} from '@sanity/client'
|
|
|
5
5
|
*/
|
|
6
6
|
export type SanityProject = _SanityProject
|
|
7
7
|
|
|
8
|
+
export type {
|
|
9
|
+
AgentGenerateOptions,
|
|
10
|
+
AgentGenerateResult,
|
|
11
|
+
AgentPatchOptions,
|
|
12
|
+
AgentPatchResult,
|
|
13
|
+
AgentPromptOptions,
|
|
14
|
+
AgentPromptResult,
|
|
15
|
+
AgentTransformOptions,
|
|
16
|
+
AgentTransformResult,
|
|
17
|
+
AgentTranslateOptions,
|
|
18
|
+
AgentTranslateResult,
|
|
19
|
+
} from '../agent/agentActions'
|
|
20
|
+
export {
|
|
21
|
+
agentGenerate,
|
|
22
|
+
agentPatch,
|
|
23
|
+
agentPrompt,
|
|
24
|
+
agentTransform,
|
|
25
|
+
agentTranslate,
|
|
26
|
+
} from '../agent/agentActions'
|
|
8
27
|
export {AuthStateType} from '../auth/authStateType'
|
|
9
28
|
export {
|
|
10
29
|
type AuthState,
|
|
@@ -24,6 +43,13 @@ export {
|
|
|
24
43
|
export {observeOrganizationVerificationState} from '../auth/getOrganizationVerificationState'
|
|
25
44
|
export {handleAuthCallback} from '../auth/handleAuthCallback'
|
|
26
45
|
export {logout} from '../auth/logout'
|
|
46
|
+
export {
|
|
47
|
+
type ApiErrorBody,
|
|
48
|
+
getClientErrorApiBody,
|
|
49
|
+
getClientErrorApiDescription,
|
|
50
|
+
getClientErrorApiType,
|
|
51
|
+
isProjectUserNotFoundClientError,
|
|
52
|
+
} from '../auth/utils'
|
|
27
53
|
export type {ClientStoreState as ClientState} from '../client/clientStore'
|
|
28
54
|
export {type ClientOptions, getClient, getClientState} from '../client/clientStore'
|
|
29
55
|
export {
|
|
@@ -50,9 +76,13 @@ export {
|
|
|
50
76
|
createProjectHandle,
|
|
51
77
|
} from '../config/handles'
|
|
52
78
|
export {
|
|
79
|
+
canvasSource,
|
|
53
80
|
type DatasetHandle,
|
|
81
|
+
datasetSource,
|
|
54
82
|
type DocumentHandle,
|
|
83
|
+
type DocumentSource,
|
|
55
84
|
type DocumentTypeHandle,
|
|
85
|
+
mediaLibrarySource,
|
|
56
86
|
type PerspectiveHandle,
|
|
57
87
|
type ProjectHandle,
|
|
58
88
|
type ReleasePerspective,
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import {firstValueFrom, of} from 'rxjs'
|
|
3
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
agentGenerate,
|
|
7
|
+
agentPatch,
|
|
8
|
+
agentPrompt,
|
|
9
|
+
agentTransform,
|
|
10
|
+
agentTranslate,
|
|
11
|
+
} from './agentActions'
|
|
12
|
+
|
|
13
|
+
let mockClient: any
|
|
14
|
+
|
|
15
|
+
vi.mock('../client/clientStore', () => {
|
|
16
|
+
return {
|
|
17
|
+
getClientState: () => ({observable: of(mockClient)}),
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('agent actions', () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
mockClient = {
|
|
24
|
+
observable: {
|
|
25
|
+
agent: {
|
|
26
|
+
action: {
|
|
27
|
+
generate: vi.fn(),
|
|
28
|
+
transform: vi.fn(),
|
|
29
|
+
translate: vi.fn(),
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
agent: {
|
|
34
|
+
action: {
|
|
35
|
+
prompt: vi.fn(),
|
|
36
|
+
patch: vi.fn(),
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('agentGenerate returns observable from client', async () => {
|
|
43
|
+
mockClient.observable.agent.action.generate.mockReturnValue(of('gen'))
|
|
44
|
+
const instance = {config: {projectId: 'p', dataset: 'd'}} as any
|
|
45
|
+
const value = await firstValueFrom(agentGenerate(instance, {foo: 'bar'} as any))
|
|
46
|
+
expect(value).toBe('gen')
|
|
47
|
+
expect(mockClient.observable.agent.action.generate).toHaveBeenCalledWith({foo: 'bar'})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('agentTransform returns observable from client', async () => {
|
|
51
|
+
mockClient.observable.agent.action.transform.mockReturnValue(of('xform'))
|
|
52
|
+
const instance = {config: {projectId: 'p', dataset: 'd'}} as any
|
|
53
|
+
const value = await firstValueFrom(agentTransform(instance, {a: 1} as any))
|
|
54
|
+
expect(value).toBe('xform')
|
|
55
|
+
expect(mockClient.observable.agent.action.transform).toHaveBeenCalledWith({a: 1})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('agentTranslate returns observable from client', async () => {
|
|
59
|
+
mockClient.observable.agent.action.translate.mockReturnValue(of('xlate'))
|
|
60
|
+
const instance = {config: {projectId: 'p', dataset: 'd'}} as any
|
|
61
|
+
const value = await firstValueFrom(agentTranslate(instance, {b: 2} as any))
|
|
62
|
+
expect(value).toBe('xlate')
|
|
63
|
+
expect(mockClient.observable.agent.action.translate).toHaveBeenCalledWith({b: 2})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('agentPrompt wraps promise into observable', async () => {
|
|
67
|
+
mockClient.agent.action.prompt.mockResolvedValue('prompted')
|
|
68
|
+
const instance = {config: {projectId: 'p', dataset: 'd'}} as any
|
|
69
|
+
const value = await firstValueFrom(agentPrompt(instance, {p: true} as any))
|
|
70
|
+
expect(value).toBe('prompted')
|
|
71
|
+
expect(mockClient.agent.action.prompt).toHaveBeenCalledWith({p: true})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('agentPatch wraps promise into observable', async () => {
|
|
75
|
+
mockClient.agent.action.patch.mockResolvedValue('patched')
|
|
76
|
+
const instance = {config: {projectId: 'p', dataset: 'd'}} as any
|
|
77
|
+
const value = await firstValueFrom(agentPatch(instance, {q: false} as any))
|
|
78
|
+
expect(value).toBe('patched')
|
|
79
|
+
expect(mockClient.agent.action.patch).toHaveBeenCalledWith({q: false})
|
|
80
|
+
})
|
|
81
|
+
})
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {type SanityClient} from '@sanity/client'
|
|
2
|
+
import {from, Observable, switchMap} from 'rxjs'
|
|
3
|
+
|
|
4
|
+
import {getClientState} from '../client/clientStore'
|
|
5
|
+
import {type SanityInstance} from '../store/createSanityInstance'
|
|
6
|
+
|
|
7
|
+
const API_VERSION = 'vX'
|
|
8
|
+
|
|
9
|
+
/** @alpha */
|
|
10
|
+
export type AgentGenerateOptions = Parameters<
|
|
11
|
+
SanityClient['observable']['agent']['action']['generate']
|
|
12
|
+
>[0]
|
|
13
|
+
|
|
14
|
+
/** @alpha */
|
|
15
|
+
export type AgentTransformOptions = Parameters<
|
|
16
|
+
SanityClient['observable']['agent']['action']['transform']
|
|
17
|
+
>[0]
|
|
18
|
+
|
|
19
|
+
/** @alpha */
|
|
20
|
+
export type AgentTranslateOptions = Parameters<
|
|
21
|
+
SanityClient['observable']['agent']['action']['translate']
|
|
22
|
+
>[0]
|
|
23
|
+
|
|
24
|
+
/** @alpha */
|
|
25
|
+
export type AgentPromptOptions = Parameters<SanityClient['agent']['action']['prompt']>[0]
|
|
26
|
+
|
|
27
|
+
/** @alpha */
|
|
28
|
+
export type AgentPatchOptions = Parameters<SanityClient['agent']['action']['patch']>[0]
|
|
29
|
+
|
|
30
|
+
/** @alpha */
|
|
31
|
+
export type AgentGenerateResult = Awaited<
|
|
32
|
+
ReturnType<SanityClient['observable']['agent']['action']['generate']>
|
|
33
|
+
>
|
|
34
|
+
|
|
35
|
+
/** @alpha */
|
|
36
|
+
export type AgentTransformResult = Awaited<
|
|
37
|
+
ReturnType<SanityClient['observable']['agent']['action']['transform']>
|
|
38
|
+
>
|
|
39
|
+
|
|
40
|
+
/** @alpha */
|
|
41
|
+
export type AgentTranslateResult = Awaited<
|
|
42
|
+
ReturnType<SanityClient['observable']['agent']['action']['translate']>
|
|
43
|
+
>
|
|
44
|
+
|
|
45
|
+
/** @alpha */
|
|
46
|
+
export type AgentPromptResult = Awaited<ReturnType<SanityClient['agent']['action']['prompt']>>
|
|
47
|
+
|
|
48
|
+
/** @alpha */
|
|
49
|
+
export type AgentPatchResult = Awaited<ReturnType<SanityClient['agent']['action']['patch']>>
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Generates a new document using the agent.
|
|
53
|
+
* @param instance - The Sanity instance.
|
|
54
|
+
* @param options - The options for the agent generate action. See the [Agent Actions API](https://www.sanity.io/docs/agent-actions/introduction) for more details.
|
|
55
|
+
* @returns An Observable emitting the result of the agent generate action.
|
|
56
|
+
* @alpha
|
|
57
|
+
*/
|
|
58
|
+
export function agentGenerate(
|
|
59
|
+
instance: SanityInstance,
|
|
60
|
+
options: AgentGenerateOptions,
|
|
61
|
+
): AgentGenerateResult {
|
|
62
|
+
return getClientState(instance, {
|
|
63
|
+
apiVersion: API_VERSION,
|
|
64
|
+
projectId: instance.config.projectId,
|
|
65
|
+
dataset: instance.config.dataset,
|
|
66
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.generate(options)))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Transforms a document using the agent.
|
|
71
|
+
* @param instance - The Sanity instance.
|
|
72
|
+
* @param options - The options for the agent transform action. See the [Agent Actions API](https://www.sanity.io/docs/agent-actions/introduction) for more details.
|
|
73
|
+
* @returns An Observable emitting the result of the agent transform action.
|
|
74
|
+
* @alpha
|
|
75
|
+
*/
|
|
76
|
+
export function agentTransform(
|
|
77
|
+
instance: SanityInstance,
|
|
78
|
+
options: AgentTransformOptions,
|
|
79
|
+
): AgentTransformResult {
|
|
80
|
+
return getClientState(instance, {
|
|
81
|
+
apiVersion: API_VERSION,
|
|
82
|
+
projectId: instance.config.projectId,
|
|
83
|
+
dataset: instance.config.dataset,
|
|
84
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.transform(options)))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Translates a document using the agent.
|
|
89
|
+
* @param instance - The Sanity instance.
|
|
90
|
+
* @param options - The options for the agent translate action. See the [Agent Actions API](https://www.sanity.io/docs/agent-actions/introduction) for more details.
|
|
91
|
+
* @returns An Observable emitting the result of the agent translate action.
|
|
92
|
+
* @alpha
|
|
93
|
+
*/
|
|
94
|
+
export function agentTranslate(
|
|
95
|
+
instance: SanityInstance,
|
|
96
|
+
options: AgentTranslateOptions,
|
|
97
|
+
): AgentTranslateResult {
|
|
98
|
+
return getClientState(instance, {
|
|
99
|
+
apiVersion: API_VERSION,
|
|
100
|
+
projectId: instance.config.projectId,
|
|
101
|
+
dataset: instance.config.dataset,
|
|
102
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.translate(options)))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Prompts the agent using the same instruction template format as the other actions, but returns text or json instead of acting on a document.
|
|
107
|
+
* @param instance - The Sanity instance.
|
|
108
|
+
* @param options - The options for the agent prompt action. See the [Agent Actions API](https://www.sanity.io/docs/agent-actions/introduction) for more details.
|
|
109
|
+
* @returns An Observable emitting the result of the agent prompt action.
|
|
110
|
+
* @alpha
|
|
111
|
+
*/
|
|
112
|
+
export function agentPrompt(
|
|
113
|
+
instance: SanityInstance,
|
|
114
|
+
options: AgentPromptOptions,
|
|
115
|
+
): Observable<AgentPromptResult> {
|
|
116
|
+
return getClientState(instance, {
|
|
117
|
+
apiVersion: API_VERSION,
|
|
118
|
+
projectId: instance.config.projectId,
|
|
119
|
+
dataset: instance.config.dataset,
|
|
120
|
+
}).observable.pipe(switchMap((client) => from(client.agent.action.prompt(options))))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Patches a document using the agent.
|
|
125
|
+
* @param instance - The Sanity instance.
|
|
126
|
+
* @param options - The options for the agent patch action. See the [Agent Actions API](https://www.sanity.io/docs/agent-actions/introduction) for more details.
|
|
127
|
+
* @returns An Observable emitting the result of the agent patch action.
|
|
128
|
+
* @alpha
|
|
129
|
+
*/
|
|
130
|
+
export function agentPatch(
|
|
131
|
+
instance: SanityInstance,
|
|
132
|
+
options: AgentPatchOptions,
|
|
133
|
+
): Observable<AgentPatchResult> {
|
|
134
|
+
return getClientState(instance, {
|
|
135
|
+
apiVersion: API_VERSION,
|
|
136
|
+
projectId: instance.config.projectId,
|
|
137
|
+
dataset: instance.config.dataset,
|
|
138
|
+
}).observable.pipe(switchMap((client) => from(client.agent.action.patch(options))))
|
|
139
|
+
}
|
|
@@ -92,7 +92,7 @@ describe('authStore', () => {
|
|
|
92
92
|
},
|
|
93
93
|
})
|
|
94
94
|
|
|
95
|
-
const {options, dashboardContext} = authStore.getInitialState(instance)
|
|
95
|
+
const {options, dashboardContext} = authStore.getInitialState(instance, null)
|
|
96
96
|
|
|
97
97
|
expect(options.apiHost).toBe(apiHost)
|
|
98
98
|
expect(options.callbackUrl).toBe(callbackUrl)
|
|
@@ -114,7 +114,7 @@ describe('authStore', () => {
|
|
|
114
114
|
auth: {initialLocationHref},
|
|
115
115
|
})
|
|
116
116
|
|
|
117
|
-
const {dashboardContext, authState} = authStore.getInitialState(instance)
|
|
117
|
+
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
|
|
118
118
|
expect(dashboardContext).toEqual(context)
|
|
119
119
|
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
|
|
120
120
|
})
|
|
@@ -129,7 +129,7 @@ describe('authStore', () => {
|
|
|
129
129
|
auth: {initialLocationHref},
|
|
130
130
|
})
|
|
131
131
|
|
|
132
|
-
const {dashboardContext, authState} = authStore.getInitialState(instance)
|
|
132
|
+
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
|
|
133
133
|
expect(dashboardContext).toEqual(expectedContext)
|
|
134
134
|
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
|
|
135
135
|
})
|
|
@@ -143,7 +143,7 @@ describe('authStore', () => {
|
|
|
143
143
|
auth: {initialLocationHref},
|
|
144
144
|
})
|
|
145
145
|
|
|
146
|
-
const {dashboardContext, authState} = authStore.getInitialState(instance)
|
|
146
|
+
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
|
|
147
147
|
expect(dashboardContext).toStrictEqual({})
|
|
148
148
|
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
|
|
149
149
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
@@ -166,7 +166,7 @@ describe('authStore', () => {
|
|
|
166
166
|
},
|
|
167
167
|
})
|
|
168
168
|
|
|
169
|
-
const {authState, dashboardContext} = authStore.getInitialState(instance)
|
|
169
|
+
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
|
|
170
170
|
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token})
|
|
171
171
|
expect(dashboardContext).toEqual(context)
|
|
172
172
|
})
|
|
@@ -182,7 +182,7 @@ describe('authStore', () => {
|
|
|
182
182
|
|
|
183
183
|
vi.mocked(getAuthCode).mockReturnValue('auth-code')
|
|
184
184
|
|
|
185
|
-
const {authState, dashboardContext} = authStore.getInitialState(instance)
|
|
185
|
+
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
|
|
186
186
|
expect(authState).toMatchObject({type: AuthStateType.LOGGING_IN})
|
|
187
187
|
expect(dashboardContext).toEqual(context)
|
|
188
188
|
})
|
|
@@ -197,7 +197,7 @@ describe('authStore', () => {
|
|
|
197
197
|
vi.mocked(getAuthCode).mockReturnValue(null)
|
|
198
198
|
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)
|
|
199
199
|
|
|
200
|
-
const {authState, dashboardContext} = authStore.getInitialState(instance)
|
|
200
|
+
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
|
|
201
201
|
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token: storageToken})
|
|
202
202
|
expect(dashboardContext).toStrictEqual({})
|
|
203
203
|
})
|
|
@@ -215,7 +215,7 @@ describe('authStore', () => {
|
|
|
215
215
|
vi.mocked(getAuthCode).mockReturnValue(null)
|
|
216
216
|
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)
|
|
217
217
|
|
|
218
|
-
const {authState, dashboardContext} = authStore.getInitialState(instance)
|
|
218
|
+
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
|
|
219
219
|
expect(authState).toMatchObject({type: AuthStateType.LOGGED_OUT})
|
|
220
220
|
expect(dashboardContext).toEqual(context)
|
|
221
221
|
})
|
|
@@ -229,7 +229,7 @@ describe('authStore', () => {
|
|
|
229
229
|
vi.mocked(getAuthCode).mockReturnValue(null)
|
|
230
230
|
vi.mocked(getTokenFromStorage).mockReturnValue(null)
|
|
231
231
|
|
|
232
|
-
const {authState, dashboardContext} = authStore.getInitialState(instance)
|
|
232
|
+
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
|
|
233
233
|
expect(authState).toMatchObject({type: AuthStateType.LOGGED_OUT})
|
|
234
234
|
expect(dashboardContext).toStrictEqual({})
|
|
235
235
|
})
|
|
@@ -252,7 +252,7 @@ describe('authStore', () => {
|
|
|
252
252
|
auth: {storageArea: mockStorage}, // Provide mock storage
|
|
253
253
|
})
|
|
254
254
|
|
|
255
|
-
const {authState, options} = authStore.getInitialState(instance)
|
|
255
|
+
const {authState, options} = authStore.getInitialState(instance, null)
|
|
256
256
|
expect(getStudioTokenFromLocalStorage).toHaveBeenCalledWith(mockStorage, studioStorageKey)
|
|
257
257
|
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token: studioToken})
|
|
258
258
|
expect(options.authMethod).toBe('localstorage')
|
|
@@ -277,7 +277,7 @@ describe('authStore', () => {
|
|
|
277
277
|
})
|
|
278
278
|
|
|
279
279
|
// Verify initial state without async cookie probe
|
|
280
|
-
const {authState: initialAuthState} = authStore.getInitialState(instance)
|
|
280
|
+
const {authState: initialAuthState} = authStore.getInitialState(instance, null)
|
|
281
281
|
expect(initialAuthState.type).toBe(AuthStateType.LOGGED_OUT)
|
|
282
282
|
expect(getStudioTokenFromLocalStorage).toHaveBeenCalledWith(mockStorage, studioStorageKey)
|
|
283
283
|
|
|
@@ -296,7 +296,7 @@ describe('authStore', () => {
|
|
|
296
296
|
dataset: 'd',
|
|
297
297
|
})
|
|
298
298
|
|
|
299
|
-
const {authState, options} = authStore.getInitialState(instance)
|
|
299
|
+
const {authState, options} = authStore.getInitialState(instance, null)
|
|
300
300
|
expect(getStudioTokenFromLocalStorage).not.toHaveBeenCalled()
|
|
301
301
|
expect(checkForCookieAuth).not.toHaveBeenCalled()
|
|
302
302
|
expect(getTokenFromStorage).toHaveBeenCalled()
|
|
@@ -314,7 +314,7 @@ describe('authStore', () => {
|
|
|
314
314
|
vi.mocked(getAuthCode).mockReturnValue(null)
|
|
315
315
|
vi.mocked(getTokenFromLocation).mockReturnValue('hash-token')
|
|
316
316
|
|
|
317
|
-
const {authState} = authStore.getInitialState(instance)
|
|
317
|
+
const {authState} = authStore.getInitialState(instance, null)
|
|
318
318
|
expect(authState).toMatchObject({
|
|
319
319
|
type: AuthStateType.LOGGING_IN,
|
|
320
320
|
isExchangingToken: false,
|
|
@@ -122,7 +122,7 @@ describe('refreshStampedToken', () => {
|
|
|
122
122
|
dataset: 'd',
|
|
123
123
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
124
124
|
})
|
|
125
|
-
const initialState = authStore.getInitialState(instance)
|
|
125
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
126
126
|
initialState.authState = {
|
|
127
127
|
type: AuthStateType.LOGGED_IN,
|
|
128
128
|
token: 'sk-initial-token-st123',
|
|
@@ -131,7 +131,7 @@ describe('refreshStampedToken', () => {
|
|
|
131
131
|
initialState.dashboardContext = {mode: 'test'}
|
|
132
132
|
const state = createStoreState(initialState)
|
|
133
133
|
|
|
134
|
-
const subscription = refreshStampedToken({state, instance})
|
|
134
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
135
135
|
subscriptions.push(subscription)
|
|
136
136
|
|
|
137
137
|
await vi.advanceTimersToNextTimerAsync()
|
|
@@ -168,7 +168,7 @@ describe('refreshStampedToken', () => {
|
|
|
168
168
|
dataset: 'd',
|
|
169
169
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
170
170
|
})
|
|
171
|
-
const initialState = authStore.getInitialState(instance)
|
|
171
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
172
172
|
initialState.authState = {
|
|
173
173
|
type: AuthStateType.LOGGED_IN,
|
|
174
174
|
token: 'sk-initial-token-st123',
|
|
@@ -177,7 +177,7 @@ describe('refreshStampedToken', () => {
|
|
|
177
177
|
initialState.dashboardContext = {mode: 'test'}
|
|
178
178
|
const state = createStoreState(initialState)
|
|
179
179
|
|
|
180
|
-
const subscription = refreshStampedToken({state, instance})
|
|
180
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
181
181
|
subscriptions.push(subscription)
|
|
182
182
|
|
|
183
183
|
await vi.advanceTimersToNextTimerAsync()
|
|
@@ -202,7 +202,7 @@ describe('refreshStampedToken', () => {
|
|
|
202
202
|
dataset: 'd',
|
|
203
203
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
204
204
|
})
|
|
205
|
-
const initialState = authStore.getInitialState(instance)
|
|
205
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
206
206
|
initialState.authState = {
|
|
207
207
|
type: AuthStateType.LOGGED_IN,
|
|
208
208
|
token: 'sk-initial-token-st123',
|
|
@@ -213,7 +213,7 @@ describe('refreshStampedToken', () => {
|
|
|
213
213
|
let subscription: Subscription | undefined
|
|
214
214
|
// We expect this NOT to throw, but accept we can't easily test the lock call or outcome
|
|
215
215
|
expect(() => {
|
|
216
|
-
subscription = refreshStampedToken({state, instance})
|
|
216
|
+
subscription = refreshStampedToken({state, instance, key: null})
|
|
217
217
|
subscriptions.push(subscription!)
|
|
218
218
|
}).not.toThrow()
|
|
219
219
|
|
|
@@ -253,7 +253,7 @@ describe('refreshStampedToken', () => {
|
|
|
253
253
|
dataset: 'd',
|
|
254
254
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
255
255
|
})
|
|
256
|
-
const initialState = authStore.getInitialState(instance)
|
|
256
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
257
257
|
initialState.authState = {
|
|
258
258
|
type: AuthStateType.LOGGED_IN,
|
|
259
259
|
token: 'sk-initial-token-st123',
|
|
@@ -261,7 +261,7 @@ describe('refreshStampedToken', () => {
|
|
|
261
261
|
}
|
|
262
262
|
const state = createStoreState(initialState)
|
|
263
263
|
|
|
264
|
-
const subscription = refreshStampedToken({state, instance})
|
|
264
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
265
265
|
subscriptions.push(subscription)
|
|
266
266
|
|
|
267
267
|
// DO NOT advance timers or yield here - focus on immediate observable logic
|
|
@@ -303,7 +303,7 @@ describe('refreshStampedToken', () => {
|
|
|
303
303
|
dataset: 'd',
|
|
304
304
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
305
305
|
})
|
|
306
|
-
const initialState = authStore.getInitialState(instance)
|
|
306
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
307
307
|
initialState.authState = {
|
|
308
308
|
type: AuthStateType.LOGGED_IN,
|
|
309
309
|
token: 'sk-initial-token-st123',
|
|
@@ -311,7 +311,7 @@ describe('refreshStampedToken', () => {
|
|
|
311
311
|
}
|
|
312
312
|
const state = createStoreState(initialState)
|
|
313
313
|
|
|
314
|
-
const subscription = refreshStampedToken({state, instance})
|
|
314
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
315
315
|
subscriptions.push(subscription)
|
|
316
316
|
|
|
317
317
|
// Advance timers to allow the async `performRefresh` to execute
|
|
@@ -349,7 +349,7 @@ describe('refreshStampedToken', () => {
|
|
|
349
349
|
dataset: 'd',
|
|
350
350
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
351
351
|
})
|
|
352
|
-
const initialState = authStore.getInitialState(instance)
|
|
352
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
353
353
|
initialState.authState = {
|
|
354
354
|
type: AuthStateType.LOGGED_IN,
|
|
355
355
|
token: 'sk-initial-token-st123',
|
|
@@ -358,7 +358,7 @@ describe('refreshStampedToken', () => {
|
|
|
358
358
|
initialState.dashboardContext = {mode: 'test'}
|
|
359
359
|
const state = createStoreState(initialState)
|
|
360
360
|
|
|
361
|
-
const subscription = refreshStampedToken({state, instance})
|
|
361
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
362
362
|
subscriptions.push(subscription)
|
|
363
363
|
|
|
364
364
|
await vi.advanceTimersToNextTimerAsync()
|
|
@@ -378,14 +378,14 @@ describe('refreshStampedToken', () => {
|
|
|
378
378
|
dataset: 'd',
|
|
379
379
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
380
380
|
})
|
|
381
|
-
const initialState = authStore.getInitialState(instance)
|
|
381
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
382
382
|
initialState.authState = {
|
|
383
383
|
type: AuthStateType.LOGGED_OUT,
|
|
384
384
|
isDestroyingSession: false,
|
|
385
385
|
} as AuthState
|
|
386
386
|
const state = createStoreState(initialState)
|
|
387
387
|
|
|
388
|
-
const subscription = refreshStampedToken({state, instance})
|
|
388
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
389
389
|
subscriptions.push(subscription)
|
|
390
390
|
|
|
391
391
|
await vi.advanceTimersByTimeAsync(0)
|
|
@@ -404,7 +404,7 @@ describe('refreshStampedToken', () => {
|
|
|
404
404
|
dataset: 'd',
|
|
405
405
|
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
|
|
406
406
|
})
|
|
407
|
-
const initialState = authStore.getInitialState(instance)
|
|
407
|
+
const initialState = authStore.getInitialState(instance, null)
|
|
408
408
|
initialState.authState = {
|
|
409
409
|
type: AuthStateType.LOGGED_IN,
|
|
410
410
|
token: 'sk-nonstamped-token',
|
|
@@ -412,7 +412,7 @@ describe('refreshStampedToken', () => {
|
|
|
412
412
|
}
|
|
413
413
|
const state = createStoreState(initialState)
|
|
414
414
|
|
|
415
|
-
const subscription = refreshStampedToken({state, instance})
|
|
415
|
+
const subscription = refreshStampedToken({state, instance, key: null})
|
|
416
416
|
subscriptions.push(subscription)
|
|
417
417
|
|
|
418
418
|
await vi.advanceTimersByTimeAsync(0)
|
|
@@ -20,8 +20,8 @@ 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 = createStoreState(authStore.getInitialState(instance))
|
|
24
|
-
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
23
|
+
const state = createStoreState(authStore.getInitialState(instance, null))
|
|
24
|
+
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})
|
|
25
25
|
|
|
26
26
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
27
27
|
|
|
@@ -52,8 +52,8 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
|
|
|
52
52
|
const clientFactory = vi.fn().mockReturnValue(mockClient)
|
|
53
53
|
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
|
|
54
54
|
|
|
55
|
-
const state = createStoreState(authStore.getInitialState(instance))
|
|
56
|
-
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
55
|
+
const state = createStoreState(authStore.getInitialState(instance, null))
|
|
56
|
+
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})
|
|
57
57
|
|
|
58
58
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
59
59
|
|
|
@@ -88,8 +88,8 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
|
|
|
88
88
|
const clientFactory = vi.fn().mockReturnValue(mockClient)
|
|
89
89
|
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})
|
|
90
90
|
|
|
91
|
-
const state = createStoreState(authStore.getInitialState(instance))
|
|
92
|
-
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
|
|
91
|
+
const state = createStoreState(authStore.getInitialState(instance, null))
|
|
92
|
+
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})
|
|
93
93
|
|
|
94
94
|
expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})
|
|
95
95
|
|
|
@@ -34,9 +34,9 @@ describe('subscribeToStorageEventsAndSetToken', () => {
|
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
it('sets the state to logged in when a matching storage event returns a token', () => {
|
|
37
|
-
const state = createStoreState(authStore.getInitialState(instance))
|
|
37
|
+
const state = createStoreState(authStore.getInitialState(instance, null))
|
|
38
38
|
const {storageKey} = state.get().options
|
|
39
|
-
const subscription = subscribeToStorageEventsAndSetToken({state, instance})
|
|
39
|
+
const subscription = subscribeToStorageEventsAndSetToken({state, instance, key: null})
|
|
40
40
|
|
|
41
41
|
expect(state.get()).toMatchObject({
|
|
42
42
|
authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
|
|
@@ -54,10 +54,10 @@ describe('subscribeToStorageEventsAndSetToken', () => {
|
|
|
54
54
|
|
|
55
55
|
it('sets the state to logged in when a matching storage event returns null', () => {
|
|
56
56
|
vi.mocked(getTokenFromStorage).mockReturnValue('existing-token')
|
|
57
|
-
const state = createStoreState(authStore.getInitialState(instance))
|
|
57
|
+
const state = createStoreState(authStore.getInitialState(instance, null))
|
|
58
58
|
const {storageKey} = state.get().options
|
|
59
59
|
|
|
60
|
-
const subscription = subscribeToStorageEventsAndSetToken({state, instance})
|
|
60
|
+
const subscription = subscribeToStorageEventsAndSetToken({state, instance, key: null})
|
|
61
61
|
|
|
62
62
|
expect(state.get()).toMatchObject({
|
|
63
63
|
authState: {type: AuthStateType.LOGGED_IN, token: 'existing-token', currentUser: null},
|