@sanity/client 6.28.3-instruct.0 → 6.28.3-resources.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/_chunks-cjs/config.cjs +25 -16
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +25 -16
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +69 -74
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +7 -240
- package/dist/index.browser.d.ts +7 -240
- package/dist/index.browser.js +69 -74
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +47 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -229
- package/dist/index.d.ts +7 -229
- package/dist/index.js +48 -60
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +7 -234
- package/dist/stega.browser.d.ts +7 -234
- package/dist/stega.d.cts +7 -234
- package/dist/stega.d.ts +7 -234
- package/package.json +20 -20
- package/src/SanityClient.ts +1 -5
- package/src/assets/AssetsClient.ts +15 -3
- package/src/config.ts +15 -20
- package/src/data/dataMethods.ts +45 -5
- package/src/data/listen.ts +1 -1
- package/src/data/live.ts +2 -0
- package/src/datasets/DatasetsClient.ts +5 -0
- package/src/http/requestOptions.ts +1 -1
- package/src/projects/ProjectsClient.ts +5 -0
- package/src/types.ts +8 -10
- package/src/validators.ts +23 -0
- package/src/warnings.ts +5 -0
- package/umd/sanityClient.js +76 -79
- package/umd/sanityClient.min.js +2 -2
- package/src/assist/AssistClient.ts +0 -87
- package/src/assist/types.ts +0 -203
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "6.28.3-
|
|
3
|
+
"version": "6.28.3-resources.0",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -125,39 +125,39 @@
|
|
|
125
125
|
"devDependencies": {
|
|
126
126
|
"@edge-runtime/types": "^4.0.0",
|
|
127
127
|
"@edge-runtime/vm": "^5.0.0",
|
|
128
|
-
"@eslint/eslintrc": "^3.
|
|
129
|
-
"@eslint/js": "^9.
|
|
130
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
131
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
128
|
+
"@eslint/eslintrc": "^3.3.0",
|
|
129
|
+
"@eslint/js": "^9.22.0",
|
|
130
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
131
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
132
132
|
"@sanity/client-latest": "npm:@sanity/client@latest",
|
|
133
|
-
"@sanity/pkg-utils": "^7.
|
|
133
|
+
"@sanity/pkg-utils": "^7.1.1",
|
|
134
134
|
"@types/json-diff": "^1.0.3",
|
|
135
135
|
"@types/node": "^22.9.0",
|
|
136
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
137
|
-
"@typescript-eslint/parser": "^8.
|
|
136
|
+
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
137
|
+
"@typescript-eslint/parser": "^8.26.1",
|
|
138
138
|
"@vercel/stega": "0.1.2",
|
|
139
|
-
"@vitest/coverage-v8": "3.0.
|
|
140
|
-
"eslint": "^9.
|
|
141
|
-
"eslint-config-prettier": "^10.
|
|
139
|
+
"@vitest/coverage-v8": "3.0.9",
|
|
140
|
+
"eslint": "^9.22.0",
|
|
141
|
+
"eslint-config-prettier": "^10.1.1",
|
|
142
142
|
"eslint-formatter-compact": "^8.40.0",
|
|
143
143
|
"eslint-plugin-prettier": "^5.2.3",
|
|
144
144
|
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
145
145
|
"faucet": "^0.0.4",
|
|
146
|
-
"globals": "^15.
|
|
146
|
+
"globals": "^15.15.0",
|
|
147
147
|
"happy-dom": "^12.10.3",
|
|
148
148
|
"json-diff": "^1.0.6",
|
|
149
149
|
"ls-engines": "^0.9.3",
|
|
150
|
-
"msw": "^2.7.
|
|
151
|
-
"next": "^15.
|
|
150
|
+
"msw": "^2.7.3",
|
|
151
|
+
"next": "^15.2.3",
|
|
152
152
|
"nock": "^13.5.6",
|
|
153
|
-
"prettier": "^3.5.
|
|
154
|
-
"prettier-plugin-packagejson": "^2.5.
|
|
153
|
+
"prettier": "^3.5.3",
|
|
154
|
+
"prettier-plugin-packagejson": "^2.5.10",
|
|
155
155
|
"rimraf": "^5.0.7",
|
|
156
|
-
"rollup": "^4.
|
|
156
|
+
"rollup": "^4.36.0",
|
|
157
157
|
"sse-channel": "^4.0.0",
|
|
158
|
-
"terser": "^5.
|
|
159
|
-
"typescript": "5.
|
|
160
|
-
"vitest": "3.0.
|
|
158
|
+
"terser": "^5.39.0",
|
|
159
|
+
"typescript": "5.8.2",
|
|
160
|
+
"vitest": "3.0.9"
|
|
161
161
|
},
|
|
162
162
|
"packageManager": "npm@11.0.0",
|
|
163
163
|
"engines": {
|
package/src/SanityClient.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {lastValueFrom, Observable} from 'rxjs'
|
|
2
2
|
|
|
3
3
|
import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
|
|
4
|
-
import {AssistClient, ObservableAssistClient} from './assist/AssistClient'
|
|
5
4
|
import {defaultConfig, initConfig} from './config'
|
|
6
5
|
import * as dataMethods from './data/dataMethods'
|
|
7
6
|
import {_listen} from './data/listen'
|
|
@@ -66,7 +65,7 @@ export class ObservableSanityClient {
|
|
|
66
65
|
live: LiveClient
|
|
67
66
|
projects: ObservableProjectsClient
|
|
68
67
|
users: ObservableUsersClient
|
|
69
|
-
|
|
68
|
+
|
|
70
69
|
/**
|
|
71
70
|
* Private properties
|
|
72
71
|
*/
|
|
@@ -88,7 +87,6 @@ export class ObservableSanityClient {
|
|
|
88
87
|
this.live = new LiveClient(this)
|
|
89
88
|
this.projects = new ObservableProjectsClient(this, this.#httpRequest)
|
|
90
89
|
this.users = new ObservableUsersClient(this, this.#httpRequest)
|
|
91
|
-
this.assist = new ObservableAssistClient(this, this.#httpRequest)
|
|
92
90
|
}
|
|
93
91
|
|
|
94
92
|
/**
|
|
@@ -735,7 +733,6 @@ export class SanityClient {
|
|
|
735
733
|
live: LiveClient
|
|
736
734
|
projects: ProjectsClient
|
|
737
735
|
users: UsersClient
|
|
738
|
-
assist: AssistClient
|
|
739
736
|
|
|
740
737
|
/**
|
|
741
738
|
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
@@ -763,7 +760,6 @@ export class SanityClient {
|
|
|
763
760
|
this.live = new LiveClient(this)
|
|
764
761
|
this.projects = new ProjectsClient(this, this.#httpRequest)
|
|
765
762
|
this.users = new UsersClient(this, this.#httpRequest)
|
|
766
|
-
this.assist = new AssistClient(this, this.#httpRequest)
|
|
767
763
|
|
|
768
764
|
this.observable = new ObservableSanityClient(httpRequest, config)
|
|
769
765
|
}
|
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
Any,
|
|
8
8
|
HttpRequest,
|
|
9
9
|
HttpRequestEvent,
|
|
10
|
+
InitializedClientConfig,
|
|
10
11
|
ResponseEvent,
|
|
11
12
|
SanityAssetDocument,
|
|
12
13
|
SanityImageAssetDocument,
|
|
@@ -149,8 +150,7 @@ function _upload(
|
|
|
149
150
|
meta = ['none']
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
const
|
|
153
|
-
const assetEndpoint = assetType === 'image' ? 'images' : 'files'
|
|
153
|
+
const config = client.config()
|
|
154
154
|
const options = optionsFromFile(opts, body)
|
|
155
155
|
const {tag, label, title, description, creditLine, filename, source} = options
|
|
156
156
|
const query: Any = {
|
|
@@ -166,17 +166,29 @@ function _upload(
|
|
|
166
166
|
query.sourceName = source.name
|
|
167
167
|
query.sourceUrl = source.url
|
|
168
168
|
}
|
|
169
|
+
|
|
169
170
|
return _requestObservable(client, httpRequest, {
|
|
170
171
|
tag,
|
|
171
172
|
method: 'POST',
|
|
172
173
|
timeout: options.timeout || 0,
|
|
173
|
-
uri:
|
|
174
|
+
uri: buildAssetUploadUrl(config, assetType),
|
|
174
175
|
headers: options.contentType ? {'Content-Type': options.contentType} : {},
|
|
175
176
|
query,
|
|
176
177
|
body,
|
|
177
178
|
})
|
|
178
179
|
}
|
|
179
180
|
|
|
181
|
+
function buildAssetUploadUrl(config: InitializedClientConfig, assetType: 'image' | 'file'): string {
|
|
182
|
+
const assetTypeEndpoint = assetType === 'image' ? 'images' : 'files'
|
|
183
|
+
|
|
184
|
+
if (config['~experimental_resource']) {
|
|
185
|
+
return `${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/assets/${assetTypeEndpoint}`
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const dataset = validators.hasDataset(config)
|
|
189
|
+
return `assets/${assetTypeEndpoint}/${dataset}`
|
|
190
|
+
}
|
|
191
|
+
|
|
180
192
|
function optionsFromFile(opts: Record<string, Any>, file: Any) {
|
|
181
193
|
if (typeof File === 'undefined' || !(file instanceof File)) {
|
|
182
194
|
return opts
|
package/src/config.ts
CHANGED
|
@@ -28,30 +28,15 @@ function validateApiVersion(apiVersion: string) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i
|
|
32
|
-
|
|
33
31
|
/**
|
|
34
32
|
* @internal - it may have breaking changes in any release
|
|
35
33
|
*/
|
|
36
34
|
export function validateApiPerspective(
|
|
37
35
|
perspective: unknown,
|
|
38
36
|
): asserts perspective is ClientPerspective {
|
|
39
|
-
if (Array.isArray(perspective)) {
|
|
40
|
-
if (perspective.includes('raw')) {
|
|
41
|
-
throw new TypeError(
|
|
42
|
-
`Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
|
|
48
|
-
(perspectiveName) =>
|
|
49
|
-
typeof perspectiveName !== 'string' || !VALID_PERSPECTIVE.test(perspectiveName),
|
|
50
|
-
)
|
|
51
|
-
if (invalid.length > 0) {
|
|
52
|
-
const formatted = invalid.map((v) => JSON.stringify(v))
|
|
37
|
+
if (Array.isArray(perspective) && perspective.length > 1 && perspective.includes('raw')) {
|
|
53
38
|
throw new TypeError(
|
|
54
|
-
`Invalid API perspective value
|
|
39
|
+
`Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
|
|
55
40
|
)
|
|
56
41
|
}
|
|
57
42
|
}
|
|
@@ -78,7 +63,7 @@ export const initConfig = (
|
|
|
78
63
|
...defaultConfig,
|
|
79
64
|
...specifiedConfig,
|
|
80
65
|
} as InitializedClientConfig
|
|
81
|
-
const projectBased = newConfig.useProjectHostname
|
|
66
|
+
const projectBased = newConfig.useProjectHostname && !newConfig['~experimental_resource']
|
|
82
67
|
|
|
83
68
|
if (typeof Promise === 'undefined') {
|
|
84
69
|
const helpUrl = generateHelpUrl('js-client-promise-polyfill')
|
|
@@ -89,6 +74,10 @@ export const initConfig = (
|
|
|
89
74
|
throw new Error('Configuration must contain `projectId`')
|
|
90
75
|
}
|
|
91
76
|
|
|
77
|
+
if (newConfig['~experimental_resource']) {
|
|
78
|
+
validate.resourceBase(newConfig)
|
|
79
|
+
}
|
|
80
|
+
|
|
92
81
|
if (typeof newConfig.perspective !== 'undefined') {
|
|
93
82
|
validateApiPerspective(newConfig.perspective)
|
|
94
83
|
}
|
|
@@ -122,7 +111,13 @@ export const initConfig = (
|
|
|
122
111
|
const isBrowser = typeof window !== 'undefined' && window.location && window.location.hostname
|
|
123
112
|
const isLocalhost = isBrowser && isLocal(window.location.hostname)
|
|
124
113
|
|
|
125
|
-
|
|
114
|
+
const hasToken = Boolean(newConfig.token)
|
|
115
|
+
if (newConfig.withCredentials && hasToken) {
|
|
116
|
+
warnings.printCredentialedTokenWarning()
|
|
117
|
+
newConfig.withCredentials = false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true) {
|
|
126
121
|
warnings.printBrowserTokenWarning()
|
|
127
122
|
} else if (typeof newConfig.useCdn === 'undefined') {
|
|
128
123
|
warnings.printCdnWarning()
|
|
@@ -160,7 +155,7 @@ export const initConfig = (
|
|
|
160
155
|
const host = hostParts[1]
|
|
161
156
|
const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host
|
|
162
157
|
|
|
163
|
-
if (
|
|
158
|
+
if (projectBased) {
|
|
164
159
|
newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`
|
|
165
160
|
newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`
|
|
166
161
|
} else {
|
package/src/data/dataMethods.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
Any,
|
|
13
13
|
BaseActionOptions,
|
|
14
14
|
BaseMutationOptions,
|
|
15
|
+
ClientConfig,
|
|
15
16
|
FirstDocumentIdMutationOptions,
|
|
16
17
|
FirstDocumentMutationOptions,
|
|
17
18
|
HttpRequest,
|
|
@@ -366,6 +367,43 @@ export function _create<R extends Record<string, Any>>(
|
|
|
366
367
|
const opts = Object.assign({returnFirst: true, returnDocuments: true}, options)
|
|
367
368
|
return _dataRequest(client, httpRequest, 'mutate', {mutations: [mutation]}, opts)
|
|
368
369
|
}
|
|
370
|
+
const isQuery = (config: ClientConfig, uri: string) =>
|
|
371
|
+
uri.startsWith('/data/query/') ||
|
|
372
|
+
(config['~experimental_resource'] &&
|
|
373
|
+
uri.startsWith(
|
|
374
|
+
`/${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/query`,
|
|
375
|
+
))
|
|
376
|
+
const isMutate = (config: ClientConfig, uri: string) =>
|
|
377
|
+
uri.startsWith('/data/mutate/') ||
|
|
378
|
+
(config['~experimental_resource'] &&
|
|
379
|
+
uri.startsWith(
|
|
380
|
+
`/${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/mutate`,
|
|
381
|
+
))
|
|
382
|
+
const isDoc = (config: ClientConfig, uri: string) =>
|
|
383
|
+
uri.startsWith('/data/doc/') ||
|
|
384
|
+
(config['~experimental_resource'] &&
|
|
385
|
+
uri.startsWith(
|
|
386
|
+
`/${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/doc/`,
|
|
387
|
+
))
|
|
388
|
+
const isListener = (config: ClientConfig, uri: string) =>
|
|
389
|
+
uri.startsWith('/data/listen/') ||
|
|
390
|
+
(config['~experimental_resource'] &&
|
|
391
|
+
uri.startsWith(
|
|
392
|
+
`/${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/listen`,
|
|
393
|
+
))
|
|
394
|
+
const isHistory = (config: ClientConfig, uri: string) =>
|
|
395
|
+
uri.startsWith('/data/history/') ||
|
|
396
|
+
(config['~experimental_resource'] &&
|
|
397
|
+
uri.startsWith(
|
|
398
|
+
`/${config['~experimental_resource'].type}/${config['~experimental_resource'].id}/history/`,
|
|
399
|
+
))
|
|
400
|
+
const isData = (config: ClientConfig, uri: string) =>
|
|
401
|
+
uri.startsWith('/data/') ||
|
|
402
|
+
isQuery(config, uri) ||
|
|
403
|
+
isMutate(config, uri) ||
|
|
404
|
+
isDoc(config, uri) ||
|
|
405
|
+
isListener(config, uri) ||
|
|
406
|
+
isHistory(config, uri)
|
|
369
407
|
|
|
370
408
|
/**
|
|
371
409
|
* @internal
|
|
@@ -382,7 +420,7 @@ export function _requestObservable<R>(
|
|
|
382
420
|
// Only the /data endpoint is currently available through API-CDN.
|
|
383
421
|
const canUseCdn =
|
|
384
422
|
typeof options.canUseCdn === 'undefined'
|
|
385
|
-
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && uri
|
|
423
|
+
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && isData(config, uri)
|
|
386
424
|
: options.canUseCdn
|
|
387
425
|
|
|
388
426
|
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn
|
|
@@ -397,10 +435,7 @@ export function _requestObservable<R>(
|
|
|
397
435
|
}
|
|
398
436
|
|
|
399
437
|
// GROQ query-only parameters
|
|
400
|
-
if (
|
|
401
|
-
['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 &&
|
|
402
|
-
uri.indexOf('/data/query/') === 0
|
|
403
|
-
) {
|
|
438
|
+
if (['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 && isQuery(config, uri)) {
|
|
404
439
|
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap
|
|
405
440
|
if (resultSourceMap !== undefined && resultSourceMap !== false) {
|
|
406
441
|
options.query = {resultSourceMap, ...options.query}
|
|
@@ -482,6 +517,11 @@ export function _getDataUrl(
|
|
|
482
517
|
path?: string,
|
|
483
518
|
): string {
|
|
484
519
|
const config = client.config()
|
|
520
|
+
if (config['~experimental_resource']) {
|
|
521
|
+
const resourceBase = validators.resourceBase(config)
|
|
522
|
+
const uri = path ? `${operation}/${path}` : operation
|
|
523
|
+
return `${resourceBase}/${uri}`.replace(/\/($|\?)/, '$1')
|
|
524
|
+
}
|
|
485
525
|
const catalog = validators.hasDataset(config)
|
|
486
526
|
const baseUri = `/${operation}/${catalog}`
|
|
487
527
|
const uri = path ? `${baseUri}/${path}` : baseUri
|
package/src/data/listen.ts
CHANGED
|
@@ -84,7 +84,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
84
84
|
const listenFor = options.events ? options.events : ['mutation']
|
|
85
85
|
|
|
86
86
|
const esOptions: EventSourceInit & {headers?: Record<string, string>} = {}
|
|
87
|
-
if (
|
|
87
|
+
if (withCredentials) {
|
|
88
88
|
esOptions.withCredentials = true
|
|
89
89
|
}
|
|
90
90
|
|
package/src/data/live.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
SyncTag,
|
|
13
13
|
} from '../types'
|
|
14
14
|
import {shareReplayLatest} from '../util/shareReplayLatest'
|
|
15
|
+
import * as validate from '../validators'
|
|
15
16
|
import {_getDataUrl} from './dataMethods'
|
|
16
17
|
import {connectEventSource} from './eventsource'
|
|
17
18
|
import {eventSourcePolyfill} from './eventsourcePolyfill'
|
|
@@ -43,6 +44,7 @@ export class LiveClient {
|
|
|
43
44
|
*/
|
|
44
45
|
tag?: string
|
|
45
46
|
} = {}): Observable<LiveEvent> {
|
|
47
|
+
validate.resourceGuard('live', this.#client.config())
|
|
46
48
|
const {
|
|
47
49
|
projectId,
|
|
48
50
|
apiVersion: _apiVersion,
|
|
@@ -70,6 +70,7 @@ export class DatasetsClient {
|
|
|
70
70
|
* @param options - Options for the dataset
|
|
71
71
|
*/
|
|
72
72
|
create(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
|
|
73
|
+
validate.resourceGuard('dataset', this.#client.config())
|
|
73
74
|
return lastValueFrom(
|
|
74
75
|
_modify<DatasetResponse>(this.#client, this.#httpRequest, 'PUT', name, options),
|
|
75
76
|
)
|
|
@@ -82,6 +83,7 @@ export class DatasetsClient {
|
|
|
82
83
|
* @param options - New options for the dataset
|
|
83
84
|
*/
|
|
84
85
|
edit(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
|
|
86
|
+
validate.resourceGuard('dataset', this.#client.config())
|
|
85
87
|
return lastValueFrom(
|
|
86
88
|
_modify<DatasetResponse>(this.#client, this.#httpRequest, 'PATCH', name, options),
|
|
87
89
|
)
|
|
@@ -93,6 +95,7 @@ export class DatasetsClient {
|
|
|
93
95
|
* @param name - Name of the dataset to delete
|
|
94
96
|
*/
|
|
95
97
|
delete(name: string): Promise<{deleted: true}> {
|
|
98
|
+
validate.resourceGuard('dataset', this.#client.config())
|
|
96
99
|
return lastValueFrom(_modify<{deleted: true}>(this.#client, this.#httpRequest, 'DELETE', name))
|
|
97
100
|
}
|
|
98
101
|
|
|
@@ -100,6 +103,7 @@ export class DatasetsClient {
|
|
|
100
103
|
* Fetch a list of datasets for the configured project
|
|
101
104
|
*/
|
|
102
105
|
list(): Promise<DatasetsResponse> {
|
|
106
|
+
validate.resourceGuard('dataset', this.#client.config())
|
|
103
107
|
return lastValueFrom(
|
|
104
108
|
_request<DatasetsResponse>(this.#client, this.#httpRequest, {uri: '/datasets', tag: null}),
|
|
105
109
|
)
|
|
@@ -113,6 +117,7 @@ function _modify<R = unknown>(
|
|
|
113
117
|
name: string,
|
|
114
118
|
options?: {aclMode?: DatasetAclMode},
|
|
115
119
|
) {
|
|
120
|
+
validate.resourceGuard('dataset', client.config())
|
|
116
121
|
validate.dataset(name)
|
|
117
122
|
return _request<R>(client, httpRequest, {
|
|
118
123
|
method,
|
|
@@ -18,7 +18,7 @@ export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOp
|
|
|
18
18
|
|
|
19
19
|
const withCredentials = Boolean(
|
|
20
20
|
typeof overrides.withCredentials === 'undefined'
|
|
21
|
-
? config.
|
|
21
|
+
? config.withCredentials
|
|
22
22
|
: overrides.withCredentials,
|
|
23
23
|
)
|
|
24
24
|
|
|
@@ -3,6 +3,7 @@ import {lastValueFrom, type Observable} from 'rxjs'
|
|
|
3
3
|
import {_request} from '../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
5
|
import type {HttpRequest, SanityProject} from '../types'
|
|
6
|
+
import * as validate from '../validators'
|
|
6
7
|
|
|
7
8
|
/** @internal */
|
|
8
9
|
export class ObservableProjectsClient {
|
|
@@ -24,6 +25,7 @@ export class ObservableProjectsClient {
|
|
|
24
25
|
list(options?: {
|
|
25
26
|
includeMembers?: boolean
|
|
26
27
|
}): Observable<SanityProject[] | Omit<SanityProject, 'members'>[]> {
|
|
28
|
+
validate.resourceGuard('projects', this.#client.config())
|
|
27
29
|
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
|
|
28
30
|
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri})
|
|
29
31
|
}
|
|
@@ -34,6 +36,7 @@ export class ObservableProjectsClient {
|
|
|
34
36
|
* @param projectId - ID of the project to fetch
|
|
35
37
|
*/
|
|
36
38
|
getById(projectId: string): Observable<SanityProject> {
|
|
39
|
+
validate.resourceGuard('projects', this.#client.config())
|
|
37
40
|
return _request<SanityProject>(this.#client, this.#httpRequest, {uri: `/projects/${projectId}`})
|
|
38
41
|
}
|
|
39
42
|
}
|
|
@@ -56,6 +59,7 @@ export class ProjectsClient {
|
|
|
56
59
|
list(options?: {includeMembers?: true}): Promise<SanityProject[]>
|
|
57
60
|
list(options?: {includeMembers?: false}): Promise<Omit<SanityProject, 'members'>[]>
|
|
58
61
|
list(options?: {includeMembers?: boolean}): Promise<SanityProject[]> {
|
|
62
|
+
validate.resourceGuard('projects', this.#client.config())
|
|
59
63
|
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
|
|
60
64
|
return lastValueFrom(_request<SanityProject[]>(this.#client, this.#httpRequest, {uri}))
|
|
61
65
|
}
|
|
@@ -66,6 +70,7 @@ export class ProjectsClient {
|
|
|
66
70
|
* @param projectId - ID of the project to fetch
|
|
67
71
|
*/
|
|
68
72
|
getById(projectId: string): Promise<SanityProject> {
|
|
73
|
+
validate.resourceGuard('projects', this.#client.config())
|
|
69
74
|
return lastValueFrom(
|
|
70
75
|
_request<SanityProject>(this.#client, this.#httpRequest, {uri: `/projects/${projectId}`}),
|
|
71
76
|
)
|
package/src/types.ts
CHANGED
|
@@ -53,6 +53,11 @@ export type ClientPerspective =
|
|
|
53
53
|
| 'raw'
|
|
54
54
|
| StackablePerspective[]
|
|
55
55
|
|
|
56
|
+
type ClientConfigResource = {
|
|
57
|
+
type: string
|
|
58
|
+
id: string
|
|
59
|
+
}
|
|
60
|
+
|
|
56
61
|
/** @public */
|
|
57
62
|
export interface ClientConfig {
|
|
58
63
|
projectId?: string
|
|
@@ -61,6 +66,9 @@ export interface ClientConfig {
|
|
|
61
66
|
useCdn?: boolean
|
|
62
67
|
token?: string
|
|
63
68
|
|
|
69
|
+
/** @internal */
|
|
70
|
+
'~experimental_resource'?: ClientConfigResource
|
|
71
|
+
|
|
64
72
|
/**
|
|
65
73
|
* What perspective to use for the client. See {@link https://www.sanity.io/docs/perspectives|perspective documentation}
|
|
66
74
|
* @remarks
|
|
@@ -1336,16 +1344,6 @@ export type ClientReturn<
|
|
|
1336
1344
|
Fallback = Any,
|
|
1337
1345
|
> = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
|
|
1338
1346
|
|
|
1339
|
-
export type {
|
|
1340
|
-
AssistAsyncInstruction,
|
|
1341
|
-
AssistInstruction,
|
|
1342
|
-
AssistSyncInstruction,
|
|
1343
|
-
ConstantInstructionParam,
|
|
1344
|
-
FieldInstructionParam,
|
|
1345
|
-
GroqInstructionParam,
|
|
1346
|
-
InstructionParam,
|
|
1347
|
-
InstructionParams,
|
|
1348
|
-
} from './assist/types'
|
|
1349
1347
|
export type {
|
|
1350
1348
|
ContentSourceMapParsedPath,
|
|
1351
1349
|
ContentSourceMapParsedPathKeyedSegment,
|
package/src/validators.ts
CHANGED
|
@@ -76,3 +76,26 @@ export const requestTag = (tag: string) => {
|
|
|
76
76
|
|
|
77
77
|
return tag
|
|
78
78
|
}
|
|
79
|
+
|
|
80
|
+
export const resourceBase = (config: InitializedClientConfig): string => {
|
|
81
|
+
if (!config['~experimental_resource']) {
|
|
82
|
+
throw new Error('`resource` must be provided to perform queries')
|
|
83
|
+
}
|
|
84
|
+
const resourceConfig = config['~experimental_resource']
|
|
85
|
+
|
|
86
|
+
if (resourceConfig.type === 'dataset') {
|
|
87
|
+
const segments = resourceConfig.id.split('.')
|
|
88
|
+
if (segments.length !== 2) {
|
|
89
|
+
throw new Error('Dataset ID must be in the format "project.dataset"')
|
|
90
|
+
}
|
|
91
|
+
return `/projects/${segments[0]}/datasets/${segments[1]}`
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return `/${resourceConfig.type}/${resourceConfig.id}`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const resourceGuard = (service: string, config: InitializedClientConfig): void => {
|
|
98
|
+
if (config['~experimental_resource']) {
|
|
99
|
+
throw new Error(`\`${service}\` does not support resource-based operations`)
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/warnings.ts
CHANGED
|
@@ -33,6 +33,11 @@ export const printBrowserTokenWarning = createWarningPrinter([
|
|
|
33
33
|
)} for more information and how to hide this warning.`,
|
|
34
34
|
])
|
|
35
35
|
|
|
36
|
+
export const printCredentialedTokenWarning = createWarningPrinter([
|
|
37
|
+
'You have configured Sanity client to use a token, but also provided `withCredentials: true`.',
|
|
38
|
+
'This is no longer supported - only token will be used - remove `withCredentials: true`.',
|
|
39
|
+
])
|
|
40
|
+
|
|
36
41
|
export const printNoApiVersionSpecifiedWarning = createWarningPrinter([
|
|
37
42
|
'Using the Sanity client without specifying an API version is deprecated.',
|
|
38
43
|
`See ${generateHelpUrl('js-client-api-version')}`,
|