@sanity/sdk 2.4.0 → 2.6.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 +346 -110
- package/dist/index.js +428 -136
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
- package/src/_exports/index.ts +15 -3
- 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/client/clientStore.test.ts +45 -43
- package/src/client/clientStore.ts +23 -9
- 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/loggingConfig.ts +149 -0
- package/src/config/sanityConfig.ts +47 -23
- package/src/document/actions.ts +11 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +128 -115
- 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 +248 -0
- package/src/document/processActions.ts +173 -0
- 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 +3 -1
- 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 +54 -28
- package/src/store/createSanityInstance.test.ts +85 -1
- package/src/store/createSanityInstance.ts +53 -4
- 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/src/utils/logger-usage-example.md +141 -0
- package/src/utils/logger.test.ts +757 -0
- package/src/utils/logger.ts +537 -0
package/src/store/defineStore.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {type StoreState} from './createStoreState'
|
|
|
4
4
|
/**
|
|
5
5
|
* Context object provided to store initialization functions
|
|
6
6
|
*/
|
|
7
|
-
export interface StoreContext<TState> {
|
|
7
|
+
export interface StoreContext<TState, TKey = unknown> {
|
|
8
8
|
/**
|
|
9
9
|
* Sanity instance associated with this store
|
|
10
10
|
*
|
|
@@ -20,6 +20,11 @@ export interface StoreContext<TState> {
|
|
|
20
20
|
* Contains methods for getting/setting state and observing changes
|
|
21
21
|
*/
|
|
22
22
|
state: StoreState<TState>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The key used to instantiate the store.
|
|
26
|
+
*/
|
|
27
|
+
key: TKey
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
/**
|
|
@@ -29,7 +34,7 @@ export interface StoreContext<TState> {
|
|
|
29
34
|
* Stores are isolated state containers that can be associated with Sanity instances.
|
|
30
35
|
* Each store definition creates a separate state instance per composite key.
|
|
31
36
|
*/
|
|
32
|
-
export interface StoreDefinition<TState> {
|
|
37
|
+
export interface StoreDefinition<TState, TKey = unknown> {
|
|
33
38
|
/**
|
|
34
39
|
* Unique name for the store
|
|
35
40
|
*
|
|
@@ -47,7 +52,7 @@ export interface StoreDefinition<TState> {
|
|
|
47
52
|
* Called when a new store instance is created. Can use Sanity instance
|
|
48
53
|
* configuration to determine initial state.
|
|
49
54
|
*/
|
|
50
|
-
getInitialState: (instance: SanityInstance) => TState
|
|
55
|
+
getInitialState: (instance: SanityInstance, key: TKey) => TState
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
58
|
* Optional initialization function
|
|
@@ -65,7 +70,7 @@ export interface StoreDefinition<TState> {
|
|
|
65
70
|
* - Cancel pending operations
|
|
66
71
|
* - Dispose external connections
|
|
67
72
|
*/
|
|
68
|
-
initialize?: (context: StoreContext<TState>) => (() => void) | undefined
|
|
73
|
+
initialize?: (context: StoreContext<TState, TKey>) => (() => void) | undefined
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
/**
|
|
@@ -74,8 +79,8 @@ export interface StoreDefinition<TState> {
|
|
|
74
79
|
* @param storeDefinition - Configuration object defining the store
|
|
75
80
|
* @returns The finalized store definition
|
|
76
81
|
*/
|
|
77
|
-
export function defineStore<TState>(
|
|
78
|
-
storeDefinition: StoreDefinition<TState>,
|
|
79
|
-
): StoreDefinition<TState> {
|
|
82
|
+
export function defineStore<TState, TKey = unknown>(
|
|
83
|
+
storeDefinition: StoreDefinition<TState, TKey>,
|
|
84
|
+
): StoreDefinition<TState, TKey> {
|
|
80
85
|
return storeDefinition
|
|
81
86
|
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Logger Usage Examples
|
|
2
|
+
|
|
3
|
+
This document shows how to use the logging infrastructure in different scenarios.
|
|
4
|
+
|
|
5
|
+
## For SDK Users
|
|
6
|
+
|
|
7
|
+
### Basic Setup
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import {configureLogging, createSanityInstance} from '@sanity/sdk'
|
|
11
|
+
|
|
12
|
+
// Enable logging in development
|
|
13
|
+
if (process.env.NODE_ENV === 'development') {
|
|
14
|
+
configureLogging({
|
|
15
|
+
level: 'info',
|
|
16
|
+
namespaces: ['auth', 'document', 'query'],
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const instance = createSanityInstance({
|
|
21
|
+
projectId: 'my-project',
|
|
22
|
+
dataset: 'production',
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Expected Output
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
[2024-12-01T22:15:29.000Z] [INFO] [sdk] Logging configured
|
|
30
|
+
level: "info"
|
|
31
|
+
namespaces: ["auth", "document", "query"]
|
|
32
|
+
[2024-12-01T22:15:30.123Z] [INFO] [auth] Checking URL for auth code
|
|
33
|
+
[2024-12-01T22:15:30.456Z] [INFO] [auth] [project:my-project] [dataset:production] User logged in
|
|
34
|
+
userId: "user-123"
|
|
35
|
+
[2024-12-01T22:15:31.789Z] [INFO] [document] [project:my-project] [dataset:production] Document synced
|
|
36
|
+
documentId: "draft.post-1"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## For SDK Maintainers
|
|
40
|
+
|
|
41
|
+
### Using Logger in Stores
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// In authStore.ts
|
|
45
|
+
import {createLogger, getInstanceContext} from '../utils/logger'
|
|
46
|
+
import {defineStore} from '../store/defineStore'
|
|
47
|
+
|
|
48
|
+
export const authStore = defineStore<AuthStoreState>({
|
|
49
|
+
name: 'Auth',
|
|
50
|
+
|
|
51
|
+
initialize(context) {
|
|
52
|
+
const {instance} = context
|
|
53
|
+
|
|
54
|
+
// Create logger with instance context
|
|
55
|
+
const logger = createLogger('auth', {
|
|
56
|
+
instanceContext: getInstanceContext(instance),
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// Logs will automatically include [project:x] [dataset:y]
|
|
60
|
+
logger.info('Auth store initialized')
|
|
61
|
+
|
|
62
|
+
// With additional context
|
|
63
|
+
logger.debug('Fetching current user', {
|
|
64
|
+
method: 'cookie',
|
|
65
|
+
projectId: instance.config.projectId,
|
|
66
|
+
})
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Using Logger in Static Utils
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// In utils/ids.ts (no instance context)
|
|
75
|
+
import {createLogger} from './logger'
|
|
76
|
+
|
|
77
|
+
const logger = createLogger('sdk')
|
|
78
|
+
|
|
79
|
+
export function getDraftId(id: string): string {
|
|
80
|
+
logger.trace('Converting to draft ID', {inputId: id})
|
|
81
|
+
return `drafts.${id}`
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Performance Timing
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import {createTimer} from '../utils/logger'
|
|
89
|
+
|
|
90
|
+
async function fetchDocument(id: string) {
|
|
91
|
+
const timer = createTimer('document', 'fetchDocument')
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const doc = await client.fetch(query, {id})
|
|
95
|
+
timer.end('Document fetched successfully', {documentId: id})
|
|
96
|
+
return doc
|
|
97
|
+
} catch (error) {
|
|
98
|
+
timer.end('Document fetch failed', {documentId: id, error})
|
|
99
|
+
throw error
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Multi-Instance Debugging
|
|
105
|
+
|
|
106
|
+
When working with multiple instances, logs automatically show which instance they came from:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const prodInstance = createSanityInstance({
|
|
110
|
+
projectId: 'my-project',
|
|
111
|
+
dataset: 'production',
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
const stagingInstance = createSanityInstance({
|
|
115
|
+
projectId: 'my-project',
|
|
116
|
+
dataset: 'staging',
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// Logs will clearly show which instance:
|
|
120
|
+
// [INFO] [query] [project:my-project] [dataset:production] Query executed
|
|
121
|
+
// [INFO] [query] [project:my-project] [dataset:staging] Query executed
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Environment-Based Configuration
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// Configure based on environment
|
|
128
|
+
const logLevel = process.env.SANITY_LOG_LEVEL || 'warn'
|
|
129
|
+
const logNamespaces = process.env.SANITY_LOG_NAMESPACES?.split(',') || []
|
|
130
|
+
|
|
131
|
+
configureLogging({
|
|
132
|
+
level: logLevel as LogLevel,
|
|
133
|
+
namespaces: logNamespaces,
|
|
134
|
+
})
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Then run your app with:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
SANITY_LOG_LEVEL=debug SANITY_LOG_NAMESPACES=auth,document npm start
|
|
141
|
+
```
|