@sanity/client 8.0.0 → 8.2.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 +265 -0
- package/dist/{browserUpload-CQgx9YYo.js → browserUpload-2tz6Sdqp.js} +4 -3
- package/dist/browserUpload-2tz6Sdqp.js.map +1 -0
- package/dist/{browserUpload-icWlVP15.js → browserUpload-CwpNx7Vl.js} +4 -3
- package/dist/browserUpload-CwpNx7Vl.js.map +1 -0
- package/dist/{config-a8VajuEY.js → config-3wiPP-sZ.js} +2 -2
- package/dist/config-3wiPP-sZ.js.map +1 -0
- package/dist/csm.js +2 -2
- package/dist/csm.js.map +1 -1
- package/dist/index.d.ts +17 -11
- package/dist/index.js +610 -136
- package/dist/index.js.map +1 -1
- package/dist/index.node.d.ts +918 -26
- package/dist/index.node.js +559 -66
- package/dist/index.node.js.map +1 -1
- package/dist/media-library.d.ts +1 -1
- package/dist/{request-CJxcN16k.js → request-BhMuKj0D.js} +10 -9
- package/dist/request-BhMuKj0D.js.map +1 -0
- package/dist/{request-k7VS_NnC.js → request-SnMg7nUX.js} +10 -9
- package/dist/request-SnMg7nUX.js.map +1 -0
- package/dist/{resolveEditInfo-sq7yF78q.js → resolveEditInfo-Cz-smq3a.js} +17 -3
- package/dist/resolveEditInfo-Cz-smq3a.js.map +1 -0
- package/dist/stega.js +1 -1
- package/dist/{stegaEncodeSourceMap-DkoIlutY.js → stegaEncodeSourceMap-DbM2fTN4.js} +8 -2
- package/dist/stegaEncodeSourceMap-DbM2fTN4.js.map +1 -0
- package/dist/{stegaEncodeSourceMap-B2fGArSf.js → stegaEncodeSourceMap-YR3NQ3iz.js} +2 -2
- package/dist/{stegaEncodeSourceMap-B2fGArSf.js.map → stegaEncodeSourceMap-YR3NQ3iz.js.map} +1 -1
- package/dist/{types-CUxZSgB2.d.ts → types-nJhm5Nyq.d.ts} +910 -24
- package/package.json +26 -11
- package/src/SanityClient.ts +39 -20
- package/src/assets/AssetsClient.ts +54 -5
- package/src/collaboration/CollaborationCommentsClient.ts +387 -0
- package/src/collaboration/comments.ts +313 -0
- package/src/collaboration/types.ts +252 -0
- package/src/csm/applySourceDocuments.ts +2 -4
- package/src/csm/draftUtils.ts +23 -4
- package/src/data/dataMethods.ts +9 -20
- package/src/data/eventsource.ts +71 -41
- package/src/data/listen.ts +20 -5
- package/src/data/live.ts +17 -9
- package/src/data/resolveEventSourceFetch.ts +9 -1
- package/src/defineCreateClient.ts +5 -1
- package/src/functions/FunctionsClient.ts +66 -0
- package/src/functions/invoke.ts +176 -0
- package/src/http/browserUpload.ts +1 -0
- package/src/http/errors.ts +2 -1
- package/src/http/request.ts +8 -14
- package/src/mediaLibrary/MediaLibraryVideoClient.ts +1 -1
- package/src/types.ts +420 -4
- package/src/validators.ts +1 -1
- package/src/warnings.ts +7 -1
- package/dist/browserUpload-CQgx9YYo.js.map +0 -1
- package/dist/browserUpload-icWlVP15.js.map +0 -1
- package/dist/config-a8VajuEY.js.map +0 -1
- package/dist/request-CJxcN16k.js.map +0 -1
- package/dist/request-k7VS_NnC.js.map +0 -1
- package/dist/resolveEditInfo-sq7yF78q.js.map +0 -1
- package/dist/stegaEncodeSourceMap-DkoIlutY.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -49,15 +49,15 @@
|
|
|
49
49
|
"./package.json": "./package.json"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"eventsource": "^5.
|
|
53
|
-
"get-it": "^9.
|
|
52
|
+
"eventsource": "^5.1.0",
|
|
53
|
+
"get-it": "^9.5.0",
|
|
54
54
|
"nanoid": "^6.0.1",
|
|
55
55
|
"obug": "^2.1.4",
|
|
56
56
|
"rxjs": "^7.8.2"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@changesets/cli": "^2.31.1",
|
|
60
|
-
"@cloudflare/vitest-pool-workers": "^0.
|
|
60
|
+
"@cloudflare/vitest-pool-workers": "^0.21.3",
|
|
61
61
|
"@edge-runtime/types": "^4.0.0",
|
|
62
62
|
"@edge-runtime/vm": "^5.0.0",
|
|
63
63
|
"@sanity/pkg-utils": "^12.1.2",
|
|
@@ -65,17 +65,22 @@
|
|
|
65
65
|
"@types/node": "^22.20.1",
|
|
66
66
|
"@types/react": "^19.2.18",
|
|
67
67
|
"@vercel/stega": "1.1.0",
|
|
68
|
+
"@vitest/browser": "^4.1.10",
|
|
69
|
+
"@vitest/browser-playwright": "^4.1.10",
|
|
68
70
|
"@vitest/coverage-v8": "^4.1.10",
|
|
69
71
|
"eventsource-encoder": "^1.0.2",
|
|
70
|
-
"happy-dom": "^20.11.
|
|
72
|
+
"happy-dom": "^20.11.2",
|
|
71
73
|
"json-diff": "^1.0.6",
|
|
74
|
+
"knip": "^6.32.2",
|
|
72
75
|
"ls-engines": "^0.10.1",
|
|
73
|
-
"next": "^16.3.
|
|
76
|
+
"next": "^16.3.1",
|
|
74
77
|
"oxfmt": "^0.63.0",
|
|
75
78
|
"oxlint": "^1.78.0",
|
|
76
79
|
"oxlint-tsgolint": "^7.0.2001",
|
|
77
|
-
"pkg-pr-new": "^0.0.
|
|
80
|
+
"pkg-pr-new": "^0.0.88",
|
|
81
|
+
"playwright": "^1.62.1",
|
|
78
82
|
"typescript": "7.0.2",
|
|
83
|
+
"undici": "^7.29.0",
|
|
79
84
|
"vitest": "^4.1.10"
|
|
80
85
|
},
|
|
81
86
|
"browserslist": "extends @sanity/browserslist-config",
|
|
@@ -92,13 +97,23 @@
|
|
|
92
97
|
"release": "changeset publish",
|
|
93
98
|
"test": "vitest",
|
|
94
99
|
"test:browser": "vitest run --config ./vitest.browser.config.ts",
|
|
95
|
-
"test:bun": "bun
|
|
96
|
-
"test:deno": "
|
|
100
|
+
"test:bun": "bun run vitest run",
|
|
101
|
+
"test:deno": "deno run -A --node-modules-dir=manual npm:vitest run --config ./vitest.deno.config.ts",
|
|
102
|
+
"test:happy-dom": "vitest run --config ./vitest.happy-dom.config.ts",
|
|
103
|
+
"test:integration": "vitest run --config ./vitest.integration.config.ts",
|
|
104
|
+
"test:integration:bun": "bun run vitest run --config ./vitest.integration.config.ts",
|
|
105
|
+
"test:integration:deno": "deno run -A --node-modules-dir=manual npm:vitest run --config ./vitest.integration.config.ts",
|
|
106
|
+
"test:integration:node": "vitest run --config ./vitest.integration.config.ts",
|
|
107
|
+
"test:integration:vercel-edge": "vitest run --config ./vitest.integration.vercel-edge.config.ts",
|
|
108
|
+
"test:integration:workerd": "vitest run --config ./vitest.integration.workerd.config.ts",
|
|
97
109
|
"test:next": "vitest run --config ./vitest.next.config.ts",
|
|
98
|
-
"test:
|
|
110
|
+
"test:packaging": "vitest run --config ./vitest.packaging.config.ts",
|
|
111
|
+
"test:react-server": "vitest run --config ./vitest.react-server.config.ts",
|
|
99
112
|
"test:vercel-edge": "vitest run --config ./vitest.vercel-edge.config.ts",
|
|
100
113
|
"test:workerd": "vitest run --config ./vitest.workerd.config.ts",
|
|
101
114
|
"format": "oxfmt",
|
|
102
|
-
"format:check": "oxfmt --check"
|
|
115
|
+
"format:check": "oxfmt --check",
|
|
116
|
+
"knip": "knip",
|
|
117
|
+
"typecheck": "tsc --noEmit"
|
|
103
118
|
}
|
|
104
119
|
}
|
package/src/SanityClient.ts
CHANGED
|
@@ -3,6 +3,10 @@ import {Observable} from 'rxjs'
|
|
|
3
3
|
|
|
4
4
|
import {AgentActionsClient, ObservableAgentsActionClient} from './agent/actions/AgentActionsClient'
|
|
5
5
|
import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
|
|
6
|
+
import {
|
|
7
|
+
CollaborationCommentsClient,
|
|
8
|
+
ObservableCollaborationCommentsClient,
|
|
9
|
+
} from './collaboration/CollaborationCommentsClient'
|
|
6
10
|
import {defaultConfig, initConfig} from './config'
|
|
7
11
|
import * as dataMethods from './data/dataMethods'
|
|
8
12
|
import {_listen} from './data/listen'
|
|
@@ -10,6 +14,7 @@ import {LiveClient} from './data/live'
|
|
|
10
14
|
import {ObservablePatch, Patch} from './data/patch'
|
|
11
15
|
import {ObservableTransaction, Transaction} from './data/transaction'
|
|
12
16
|
import {DatasetsClient, ObservableDatasetsClient} from './datasets/DatasetsClient'
|
|
17
|
+
import {FunctionsClient, ObservableFunctionsClient} from './functions/FunctionsClient'
|
|
13
18
|
import {
|
|
14
19
|
MediaLibraryVideoClient,
|
|
15
20
|
ObservableMediaLibraryVideoClient,
|
|
@@ -56,10 +61,12 @@ import {deriveDocumentVersionId, getDocumentVersionId} from './util/createVersio
|
|
|
56
61
|
export type {
|
|
57
62
|
_listen,
|
|
58
63
|
AssetsClient,
|
|
64
|
+
CollaborationCommentsClient,
|
|
59
65
|
DatasetsClient,
|
|
60
66
|
LiveClient,
|
|
61
67
|
MediaLibraryVideoClient,
|
|
62
68
|
ObservableAssetsClient,
|
|
69
|
+
ObservableCollaborationCommentsClient,
|
|
63
70
|
ObservableDatasetsClient,
|
|
64
71
|
ObservableMediaLibraryVideoClient,
|
|
65
72
|
ObservableProjectsClient,
|
|
@@ -81,6 +88,11 @@ export class ObservableSanityClient {
|
|
|
81
88
|
agent: {
|
|
82
89
|
action: ObservableAgentsActionClient
|
|
83
90
|
}
|
|
91
|
+
collaboration: {
|
|
92
|
+
/** @alpha */
|
|
93
|
+
comments: ObservableCollaborationCommentsClient
|
|
94
|
+
}
|
|
95
|
+
functions: ObservableFunctionsClient
|
|
84
96
|
releases: ObservableReleasesClient
|
|
85
97
|
|
|
86
98
|
/**
|
|
@@ -110,6 +122,10 @@ export class ObservableSanityClient {
|
|
|
110
122
|
this.agent = {
|
|
111
123
|
action: new ObservableAgentsActionClient(this, this.#httpRequest),
|
|
112
124
|
}
|
|
125
|
+
this.collaboration = {
|
|
126
|
+
comments: new ObservableCollaborationCommentsClient(this, this.#httpRequest),
|
|
127
|
+
}
|
|
128
|
+
this.functions = new ObservableFunctionsClient(this, this.#httpRequest)
|
|
113
129
|
this.releases = new ObservableReleasesClient(this, this.#httpRequest)
|
|
114
130
|
}
|
|
115
131
|
|
|
@@ -707,15 +723,12 @@ export class ObservableSanityClient {
|
|
|
707
723
|
delete(id: string, options: AllDocumentIdsMutationOptions): Observable<MultipleMutationResult>
|
|
708
724
|
/**
|
|
709
725
|
* Deletes a document with the given document ID.
|
|
710
|
-
* Returns an observable that resolves to the deleted document.
|
|
726
|
+
* Returns an observable that resolves to a mutation result object containing the deleted document ID.
|
|
711
727
|
*
|
|
712
728
|
* @param id - Document ID to delete
|
|
713
729
|
* @param options - Options for the mutation
|
|
714
730
|
*/
|
|
715
|
-
delete
|
|
716
|
-
id: string,
|
|
717
|
-
options?: BaseMutationOptions,
|
|
718
|
-
): Observable<SanityDocument<R>>
|
|
731
|
+
delete(id: string, options?: BaseMutationOptions): Observable<MultipleMutationResult>
|
|
719
732
|
/**
|
|
720
733
|
* Deletes one or more documents matching the given query or document ID.
|
|
721
734
|
* Returns an observable that resolves to first deleted document.
|
|
@@ -762,15 +775,15 @@ export class ObservableSanityClient {
|
|
|
762
775
|
): Observable<MultipleMutationResult>
|
|
763
776
|
/**
|
|
764
777
|
* Deletes one or more documents matching the given query or document ID.
|
|
765
|
-
* Returns an observable that resolves to
|
|
778
|
+
* Returns an observable that resolves to a mutation result object containing the document IDs that were deleted.
|
|
766
779
|
*
|
|
767
780
|
* @param selection - An object with either an `id` or `query` key defining what to delete
|
|
768
781
|
* @param options - Options for the mutation
|
|
769
782
|
*/
|
|
770
|
-
delete
|
|
783
|
+
delete(
|
|
771
784
|
selection: MutationSelection,
|
|
772
785
|
options?: BaseMutationOptions,
|
|
773
|
-
): Observable<
|
|
786
|
+
): Observable<MultipleMutationResult>
|
|
774
787
|
delete<R extends Record<string, Any> = Record<string, Any>>(
|
|
775
788
|
selection: string | MutationSelection,
|
|
776
789
|
options?:
|
|
@@ -1020,7 +1033,7 @@ export class ObservableSanityClient {
|
|
|
1020
1033
|
): Observable<MultipleMutationResult>
|
|
1021
1034
|
/**
|
|
1022
1035
|
* Perform mutation operations against the configured dataset
|
|
1023
|
-
* Returns an observable that resolves to the
|
|
1036
|
+
* Returns an observable that resolves to a mutation result object containing the mutated document IDs.
|
|
1024
1037
|
*
|
|
1025
1038
|
* @param operations - Mutation operations to execute
|
|
1026
1039
|
* @param options - Mutation options
|
|
@@ -1028,7 +1041,7 @@ export class ObservableSanityClient {
|
|
|
1028
1041
|
mutate<R extends Record<string, Any> = Record<string, Any>>(
|
|
1029
1042
|
operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
|
|
1030
1043
|
options?: BaseMutationOptions,
|
|
1031
|
-
): Observable<
|
|
1044
|
+
): Observable<MultipleMutationResult>
|
|
1032
1045
|
mutate<R extends Record<string, Any> = Record<string, Any>>(
|
|
1033
1046
|
operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
|
|
1034
1047
|
options?:
|
|
@@ -1148,6 +1161,11 @@ export class SanityClient {
|
|
|
1148
1161
|
agent: {
|
|
1149
1162
|
action: AgentActionsClient
|
|
1150
1163
|
}
|
|
1164
|
+
collaboration: {
|
|
1165
|
+
/** @alpha */
|
|
1166
|
+
comments: CollaborationCommentsClient
|
|
1167
|
+
}
|
|
1168
|
+
functions: FunctionsClient
|
|
1151
1169
|
releases: ReleasesClient
|
|
1152
1170
|
|
|
1153
1171
|
/**
|
|
@@ -1182,6 +1200,10 @@ export class SanityClient {
|
|
|
1182
1200
|
this.agent = {
|
|
1183
1201
|
action: new AgentActionsClient(this, this.#httpRequest),
|
|
1184
1202
|
}
|
|
1203
|
+
this.collaboration = {
|
|
1204
|
+
comments: new CollaborationCommentsClient(this, this.#httpRequest),
|
|
1205
|
+
}
|
|
1206
|
+
this.functions = new FunctionsClient(this, this.#httpRequest)
|
|
1185
1207
|
this.releases = new ReleasesClient(this, this.#httpRequest)
|
|
1186
1208
|
|
|
1187
1209
|
this.observable = new ObservableSanityClient(httpRequest, config)
|
|
@@ -1773,15 +1795,12 @@ export class SanityClient {
|
|
|
1773
1795
|
delete(id: string, options: AllDocumentIdsMutationOptions): Promise<MultipleMutationResult>
|
|
1774
1796
|
/**
|
|
1775
1797
|
* Deletes a document with the given document ID.
|
|
1776
|
-
* Returns a promise that resolves to the deleted document.
|
|
1798
|
+
* Returns a promise that resolves to a mutation result object containing the deleted document ID.
|
|
1777
1799
|
*
|
|
1778
1800
|
* @param id - Document ID to delete
|
|
1779
1801
|
* @param options - Options for the mutation
|
|
1780
1802
|
*/
|
|
1781
|
-
delete
|
|
1782
|
-
id: string,
|
|
1783
|
-
options?: BaseMutationOptions,
|
|
1784
|
-
): Promise<SanityDocument<R>>
|
|
1803
|
+
delete(id: string, options?: BaseMutationOptions): Promise<MultipleMutationResult>
|
|
1785
1804
|
/**
|
|
1786
1805
|
* Deletes one or more documents matching the given query or document ID.
|
|
1787
1806
|
* Returns a promise that resolves to first deleted document.
|
|
@@ -1828,15 +1847,15 @@ export class SanityClient {
|
|
|
1828
1847
|
): Promise<MultipleMutationResult>
|
|
1829
1848
|
/**
|
|
1830
1849
|
* Deletes one or more documents matching the given query or document ID.
|
|
1831
|
-
* Returns a promise that resolves to
|
|
1850
|
+
* Returns a promise that resolves to a mutation result object containing the document IDs that were deleted.
|
|
1832
1851
|
*
|
|
1833
1852
|
* @param selection - An object with either an `id` or `query` key defining what to delete
|
|
1834
1853
|
* @param options - Options for the mutation
|
|
1835
1854
|
*/
|
|
1836
|
-
delete
|
|
1855
|
+
delete(
|
|
1837
1856
|
selection: MutationSelection,
|
|
1838
1857
|
options?: BaseMutationOptions,
|
|
1839
|
-
): Promise<
|
|
1858
|
+
): Promise<MultipleMutationResult>
|
|
1840
1859
|
delete<R extends Record<string, Any> = Record<string, Any>>(
|
|
1841
1860
|
selection: string | MutationSelection,
|
|
1842
1861
|
options?:
|
|
@@ -2070,7 +2089,7 @@ export class SanityClient {
|
|
|
2070
2089
|
): Promise<MultipleMutationResult>
|
|
2071
2090
|
/**
|
|
2072
2091
|
* Perform mutation operations against the configured dataset
|
|
2073
|
-
* Returns a promise that resolves to the
|
|
2092
|
+
* Returns a promise that resolves to a mutation result object containing the mutated document IDs.
|
|
2074
2093
|
*
|
|
2075
2094
|
* @param operations - Mutation operations to execute
|
|
2076
2095
|
* @param options - Mutation options
|
|
@@ -2078,7 +2097,7 @@ export class SanityClient {
|
|
|
2078
2097
|
mutate<R extends Record<string, Any> = Record<string, Any>>(
|
|
2079
2098
|
operations: Mutation<R>[] | Patch | Transaction,
|
|
2080
2099
|
options?: BaseMutationOptions,
|
|
2081
|
-
): Promise<
|
|
2100
|
+
): Promise<MultipleMutationResult>
|
|
2082
2101
|
mutate<R extends Record<string, Any> = Record<string, Any>>(
|
|
2083
2102
|
operations: Mutation<R>[] | Patch | Transaction,
|
|
2084
2103
|
options?:
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
Any,
|
|
9
9
|
HttpRequest,
|
|
10
10
|
InitializedClientConfig,
|
|
11
|
+
MediaLibraryAssetDocument,
|
|
11
12
|
SanityAssetDocument,
|
|
12
13
|
SanityImageAssetDocument,
|
|
13
14
|
UploadBody,
|
|
@@ -67,7 +68,12 @@ export class ObservableAssetsClient {
|
|
|
67
68
|
assetType: 'file' | 'image',
|
|
68
69
|
body: UploadBody,
|
|
69
70
|
options?: UploadClientConfig,
|
|
70
|
-
): Observable<
|
|
71
|
+
): Observable<
|
|
72
|
+
UploadEvent<
|
|
73
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
74
|
+
| {asset: MediaLibraryAssetDocument}
|
|
75
|
+
>
|
|
76
|
+
> {
|
|
71
77
|
return _upload(this.#client, this.#httpRequest, assetType, body, options)
|
|
72
78
|
}
|
|
73
79
|
}
|
|
@@ -84,6 +90,16 @@ export class AssetsClient {
|
|
|
84
90
|
/**
|
|
85
91
|
* Uploads a file asset to the configured dataset
|
|
86
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
|
+
*
|
|
87
103
|
* @param assetType - Asset type (file)
|
|
88
104
|
* @param body - Asset content - can be a browser File instance, a Blob, a Node.js Buffer instance or a Node.js ReadableStream.
|
|
89
105
|
* @param options - Options to use for the upload
|
|
@@ -96,6 +112,10 @@ export class AssetsClient {
|
|
|
96
112
|
/**
|
|
97
113
|
* Uploads an image asset to the configured dataset
|
|
98
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
|
+
*
|
|
99
119
|
* @param assetType - Asset type (image)
|
|
100
120
|
* @param body - Asset content - can be a browser File instance, a Blob, a Node.js Buffer instance or a Node.js ReadableStream.
|
|
101
121
|
* @param options - Options to use for the upload
|
|
@@ -121,19 +141,48 @@ export class AssetsClient {
|
|
|
121
141
|
assetType: 'file' | 'image',
|
|
122
142
|
body: UploadBody,
|
|
123
143
|
options?: UploadClientConfig,
|
|
124
|
-
): Promise<SanityAssetDocument | SanityImageAssetDocument> {
|
|
125
|
-
type Doc =
|
|
144
|
+
): Promise<SanityAssetDocument | SanityImageAssetDocument | MediaLibraryAssetDocument> {
|
|
145
|
+
type Doc =
|
|
146
|
+
| {document: SanityAssetDocument | SanityImageAssetDocument}
|
|
147
|
+
| {asset: MediaLibraryAssetDocument}
|
|
126
148
|
const observable = _upload<Doc>(this.#client, this.#httpRequest, assetType, body, options)
|
|
127
149
|
return lastValueFrom(
|
|
128
150
|
observable.pipe(
|
|
129
151
|
filter((event): event is UploadResponseEvent<Doc> => event.type === 'response'),
|
|
130
|
-
map((event) => event.body
|
|
152
|
+
map((event) => pluckUploadedAsset(event.body)),
|
|
131
153
|
),
|
|
132
154
|
)
|
|
133
155
|
}
|
|
134
156
|
}
|
|
135
157
|
|
|
136
|
-
|
|
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
|
+
>(
|
|
137
186
|
client: SanityClient | ObservableSanityClient,
|
|
138
187
|
_httpRequest: HttpRequest,
|
|
139
188
|
assetType: 'image' | 'file',
|