@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.
Files changed (133) hide show
  1. package/README.md +63 -341
  2. package/dist/browserUpload-2tz6Sdqp.js +87 -0
  3. package/dist/browserUpload-2tz6Sdqp.js.map +1 -0
  4. package/dist/browserUpload-CwpNx7Vl.js +87 -0
  5. package/dist/browserUpload-CwpNx7Vl.js.map +1 -0
  6. package/dist/config-3wiPP-sZ.js +126 -0
  7. package/dist/config-3wiPP-sZ.js.map +1 -0
  8. package/dist/csm.d.ts +122 -335
  9. package/dist/csm.js +79 -91
  10. package/dist/csm.js.map +1 -1
  11. package/dist/dist-C9ExSk2R.js +73 -0
  12. package/dist/dist-C9ExSk2R.js.map +1 -0
  13. package/dist/dist-Z8cIRxoB.js +69 -0
  14. package/dist/dist-Z8cIRxoB.js.map +1 -0
  15. package/dist/index.d.ts +112 -6722
  16. package/dist/index.js +2950 -2848
  17. package/dist/index.js.map +1 -1
  18. package/dist/{index.d.cts → index.node.d.ts} +4529 -5322
  19. package/dist/index.node.js +3264 -0
  20. package/dist/index.node.js.map +1 -0
  21. package/dist/isRecord-Kfmt-nk-.js +7 -0
  22. package/dist/isRecord-Kfmt-nk-.js.map +1 -0
  23. package/dist/media-library.d.ts +9 -100
  24. package/dist/media-library.js +36 -14
  25. package/dist/media-library.js.map +1 -1
  26. package/dist/request-BhMuKj0D.js +373 -0
  27. package/dist/request-BhMuKj0D.js.map +1 -0
  28. package/dist/request-SnMg7nUX.js +355 -0
  29. package/dist/request-SnMg7nUX.js.map +1 -0
  30. package/dist/resolveEditInfo-Cz-smq3a.js +339 -0
  31. package/dist/resolveEditInfo-Cz-smq3a.js.map +1 -0
  32. package/dist/rolldown-runtime-vyAXikos.js +9 -0
  33. package/dist/stega.d.ts +78 -7137
  34. package/dist/stega.js +27 -20
  35. package/dist/stega.js.map +1 -1
  36. package/dist/stegaClean-C18wLWau.js +21 -0
  37. package/dist/stegaClean-C18wLWau.js.map +1 -0
  38. package/dist/stegaEncodeSourceMap-DbM2fTN4.js +373 -0
  39. package/dist/stegaEncodeSourceMap-DbM2fTN4.js.map +1 -0
  40. package/dist/stegaEncodeSourceMap-YR3NQ3iz.js +163 -0
  41. package/dist/stegaEncodeSourceMap-YR3NQ3iz.js.map +1 -0
  42. package/dist/{index.browser.d.cts → types-BODIEY7F.d.ts} +4196 -5258
  43. package/dist/types-CfGzbXrl.d.ts +108 -0
  44. package/package.json +80 -142
  45. package/src/SanityClient.ts +101 -137
  46. package/src/agent/actions/AgentActionsClient.ts +12 -12
  47. package/src/agent/actions/commonTypes.ts +2 -1
  48. package/src/agent/actions/generate.ts +26 -6
  49. package/src/agent/actions/patch.ts +27 -7
  50. package/src/agent/actions/prompt.ts +19 -5
  51. package/src/agent/actions/transform.ts +23 -5
  52. package/src/agent/actions/translate.ts +23 -5
  53. package/src/assets/AssetsClient.ts +121 -24
  54. package/src/csm/applySourceDocuments.ts +5 -5
  55. package/src/csm/createEditUrl.ts +1 -1
  56. package/src/csm/draftUtils.ts +23 -5
  57. package/src/csm/index.ts +2 -2
  58. package/src/csm/studioPath.ts +2 -1
  59. package/src/csm/types.ts +8 -2
  60. package/src/data/dataMethods.ts +784 -349
  61. package/src/data/eventsource.ts +82 -49
  62. package/src/data/listen.ts +18 -23
  63. package/src/data/live.ts +60 -35
  64. package/src/data/resolveEventSourceFetch.ts +104 -0
  65. package/src/datasets/DatasetsClient.ts +58 -41
  66. package/src/defineCreateClient.ts +38 -22
  67. package/src/functions/FunctionsClient.ts +66 -0
  68. package/src/functions/invoke.ts +176 -0
  69. package/src/http/browserMiddleware.ts +7 -1
  70. package/src/http/browserUpload.ts +163 -0
  71. package/src/http/errors.ts +61 -6
  72. package/src/http/nodeMiddleware.ts +94 -30
  73. package/src/http/request.ts +304 -87
  74. package/src/http/requestOptions.ts +131 -15
  75. package/src/{index.browser.ts → index.node.ts} +9 -4
  76. package/src/index.ts +4 -3
  77. package/src/mediaLibrary/MediaLibraryVideoClient.ts +27 -12
  78. package/src/projects/ProjectsClient.ts +18 -16
  79. package/src/releases/ReleasesClient.ts +38 -33
  80. package/src/stega/index.ts +1 -2
  81. package/src/stega/stegaEncodeSourceMap.ts +5 -0
  82. package/src/stega/types.ts +2 -2
  83. package/src/types.ts +328 -103
  84. package/src/users/UsersClient.ts +10 -14
  85. package/src/util/shareReplayLatest.ts +1 -1
  86. package/src/validators.ts +1 -1
  87. package/src/warnings.ts +13 -2
  88. package/dist/_chunks-cjs/config.cjs +0 -206
  89. package/dist/_chunks-cjs/config.cjs.map +0 -1
  90. package/dist/_chunks-cjs/isRecord.cjs +0 -6
  91. package/dist/_chunks-cjs/isRecord.cjs.map +0 -1
  92. package/dist/_chunks-cjs/resolveEditInfo.cjs +0 -334
  93. package/dist/_chunks-cjs/resolveEditInfo.cjs.map +0 -1
  94. package/dist/_chunks-cjs/stegaClean.cjs +0 -46
  95. package/dist/_chunks-cjs/stegaClean.cjs.map +0 -1
  96. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +0 -388
  97. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +0 -1
  98. package/dist/_chunks-es/config.js +0 -207
  99. package/dist/_chunks-es/config.js.map +0 -1
  100. package/dist/_chunks-es/isRecord.js +0 -7
  101. package/dist/_chunks-es/isRecord.js.map +0 -1
  102. package/dist/_chunks-es/resolveEditInfo.js +0 -335
  103. package/dist/_chunks-es/resolveEditInfo.js.map +0 -1
  104. package/dist/_chunks-es/stegaClean.js +0 -47
  105. package/dist/_chunks-es/stegaClean.js.map +0 -1
  106. package/dist/_chunks-es/stegaEncodeSourceMap.js +0 -389
  107. package/dist/_chunks-es/stegaEncodeSourceMap.js.map +0 -1
  108. package/dist/csm.cjs +0 -99
  109. package/dist/csm.cjs.map +0 -1
  110. package/dist/csm.d.cts +0 -391
  111. package/dist/index.browser.cjs +0 -3106
  112. package/dist/index.browser.cjs.map +0 -1
  113. package/dist/index.browser.d.ts +0 -6802
  114. package/dist/index.browser.js +0 -3082
  115. package/dist/index.browser.js.map +0 -1
  116. package/dist/index.cjs +0 -2946
  117. package/dist/index.cjs.map +0 -1
  118. package/dist/media-library.cjs +0 -20
  119. package/dist/media-library.cjs.map +0 -1
  120. package/dist/media-library.d.cts +0 -117
  121. package/dist/stega.browser.cjs +0 -29
  122. package/dist/stega.browser.cjs.map +0 -1
  123. package/dist/stega.browser.d.cts +0 -7200
  124. package/dist/stega.browser.d.ts +0 -7200
  125. package/dist/stega.browser.js +0 -24
  126. package/dist/stega.browser.js.map +0 -1
  127. package/dist/stega.cjs +0 -29
  128. package/dist/stega.cjs.map +0 -1
  129. package/dist/stega.d.cts +0 -7200
  130. package/src/data/eventsourcePolyfill.ts +0 -7
  131. package/umd/.gitkeep +0 -1
  132. package/umd/sanityClient.js +0 -6543
  133. package/umd/sanityClient.min.js +0 -6
@@ -1,12 +1,12 @@
1
- import {lastValueFrom, type Observable} from 'rxjs'
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 _generate(this.#client, this.#httpRequest, request)
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 _transform(this.#client, this.#httpRequest, request)
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 _translate(this.#client, this.#httpRequest, request)
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 lastValueFrom(_generate(this.#client, this.#httpRequest, request))
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 lastValueFrom(_transform(this.#client, this.#httpRequest, request))
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 lastValueFrom(_translate(this.#client, this.#httpRequest, request))
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 lastValueFrom(_prompt(this.#client, this.#httpRequest, request))
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 lastValueFrom(_patch(this.#client, this.#httpRequest, request))
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} | {exclude: string[]; include?: 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
- GenerateExistingDocumentRequest | GenerateTargetDocumentRequest<T>
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
- GenerateExistingDocumentRequest | GenerateTargetDocumentRequest<T>
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
- GenerateSyncInstruction<T> | GenerateAsyncInstruction<T>
290
+ | GenerateSyncInstruction<T>
291
+ | GenerateAsyncInstruction<T>
289
292
 
290
- export function _generate<DocumentShape extends Record<string, Any>>(
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
- uri: `/agent/action/generate/${dataset}`,
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
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
- PatchExistingDocumentRequest | PatchTargetDocumentRequest<T>
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
- PatchExistingDocumentRequest | PatchTargetDocumentRequest<T>
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
- PatchDocumentSync<T> | PatchDocumentAsync<T>
118
+ | PatchDocumentSync<T>
119
+ | PatchDocumentAsync<T>
117
120
 
118
- export function _patch<DocumentShape extends Record<string, Any>>(
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
- uri: `/agent/action/patch/${dataset}`,
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
- // eslint-disable-next-line unused-imports/no-unused-vars
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
- PromptTextResponse | PromptJsonResponse<T>
123
+ | PromptTextResponse
124
+ | PromptJsonResponse<T>
124
125
  ) &
125
126
  PromptRequestBase
126
127
 
127
- export function _prompt<const DocumentShape extends Record<string, Any>>(
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
- uri: `/agent/action/prompt/${dataset}`,
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
- // eslint-disable-next-line unused-imports/no-unused-vars
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> | TransformDocumentAsync
325
+ | TransformDocumentSync<T>
326
+ | TransformDocumentAsync
326
327
 
327
- export function _transform<DocumentShape extends Record<string, Any>>(
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
- uri: `/agent/action/transform/${dataset}`,
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
- // eslint-disable-next-line unused-imports/no-unused-vars
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> | TranslateDocumentAsync
150
+ | TranslateDocumentSync<T>
151
+ | TranslateDocumentAsync
151
152
 
152
- export function _translate<DocumentShape extends Record<string, Any>>(
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
- uri: `/agent/action/translate/${dataset}`,
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 {_requestObservable} from '../data/dataMethods'
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
- ResponseEvent,
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<HttpRequestEvent<{document: SanityAssetDocument}>>
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<HttpRequestEvent<{document: SanityImageAssetDocument}>>
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<HttpRequestEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>>
66
+ ): Observable<UploadEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>>
65
67
  upload(
66
68
  assetType: 'file' | 'image',
67
69
  body: UploadBody,
68
70
  options?: UploadClientConfig,
69
- ): Observable<HttpRequestEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>> {
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
- const observable = _upload(this.#client, this.#httpRequest, assetType, body, options)
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: Any) => event.type === 'response'),
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
- function _upload(
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
- httpRequest: HttpRequest,
187
+ _httpRequest: HttpRequest,
141
188
  assetType: 'image' | 'file',
142
189
  body: UploadBody,
143
190
  opts: UploadClientConfig = {},
144
- ): Observable<HttpRequestEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>> {
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
- return _requestObservable(client, httpRequest, {
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
- uri: buildAssetUploadUrl(config, assetType),
188
- headers: options.contentType ? {'Content-Type': options.contentType} : {},
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 {