@sanity/client 7.26.2 → 8.1.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 +63 -341
- package/dist/browserUpload-2tz6Sdqp.js +87 -0
- package/dist/browserUpload-2tz6Sdqp.js.map +1 -0
- package/dist/browserUpload-CwpNx7Vl.js +87 -0
- package/dist/browserUpload-CwpNx7Vl.js.map +1 -0
- package/dist/config-3wiPP-sZ.js +126 -0
- package/dist/config-3wiPP-sZ.js.map +1 -0
- package/dist/csm.d.ts +122 -335
- package/dist/csm.js +79 -91
- package/dist/csm.js.map +1 -1
- package/dist/dist-C9ExSk2R.js +73 -0
- package/dist/dist-C9ExSk2R.js.map +1 -0
- package/dist/dist-Z8cIRxoB.js +69 -0
- package/dist/dist-Z8cIRxoB.js.map +1 -0
- package/dist/index.d.ts +112 -6722
- package/dist/index.js +2950 -2848
- package/dist/index.js.map +1 -1
- package/dist/{index.d.cts → index.node.d.ts} +4529 -5322
- package/dist/index.node.js +3264 -0
- package/dist/index.node.js.map +1 -0
- package/dist/isRecord-Kfmt-nk-.js +7 -0
- package/dist/isRecord-Kfmt-nk-.js.map +1 -0
- package/dist/media-library.d.ts +9 -100
- package/dist/media-library.js +36 -14
- package/dist/media-library.js.map +1 -1
- package/dist/request-BhMuKj0D.js +373 -0
- package/dist/request-BhMuKj0D.js.map +1 -0
- package/dist/request-SnMg7nUX.js +355 -0
- package/dist/request-SnMg7nUX.js.map +1 -0
- package/dist/resolveEditInfo-Cz-smq3a.js +339 -0
- package/dist/resolveEditInfo-Cz-smq3a.js.map +1 -0
- package/dist/rolldown-runtime-vyAXikos.js +9 -0
- package/dist/stega.d.ts +78 -7137
- package/dist/stega.js +27 -20
- package/dist/stega.js.map +1 -1
- package/dist/stegaClean-C18wLWau.js +21 -0
- package/dist/stegaClean-C18wLWau.js.map +1 -0
- package/dist/stegaEncodeSourceMap-DbM2fTN4.js +373 -0
- package/dist/stegaEncodeSourceMap-DbM2fTN4.js.map +1 -0
- package/dist/stegaEncodeSourceMap-YR3NQ3iz.js +163 -0
- package/dist/stegaEncodeSourceMap-YR3NQ3iz.js.map +1 -0
- package/dist/{index.browser.d.cts → types-BODIEY7F.d.ts} +4196 -5258
- package/dist/types-CfGzbXrl.d.ts +108 -0
- package/package.json +80 -142
- package/src/SanityClient.ts +101 -137
- package/src/agent/actions/AgentActionsClient.ts +12 -12
- package/src/agent/actions/commonTypes.ts +2 -1
- package/src/agent/actions/generate.ts +26 -6
- package/src/agent/actions/patch.ts +27 -7
- package/src/agent/actions/prompt.ts +19 -5
- package/src/agent/actions/transform.ts +23 -5
- package/src/agent/actions/translate.ts +23 -5
- package/src/assets/AssetsClient.ts +121 -24
- package/src/csm/applySourceDocuments.ts +5 -5
- package/src/csm/createEditUrl.ts +1 -1
- package/src/csm/draftUtils.ts +23 -5
- package/src/csm/index.ts +2 -2
- package/src/csm/studioPath.ts +2 -1
- package/src/csm/types.ts +8 -2
- package/src/data/dataMethods.ts +784 -349
- package/src/data/eventsource.ts +82 -49
- package/src/data/listen.ts +18 -23
- package/src/data/live.ts +60 -35
- package/src/data/resolveEventSourceFetch.ts +104 -0
- package/src/datasets/DatasetsClient.ts +58 -41
- package/src/defineCreateClient.ts +38 -22
- package/src/functions/FunctionsClient.ts +66 -0
- package/src/functions/invoke.ts +176 -0
- package/src/http/browserMiddleware.ts +7 -1
- package/src/http/browserUpload.ts +163 -0
- package/src/http/errors.ts +61 -6
- package/src/http/nodeMiddleware.ts +94 -30
- package/src/http/request.ts +304 -87
- package/src/http/requestOptions.ts +131 -15
- package/src/{index.browser.ts → index.node.ts} +9 -4
- package/src/index.ts +4 -3
- package/src/mediaLibrary/MediaLibraryVideoClient.ts +27 -12
- package/src/projects/ProjectsClient.ts +18 -16
- package/src/releases/ReleasesClient.ts +38 -33
- package/src/stega/index.ts +1 -2
- package/src/stega/stegaEncodeSourceMap.ts +5 -0
- package/src/stega/types.ts +2 -2
- package/src/types.ts +328 -103
- package/src/users/UsersClient.ts +10 -14
- package/src/util/shareReplayLatest.ts +1 -1
- package/src/validators.ts +1 -1
- package/src/warnings.ts +13 -2
- package/dist/_chunks-cjs/config.cjs +0 -206
- package/dist/_chunks-cjs/config.cjs.map +0 -1
- package/dist/_chunks-cjs/isRecord.cjs +0 -6
- package/dist/_chunks-cjs/isRecord.cjs.map +0 -1
- package/dist/_chunks-cjs/resolveEditInfo.cjs +0 -334
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +0 -1
- package/dist/_chunks-cjs/stegaClean.cjs +0 -46
- package/dist/_chunks-cjs/stegaClean.cjs.map +0 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +0 -388
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +0 -1
- package/dist/_chunks-es/config.js +0 -207
- package/dist/_chunks-es/config.js.map +0 -1
- package/dist/_chunks-es/isRecord.js +0 -7
- package/dist/_chunks-es/isRecord.js.map +0 -1
- package/dist/_chunks-es/resolveEditInfo.js +0 -335
- package/dist/_chunks-es/resolveEditInfo.js.map +0 -1
- package/dist/_chunks-es/stegaClean.js +0 -47
- package/dist/_chunks-es/stegaClean.js.map +0 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +0 -389
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +0 -1
- package/dist/csm.cjs +0 -99
- package/dist/csm.cjs.map +0 -1
- package/dist/csm.d.cts +0 -391
- package/dist/index.browser.cjs +0 -3106
- package/dist/index.browser.cjs.map +0 -1
- package/dist/index.browser.d.ts +0 -6802
- package/dist/index.browser.js +0 -3082
- package/dist/index.browser.js.map +0 -1
- package/dist/index.cjs +0 -2946
- package/dist/index.cjs.map +0 -1
- package/dist/media-library.cjs +0 -20
- package/dist/media-library.cjs.map +0 -1
- package/dist/media-library.d.cts +0 -117
- package/dist/stega.browser.cjs +0 -29
- package/dist/stega.browser.cjs.map +0 -1
- package/dist/stega.browser.d.cts +0 -7200
- package/dist/stega.browser.d.ts +0 -7200
- package/dist/stega.browser.js +0 -24
- package/dist/stega.browser.js.map +0 -1
- package/dist/stega.cjs +0 -29
- package/dist/stega.cjs.map +0 -1
- package/dist/stega.d.cts +0 -7200
- package/src/data/eventsourcePolyfill.ts +0 -7
- package/umd/.gitkeep +0 -1
- package/umd/sanityClient.js +0 -6543
- package/umd/sanityClient.min.js +0 -6
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
3
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
4
4
|
import type {Any, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
|
|
5
|
-
import {_generate, type GenerateInstruction} from './generate'
|
|
5
|
+
import {_generate, _generateObservable, type GenerateInstruction} from './generate'
|
|
6
6
|
import {_patch, type PatchDocument} from './patch'
|
|
7
7
|
import {_prompt, type PromptRequest} from './prompt'
|
|
8
|
-
import {_transform, type TransformDocument} from './transform'
|
|
9
|
-
import {_translate, type TranslateDocument} from './translate'
|
|
8
|
+
import {_transform, _transformObservable, type TransformDocument} from './transform'
|
|
9
|
+
import {_translate, _translateObservable, type TranslateDocument} from './translate'
|
|
10
10
|
|
|
11
11
|
/** @public */
|
|
12
12
|
export class ObservableAgentsActionClient {
|
|
@@ -28,7 +28,7 @@ export class ObservableAgentsActionClient {
|
|
|
28
28
|
? {_id: string}
|
|
29
29
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
30
30
|
> {
|
|
31
|
-
return
|
|
31
|
+
return _generateObservable(this.#client, this.#httpRequest, request)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
@@ -42,7 +42,7 @@ export class ObservableAgentsActionClient {
|
|
|
42
42
|
? {_id: string}
|
|
43
43
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
44
44
|
> {
|
|
45
|
-
return
|
|
45
|
+
return _transformObservable(this.#client, this.#httpRequest, request)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -56,7 +56,7 @@ export class ObservableAgentsActionClient {
|
|
|
56
56
|
? {_id: string}
|
|
57
57
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
58
58
|
> {
|
|
59
|
-
return
|
|
59
|
+
return _translateObservable(this.#client, this.#httpRequest, request)
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -80,7 +80,7 @@ export class AgentActionsClient {
|
|
|
80
80
|
? {_id: string}
|
|
81
81
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
82
82
|
> {
|
|
83
|
-
return
|
|
83
|
+
return _generate(this.#client, this.#httpRequest, request)
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
@@ -94,7 +94,7 @@ export class AgentActionsClient {
|
|
|
94
94
|
? {_id: string}
|
|
95
95
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
96
96
|
> {
|
|
97
|
-
return
|
|
97
|
+
return _transform(this.#client, this.#httpRequest, request)
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
@@ -108,7 +108,7 @@ export class AgentActionsClient {
|
|
|
108
108
|
? {_id: string}
|
|
109
109
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
110
110
|
> {
|
|
111
|
-
return
|
|
111
|
+
return _translate(this.#client, this.#httpRequest, request)
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
/**
|
|
@@ -118,7 +118,7 @@ export class AgentActionsClient {
|
|
|
118
118
|
prompt<const DocumentShape extends Record<string, Any>>(
|
|
119
119
|
request: PromptRequest<DocumentShape>,
|
|
120
120
|
): Promise<(typeof request)['format'] extends 'json' ? DocumentShape : string> {
|
|
121
|
-
return
|
|
121
|
+
return _prompt(this.#client, this.#httpRequest, request)
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
@@ -133,6 +133,6 @@ export class AgentActionsClient {
|
|
|
133
133
|
? {_id: string}
|
|
134
134
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
135
135
|
> {
|
|
136
|
-
return
|
|
136
|
+
return _patch(this.#client, this.#httpRequest, request)
|
|
137
137
|
}
|
|
138
138
|
}
|
|
@@ -135,7 +135,8 @@ export interface GroqAgentActionParam {
|
|
|
135
135
|
|
|
136
136
|
/** @beta */
|
|
137
137
|
export type AgentActionTypeConfig =
|
|
138
|
-
{include: string[]; exclude?: never}
|
|
138
|
+
| {include: string[]; exclude?: never}
|
|
139
|
+
| {exclude: string[]; include?: never}
|
|
139
140
|
|
|
140
141
|
/** @beta */
|
|
141
142
|
export type AgentActionPathSegment = string | {_key: string}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {_request} from '../../data/dataMethods'
|
|
3
|
+
import {_request, _requestObservable} from '../../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
5
|
import type {AgentActionParams, Any, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
|
|
6
6
|
import {hasDataset} from '../../validators'
|
|
@@ -271,23 +271,26 @@ interface GenerateTargetDocumentRequest<T extends Record<string, Any> = Record<s
|
|
|
271
271
|
|
|
272
272
|
/** @beta */
|
|
273
273
|
export type GenerateSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
274
|
-
|
|
274
|
+
| GenerateExistingDocumentRequest
|
|
275
|
+
| GenerateTargetDocumentRequest<T>
|
|
275
276
|
) &
|
|
276
277
|
GenerateRequestBase &
|
|
277
278
|
AgentActionSync
|
|
278
279
|
|
|
279
280
|
/** @beta */
|
|
280
281
|
export type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
281
|
-
|
|
282
|
+
| GenerateExistingDocumentRequest
|
|
283
|
+
| GenerateTargetDocumentRequest<T>
|
|
282
284
|
) &
|
|
283
285
|
GenerateRequestBase &
|
|
284
286
|
AgentActionAsync
|
|
285
287
|
|
|
286
288
|
/** @beta */
|
|
287
289
|
export type GenerateInstruction<T extends Record<string, Any> = Record<string, Any>> =
|
|
288
|
-
|
|
290
|
+
| GenerateSyncInstruction<T>
|
|
291
|
+
| GenerateAsyncInstruction<T>
|
|
289
292
|
|
|
290
|
-
export function
|
|
293
|
+
export function _generateObservable<DocumentShape extends Record<string, Any>>(
|
|
291
294
|
client: SanityClient | ObservableSanityClient,
|
|
292
295
|
httpRequest: HttpRequest,
|
|
293
296
|
request: GenerateInstruction<DocumentShape>,
|
|
@@ -295,11 +298,28 @@ export function _generate<DocumentShape extends Record<string, Any>>(
|
|
|
295
298
|
(typeof request)['async'] extends true
|
|
296
299
|
? {_id: string}
|
|
297
300
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
301
|
+
> {
|
|
302
|
+
const dataset = hasDataset(client.config())
|
|
303
|
+
return _requestObservable(client, httpRequest, {
|
|
304
|
+
method: 'POST',
|
|
305
|
+
url: `/agent/action/generate/${dataset}`,
|
|
306
|
+
body: request,
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function _generate<DocumentShape extends Record<string, Any>>(
|
|
311
|
+
client: SanityClient | ObservableSanityClient,
|
|
312
|
+
httpRequest: HttpRequest,
|
|
313
|
+
request: GenerateInstruction<DocumentShape>,
|
|
314
|
+
): Promise<
|
|
315
|
+
(typeof request)['async'] extends true
|
|
316
|
+
? {_id: string}
|
|
317
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
298
318
|
> {
|
|
299
319
|
const dataset = hasDataset(client.config())
|
|
300
320
|
return _request(client, httpRequest, {
|
|
301
321
|
method: 'POST',
|
|
302
|
-
|
|
322
|
+
url: `/agent/action/generate/${dataset}`,
|
|
303
323
|
body: request,
|
|
304
324
|
})
|
|
305
325
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {_request} from '../../data/dataMethods'
|
|
3
|
+
import {_request, _requestObservable} from '../../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
5
|
import type {
|
|
6
6
|
AgentActionPath,
|
|
@@ -16,7 +16,7 @@ import type {AgentActionAsync, AgentActionSchema, AgentActionSync} from './commo
|
|
|
16
16
|
/** @beta */
|
|
17
17
|
export type PatchOperation = 'set' | 'append' | 'mixed' | 'unset'
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
20
20
|
type AnyNonNullable = Exclude<any, null | undefined>
|
|
21
21
|
|
|
22
22
|
/** @beta */
|
|
@@ -99,23 +99,26 @@ interface PatchTargetDocumentRequest<T extends Record<string, Any> = Record<stri
|
|
|
99
99
|
|
|
100
100
|
/** @beta */
|
|
101
101
|
export type PatchDocumentSync<T extends Record<string, Any> = Record<string, Any>> = (
|
|
102
|
-
|
|
102
|
+
| PatchExistingDocumentRequest
|
|
103
|
+
| PatchTargetDocumentRequest<T>
|
|
103
104
|
) &
|
|
104
105
|
PatchRequestBase &
|
|
105
106
|
AgentActionSync
|
|
106
107
|
|
|
107
108
|
/** @beta */
|
|
108
109
|
export type PatchDocumentAsync<T extends Record<string, Any> = Record<string, Any>> = (
|
|
109
|
-
|
|
110
|
+
| PatchExistingDocumentRequest
|
|
111
|
+
| PatchTargetDocumentRequest<T>
|
|
110
112
|
) &
|
|
111
113
|
PatchRequestBase &
|
|
112
114
|
AgentActionAsync
|
|
113
115
|
|
|
114
116
|
/** @beta */
|
|
115
117
|
export type PatchDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
116
|
-
|
|
118
|
+
| PatchDocumentSync<T>
|
|
119
|
+
| PatchDocumentAsync<T>
|
|
117
120
|
|
|
118
|
-
export function
|
|
121
|
+
export function _patchObservable<DocumentShape extends Record<string, Any>>(
|
|
119
122
|
client: SanityClient | ObservableSanityClient,
|
|
120
123
|
httpRequest: HttpRequest,
|
|
121
124
|
request: PatchDocument<DocumentShape>,
|
|
@@ -123,11 +126,28 @@ export function _patch<DocumentShape extends Record<string, Any>>(
|
|
|
123
126
|
(typeof request)['async'] extends true
|
|
124
127
|
? {_id: string}
|
|
125
128
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
129
|
+
> {
|
|
130
|
+
const dataset = hasDataset(client.config())
|
|
131
|
+
return _requestObservable(client, httpRequest, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
url: `/agent/action/patch/${dataset}`,
|
|
134
|
+
body: request,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function _patch<DocumentShape extends Record<string, Any>>(
|
|
139
|
+
client: SanityClient | ObservableSanityClient,
|
|
140
|
+
httpRequest: HttpRequest,
|
|
141
|
+
request: PatchDocument<DocumentShape>,
|
|
142
|
+
): Promise<
|
|
143
|
+
(typeof request)['async'] extends true
|
|
144
|
+
? {_id: string}
|
|
145
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
126
146
|
> {
|
|
127
147
|
const dataset = hasDataset(client.config())
|
|
128
148
|
return _request(client, httpRequest, {
|
|
129
149
|
method: 'POST',
|
|
130
|
-
|
|
150
|
+
url: `/agent/action/patch/${dataset}`,
|
|
131
151
|
body: request,
|
|
132
152
|
})
|
|
133
153
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {_request} from '../../data/dataMethods'
|
|
3
|
+
import {_request, _requestObservable} from '../../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
5
|
import type {AgentActionParams, Any, HttpRequest} from '../../types'
|
|
6
6
|
import {hasDataset} from '../../validators'
|
|
@@ -101,7 +101,7 @@ export interface PromptRequestBase {
|
|
|
101
101
|
* @beta
|
|
102
102
|
*/
|
|
103
103
|
// need the unused generic here to allow for optional callsite casting
|
|
104
|
-
//
|
|
104
|
+
// oxlint-disable-next-line no-unused-vars
|
|
105
105
|
interface PromptJsonResponse<T extends Record<string, Any> = Record<string, Any>> {
|
|
106
106
|
/**
|
|
107
107
|
*
|
|
@@ -120,19 +120,33 @@ interface PromptTextResponse {
|
|
|
120
120
|
|
|
121
121
|
/** @beta */
|
|
122
122
|
export type PromptRequest<T extends Record<string, Any> = Record<string, Any>> = (
|
|
123
|
-
|
|
123
|
+
| PromptTextResponse
|
|
124
|
+
| PromptJsonResponse<T>
|
|
124
125
|
) &
|
|
125
126
|
PromptRequestBase
|
|
126
127
|
|
|
127
|
-
export function
|
|
128
|
+
export function _promptObservable<const DocumentShape extends Record<string, Any>>(
|
|
128
129
|
client: SanityClient | ObservableSanityClient,
|
|
129
130
|
httpRequest: HttpRequest,
|
|
130
131
|
request: PromptRequest<DocumentShape>,
|
|
131
132
|
): Observable<(typeof request)['format'] extends 'json' ? DocumentShape : string> {
|
|
133
|
+
const dataset = hasDataset(client.config())
|
|
134
|
+
return _requestObservable(client, httpRequest, {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
url: `/agent/action/prompt/${dataset}`,
|
|
137
|
+
body: request,
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function _prompt<const DocumentShape extends Record<string, Any>>(
|
|
142
|
+
client: SanityClient | ObservableSanityClient,
|
|
143
|
+
httpRequest: HttpRequest,
|
|
144
|
+
request: PromptRequest<DocumentShape>,
|
|
145
|
+
): Promise<(typeof request)['format'] extends 'json' ? DocumentShape : string> {
|
|
132
146
|
const dataset = hasDataset(client.config())
|
|
133
147
|
return _request(client, httpRequest, {
|
|
134
148
|
method: 'POST',
|
|
135
|
-
|
|
149
|
+
url: `/agent/action/prompt/${dataset}`,
|
|
136
150
|
body: request,
|
|
137
151
|
})
|
|
138
152
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {_request} from '../../data/dataMethods'
|
|
3
|
+
import {_request, _requestObservable} from '../../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
5
|
import type {
|
|
6
6
|
AgentActionParams,
|
|
@@ -313,7 +313,7 @@ export interface TransformTarget extends AgentActionTarget {
|
|
|
313
313
|
|
|
314
314
|
/** @beta */
|
|
315
315
|
// need the generics to hold optional call-site response generics
|
|
316
|
-
//
|
|
316
|
+
// oxlint-disable-next-line no-unused-vars
|
|
317
317
|
export type TransformDocumentSync<T extends Record<string, Any> = Record<string, Any>> =
|
|
318
318
|
TransformRequestBase & AgentActionSync
|
|
319
319
|
|
|
@@ -322,9 +322,10 @@ export type TransformDocumentAsync = TransformRequestBase & AgentActionAsync
|
|
|
322
322
|
|
|
323
323
|
/** @beta */
|
|
324
324
|
export type TransformDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
325
|
-
TransformDocumentSync<T>
|
|
325
|
+
| TransformDocumentSync<T>
|
|
326
|
+
| TransformDocumentAsync
|
|
326
327
|
|
|
327
|
-
export function
|
|
328
|
+
export function _transformObservable<DocumentShape extends Record<string, Any>>(
|
|
328
329
|
client: SanityClient | ObservableSanityClient,
|
|
329
330
|
httpRequest: HttpRequest,
|
|
330
331
|
request: TransformDocument<DocumentShape>,
|
|
@@ -332,11 +333,28 @@ export function _transform<DocumentShape extends Record<string, Any>>(
|
|
|
332
333
|
(typeof request)['async'] extends true
|
|
333
334
|
? {_id: string}
|
|
334
335
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
336
|
+
> {
|
|
337
|
+
const dataset = hasDataset(client.config())
|
|
338
|
+
return _requestObservable(client, httpRequest, {
|
|
339
|
+
method: 'POST',
|
|
340
|
+
url: `/agent/action/transform/${dataset}`,
|
|
341
|
+
body: request,
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function _transform<DocumentShape extends Record<string, Any>>(
|
|
346
|
+
client: SanityClient | ObservableSanityClient,
|
|
347
|
+
httpRequest: HttpRequest,
|
|
348
|
+
request: TransformDocument<DocumentShape>,
|
|
349
|
+
): Promise<
|
|
350
|
+
(typeof request)['async'] extends true
|
|
351
|
+
? {_id: string}
|
|
352
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
335
353
|
> {
|
|
336
354
|
const dataset = hasDataset(client.config())
|
|
337
355
|
return _request(client, httpRequest, {
|
|
338
356
|
method: 'POST',
|
|
339
|
-
|
|
357
|
+
url: `/agent/action/transform/${dataset}`,
|
|
340
358
|
body: request,
|
|
341
359
|
})
|
|
342
360
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {_request} from '../../data/dataMethods'
|
|
3
|
+
import {_request, _requestObservable} from '../../data/dataMethods'
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
5
|
import type {
|
|
6
6
|
AgentActionParams,
|
|
@@ -138,7 +138,7 @@ export interface TranslateTarget extends AgentActionTarget {
|
|
|
138
138
|
|
|
139
139
|
/** @beta */
|
|
140
140
|
// need the generics to hold optional call-site response generics
|
|
141
|
-
//
|
|
141
|
+
// oxlint-disable-next-line no-unused-vars
|
|
142
142
|
export type TranslateDocumentSync<T extends Record<string, Any> = Record<string, Any>> =
|
|
143
143
|
TranslateRequestBase & AgentActionSync
|
|
144
144
|
|
|
@@ -147,9 +147,10 @@ export type TranslateDocumentAsync = TranslateRequestBase & AgentActionAsync
|
|
|
147
147
|
|
|
148
148
|
/** @beta */
|
|
149
149
|
export type TranslateDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
150
|
-
TranslateDocumentSync<T>
|
|
150
|
+
| TranslateDocumentSync<T>
|
|
151
|
+
| TranslateDocumentAsync
|
|
151
152
|
|
|
152
|
-
export function
|
|
153
|
+
export function _translateObservable<DocumentShape extends Record<string, Any>>(
|
|
153
154
|
client: SanityClient | ObservableSanityClient,
|
|
154
155
|
httpRequest: HttpRequest,
|
|
155
156
|
request: TranslateDocument<DocumentShape>,
|
|
@@ -157,11 +158,28 @@ export function _translate<DocumentShape extends Record<string, Any>>(
|
|
|
157
158
|
(typeof request)['async'] extends true
|
|
158
159
|
? {_id: string}
|
|
159
160
|
: IdentifiedSanityDocumentStub & DocumentShape
|
|
161
|
+
> {
|
|
162
|
+
const dataset = hasDataset(client.config())
|
|
163
|
+
return _requestObservable(client, httpRequest, {
|
|
164
|
+
method: 'POST',
|
|
165
|
+
url: `/agent/action/translate/${dataset}`,
|
|
166
|
+
body: request,
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function _translate<DocumentShape extends Record<string, Any>>(
|
|
171
|
+
client: SanityClient | ObservableSanityClient,
|
|
172
|
+
httpRequest: HttpRequest,
|
|
173
|
+
request: TranslateDocument<DocumentShape>,
|
|
174
|
+
): Promise<
|
|
175
|
+
(typeof request)['async'] extends true
|
|
176
|
+
? {_id: string}
|
|
177
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
160
178
|
> {
|
|
161
179
|
const dataset = hasDataset(client.config())
|
|
162
180
|
return _request(client, httpRequest, {
|
|
163
181
|
method: 'POST',
|
|
164
|
-
|
|
182
|
+
url: `/agent/action/translate/${dataset}`,
|
|
165
183
|
body: request,
|
|
166
184
|
})
|
|
167
185
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import {lastValueFrom, type Observable} from 'rxjs'
|
|
2
|
-
import {filter, map} from 'rxjs/operators'
|
|
1
|
+
import {defer, lastValueFrom, type Observable} from 'rxjs'
|
|
2
|
+
import {filter, map, mergeAll} from 'rxjs/operators'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {_prepareRequest, _uploadObservable} from '../data/dataMethods'
|
|
5
|
+
import type {FetchRequest} from '../http/requestOptions'
|
|
5
6
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
6
7
|
import type {
|
|
7
8
|
Any,
|
|
8
9
|
HttpRequest,
|
|
9
|
-
HttpRequestEvent,
|
|
10
10
|
InitializedClientConfig,
|
|
11
|
-
|
|
11
|
+
MediaLibraryAssetDocument,
|
|
12
12
|
SanityAssetDocument,
|
|
13
13
|
SanityImageAssetDocument,
|
|
14
14
|
UploadBody,
|
|
15
15
|
UploadClientConfig,
|
|
16
|
+
UploadEvent,
|
|
17
|
+
UploadResponseEvent,
|
|
16
18
|
} from '../types'
|
|
17
19
|
import * as validators from '../validators'
|
|
18
20
|
|
|
@@ -36,7 +38,7 @@ export class ObservableAssetsClient {
|
|
|
36
38
|
assetType: 'file',
|
|
37
39
|
body: UploadBody,
|
|
38
40
|
options?: UploadClientConfig,
|
|
39
|
-
): Observable<
|
|
41
|
+
): Observable<UploadEvent<{document: SanityAssetDocument}>>
|
|
40
42
|
|
|
41
43
|
/**
|
|
42
44
|
* Uploads an image asset to the configured dataset
|
|
@@ -49,7 +51,7 @@ export class ObservableAssetsClient {
|
|
|
49
51
|
assetType: 'image',
|
|
50
52
|
body: UploadBody,
|
|
51
53
|
options?: UploadClientConfig,
|
|
52
|
-
): Observable<
|
|
54
|
+
): Observable<UploadEvent<{document: SanityImageAssetDocument}>>
|
|
53
55
|
/**
|
|
54
56
|
* Uploads a file or an image asset to the configured dataset
|
|
55
57
|
*
|
|
@@ -61,12 +63,17 @@ export class ObservableAssetsClient {
|
|
|
61
63
|
assetType: 'file' | 'image',
|
|
62
64
|
body: UploadBody,
|
|
63
65
|
options?: UploadClientConfig,
|
|
64
|
-
): Observable<
|
|
66
|
+
): Observable<UploadEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>>
|
|
65
67
|
upload(
|
|
66
68
|
assetType: 'file' | 'image',
|
|
67
69
|
body: UploadBody,
|
|
68
70
|
options?: UploadClientConfig,
|
|
69
|
-
): Observable<
|
|
71
|
+
): Observable<
|
|
72
|
+
UploadEvent<
|
|
73
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
74
|
+
| {asset: MediaLibraryAssetDocument}
|
|
75
|
+
>
|
|
76
|
+
> {
|
|
70
77
|
return _upload(this.#client, this.#httpRequest, assetType, body, options)
|
|
71
78
|
}
|
|
72
79
|
}
|
|
@@ -83,6 +90,16 @@ export class AssetsClient {
|
|
|
83
90
|
/**
|
|
84
91
|
* Uploads a file asset to the configured dataset
|
|
85
92
|
*
|
|
93
|
+
* Note: when the client is configured against a Media Library
|
|
94
|
+
* (`resource: {type: 'media-library', id}`), this resolves to a
|
|
95
|
+
* {@link MediaLibraryAssetDocument} at runtime, not to a
|
|
96
|
+
* {@link SanityAssetDocument}. The declared type cannot express that: the
|
|
97
|
+
* shape depends on the client's configuration rather than on the arguments,
|
|
98
|
+
* so an overload cannot discriminate it, and widening the return type into a
|
|
99
|
+
* union would be a breaking change for every existing caller. Narrow the
|
|
100
|
+
* result yourself (for example, check for `currentVersion`) if you upload to
|
|
101
|
+
* a Media Library. Typing this honestly is deferred to the next major.
|
|
102
|
+
*
|
|
86
103
|
* @param assetType - Asset type (file)
|
|
87
104
|
* @param body - Asset content - can be a browser File instance, a Blob, a Node.js Buffer instance or a Node.js ReadableStream.
|
|
88
105
|
* @param options - Options to use for the upload
|
|
@@ -95,6 +112,10 @@ export class AssetsClient {
|
|
|
95
112
|
/**
|
|
96
113
|
* Uploads an image asset to the configured dataset
|
|
97
114
|
*
|
|
115
|
+
* Note: against a Media Library this resolves to a
|
|
116
|
+
* {@link MediaLibraryAssetDocument} at runtime. See the `'file'` overload
|
|
117
|
+
* above for why the declared type cannot say so.
|
|
118
|
+
*
|
|
98
119
|
* @param assetType - Asset type (image)
|
|
99
120
|
* @param body - Asset content - can be a browser File instance, a Blob, a Node.js Buffer instance or a Node.js ReadableStream.
|
|
100
121
|
* @param options - Options to use for the upload
|
|
@@ -120,28 +141,54 @@ export class AssetsClient {
|
|
|
120
141
|
assetType: 'file' | 'image',
|
|
121
142
|
body: UploadBody,
|
|
122
143
|
options?: UploadClientConfig,
|
|
123
|
-
): Promise<SanityAssetDocument | SanityImageAssetDocument> {
|
|
124
|
-
|
|
144
|
+
): Promise<SanityAssetDocument | SanityImageAssetDocument | MediaLibraryAssetDocument> {
|
|
145
|
+
type Doc =
|
|
146
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
147
|
+
| {asset: MediaLibraryAssetDocument}
|
|
148
|
+
const observable = _upload<Doc>(this.#client, this.#httpRequest, assetType, body, options)
|
|
125
149
|
return lastValueFrom(
|
|
126
150
|
observable.pipe(
|
|
127
|
-
filter((event:
|
|
128
|
-
map(
|
|
129
|
-
(event) =>
|
|
130
|
-
(event as ResponseEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>)
|
|
131
|
-
.body.document,
|
|
132
|
-
),
|
|
151
|
+
filter((event): event is UploadResponseEvent<Doc> => event.type === 'response'),
|
|
152
|
+
map((event) => pluckUploadedAsset(event.body)),
|
|
133
153
|
),
|
|
134
154
|
)
|
|
135
155
|
}
|
|
136
156
|
}
|
|
137
157
|
|
|
138
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Content Lake's upload endpoint responds with `{document: ...}`; the Media
|
|
160
|
+
* Library upload endpoint responds with `{asset: ...}` instead (a
|
|
161
|
+
* `sanity.asset` document, not a Content Lake asset document). Narrowing on
|
|
162
|
+
* the response body itself - rather than on the client's `resource` config -
|
|
163
|
+
* keeps this correct regardless of how the two are ever wired together.
|
|
164
|
+
*/
|
|
165
|
+
function isMediaLibraryUploadBody(
|
|
166
|
+
body:
|
|
167
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
168
|
+
| {asset: MediaLibraryAssetDocument},
|
|
169
|
+
): body is {asset: MediaLibraryAssetDocument} {
|
|
170
|
+
return 'asset' in body
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function pluckUploadedAsset(
|
|
174
|
+
body:
|
|
175
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
176
|
+
| {asset: MediaLibraryAssetDocument},
|
|
177
|
+
): SanityAssetDocument | SanityImageAssetDocument | MediaLibraryAssetDocument {
|
|
178
|
+
return isMediaLibraryUploadBody(body) ? body.asset : body.document
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function _upload<
|
|
182
|
+
T =
|
|
183
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
184
|
+
| {asset: MediaLibraryAssetDocument},
|
|
185
|
+
>(
|
|
139
186
|
client: SanityClient | ObservableSanityClient,
|
|
140
|
-
|
|
187
|
+
_httpRequest: HttpRequest,
|
|
141
188
|
assetType: 'image' | 'file',
|
|
142
189
|
body: UploadBody,
|
|
143
190
|
opts: UploadClientConfig = {},
|
|
144
|
-
): Observable<
|
|
191
|
+
): Observable<UploadEvent<T>> {
|
|
145
192
|
validators.validateAssetType(assetType)
|
|
146
193
|
|
|
147
194
|
// If an empty array is given, explicitly set `none` to override API defaults
|
|
@@ -180,15 +227,65 @@ function _upload(
|
|
|
180
227
|
query.sourceUrl = source.url
|
|
181
228
|
}
|
|
182
229
|
|
|
183
|
-
|
|
230
|
+
const headers: Record<string, string> = options.contentType
|
|
231
|
+
? {'Content-Type': options.contentType}
|
|
232
|
+
: {}
|
|
233
|
+
const baseRequest = {
|
|
184
234
|
tag,
|
|
185
235
|
method: 'POST',
|
|
236
|
+
// Uploads have NO timeout unless the caller opts in — uploads can
|
|
237
|
+
// legitimately be slow. `0` translates to "disabled" at the request
|
|
238
|
+
// boundary, which also shields uploads from get-it's default timeout.
|
|
186
239
|
timeout: options.timeout || 0,
|
|
187
|
-
|
|
188
|
-
headers
|
|
240
|
+
url: buildAssetUploadUrl(config, assetType),
|
|
241
|
+
headers,
|
|
189
242
|
query,
|
|
190
243
|
body,
|
|
191
|
-
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// In browsers, run uploads through `XMLHttpRequest` so we can surface
|
|
247
|
+
// per-chunk upload progress events — fetch (and therefore get-it v9) has no
|
|
248
|
+
// equivalent hook. Outside the browser (Node, edge runtimes), we fall back
|
|
249
|
+
// to the regular fetch-based path which only emits the terminal `response`
|
|
250
|
+
// event.
|
|
251
|
+
if (typeof XMLHttpRequest !== 'undefined') {
|
|
252
|
+
return defer(async () => {
|
|
253
|
+
const {uploadWithProgress} = await import('../http/browserUpload')
|
|
254
|
+
// Build the request the same way the fetch path does, so the request
|
|
255
|
+
// tag (incl. `requestTagPrefix` and validation), auth/custom headers,
|
|
256
|
+
// credentials and timeout are identical across both upload transports.
|
|
257
|
+
// The XHR API needs the query baked into the URL, though.
|
|
258
|
+
const req = _prepareRequest(client, {...baseRequest})
|
|
259
|
+
return uploadWithProgress<T>({
|
|
260
|
+
url: appendQuery(req.url, req.query),
|
|
261
|
+
method: req.method ?? 'POST',
|
|
262
|
+
headers: req.headers,
|
|
263
|
+
body,
|
|
264
|
+
withCredentials: req.credentials === 'include',
|
|
265
|
+
// XHR only has a single total-deadline timer, so a structured
|
|
266
|
+
// get-it timeout collapses to its `total` component here.
|
|
267
|
+
timeout: typeof req.timeout === 'object' ? req.timeout.total : req.timeout,
|
|
268
|
+
signal: req.signal,
|
|
269
|
+
})
|
|
270
|
+
}).pipe(mergeAll())
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return _uploadObservable<T>(client, baseRequest)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function appendQuery(url: string, query: FetchRequest['query']): string {
|
|
277
|
+
if (!query) return url
|
|
278
|
+
const params =
|
|
279
|
+
query instanceof URLSearchParams
|
|
280
|
+
? query
|
|
281
|
+
: new URLSearchParams(
|
|
282
|
+
Object.entries(query).flatMap(([key, value]) =>
|
|
283
|
+
value === undefined || value === null ? [] : [[key, `${value}`]],
|
|
284
|
+
),
|
|
285
|
+
)
|
|
286
|
+
const qs = params.toString()
|
|
287
|
+
if (!qs) return url
|
|
288
|
+
return url + (url.includes('?') ? '&' : '?') + qs
|
|
192
289
|
}
|
|
193
290
|
|
|
194
291
|
function buildAssetUploadUrl(config: InitializedClientConfig, assetType: 'image' | 'file'): string {
|