@sanity/client 5.1.0 → 5.2.1
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 +16 -72
- package/dist/index.browser.cjs +49 -11
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +49 -11
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +50 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +156 -128
- package/dist/index.js +50 -12
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
- package/src/SanityClient.ts +120 -80
- package/src/assets/AssetsClient.ts +4 -4
- package/src/config.ts +1 -1
- package/src/data/dataMethods.ts +31 -31
- package/src/data/encodeQueryString.ts +3 -3
- package/src/data/listen.ts +11 -11
- package/src/data/patch.ts +18 -18
- package/src/data/transaction.ts +13 -15
- package/src/generateHelpUrl.ts +1 -1
- package/src/http/errors.ts +7 -7
- package/src/http/request.ts +4 -4
- package/src/http/requestOptions.ts +3 -3
- package/src/index.browser.ts +9 -1
- package/src/index.ts +9 -1
- package/src/types.ts +27 -28
- package/src/util/defaults.ts +3 -3
- package/src/util/getSelection.ts +1 -1
- package/src/util/once.ts +4 -4
- package/src/util/pick.ts +3 -3
- package/src/validators.ts +4 -4
- package/src/warnings.ts +8 -4
- package/umd/sanityClient.js +49 -11
- package/umd/sanityClient.min.js +3 -3
- package/src/migrationNotice.ts +0 -9
|
@@ -4,7 +4,7 @@ import {filter, map} from 'rxjs/operators'
|
|
|
4
4
|
import {_requestObservable} from '../data/dataMethods'
|
|
5
5
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
6
6
|
import type {
|
|
7
|
-
|
|
7
|
+
Any,
|
|
8
8
|
HttpRequest,
|
|
9
9
|
HttpRequestEvent,
|
|
10
10
|
ResponseEvent,
|
|
@@ -122,7 +122,7 @@ export class AssetsClient {
|
|
|
122
122
|
const observable = _upload(this.#client, this.#httpRequest, assetType, body, options)
|
|
123
123
|
return lastValueFrom(
|
|
124
124
|
observable.pipe(
|
|
125
|
-
filter((event:
|
|
125
|
+
filter((event: Any) => event.type === 'response'),
|
|
126
126
|
map(
|
|
127
127
|
(event) =>
|
|
128
128
|
(event as ResponseEvent<{document: SanityAssetDocument | SanityImageAssetDocument}>)
|
|
@@ -152,7 +152,7 @@ function _upload(
|
|
|
152
152
|
const assetEndpoint = assetType === 'image' ? 'images' : 'files'
|
|
153
153
|
const options = optionsFromFile(opts, body)
|
|
154
154
|
const {tag, label, title, description, creditLine, filename, source} = options
|
|
155
|
-
const query:
|
|
155
|
+
const query: Any = {
|
|
156
156
|
label,
|
|
157
157
|
title,
|
|
158
158
|
description,
|
|
@@ -176,7 +176,7 @@ function _upload(
|
|
|
176
176
|
})
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
function optionsFromFile(opts: Record<string,
|
|
179
|
+
function optionsFromFile(opts: Record<string, Any>, file: Any) {
|
|
180
180
|
if (typeof window === 'undefined' || !(file instanceof window.File)) {
|
|
181
181
|
return opts
|
|
182
182
|
}
|
package/src/config.ts
CHANGED
package/src/data/dataMethods.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {type MonoTypeOperatorFunction, Observable} from 'rxjs'
|
|
2
2
|
import {filter, map} from 'rxjs/operators'
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import {requestOptions} from '../http/requestOptions'
|
|
5
5
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
6
6
|
import type {
|
|
7
7
|
AllDocumentIdsMutationOptions,
|
|
8
8
|
AllDocumentsMutationOptions,
|
|
9
|
+
Any,
|
|
9
10
|
BaseMutationOptions,
|
|
10
11
|
FilteredResponseQueryOptions,
|
|
11
12
|
FirstDocumentIdMutationOptions,
|
|
12
13
|
FirstDocumentMutationOptions,
|
|
13
|
-
FIXME,
|
|
14
14
|
HttpRequest,
|
|
15
15
|
HttpRequestEvent,
|
|
16
16
|
IdentifiedSanityDocumentStub,
|
|
@@ -25,14 +25,14 @@ import type {
|
|
|
25
25
|
SingleMutationResult,
|
|
26
26
|
UnfilteredResponseQueryOptions,
|
|
27
27
|
} from '../types'
|
|
28
|
-
import getSelection from '../util/getSelection'
|
|
28
|
+
import {getSelection} from '../util/getSelection'
|
|
29
29
|
import * as validate from '../validators'
|
|
30
30
|
import * as validators from '../validators'
|
|
31
|
-
import encodeQueryString from './encodeQueryString'
|
|
31
|
+
import {encodeQueryString} from './encodeQueryString'
|
|
32
32
|
import {ObservablePatch, Patch} from './patch'
|
|
33
33
|
import {ObservableTransaction, Transaction} from './transaction'
|
|
34
34
|
|
|
35
|
-
const excludeFalsey = (param:
|
|
35
|
+
const excludeFalsey = (param: Any, defValue: Any) => {
|
|
36
36
|
const value = typeof param === 'undefined' ? defValue : param
|
|
37
37
|
return param === false ? undefined : value
|
|
38
38
|
}
|
|
@@ -48,10 +48,10 @@ const getMutationQuery = (options: BaseMutationOptions = {}) => {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
const isResponse = (event:
|
|
52
|
-
const getBody = (event:
|
|
51
|
+
const isResponse = (event: Any) => event.type === 'response'
|
|
52
|
+
const getBody = (event: Any) => event.body
|
|
53
53
|
|
|
54
|
-
const indexBy = (docs:
|
|
54
|
+
const indexBy = (docs: Any[], attr: Any) =>
|
|
55
55
|
docs.reduce((indexed, doc) => {
|
|
56
56
|
indexed[attr(doc)] = doc
|
|
57
57
|
return indexed
|
|
@@ -68,13 +68,13 @@ export function _fetch<R, Q extends QueryParams>(
|
|
|
68
68
|
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {}
|
|
69
69
|
): Observable<RawQueryResponse<R> | R> {
|
|
70
70
|
const mapResponse =
|
|
71
|
-
options.filterResponse === false ? (res:
|
|
71
|
+
options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
|
|
72
72
|
|
|
73
73
|
return _dataRequest(client, httpRequest, 'query', {query, params}, options).pipe(map(mapResponse))
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/** @internal */
|
|
77
|
-
export function _getDocument<R extends Record<string,
|
|
77
|
+
export function _getDocument<R extends Record<string, Any>>(
|
|
78
78
|
client: ObservableSanityClient | SanityClient,
|
|
79
79
|
httpRequest: HttpRequest,
|
|
80
80
|
id: string,
|
|
@@ -88,7 +88,7 @@ export function _getDocument<R extends Record<string, FIXME>>(
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/** @internal */
|
|
91
|
-
export function _getDocuments<R extends Record<string,
|
|
91
|
+
export function _getDocuments<R extends Record<string, Any>>(
|
|
92
92
|
client: ObservableSanityClient | SanityClient,
|
|
93
93
|
httpRequest: HttpRequest,
|
|
94
94
|
ids: string[],
|
|
@@ -97,15 +97,15 @@ export function _getDocuments<R extends Record<string, FIXME>>(
|
|
|
97
97
|
const options = {uri: _getDataUrl(client, 'doc', ids.join(',')), json: true, tag: opts.tag}
|
|
98
98
|
return _requestObservable<(SanityDocument<R> | null)[]>(client, httpRequest, options).pipe(
|
|
99
99
|
filter(isResponse),
|
|
100
|
-
map((event:
|
|
101
|
-
const indexed = indexBy(event.body.documents || [], (doc:
|
|
100
|
+
map((event: Any) => {
|
|
101
|
+
const indexed = indexBy(event.body.documents || [], (doc: Any) => doc._id)
|
|
102
102
|
return ids.map((id) => indexed[id] || null)
|
|
103
103
|
})
|
|
104
104
|
)
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
/** @internal */
|
|
108
|
-
export function _createIfNotExists<R extends Record<string,
|
|
108
|
+
export function _createIfNotExists<R extends Record<string, Any>>(
|
|
109
109
|
client: ObservableSanityClient | SanityClient,
|
|
110
110
|
httpRequest: HttpRequest,
|
|
111
111
|
doc: IdentifiedSanityDocumentStub<R>,
|
|
@@ -123,7 +123,7 @@ export function _createIfNotExists<R extends Record<string, FIXME>>(
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/** @internal */
|
|
126
|
-
export function _createOrReplace<R extends Record<string,
|
|
126
|
+
export function _createOrReplace<R extends Record<string, Any>>(
|
|
127
127
|
client: ObservableSanityClient | SanityClient,
|
|
128
128
|
httpRequest: HttpRequest,
|
|
129
129
|
doc: IdentifiedSanityDocumentStub<R>,
|
|
@@ -141,7 +141,7 @@ export function _createOrReplace<R extends Record<string, FIXME>>(
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
/** @internal */
|
|
144
|
-
export function _delete<R extends Record<string,
|
|
144
|
+
export function _delete<R extends Record<string, Any>>(
|
|
145
145
|
client: ObservableSanityClient | SanityClient,
|
|
146
146
|
httpRequest: HttpRequest,
|
|
147
147
|
selection: string | MutationSelection,
|
|
@@ -164,7 +164,7 @@ export function _delete<R extends Record<string, FIXME>>(
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
/** @internal */
|
|
167
|
-
export function _mutate<R extends Record<string,
|
|
167
|
+
export function _mutate<R extends Record<string, Any>>(
|
|
168
168
|
client: SanityClient | ObservableSanityClient,
|
|
169
169
|
httpRequest: HttpRequest,
|
|
170
170
|
mutations: Mutation<R>[] | Patch | ObservablePatch | Transaction | ObservableTransaction,
|
|
@@ -186,7 +186,7 @@ export function _mutate<R extends Record<string, FIXME>>(
|
|
|
186
186
|
: mutations
|
|
187
187
|
|
|
188
188
|
const muts = Array.isArray(mut) ? mut : [mut]
|
|
189
|
-
const transactionId = options && (options as
|
|
189
|
+
const transactionId = options && (options as Any).transactionId
|
|
190
190
|
return _dataRequest(client, httpRequest, 'mutate', {mutations: muts, transactionId}, options)
|
|
191
191
|
}
|
|
192
192
|
|
|
@@ -197,9 +197,9 @@ export function _dataRequest(
|
|
|
197
197
|
client: SanityClient | ObservableSanityClient,
|
|
198
198
|
httpRequest: HttpRequest,
|
|
199
199
|
endpoint: string,
|
|
200
|
-
body:
|
|
201
|
-
options:
|
|
202
|
-
):
|
|
200
|
+
body: Any,
|
|
201
|
+
options: Any = {}
|
|
202
|
+
): Any {
|
|
203
203
|
const isMutation = endpoint === 'mutate'
|
|
204
204
|
const isQuery = endpoint === 'query'
|
|
205
205
|
|
|
@@ -240,12 +240,12 @@ export function _dataRequest(
|
|
|
240
240
|
if (options.returnDocuments) {
|
|
241
241
|
return returnFirst
|
|
242
242
|
? results[0] && results[0].document
|
|
243
|
-
: results.map((mut:
|
|
243
|
+
: results.map((mut: Any) => mut.document)
|
|
244
244
|
}
|
|
245
245
|
|
|
246
246
|
// Return a reduced subset
|
|
247
247
|
const key = returnFirst ? 'documentId' : 'documentIds'
|
|
248
|
-
const ids = returnFirst ? results[0] && results[0].id : results.map((mut:
|
|
248
|
+
const ids = returnFirst ? results[0] && results[0].id : results.map((mut: Any) => mut.id)
|
|
249
249
|
return {
|
|
250
250
|
transactionId: res.transactionId,
|
|
251
251
|
results: results,
|
|
@@ -258,12 +258,12 @@ export function _dataRequest(
|
|
|
258
258
|
/**
|
|
259
259
|
* @internal
|
|
260
260
|
*/
|
|
261
|
-
export function _create<R extends Record<string,
|
|
261
|
+
export function _create<R extends Record<string, Any>>(
|
|
262
262
|
client: SanityClient | ObservableSanityClient,
|
|
263
263
|
httpRequest: HttpRequest,
|
|
264
|
-
doc:
|
|
265
|
-
op:
|
|
266
|
-
options:
|
|
264
|
+
doc: Any,
|
|
265
|
+
op: Any,
|
|
266
|
+
options: Any = {}
|
|
267
267
|
): Observable<
|
|
268
268
|
SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
|
|
269
269
|
> {
|
|
@@ -301,7 +301,7 @@ export function _requestObservable<R>(
|
|
|
301
301
|
options.query = {tag: validate.requestTag(tag), ...options.query}
|
|
302
302
|
}
|
|
303
303
|
|
|
304
|
-
const reqOptions =
|
|
304
|
+
const reqOptions = requestOptions(
|
|
305
305
|
config,
|
|
306
306
|
Object.assign({}, options, {
|
|
307
307
|
url: _getUrl(client, uri, useCdn),
|
|
@@ -322,11 +322,11 @@ export function _requestObservable<R>(
|
|
|
322
322
|
export function _request<R>(
|
|
323
323
|
client: SanityClient | ObservableSanityClient,
|
|
324
324
|
httpRequest: HttpRequest,
|
|
325
|
-
options:
|
|
325
|
+
options: Any
|
|
326
326
|
): Observable<R> {
|
|
327
327
|
const observable = _requestObservable<R>(client, httpRequest, options).pipe(
|
|
328
|
-
filter((event:
|
|
329
|
-
map((event:
|
|
328
|
+
filter((event: Any) => event.type === 'response'),
|
|
329
|
+
map((event: Any) => event.body)
|
|
330
330
|
)
|
|
331
331
|
|
|
332
332
|
return observable
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {Any, QueryParams} from '../types'
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export const encodeQueryString = ({
|
|
4
4
|
query,
|
|
5
5
|
params = {},
|
|
6
6
|
options = {},
|
|
7
7
|
}: {
|
|
8
8
|
query: string
|
|
9
9
|
params?: QueryParams
|
|
10
|
-
options?:
|
|
10
|
+
options?: Any
|
|
11
11
|
}) => {
|
|
12
12
|
const searchParams = new URLSearchParams()
|
|
13
13
|
// We generally want tag at the start of the query string
|
package/src/data/listen.ts
CHANGED
|
@@ -2,11 +2,11 @@ import polyfilledEventSource from '@sanity/eventsource'
|
|
|
2
2
|
import {Observable} from 'rxjs'
|
|
3
3
|
|
|
4
4
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
|
-
import type {
|
|
5
|
+
import type {Any, ListenEvent, ListenOptions, MutationEvent, QueryParams} from '../types'
|
|
6
6
|
import defaults from '../util/defaults'
|
|
7
|
-
import pick from '../util/pick'
|
|
7
|
+
import {pick} from '../util/pick'
|
|
8
8
|
import {_getDataUrl} from './dataMethods'
|
|
9
|
-
import encodeQueryString from './encodeQueryString'
|
|
9
|
+
import {encodeQueryString} from './encodeQueryString'
|
|
10
10
|
|
|
11
11
|
// Limit is 16K for a _request_, eg including headers. Have to account for an
|
|
12
12
|
// unknown range of headers, but an average EventSource request from Chrome seems
|
|
@@ -34,7 +34,7 @@ const defaultOptions = {
|
|
|
34
34
|
* @param options - Listener options
|
|
35
35
|
* @internal
|
|
36
36
|
*/
|
|
37
|
-
export function _listen<R extends Record<string,
|
|
37
|
+
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
38
38
|
this: SanityClient | ObservableSanityClient,
|
|
39
39
|
query: string,
|
|
40
40
|
params?: QueryParams
|
|
@@ -47,14 +47,14 @@ export function _listen<R extends Record<string, FIXME> = Record<string, FIXME>>
|
|
|
47
47
|
* @param options - Listener options
|
|
48
48
|
* @internal
|
|
49
49
|
*/
|
|
50
|
-
export function _listen<R extends Record<string,
|
|
50
|
+
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
51
51
|
this: SanityClient | ObservableSanityClient,
|
|
52
52
|
query: string,
|
|
53
53
|
params?: QueryParams,
|
|
54
54
|
options?: ListenOptions
|
|
55
55
|
): Observable<ListenEvent<R>>
|
|
56
56
|
/** @internal */
|
|
57
|
-
export function _listen<R extends Record<string,
|
|
57
|
+
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
58
58
|
this: SanityClient | ObservableSanityClient,
|
|
59
59
|
query: string,
|
|
60
60
|
params?: QueryParams,
|
|
@@ -114,11 +114,11 @@ export function _listen<R extends Record<string, FIXME> = Record<string, FIXME>>
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
function onChannelError(err:
|
|
117
|
+
function onChannelError(err: Any) {
|
|
118
118
|
observer.error(cooerceError(err))
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
function onMessage(evt:
|
|
121
|
+
function onMessage(evt: Any) {
|
|
122
122
|
const event = parseEvent(evt)
|
|
123
123
|
return event instanceof Error ? observer.error(event) : observer.next(event)
|
|
124
124
|
}
|
|
@@ -165,7 +165,7 @@ export function _listen<R extends Record<string, FIXME> = Record<string, FIXME>>
|
|
|
165
165
|
})
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
function parseEvent(event:
|
|
168
|
+
function parseEvent(event: Any) {
|
|
169
169
|
try {
|
|
170
170
|
const data = (event.data && JSON.parse(event.data)) || {}
|
|
171
171
|
return Object.assign({type: event.type}, data)
|
|
@@ -174,7 +174,7 @@ function parseEvent(event: FIXME) {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
function cooerceError(err:
|
|
177
|
+
function cooerceError(err: Any) {
|
|
178
178
|
if (err instanceof Error) {
|
|
179
179
|
return err
|
|
180
180
|
}
|
|
@@ -183,7 +183,7 @@ function cooerceError(err: FIXME) {
|
|
|
183
183
|
return evt instanceof Error ? evt : new Error(extractErrorMessage(evt))
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
function extractErrorMessage(err:
|
|
186
|
+
function extractErrorMessage(err: Any) {
|
|
187
187
|
if (!err.error) {
|
|
188
188
|
return err.message || 'Unknown listener error'
|
|
189
189
|
}
|
package/src/data/patch.ts
CHANGED
|
@@ -4,11 +4,11 @@ import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
|
4
4
|
import type {
|
|
5
5
|
AllDocumentIdsMutationOptions,
|
|
6
6
|
AllDocumentsMutationOptions,
|
|
7
|
+
Any,
|
|
7
8
|
AttributeSet,
|
|
8
9
|
BaseMutationOptions,
|
|
9
10
|
FirstDocumentIdMutationOptions,
|
|
10
11
|
FirstDocumentMutationOptions,
|
|
11
|
-
FIXME,
|
|
12
12
|
MultipleMutationResult,
|
|
13
13
|
PatchMutationOperation,
|
|
14
14
|
PatchOperations,
|
|
@@ -16,7 +16,7 @@ import type {
|
|
|
16
16
|
SanityDocument,
|
|
17
17
|
SingleMutationResult,
|
|
18
18
|
} from '../types'
|
|
19
|
-
import getSelection from '../util/getSelection'
|
|
19
|
+
import {getSelection} from '../util/getSelection'
|
|
20
20
|
import {validateInsert, validateObject} from '../validators'
|
|
21
21
|
|
|
22
22
|
/** @internal */
|
|
@@ -99,7 +99,7 @@ export class BasePatch {
|
|
|
99
99
|
* @param selector - JSONPath expression, eg `comments[-1]` or `blocks[_key=="abc123"]`
|
|
100
100
|
* @param items - Array of items to insert/replace
|
|
101
101
|
*/
|
|
102
|
-
insert(at: 'before' | 'after' | 'replace', selector: string, items:
|
|
102
|
+
insert(at: 'before' | 'after' | 'replace', selector: string, items: Any[]): this {
|
|
103
103
|
validateInsert(at, selector, items)
|
|
104
104
|
return this._assign('insert', {[at]: selector, items})
|
|
105
105
|
}
|
|
@@ -110,7 +110,7 @@ export class BasePatch {
|
|
|
110
110
|
* @param selector - Attribute/path to append to, eg `comments` or `person.hobbies`
|
|
111
111
|
* @param items - Array of items to append to the array
|
|
112
112
|
*/
|
|
113
|
-
append(selector: string, items:
|
|
113
|
+
append(selector: string, items: Any[]): this {
|
|
114
114
|
return this.insert('after', `${selector}[-1]`, items)
|
|
115
115
|
}
|
|
116
116
|
|
|
@@ -120,7 +120,7 @@ export class BasePatch {
|
|
|
120
120
|
* @param selector - Attribute/path to prepend to, eg `comments` or `person.hobbies`
|
|
121
121
|
* @param items - Array of items to prepend to the array
|
|
122
122
|
*/
|
|
123
|
-
prepend(selector: string, items:
|
|
123
|
+
prepend(selector: string, items: Any[]): this {
|
|
124
124
|
return this.insert('before', `${selector}[0]`, items)
|
|
125
125
|
}
|
|
126
126
|
|
|
@@ -132,7 +132,7 @@ export class BasePatch {
|
|
|
132
132
|
* @param deleteCount - An integer indicating the number of old array elements to remove.
|
|
133
133
|
* @param items - The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.
|
|
134
134
|
*/
|
|
135
|
-
splice(selector: string, start: number, deleteCount?: number, items?:
|
|
135
|
+
splice(selector: string, start: number, deleteCount?: number, items?: Any[]): this {
|
|
136
136
|
// Negative indexes doesn't mean the same in Sanity as they do in JS;
|
|
137
137
|
// -1 means "actually at the end of the array", which allows inserting
|
|
138
138
|
// at the end of the array without knowing its length. We therefore have
|
|
@@ -178,7 +178,7 @@ export class BasePatch {
|
|
|
178
178
|
return this
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
protected _assign(op: keyof PatchOperations, props:
|
|
181
|
+
protected _assign(op: keyof PatchOperations, props: Any, merge = true): this {
|
|
182
182
|
validateObject(op, props)
|
|
183
183
|
this.operations = Object.assign({}, this.operations, {
|
|
184
184
|
[op]: Object.assign({}, (merge && this.operations[op]) || {}, props),
|
|
@@ -186,7 +186,7 @@ export class BasePatch {
|
|
|
186
186
|
return this
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
protected _set(op: keyof PatchOperations, props:
|
|
189
|
+
protected _set(op: keyof PatchOperations, props: Any): this {
|
|
190
190
|
return this._assign(op, props, false)
|
|
191
191
|
}
|
|
192
192
|
}
|
|
@@ -216,7 +216,7 @@ export class ObservablePatch extends BasePatch {
|
|
|
216
216
|
*
|
|
217
217
|
* @param options - Options for the mutation operation
|
|
218
218
|
*/
|
|
219
|
-
commit<R extends Record<string,
|
|
219
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
220
220
|
options: FirstDocumentMutationOptions
|
|
221
221
|
): Observable<SanityDocument<R>>
|
|
222
222
|
/**
|
|
@@ -224,7 +224,7 @@ export class ObservablePatch extends BasePatch {
|
|
|
224
224
|
*
|
|
225
225
|
* @param options - Options for the mutation operation
|
|
226
226
|
*/
|
|
227
|
-
commit<R extends Record<string,
|
|
227
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
228
228
|
options: AllDocumentsMutationOptions
|
|
229
229
|
): Observable<SanityDocument<R>[]>
|
|
230
230
|
/**
|
|
@@ -244,10 +244,10 @@ export class ObservablePatch extends BasePatch {
|
|
|
244
244
|
*
|
|
245
245
|
* @param options - Options for the mutation operation
|
|
246
246
|
*/
|
|
247
|
-
commit<R extends Record<string,
|
|
247
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
248
248
|
options?: BaseMutationOptions
|
|
249
249
|
): Observable<SanityDocument<R>>
|
|
250
|
-
commit<R extends Record<string,
|
|
250
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
251
251
|
options?:
|
|
252
252
|
| FirstDocumentMutationOptions
|
|
253
253
|
| AllDocumentsMutationOptions
|
|
@@ -266,7 +266,7 @@ export class ObservablePatch extends BasePatch {
|
|
|
266
266
|
|
|
267
267
|
const returnFirst = typeof this.selection === 'string'
|
|
268
268
|
const opts = Object.assign({returnFirst, returnDocuments: true}, options)
|
|
269
|
-
return this.#client.mutate<R>({patch: this.serialize()} as
|
|
269
|
+
return this.#client.mutate<R>({patch: this.serialize()} as Any, opts)
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
|
|
@@ -290,7 +290,7 @@ export class Patch extends BasePatch {
|
|
|
290
290
|
*
|
|
291
291
|
* @param options - Options for the mutation operation
|
|
292
292
|
*/
|
|
293
|
-
commit<R extends Record<string,
|
|
293
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
294
294
|
options: FirstDocumentMutationOptions
|
|
295
295
|
): Promise<SanityDocument<R>>
|
|
296
296
|
/**
|
|
@@ -298,7 +298,7 @@ export class Patch extends BasePatch {
|
|
|
298
298
|
*
|
|
299
299
|
* @param options - Options for the mutation operation
|
|
300
300
|
*/
|
|
301
|
-
commit<R extends Record<string,
|
|
301
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
302
302
|
options: AllDocumentsMutationOptions
|
|
303
303
|
): Promise<SanityDocument<R>[]>
|
|
304
304
|
/**
|
|
@@ -318,10 +318,10 @@ export class Patch extends BasePatch {
|
|
|
318
318
|
*
|
|
319
319
|
* @param options - Options for the mutation operation
|
|
320
320
|
*/
|
|
321
|
-
commit<R extends Record<string,
|
|
321
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
322
322
|
options?: BaseMutationOptions
|
|
323
323
|
): Promise<SanityDocument<R>>
|
|
324
|
-
commit<R extends Record<string,
|
|
324
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
325
325
|
options?:
|
|
326
326
|
| FirstDocumentMutationOptions
|
|
327
327
|
| AllDocumentsMutationOptions
|
|
@@ -340,6 +340,6 @@ export class Patch extends BasePatch {
|
|
|
340
340
|
|
|
341
341
|
const returnFirst = typeof this.selection === 'string'
|
|
342
342
|
const opts = Object.assign({returnFirst, returnDocuments: true}, options)
|
|
343
|
-
return this.#client.mutate<R>({patch: this.serialize()} as
|
|
343
|
+
return this.#client.mutate<R>({patch: this.serialize()} as Any, opts)
|
|
344
344
|
}
|
|
345
345
|
}
|
package/src/data/transaction.ts
CHANGED
|
@@ -2,8 +2,8 @@ import type {Observable} from 'rxjs'
|
|
|
2
2
|
|
|
3
3
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
4
4
|
import type {
|
|
5
|
+
Any,
|
|
5
6
|
BaseMutationOptions,
|
|
6
|
-
FIXME,
|
|
7
7
|
IdentifiedSanityDocumentStub,
|
|
8
8
|
MultipleMutationResult,
|
|
9
9
|
Mutation,
|
|
@@ -40,9 +40,7 @@ export class BaseTransaction {
|
|
|
40
40
|
*
|
|
41
41
|
* @param doc - Document to create. Requires a `_type` property.
|
|
42
42
|
*/
|
|
43
|
-
create<R extends Record<string,
|
|
44
|
-
doc: SanityDocumentStub<R>
|
|
45
|
-
): this {
|
|
43
|
+
create<R extends Record<string, Any> = Record<string, Any>>(doc: SanityDocumentStub<R>): this {
|
|
46
44
|
validators.validateObject('create', doc)
|
|
47
45
|
return this._add({create: doc})
|
|
48
46
|
}
|
|
@@ -53,7 +51,7 @@ export class BaseTransaction {
|
|
|
53
51
|
*
|
|
54
52
|
* @param doc - Document to create if it does not already exist. Requires `_id` and `_type` properties.
|
|
55
53
|
*/
|
|
56
|
-
createIfNotExists<R extends Record<string,
|
|
54
|
+
createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
|
|
57
55
|
doc: IdentifiedSanityDocumentStub<R>
|
|
58
56
|
): this {
|
|
59
57
|
const op = 'createIfNotExists'
|
|
@@ -68,7 +66,7 @@ export class BaseTransaction {
|
|
|
68
66
|
*
|
|
69
67
|
* @param doc - Document to create or replace. Requires `_id` and `_type` properties.
|
|
70
68
|
*/
|
|
71
|
-
createOrReplace<R extends Record<string,
|
|
69
|
+
createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
|
|
72
70
|
doc: IdentifiedSanityDocumentStub<R>
|
|
73
71
|
): this {
|
|
74
72
|
const op = 'createOrReplace'
|
|
@@ -89,7 +87,7 @@ export class BaseTransaction {
|
|
|
89
87
|
}
|
|
90
88
|
|
|
91
89
|
/**
|
|
92
|
-
* Gets the current transaction ID, if
|
|
90
|
+
* Gets the current transaction ID, if any
|
|
93
91
|
*/
|
|
94
92
|
transactionId(): string | undefined
|
|
95
93
|
/**
|
|
@@ -155,7 +153,7 @@ export class Transaction extends BaseTransaction {
|
|
|
155
153
|
*
|
|
156
154
|
* @param options - Options for the mutation operation
|
|
157
155
|
*/
|
|
158
|
-
commit<R extends Record<string,
|
|
156
|
+
commit<R extends Record<string, Any>>(
|
|
159
157
|
options: TransactionFirstDocumentMutationOptions
|
|
160
158
|
): Promise<SanityDocument<R>>
|
|
161
159
|
/**
|
|
@@ -163,7 +161,7 @@ export class Transaction extends BaseTransaction {
|
|
|
163
161
|
*
|
|
164
162
|
* @param options - Options for the mutation operation
|
|
165
163
|
*/
|
|
166
|
-
commit<R extends Record<string,
|
|
164
|
+
commit<R extends Record<string, Any>>(
|
|
167
165
|
options: TransactionAllDocumentsMutationOptions
|
|
168
166
|
): Promise<SanityDocument<R>[]>
|
|
169
167
|
/**
|
|
@@ -184,7 +182,7 @@ export class Transaction extends BaseTransaction {
|
|
|
184
182
|
* @param options - Options for the mutation operation
|
|
185
183
|
*/
|
|
186
184
|
commit(options?: BaseMutationOptions): Promise<MultipleMutationResult>
|
|
187
|
-
commit<R extends Record<string,
|
|
185
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
188
186
|
options?:
|
|
189
187
|
| TransactionFirstDocumentMutationOptions
|
|
190
188
|
| TransactionAllDocumentsMutationOptions
|
|
@@ -202,7 +200,7 @@ export class Transaction extends BaseTransaction {
|
|
|
202
200
|
}
|
|
203
201
|
|
|
204
202
|
return this.#client.mutate<R>(
|
|
205
|
-
this.serialize() as
|
|
203
|
+
this.serialize() as Any,
|
|
206
204
|
Object.assign({transactionId: this.trxId}, defaultMutateOptions, options || {})
|
|
207
205
|
)
|
|
208
206
|
}
|
|
@@ -265,7 +263,7 @@ export class ObservableTransaction extends BaseTransaction {
|
|
|
265
263
|
*
|
|
266
264
|
* @param options - Options for the mutation operation
|
|
267
265
|
*/
|
|
268
|
-
commit<R extends Record<string,
|
|
266
|
+
commit<R extends Record<string, Any>>(
|
|
269
267
|
options: TransactionFirstDocumentMutationOptions
|
|
270
268
|
): Observable<SanityDocument<R>>
|
|
271
269
|
/**
|
|
@@ -273,7 +271,7 @@ export class ObservableTransaction extends BaseTransaction {
|
|
|
273
271
|
*
|
|
274
272
|
* @param options - Options for the mutation operation
|
|
275
273
|
*/
|
|
276
|
-
commit<R extends Record<string,
|
|
274
|
+
commit<R extends Record<string, Any>>(
|
|
277
275
|
options: TransactionAllDocumentsMutationOptions
|
|
278
276
|
): Observable<SanityDocument<R>[]>
|
|
279
277
|
/**
|
|
@@ -294,7 +292,7 @@ export class ObservableTransaction extends BaseTransaction {
|
|
|
294
292
|
* @param options - Options for the mutation operation
|
|
295
293
|
*/
|
|
296
294
|
commit(options?: BaseMutationOptions): Observable<MultipleMutationResult>
|
|
297
|
-
commit<R extends Record<string,
|
|
295
|
+
commit<R extends Record<string, Any> = Record<string, Any>>(
|
|
298
296
|
options?:
|
|
299
297
|
| TransactionFirstDocumentMutationOptions
|
|
300
298
|
| TransactionAllDocumentsMutationOptions
|
|
@@ -312,7 +310,7 @@ export class ObservableTransaction extends BaseTransaction {
|
|
|
312
310
|
}
|
|
313
311
|
|
|
314
312
|
return this.#client.mutate<R>(
|
|
315
|
-
this.serialize() as
|
|
313
|
+
this.serialize() as Any,
|
|
316
314
|
Object.assign({transactionId: this.trxId}, defaultMutateOptions, options || {})
|
|
317
315
|
)
|
|
318
316
|
}
|
package/src/generateHelpUrl.ts
CHANGED
package/src/http/errors.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {Any, ErrorProps} from '../types'
|
|
2
2
|
|
|
3
3
|
/** @public */
|
|
4
4
|
export class ClientError extends Error {
|
|
@@ -7,7 +7,7 @@ export class ClientError extends Error {
|
|
|
7
7
|
responseBody: ErrorProps['responseBody']
|
|
8
8
|
details: ErrorProps['details']
|
|
9
9
|
|
|
10
|
-
constructor(res:
|
|
10
|
+
constructor(res: Any) {
|
|
11
11
|
const props = extractErrorProps(res)
|
|
12
12
|
super(props.message)
|
|
13
13
|
Object.assign(this, props)
|
|
@@ -21,21 +21,21 @@ export class ServerError extends Error {
|
|
|
21
21
|
responseBody: ErrorProps['responseBody']
|
|
22
22
|
details: ErrorProps['details']
|
|
23
23
|
|
|
24
|
-
constructor(res:
|
|
24
|
+
constructor(res: Any) {
|
|
25
25
|
const props = extractErrorProps(res)
|
|
26
26
|
super(props.message)
|
|
27
27
|
Object.assign(this, props)
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function extractErrorProps(res:
|
|
31
|
+
function extractErrorProps(res: Any): ErrorProps {
|
|
32
32
|
const body = res.body
|
|
33
33
|
const props = {
|
|
34
34
|
response: res,
|
|
35
35
|
statusCode: res.statusCode,
|
|
36
36
|
responseBody: stringifyBody(body, res),
|
|
37
37
|
message: '',
|
|
38
|
-
details: undefined as
|
|
38
|
+
details: undefined as Any,
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// API/Boom style errors ({statusCode, error, message})
|
|
@@ -56,12 +56,12 @@ function extractErrorProps(res: FIXME): ErrorProps {
|
|
|
56
56
|
return props
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
function httpErrorMessage(res:
|
|
59
|
+
function httpErrorMessage(res: Any) {
|
|
60
60
|
const statusMessage = res.statusMessage ? ` ${res.statusMessage}` : ''
|
|
61
61
|
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}`
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function stringifyBody(body:
|
|
64
|
+
function stringifyBody(body: Any, res: Any) {
|
|
65
65
|
const contentType = (res.headers['content-type'] || '').toLowerCase()
|
|
66
66
|
const isJson = contentType.indexOf('application/json') !== -1
|
|
67
67
|
return isJson ? JSON.stringify(body, null, 2) : body
|