@sanity/client 6.28.3-resources.1 → 6.28.3-resources.3
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 +17 -11
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +17 -11
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +62 -28
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +17 -4
- package/dist/index.browser.d.ts +17 -4
- package/dist/index.browser.js +62 -28
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +45 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -4
- package/dist/index.d.ts +17 -4
- package/dist/index.js +47 -19
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +17 -4
- package/dist/stega.browser.d.ts +17 -4
- package/dist/stega.d.cts +17 -4
- package/dist/stega.d.ts +17 -4
- package/package.json +25 -6
- package/src/assets/AssetsClient.ts +20 -1
- package/src/config.ts +1 -1
- package/src/data/dataMethods.ts +56 -40
- package/src/types.ts +17 -4
- package/src/validators.ts +19 -11
- package/umd/sanityClient.js +62 -28
- package/umd/sanityClient.min.js +2 -2
package/dist/stega.browser.d.cts
CHANGED
|
@@ -395,10 +395,23 @@ export declare interface ClientConfig {
|
|
|
395
395
|
stega?: StegaConfig | boolean
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
declare type ClientConfigResource =
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
declare type ClientConfigResource =
|
|
399
|
+
| {
|
|
400
|
+
type: 'canvas'
|
|
401
|
+
id: string
|
|
402
|
+
}
|
|
403
|
+
| {
|
|
404
|
+
type: 'media-library'
|
|
405
|
+
id: string
|
|
406
|
+
}
|
|
407
|
+
| {
|
|
408
|
+
type: 'dataset'
|
|
409
|
+
id: string
|
|
410
|
+
}
|
|
411
|
+
| {
|
|
412
|
+
type: 'dashboard'
|
|
413
|
+
id: string
|
|
414
|
+
}
|
|
402
415
|
|
|
403
416
|
/** @public */
|
|
404
417
|
export declare class ClientError extends Error {
|
package/dist/stega.browser.d.ts
CHANGED
|
@@ -395,10 +395,23 @@ export declare interface ClientConfig {
|
|
|
395
395
|
stega?: StegaConfig | boolean
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
declare type ClientConfigResource =
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
declare type ClientConfigResource =
|
|
399
|
+
| {
|
|
400
|
+
type: 'canvas'
|
|
401
|
+
id: string
|
|
402
|
+
}
|
|
403
|
+
| {
|
|
404
|
+
type: 'media-library'
|
|
405
|
+
id: string
|
|
406
|
+
}
|
|
407
|
+
| {
|
|
408
|
+
type: 'dataset'
|
|
409
|
+
id: string
|
|
410
|
+
}
|
|
411
|
+
| {
|
|
412
|
+
type: 'dashboard'
|
|
413
|
+
id: string
|
|
414
|
+
}
|
|
402
415
|
|
|
403
416
|
/** @public */
|
|
404
417
|
export declare class ClientError extends Error {
|
package/dist/stega.d.cts
CHANGED
|
@@ -395,10 +395,23 @@ export declare interface ClientConfig {
|
|
|
395
395
|
stega?: StegaConfig | boolean
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
declare type ClientConfigResource =
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
declare type ClientConfigResource =
|
|
399
|
+
| {
|
|
400
|
+
type: 'canvas'
|
|
401
|
+
id: string
|
|
402
|
+
}
|
|
403
|
+
| {
|
|
404
|
+
type: 'media-library'
|
|
405
|
+
id: string
|
|
406
|
+
}
|
|
407
|
+
| {
|
|
408
|
+
type: 'dataset'
|
|
409
|
+
id: string
|
|
410
|
+
}
|
|
411
|
+
| {
|
|
412
|
+
type: 'dashboard'
|
|
413
|
+
id: string
|
|
414
|
+
}
|
|
402
415
|
|
|
403
416
|
/** @public */
|
|
404
417
|
export declare class ClientError extends Error {
|
package/dist/stega.d.ts
CHANGED
|
@@ -395,10 +395,23 @@ export declare interface ClientConfig {
|
|
|
395
395
|
stega?: StegaConfig | boolean
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
declare type ClientConfigResource =
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
declare type ClientConfigResource =
|
|
399
|
+
| {
|
|
400
|
+
type: 'canvas'
|
|
401
|
+
id: string
|
|
402
|
+
}
|
|
403
|
+
| {
|
|
404
|
+
type: 'media-library'
|
|
405
|
+
id: string
|
|
406
|
+
}
|
|
407
|
+
| {
|
|
408
|
+
type: 'dataset'
|
|
409
|
+
id: string
|
|
410
|
+
}
|
|
411
|
+
| {
|
|
412
|
+
type: 'dashboard'
|
|
413
|
+
id: string
|
|
414
|
+
}
|
|
402
415
|
|
|
403
416
|
/** @public */
|
|
404
417
|
export declare class ClientError extends Error {
|
package/package.json
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "6.28.3-resources.
|
|
3
|
+
"version": "6.28.3-resources.3",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"sanity",
|
|
7
|
+
"cms",
|
|
8
|
+
"headless",
|
|
9
|
+
"realtime",
|
|
10
|
+
"content",
|
|
11
|
+
"client",
|
|
12
|
+
"fetch",
|
|
13
|
+
"api"
|
|
14
|
+
],
|
|
6
15
|
"homepage": "https://www.sanity.io/",
|
|
7
16
|
"bugs": {
|
|
8
17
|
"url": "https://github.com/sanity-io/client/issues"
|
|
@@ -66,11 +75,19 @@
|
|
|
66
75
|
"types": "./dist/index.d.ts",
|
|
67
76
|
"typesVersions": {
|
|
68
77
|
"*": {
|
|
69
|
-
"csm": [
|
|
70
|
-
|
|
78
|
+
"csm": [
|
|
79
|
+
"./dist/csm.d.ts"
|
|
80
|
+
],
|
|
81
|
+
"stega": [
|
|
82
|
+
"./dist/stega.d.ts"
|
|
83
|
+
]
|
|
71
84
|
}
|
|
72
85
|
},
|
|
73
|
-
"files": [
|
|
86
|
+
"files": [
|
|
87
|
+
"dist",
|
|
88
|
+
"src",
|
|
89
|
+
"umd"
|
|
90
|
+
],
|
|
74
91
|
"scripts": {
|
|
75
92
|
"benchmark": "npm test -- bench",
|
|
76
93
|
"prebuild": "npm run clean",
|
|
@@ -93,7 +110,9 @@
|
|
|
93
110
|
"browserslist": "extends @sanity/browserslist-config",
|
|
94
111
|
"prettier": {
|
|
95
112
|
"bracketSpacing": false,
|
|
96
|
-
"plugins": [
|
|
113
|
+
"plugins": [
|
|
114
|
+
"prettier-plugin-packagejson"
|
|
115
|
+
],
|
|
97
116
|
"printWidth": 100,
|
|
98
117
|
"semi": false,
|
|
99
118
|
"singleQuote": true
|
|
@@ -182,7 +182,26 @@ function buildAssetUploadUrl(config: InitializedClientConfig, assetType: 'image'
|
|
|
182
182
|
const assetTypeEndpoint = assetType === 'image' ? 'images' : 'files'
|
|
183
183
|
|
|
184
184
|
if (config['~experimental_resource']) {
|
|
185
|
-
|
|
185
|
+
const {type, id} = config['~experimental_resource']
|
|
186
|
+
switch (type) {
|
|
187
|
+
case 'dataset': {
|
|
188
|
+
throw new Error(
|
|
189
|
+
'Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead.',
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
case 'canvas': {
|
|
193
|
+
return `/canvases/${id}/assets/${assetTypeEndpoint}`
|
|
194
|
+
}
|
|
195
|
+
case 'media-library': {
|
|
196
|
+
return `/media-libraries/${id}/upload`
|
|
197
|
+
}
|
|
198
|
+
case 'dashboard': {
|
|
199
|
+
return `/dashboards/${id}/assets/${assetTypeEndpoint}`
|
|
200
|
+
}
|
|
201
|
+
default:
|
|
202
|
+
// @ts-expect-error - handle all supported resource types
|
|
203
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`)
|
|
204
|
+
}
|
|
186
205
|
}
|
|
187
206
|
|
|
188
207
|
const dataset = validators.hasDataset(config)
|
package/src/config.ts
CHANGED
package/src/data/dataMethods.ts
CHANGED
|
@@ -12,12 +12,12 @@ import type {
|
|
|
12
12
|
Any,
|
|
13
13
|
BaseActionOptions,
|
|
14
14
|
BaseMutationOptions,
|
|
15
|
-
ClientConfig,
|
|
16
15
|
FirstDocumentIdMutationOptions,
|
|
17
16
|
FirstDocumentMutationOptions,
|
|
18
17
|
HttpRequest,
|
|
19
18
|
HttpRequestEvent,
|
|
20
19
|
IdentifiedSanityDocumentStub,
|
|
20
|
+
InitializedClientConfig,
|
|
21
21
|
InitializedStegaConfig,
|
|
22
22
|
MultipleActionResult,
|
|
23
23
|
MultipleMutationResult,
|
|
@@ -367,41 +367,27 @@ export function _create<R extends Record<string, Any>>(
|
|
|
367
367
|
const opts = Object.assign({returnFirst: true, returnDocuments: true}, options)
|
|
368
368
|
return _dataRequest(client, httpRequest, 'mutate', {mutations: [mutation]}, opts)
|
|
369
369
|
}
|
|
370
|
-
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
return match[3] === endpoint
|
|
387
|
-
}
|
|
388
|
-
const isQuery = (config: ClientConfig, uri: string) =>
|
|
389
|
-
uri.startsWith('/data/query/') || isResourceDataEndpoint(config, uri, 'query')
|
|
390
|
-
const isMutate = (config: ClientConfig, uri: string) =>
|
|
391
|
-
uri.startsWith('/data/mutate/') || isResourceDataEndpoint(config, uri, 'mutate')
|
|
392
|
-
const isDoc = (config: ClientConfig, uri: string) =>
|
|
393
|
-
uri.startsWith('/data/doc/') || isResourceDataEndpoint(config, uri, 'doc')
|
|
394
|
-
const isListener = (config: ClientConfig, uri: string) =>
|
|
395
|
-
uri.startsWith('/data/listen/') || isResourceDataEndpoint(config, uri, 'listen')
|
|
396
|
-
const isHistory = (config: ClientConfig, uri: string) =>
|
|
397
|
-
uri.startsWith('/data/history/') || isResourceDataEndpoint(config, uri, 'history')
|
|
398
|
-
const isData = (config: ClientConfig, uri: string) =>
|
|
370
|
+
|
|
371
|
+
const hasDataConfig = (client: SanityClient | ObservableSanityClient) =>
|
|
372
|
+
(client.config().dataset !== undefined && client.config().projectId !== undefined) ||
|
|
373
|
+
client.config()['~experimental_resource'] !== undefined
|
|
374
|
+
const isQuery = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
375
|
+
hasDataConfig(client) && uri.startsWith(_getDataUrl(client, 'query'))
|
|
376
|
+
const isMutate = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
377
|
+
hasDataConfig(client) && uri.startsWith(_getDataUrl(client, 'mutate'))
|
|
378
|
+
const isDoc = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
379
|
+
hasDataConfig(client) && uri.startsWith(_getDataUrl(client, 'doc', ''))
|
|
380
|
+
const isListener = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
381
|
+
hasDataConfig(client) && uri.startsWith(_getDataUrl(client, 'listen'))
|
|
382
|
+
const isHistory = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
383
|
+
hasDataConfig(client) && uri.startsWith(_getDataUrl(client, 'history', ''))
|
|
384
|
+
const isData = (client: SanityClient | ObservableSanityClient, uri: string) =>
|
|
399
385
|
uri.startsWith('/data/') ||
|
|
400
|
-
isQuery(
|
|
401
|
-
isMutate(
|
|
402
|
-
isDoc(
|
|
403
|
-
isListener(
|
|
404
|
-
isHistory(
|
|
386
|
+
isQuery(client, uri) ||
|
|
387
|
+
isMutate(client, uri) ||
|
|
388
|
+
isDoc(client, uri) ||
|
|
389
|
+
isListener(client, uri) ||
|
|
390
|
+
isHistory(client, uri)
|
|
405
391
|
|
|
406
392
|
/**
|
|
407
393
|
* @internal
|
|
@@ -418,7 +404,7 @@ export function _requestObservable<R>(
|
|
|
418
404
|
// Only the /data endpoint is currently available through API-CDN.
|
|
419
405
|
const canUseCdn =
|
|
420
406
|
typeof options.canUseCdn === 'undefined'
|
|
421
|
-
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && isData(
|
|
407
|
+
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && isData(client, uri)
|
|
422
408
|
: options.canUseCdn
|
|
423
409
|
|
|
424
410
|
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn
|
|
@@ -433,7 +419,7 @@ export function _requestObservable<R>(
|
|
|
433
419
|
}
|
|
434
420
|
|
|
435
421
|
// GROQ query-only parameters
|
|
436
|
-
if (['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 && isQuery(
|
|
422
|
+
if (['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 && isQuery(client, uri)) {
|
|
437
423
|
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap
|
|
438
424
|
if (resultSourceMap !== undefined && resultSourceMap !== false) {
|
|
439
425
|
options.query = {resultSourceMap, ...options.query}
|
|
@@ -516,13 +502,14 @@ export function _getDataUrl(
|
|
|
516
502
|
): string {
|
|
517
503
|
const config = client.config()
|
|
518
504
|
if (config['~experimental_resource']) {
|
|
519
|
-
|
|
520
|
-
const
|
|
505
|
+
validators.resourceConfig(config)
|
|
506
|
+
const resourceBase = resourceDataBase(config)
|
|
507
|
+
const uri = path !== undefined ? `${operation}/${path}` : operation
|
|
521
508
|
return `${resourceBase}/${uri}`.replace(/\/($|\?)/, '$1')
|
|
522
509
|
}
|
|
523
510
|
const catalog = validators.hasDataset(config)
|
|
524
511
|
const baseUri = `/${operation}/${catalog}`
|
|
525
|
-
const uri = path ? `${baseUri}/${path}` : baseUri
|
|
512
|
+
const uri = path !== undefined ? `${baseUri}/${path}` : baseUri
|
|
526
513
|
return `/data${uri}`.replace(/\/($|\?)/, '$1')
|
|
527
514
|
}
|
|
528
515
|
|
|
@@ -585,3 +572,32 @@ function _createAbortError(signal?: AbortSignal) {
|
|
|
585
572
|
|
|
586
573
|
return error
|
|
587
574
|
}
|
|
575
|
+
|
|
576
|
+
const resourceDataBase = (config: InitializedClientConfig): string => {
|
|
577
|
+
if (!config['~experimental_resource']) {
|
|
578
|
+
throw new Error('`resource` must be provided to perform resource queries')
|
|
579
|
+
}
|
|
580
|
+
const {type, id} = config['~experimental_resource']
|
|
581
|
+
|
|
582
|
+
switch (type) {
|
|
583
|
+
case 'dataset': {
|
|
584
|
+
const segments = id.split('.')
|
|
585
|
+
if (segments.length !== 2) {
|
|
586
|
+
throw new Error('Dataset ID must be in the format "project.dataset"')
|
|
587
|
+
}
|
|
588
|
+
return `/projects/${segments[0]}/datasets/${segments[1]}`
|
|
589
|
+
}
|
|
590
|
+
case 'canvas': {
|
|
591
|
+
return `/canvases/${id}`
|
|
592
|
+
}
|
|
593
|
+
case 'media-library': {
|
|
594
|
+
return `/media-libraries/${id}`
|
|
595
|
+
}
|
|
596
|
+
case 'dashboard': {
|
|
597
|
+
return `/dashboards/${id}`
|
|
598
|
+
}
|
|
599
|
+
default:
|
|
600
|
+
// @ts-expect-error - handle all supported resource types
|
|
601
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`)
|
|
602
|
+
}
|
|
603
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -53,10 +53,23 @@ export type ClientPerspective =
|
|
|
53
53
|
| 'raw'
|
|
54
54
|
| StackablePerspective[]
|
|
55
55
|
|
|
56
|
-
type ClientConfigResource =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
type ClientConfigResource =
|
|
57
|
+
| {
|
|
58
|
+
type: 'canvas'
|
|
59
|
+
id: string
|
|
60
|
+
}
|
|
61
|
+
| {
|
|
62
|
+
type: 'media-library'
|
|
63
|
+
id: string
|
|
64
|
+
}
|
|
65
|
+
| {
|
|
66
|
+
type: 'dataset'
|
|
67
|
+
id: string
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
type: 'dashboard'
|
|
71
|
+
id: string
|
|
72
|
+
}
|
|
60
73
|
|
|
61
74
|
/** @public */
|
|
62
75
|
export interface ClientConfig {
|
package/src/validators.ts
CHANGED
|
@@ -77,21 +77,29 @@ export const requestTag = (tag: string) => {
|
|
|
77
77
|
return tag
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
export const
|
|
80
|
+
export const resourceConfig = (config: InitializedClientConfig): void => {
|
|
81
81
|
if (!config['~experimental_resource']) {
|
|
82
|
-
throw new Error('`resource` must be provided to perform queries')
|
|
82
|
+
throw new Error('`resource` must be provided to perform resource queries')
|
|
83
83
|
}
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
const {type, id} = config['~experimental_resource']
|
|
85
|
+
|
|
86
|
+
switch (type) {
|
|
87
|
+
case 'dataset': {
|
|
88
|
+
const segments = id.split('.')
|
|
89
|
+
if (segments.length !== 2) {
|
|
90
|
+
throw new Error('Dataset resource ID must be in the format "project.dataset"')
|
|
91
|
+
}
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
case 'dashboard':
|
|
95
|
+
case 'media-library':
|
|
96
|
+
case 'canvas': {
|
|
97
|
+
return
|
|
90
98
|
}
|
|
91
|
-
|
|
99
|
+
default:
|
|
100
|
+
// @ts-expect-error - handle all supported resource types
|
|
101
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`)
|
|
92
102
|
}
|
|
93
|
-
|
|
94
|
-
return `/${resourceConfig.type}/${resourceConfig.id}`
|
|
95
103
|
}
|
|
96
104
|
|
|
97
105
|
export const resourceGuard = (service: string, config: InitializedClientConfig): void => {
|
package/umd/sanityClient.js
CHANGED
|
@@ -2063,17 +2063,23 @@
|
|
|
2063
2063
|
"Tag can only contain alphanumeric characters, underscores, dashes and dots, and be between one and 75 characters long."
|
|
2064
2064
|
);
|
|
2065
2065
|
return tag;
|
|
2066
|
-
},
|
|
2066
|
+
}, resourceConfig = (config) => {
|
|
2067
2067
|
if (!config["~experimental_resource"])
|
|
2068
|
-
throw new Error("`resource` must be provided to perform queries");
|
|
2069
|
-
const
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2068
|
+
throw new Error("`resource` must be provided to perform resource queries");
|
|
2069
|
+
const { type, id } = config["~experimental_resource"];
|
|
2070
|
+
switch (type) {
|
|
2071
|
+
case "dataset": {
|
|
2072
|
+
if (id.split(".").length !== 2)
|
|
2073
|
+
throw new Error('Dataset resource ID must be in the format "project.dataset"');
|
|
2074
|
+
return;
|
|
2075
|
+
}
|
|
2076
|
+
case "dashboard":
|
|
2077
|
+
case "media-library":
|
|
2078
|
+
case "canvas":
|
|
2079
|
+
return;
|
|
2080
|
+
default:
|
|
2081
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`);
|
|
2082
|
+
}
|
|
2077
2083
|
}, resourceGuard = (service, config) => {
|
|
2078
2084
|
if (config["~experimental_resource"])
|
|
2079
2085
|
throw new Error(`\`${service}\` does not support resource-based operations`);
|
|
@@ -2149,7 +2155,7 @@
|
|
|
2149
2155
|
}
|
|
2150
2156
|
if (projectBased && !newConfig.projectId)
|
|
2151
2157
|
throw new Error("Configuration must contain `projectId`");
|
|
2152
|
-
if (newConfig["~experimental_resource"] &&
|
|
2158
|
+
if (newConfig["~experimental_resource"] && resourceConfig(newConfig), typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
|
|
2153
2159
|
throw new Error(
|
|
2154
2160
|
"It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client'. Did you mean 'stega.enabled'?"
|
|
2155
2161
|
);
|
|
@@ -2779,21 +2785,12 @@ ${selectionOpts}`);
|
|
|
2779
2785
|
const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: true, returnDocuments: true }, options);
|
|
2780
2786
|
return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
|
|
2781
2787
|
}
|
|
2782
|
-
const
|
|
2783
|
-
if (!config["~experimental_resource"])
|
|
2784
|
-
return false;
|
|
2785
|
-
if (uri.startsWith(
|
|
2786
|
-
`/${config["~experimental_resource"].type}/${config["~experimental_resource"].id}/${endpoint}`
|
|
2787
|
-
))
|
|
2788
|
-
return true;
|
|
2789
|
-
const match = uri.match(resourceDatasetRegex);
|
|
2790
|
-
return match ? match[3] === endpoint : false;
|
|
2791
|
-
}, isQuery = (config, uri) => uri.startsWith("/data/query/") || isResourceDataEndpoint(config, uri, "query"), isMutate = (config, uri) => uri.startsWith("/data/mutate/") || isResourceDataEndpoint(config, uri, "mutate"), isDoc = (config, uri) => uri.startsWith("/data/doc/") || isResourceDataEndpoint(config, uri, "doc"), isListener = (config, uri) => uri.startsWith("/data/listen/") || isResourceDataEndpoint(config, uri, "listen"), isHistory = (config, uri) => uri.startsWith("/data/history/") || isResourceDataEndpoint(config, uri, "history"), isData = (config, uri) => uri.startsWith("/data/") || isQuery(config, uri) || isMutate(config, uri) || isDoc(config, uri) || isListener(config, uri) || isHistory(config, uri);
|
|
2788
|
+
const hasDataConfig = (client) => client.config().dataset !== void 0 && client.config().projectId !== void 0 || client.config()["~experimental_resource"] !== void 0, isQuery = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "query")), isMutate = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "mutate")), isDoc = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "doc", "")), isListener = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "listen")), isHistory = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "history", "")), isData = (client, uri) => uri.startsWith("/data/") || isQuery(client, uri) || isMutate(client, uri) || isDoc(client, uri) || isListener(client, uri) || isHistory(client, uri);
|
|
2792
2789
|
function _requestObservable(client, httpRequest, options) {
|
|
2793
|
-
const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(
|
|
2790
|
+
const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
|
|
2794
2791
|
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
|
|
2795
2792
|
const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
|
|
2796
|
-
if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(
|
|
2793
|
+
if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
|
|
2797
2794
|
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
|
|
2798
2795
|
resultSourceMap !== void 0 && resultSourceMap !== false && (options.query = { resultSourceMap, ...options.query });
|
|
2799
2796
|
const perspectiveOption = options.perspective || config.perspective;
|
|
@@ -2822,11 +2819,12 @@ ${selectionOpts}`);
|
|
|
2822
2819
|
function _getDataUrl(client, operation, path) {
|
|
2823
2820
|
const config = client.config();
|
|
2824
2821
|
if (config["~experimental_resource"]) {
|
|
2825
|
-
|
|
2826
|
-
|
|
2822
|
+
resourceConfig(config);
|
|
2823
|
+
const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
|
|
2824
|
+
return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
|
|
2827
2825
|
}
|
|
2828
2826
|
const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
|
|
2829
|
-
return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
|
|
2827
|
+
return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
|
|
2830
2828
|
}
|
|
2831
2829
|
function _getUrl(client, uri, canUseCdn = false) {
|
|
2832
2830
|
const { url, cdnUrl } = client.config();
|
|
@@ -2852,6 +2850,27 @@ ${selectionOpts}`);
|
|
|
2852
2850
|
const error = new Error(signal?.reason ?? "The operation was aborted.");
|
|
2853
2851
|
return error.name = "AbortError", error;
|
|
2854
2852
|
}
|
|
2853
|
+
const resourceDataBase = (config) => {
|
|
2854
|
+
if (!config["~experimental_resource"])
|
|
2855
|
+
throw new Error("`resource` must be provided to perform resource queries");
|
|
2856
|
+
const { type, id } = config["~experimental_resource"];
|
|
2857
|
+
switch (type) {
|
|
2858
|
+
case "dataset": {
|
|
2859
|
+
const segments = id.split(".");
|
|
2860
|
+
if (segments.length !== 2)
|
|
2861
|
+
throw new Error('Dataset ID must be in the format "project.dataset"');
|
|
2862
|
+
return `/projects/${segments[0]}/datasets/${segments[1]}`;
|
|
2863
|
+
}
|
|
2864
|
+
case "canvas":
|
|
2865
|
+
return `/canvases/${id}`;
|
|
2866
|
+
case "media-library":
|
|
2867
|
+
return `/media-libraries/${id}`;
|
|
2868
|
+
case "dashboard":
|
|
2869
|
+
return `/dashboards/${id}`;
|
|
2870
|
+
default:
|
|
2871
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`);
|
|
2872
|
+
}
|
|
2873
|
+
};
|
|
2855
2874
|
class ObservableAssetsClient {
|
|
2856
2875
|
#client;
|
|
2857
2876
|
#httpRequest;
|
|
@@ -2904,8 +2923,23 @@ ${selectionOpts}`);
|
|
|
2904
2923
|
}
|
|
2905
2924
|
function buildAssetUploadUrl(config, assetType) {
|
|
2906
2925
|
const assetTypeEndpoint = assetType === "image" ? "images" : "files";
|
|
2907
|
-
if (config["~experimental_resource"])
|
|
2908
|
-
|
|
2926
|
+
if (config["~experimental_resource"]) {
|
|
2927
|
+
const { type, id } = config["~experimental_resource"];
|
|
2928
|
+
switch (type) {
|
|
2929
|
+
case "dataset":
|
|
2930
|
+
throw new Error(
|
|
2931
|
+
"Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
|
|
2932
|
+
);
|
|
2933
|
+
case "canvas":
|
|
2934
|
+
return `/canvases/${id}/assets/${assetTypeEndpoint}`;
|
|
2935
|
+
case "media-library":
|
|
2936
|
+
return `/media-libraries/${id}/upload`;
|
|
2937
|
+
case "dashboard":
|
|
2938
|
+
return `/dashboards/${id}/assets/${assetTypeEndpoint}`;
|
|
2939
|
+
default:
|
|
2940
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`);
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2909
2943
|
const dataset2 = hasDataset(config);
|
|
2910
2944
|
return `assets/${assetTypeEndpoint}/${dataset2}`;
|
|
2911
2945
|
}
|