@sanity/client 6.28.4 → 6.28.5-goaway.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/README.md +1 -0
- package/dist/_chunks-cjs/config.cjs +25 -3
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +25 -3
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +95 -21
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +32 -0
- package/dist/index.browser.d.ts +32 -0
- package/dist/index.browser.js +95 -21
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +73 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +74 -20
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +32 -0
- package/dist/stega.browser.d.ts +32 -0
- package/dist/stega.d.cts +32 -0
- package/dist/stega.d.ts +32 -0
- package/package.json +15 -15
- package/src/assets/AssetsClient.ts +34 -3
- package/src/config.ts +6 -2
- package/src/data/dataMethods.ts +82 -32
- package/src/data/live.ts +5 -1
- package/src/datasets/DatasetsClient.ts +5 -0
- package/src/projects/ProjectsClient.ts +5 -0
- package/src/types.ts +37 -1
- package/src/validators.ts +31 -0
- package/umd/sanityClient.js +95 -21
- package/umd/sanityClient.min.js +2 -2
|
@@ -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,
|
|
@@ -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,24 @@ export type ClientPerspective =
|
|
|
53
53
|
| 'raw'
|
|
54
54
|
| StackablePerspective[]
|
|
55
55
|
|
|
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
|
+
}
|
|
73
|
+
|
|
56
74
|
/** @public */
|
|
57
75
|
export interface ClientConfig {
|
|
58
76
|
projectId?: string
|
|
@@ -61,6 +79,9 @@ export interface ClientConfig {
|
|
|
61
79
|
useCdn?: boolean
|
|
62
80
|
token?: string
|
|
63
81
|
|
|
82
|
+
/** @internal */
|
|
83
|
+
'~experimental_resource'?: ClientConfigResource
|
|
84
|
+
|
|
64
85
|
/**
|
|
65
86
|
* What perspective to use for the client. See {@link https://www.sanity.io/docs/perspectives|perspective documentation}
|
|
66
87
|
* @remarks
|
|
@@ -1324,8 +1345,23 @@ export interface LiveEventMessage {
|
|
|
1324
1345
|
export interface LiveEventWelcome {
|
|
1325
1346
|
type: 'welcome'
|
|
1326
1347
|
}
|
|
1348
|
+
/**
|
|
1349
|
+
* The `id` field is the position at which the connection was rejected or closed.
|
|
1350
|
+
* The `reason` field will specify why the connection rejected/closed.
|
|
1351
|
+
* @public
|
|
1352
|
+
*/
|
|
1353
|
+
export interface LiveEventGoAway {
|
|
1354
|
+
type: 'goaway'
|
|
1355
|
+
id: string
|
|
1356
|
+
reason: string
|
|
1357
|
+
}
|
|
1327
1358
|
/** @public */
|
|
1328
|
-
export type LiveEvent =
|
|
1359
|
+
export type LiveEvent =
|
|
1360
|
+
| LiveEventRestart
|
|
1361
|
+
| LiveEventReconnect
|
|
1362
|
+
| LiveEventMessage
|
|
1363
|
+
| LiveEventWelcome
|
|
1364
|
+
| LiveEventGoAway
|
|
1329
1365
|
|
|
1330
1366
|
/** @public */
|
|
1331
1367
|
export interface SanityQueries {}
|
package/src/validators.ts
CHANGED
|
@@ -76,3 +76,34 @@ export const requestTag = (tag: string) => {
|
|
|
76
76
|
|
|
77
77
|
return tag
|
|
78
78
|
}
|
|
79
|
+
|
|
80
|
+
export const resourceConfig = (config: InitializedClientConfig): void => {
|
|
81
|
+
if (!config['~experimental_resource']) {
|
|
82
|
+
throw new Error('`resource` must be provided to perform resource queries')
|
|
83
|
+
}
|
|
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
|
|
98
|
+
}
|
|
99
|
+
default:
|
|
100
|
+
// @ts-expect-error - handle all supported resource types
|
|
101
|
+
throw new Error(`Unsupported resource type: ${type.toString()}`)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const resourceGuard = (service: string, config: InitializedClientConfig): void => {
|
|
106
|
+
if (config['~experimental_resource']) {
|
|
107
|
+
throw new Error(`\`${service}\` does not support resource-based operations`)
|
|
108
|
+
}
|
|
109
|
+
}
|
package/umd/sanityClient.js
CHANGED
|
@@ -2015,8 +2015,8 @@
|
|
|
2015
2015
|
}
|
|
2016
2016
|
function shouldRetry(err, attempt, options) {
|
|
2017
2017
|
if (options.maxRetries === 0) return false;
|
|
2018
|
-
const isSafe = options.method === "GET" || options.method === "HEAD",
|
|
2019
|
-
return (isSafe ||
|
|
2018
|
+
const isSafe = options.method === "GET" || options.method === "HEAD", isQuery2 = (options.uri || options.url).startsWith("/data/query"), isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
|
|
2019
|
+
return (isSafe || isQuery2) && isRetriableResponse ? true : P.shouldRetry(err, attempt, options);
|
|
2020
2020
|
}
|
|
2021
2021
|
const BASE_URL = "https://www.sanity.io/help/";
|
|
2022
2022
|
function generateHelpUrl(slug) {
|
|
@@ -2063,6 +2063,26 @@
|
|
|
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
|
+
}, resourceConfig = (config) => {
|
|
2067
|
+
if (!config["~experimental_resource"])
|
|
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
|
+
}
|
|
2083
|
+
}, resourceGuard = (service, config) => {
|
|
2084
|
+
if (config["~experimental_resource"])
|
|
2085
|
+
throw new Error(`\`${service}\` does not support resource-based operations`);
|
|
2066
2086
|
};
|
|
2067
2087
|
function once(fn) {
|
|
2068
2088
|
let didCall = false, returnValue;
|
|
@@ -2128,14 +2148,14 @@
|
|
|
2128
2148
|
const newConfig = {
|
|
2129
2149
|
...defaultConfig,
|
|
2130
2150
|
...specifiedConfig
|
|
2131
|
-
}, projectBased = newConfig.useProjectHostname;
|
|
2151
|
+
}, projectBased = newConfig.useProjectHostname && !newConfig["~experimental_resource"];
|
|
2132
2152
|
if (typeof Promise > "u") {
|
|
2133
2153
|
const helpUrl = generateHelpUrl("js-client-promise-polyfill");
|
|
2134
2154
|
throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
|
|
2135
2155
|
}
|
|
2136
2156
|
if (projectBased && !newConfig.projectId)
|
|
2137
2157
|
throw new Error("Configuration must contain `projectId`");
|
|
2138
|
-
if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
|
|
2158
|
+
if (newConfig["~experimental_resource"] && resourceConfig(newConfig), typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
|
|
2139
2159
|
throw new Error(
|
|
2140
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'?"
|
|
2141
2161
|
);
|
|
@@ -2154,7 +2174,7 @@
|
|
|
2154
2174
|
const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname), hasToken = !!newConfig.token;
|
|
2155
2175
|
newConfig.withCredentials && hasToken && (printCredentialedTokenWarning(), newConfig.withCredentials = false), isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === true && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
|
|
2156
2176
|
const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
2157
|
-
return
|
|
2177
|
+
return projectBased ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
|
|
2158
2178
|
};
|
|
2159
2179
|
class ConnectionFailedError extends Error {
|
|
2160
2180
|
name = "ConnectionFailedError";
|
|
@@ -2722,7 +2742,7 @@ ${selectionOpts}`);
|
|
|
2722
2742
|
);
|
|
2723
2743
|
}
|
|
2724
2744
|
function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
|
|
2725
|
-
const isMutation = endpoint === "mutate", isAction = endpoint === "actions",
|
|
2745
|
+
const isMutation = endpoint === "mutate", isAction = endpoint === "actions", isQuery2 = endpoint === "query", strQuery = isMutation || isAction ? "" : encodeQueryString(body), useGet = !isMutation && !isAction && strQuery.length < getQuerySizeLimit, stringQuery = useGet ? strQuery : "", returnFirst = options.returnFirst, { timeout, token, tag, headers, returnQuery, lastLiveEventId, cacheMode } = options, uri = _getDataUrl(client, endpoint, stringQuery), reqOptions = {
|
|
2726
2746
|
method: useGet ? "GET" : "POST",
|
|
2727
2747
|
uri,
|
|
2728
2748
|
json: true,
|
|
@@ -2737,7 +2757,7 @@ ${selectionOpts}`);
|
|
|
2737
2757
|
resultSourceMap: options.resultSourceMap,
|
|
2738
2758
|
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
|
|
2739
2759
|
cacheMode,
|
|
2740
|
-
canUseCdn:
|
|
2760
|
+
canUseCdn: isQuery2,
|
|
2741
2761
|
signal: options.signal,
|
|
2742
2762
|
fetch: options.fetch,
|
|
2743
2763
|
useAbortSignal: options.useAbortSignal,
|
|
@@ -2765,11 +2785,12 @@ ${selectionOpts}`);
|
|
|
2765
2785
|
const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: true, returnDocuments: true }, options);
|
|
2766
2786
|
return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
|
|
2767
2787
|
}
|
|
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);
|
|
2768
2789
|
function _requestObservable(client, httpRequest, options) {
|
|
2769
|
-
const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri
|
|
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;
|
|
2770
2791
|
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
|
|
2771
2792
|
const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
|
|
2772
|
-
if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 &&
|
|
2793
|
+
if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
|
|
2773
2794
|
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
|
|
2774
2795
|
resultSourceMap !== void 0 && resultSourceMap !== false && (options.query = { resultSourceMap, ...options.query });
|
|
2775
2796
|
const perspectiveOption = options.perspective || config.perspective;
|
|
@@ -2796,8 +2817,14 @@ ${selectionOpts}`);
|
|
|
2796
2817
|
);
|
|
2797
2818
|
}
|
|
2798
2819
|
function _getDataUrl(client, operation, path) {
|
|
2799
|
-
const config = client.config()
|
|
2800
|
-
|
|
2820
|
+
const config = client.config();
|
|
2821
|
+
if (config["~experimental_resource"]) {
|
|
2822
|
+
resourceConfig(config);
|
|
2823
|
+
const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
|
|
2824
|
+
return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
|
|
2825
|
+
}
|
|
2826
|
+
const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
|
|
2827
|
+
return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
|
|
2801
2828
|
}
|
|
2802
2829
|
function _getUrl(client, uri, canUseCdn = false) {
|
|
2803
2830
|
const { url, cdnUrl } = client.config();
|
|
@@ -2823,6 +2850,27 @@ ${selectionOpts}`);
|
|
|
2823
2850
|
const error = new Error(signal?.reason ?? "The operation was aborted.");
|
|
2824
2851
|
return error.name = "AbortError", error;
|
|
2825
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
|
+
};
|
|
2826
2874
|
class ObservableAssetsClient {
|
|
2827
2875
|
#client;
|
|
2828
2876
|
#httpRequest;
|
|
@@ -2855,7 +2903,7 @@ ${selectionOpts}`);
|
|
|
2855
2903
|
validateAssetType(assetType);
|
|
2856
2904
|
let meta = opts.extract || void 0;
|
|
2857
2905
|
meta && !meta.length && (meta = ["none"]);
|
|
2858
|
-
const
|
|
2906
|
+
const config = client.config(), options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
|
|
2859
2907
|
label,
|
|
2860
2908
|
title,
|
|
2861
2909
|
description,
|
|
@@ -2867,12 +2915,34 @@ ${selectionOpts}`);
|
|
|
2867
2915
|
tag,
|
|
2868
2916
|
method: "POST",
|
|
2869
2917
|
timeout: options.timeout || 0,
|
|
2870
|
-
uri:
|
|
2918
|
+
uri: buildAssetUploadUrl(config, assetType),
|
|
2871
2919
|
headers: options.contentType ? { "Content-Type": options.contentType } : {},
|
|
2872
2920
|
query,
|
|
2873
2921
|
body
|
|
2874
2922
|
});
|
|
2875
2923
|
}
|
|
2924
|
+
function buildAssetUploadUrl(config, assetType) {
|
|
2925
|
+
const assetTypeEndpoint = assetType === "image" ? "images" : "files";
|
|
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
|
+
}
|
|
2943
|
+
const dataset2 = hasDataset(config);
|
|
2944
|
+
return `assets/${assetTypeEndpoint}/${dataset2}`;
|
|
2945
|
+
}
|
|
2876
2946
|
function optionsFromFile(opts, file) {
|
|
2877
2947
|
return typeof File > "u" || !(file instanceof File) ? opts : Object.assign(
|
|
2878
2948
|
{
|
|
@@ -2964,6 +3034,7 @@ ${selectionOpts}`);
|
|
|
2964
3034
|
includeDrafts = false,
|
|
2965
3035
|
tag: _tag
|
|
2966
3036
|
} = {}) {
|
|
3037
|
+
resourceGuard("live", this.#client.config());
|
|
2967
3038
|
const {
|
|
2968
3039
|
projectId: projectId2,
|
|
2969
3040
|
apiVersion: _apiVersion,
|
|
@@ -2995,7 +3066,8 @@ ${selectionOpts}`);
|
|
|
2995
3066
|
"message",
|
|
2996
3067
|
"restart",
|
|
2997
3068
|
"welcome",
|
|
2998
|
-
"reconnect"
|
|
3069
|
+
"reconnect",
|
|
3070
|
+
"goaway"
|
|
2999
3071
|
]).pipe(
|
|
3000
3072
|
reconnectOnConnectionFailure(),
|
|
3001
3073
|
map((event) => {
|
|
@@ -3093,7 +3165,7 @@ ${selectionOpts}`);
|
|
|
3093
3165
|
* @param options - Options for the dataset
|
|
3094
3166
|
*/
|
|
3095
3167
|
create(name, options) {
|
|
3096
|
-
return lastValueFrom(
|
|
3168
|
+
return resourceGuard("dataset", this.#client.config()), lastValueFrom(
|
|
3097
3169
|
_modify(this.#client, this.#httpRequest, "PUT", name, options)
|
|
3098
3170
|
);
|
|
3099
3171
|
}
|
|
@@ -3104,7 +3176,7 @@ ${selectionOpts}`);
|
|
|
3104
3176
|
* @param options - New options for the dataset
|
|
3105
3177
|
*/
|
|
3106
3178
|
edit(name, options) {
|
|
3107
|
-
return lastValueFrom(
|
|
3179
|
+
return resourceGuard("dataset", this.#client.config()), lastValueFrom(
|
|
3108
3180
|
_modify(this.#client, this.#httpRequest, "PATCH", name, options)
|
|
3109
3181
|
);
|
|
3110
3182
|
}
|
|
@@ -3114,19 +3186,19 @@ ${selectionOpts}`);
|
|
|
3114
3186
|
* @param name - Name of the dataset to delete
|
|
3115
3187
|
*/
|
|
3116
3188
|
delete(name) {
|
|
3117
|
-
return lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name));
|
|
3189
|
+
return resourceGuard("dataset", this.#client.config()), lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name));
|
|
3118
3190
|
}
|
|
3119
3191
|
/**
|
|
3120
3192
|
* Fetch a list of datasets for the configured project
|
|
3121
3193
|
*/
|
|
3122
3194
|
list() {
|
|
3123
|
-
return lastValueFrom(
|
|
3195
|
+
return resourceGuard("dataset", this.#client.config()), lastValueFrom(
|
|
3124
3196
|
_request(this.#client, this.#httpRequest, { uri: "/datasets", tag: null })
|
|
3125
3197
|
);
|
|
3126
3198
|
}
|
|
3127
3199
|
}
|
|
3128
3200
|
function _modify(client, httpRequest, method, name, options) {
|
|
3129
|
-
return dataset(name), _request(client, httpRequest, {
|
|
3201
|
+
return resourceGuard("dataset", client.config()), dataset(name), _request(client, httpRequest, {
|
|
3130
3202
|
method,
|
|
3131
3203
|
uri: `/datasets/${name}`,
|
|
3132
3204
|
body: options,
|
|
@@ -3140,6 +3212,7 @@ ${selectionOpts}`);
|
|
|
3140
3212
|
this.#client = client, this.#httpRequest = httpRequest;
|
|
3141
3213
|
}
|
|
3142
3214
|
list(options) {
|
|
3215
|
+
resourceGuard("projects", this.#client.config());
|
|
3143
3216
|
const uri = options?.includeMembers === false ? "/projects?includeMembers=false" : "/projects";
|
|
3144
3217
|
return _request(this.#client, this.#httpRequest, { uri });
|
|
3145
3218
|
}
|
|
@@ -3149,7 +3222,7 @@ ${selectionOpts}`);
|
|
|
3149
3222
|
* @param projectId - ID of the project to fetch
|
|
3150
3223
|
*/
|
|
3151
3224
|
getById(projectId2) {
|
|
3152
|
-
return _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` });
|
|
3225
|
+
return resourceGuard("projects", this.#client.config()), _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` });
|
|
3153
3226
|
}
|
|
3154
3227
|
}
|
|
3155
3228
|
class ProjectsClient {
|
|
@@ -3159,6 +3232,7 @@ ${selectionOpts}`);
|
|
|
3159
3232
|
this.#client = client, this.#httpRequest = httpRequest;
|
|
3160
3233
|
}
|
|
3161
3234
|
list(options) {
|
|
3235
|
+
resourceGuard("projects", this.#client.config());
|
|
3162
3236
|
const uri = options?.includeMembers === false ? "/projects?includeMembers=false" : "/projects";
|
|
3163
3237
|
return lastValueFrom(_request(this.#client, this.#httpRequest, { uri }));
|
|
3164
3238
|
}
|
|
@@ -3168,7 +3242,7 @@ ${selectionOpts}`);
|
|
|
3168
3242
|
* @param projectId - ID of the project to fetch
|
|
3169
3243
|
*/
|
|
3170
3244
|
getById(projectId2) {
|
|
3171
|
-
return lastValueFrom(
|
|
3245
|
+
return resourceGuard("projects", this.#client.config()), lastValueFrom(
|
|
3172
3246
|
_request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` })
|
|
3173
3247
|
);
|
|
3174
3248
|
}
|