@sanity/client 6.24.3 → 6.24.4
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/dist/index.browser.cjs +188 -128
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +109 -0
- package/dist/index.browser.d.ts +109 -0
- package/dist/index.browser.js +189 -129
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +189 -129
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +190 -130
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +109 -0
- package/dist/stega.browser.d.ts +109 -0
- package/dist/stega.d.cts +109 -0
- package/dist/stega.d.ts +109 -0
- package/package.json +3 -3
- package/src/data/eventsource.ts +255 -0
- package/src/data/eventsourcePolyfill.ts +7 -0
- package/src/data/listen.ts +31 -142
- package/src/data/live.ts +60 -120
- package/src/data/reconnectOnConnectionFailure.ts +30 -0
- package/src/data/transaction.ts +26 -1
- package/src/defineCreateClient.ts +11 -0
- package/src/types.ts +10 -0
- package/umd/sanityClient.js +988 -144
- package/umd/sanityClient.min.js +2 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
catchError,
|
|
3
|
+
concat,
|
|
4
|
+
mergeMap,
|
|
5
|
+
Observable,
|
|
6
|
+
of,
|
|
7
|
+
type OperatorFunction,
|
|
8
|
+
throwError,
|
|
9
|
+
timer,
|
|
10
|
+
} from 'rxjs'
|
|
11
|
+
|
|
12
|
+
import {ConnectionFailedError} from './eventsource'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Note: connection failure is not the same as network disconnect which may happen more frequent.
|
|
16
|
+
* The EventSource instance will automatically reconnect in case of a network disconnect, however,
|
|
17
|
+
* in some rare cases a ConnectionFailed Error will be thrown and this operator explicitly retries these
|
|
18
|
+
*/
|
|
19
|
+
export function reconnectOnConnectionFailure<T>(): OperatorFunction<T, T | {type: 'reconnect'}> {
|
|
20
|
+
return function (source: Observable<T>) {
|
|
21
|
+
return source.pipe(
|
|
22
|
+
catchError((err, caught) => {
|
|
23
|
+
if (err instanceof ConnectionFailedError) {
|
|
24
|
+
return concat(of({type: 'reconnect' as const}), timer(1000).pipe(mergeMap(() => caught)))
|
|
25
|
+
}
|
|
26
|
+
return throwError(() => err)
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/data/transaction.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
IdentifiedSanityDocumentStub,
|
|
8
8
|
MultipleMutationResult,
|
|
9
9
|
Mutation,
|
|
10
|
+
MutationSelection,
|
|
10
11
|
PatchOperations,
|
|
11
12
|
SanityDocument,
|
|
12
13
|
SanityDocumentStub,
|
|
@@ -213,6 +214,13 @@ export class Transaction extends BaseTransaction {
|
|
|
213
214
|
* @param patchOps - Operations to perform, or a builder function
|
|
214
215
|
*/
|
|
215
216
|
patch(documentId: string, patchOps?: PatchBuilder | PatchOperations): this
|
|
217
|
+
/**
|
|
218
|
+
* Performs a patch on the given selection. Can either be a builder function or an object of patch operations.
|
|
219
|
+
*
|
|
220
|
+
* @param selection - An object with `query` and optional `params`, defining which document(s) to patch
|
|
221
|
+
* @param patchOps - Operations to perform, or a builder function
|
|
222
|
+
*/
|
|
223
|
+
patch(patch: MutationSelection, patchOps?: PatchBuilder | PatchOperations): this
|
|
216
224
|
/**
|
|
217
225
|
* Adds the given patch instance to the transaction.
|
|
218
226
|
* The operation is added to the current transaction, ready to be commited by `commit()`
|
|
@@ -220,9 +228,15 @@ export class Transaction extends BaseTransaction {
|
|
|
220
228
|
* @param patch - Patch to execute
|
|
221
229
|
*/
|
|
222
230
|
patch(patch: Patch): this
|
|
223
|
-
patch(
|
|
231
|
+
patch(
|
|
232
|
+
patchOrDocumentId: Patch | MutationSelection | string,
|
|
233
|
+
patchOps?: PatchBuilder | PatchOperations,
|
|
234
|
+
): this {
|
|
224
235
|
const isBuilder = typeof patchOps === 'function'
|
|
225
236
|
const isPatch = typeof patchOrDocumentId !== 'string' && patchOrDocumentId instanceof Patch
|
|
237
|
+
const isMutationSelection =
|
|
238
|
+
typeof patchOrDocumentId === 'object' &&
|
|
239
|
+
('query' in patchOrDocumentId || 'id' in patchOrDocumentId)
|
|
226
240
|
|
|
227
241
|
// transaction.patch(client.patch('documentId').inc({visits: 1}))
|
|
228
242
|
if (isPatch) {
|
|
@@ -239,6 +253,17 @@ export class Transaction extends BaseTransaction {
|
|
|
239
253
|
return this._add({patch: patch.serialize()})
|
|
240
254
|
}
|
|
241
255
|
|
|
256
|
+
/**
|
|
257
|
+
* transaction.patch(
|
|
258
|
+
* {query: "*[_type == 'person' && points >= $threshold]", params: { threshold: 100 }},
|
|
259
|
+
* {dec: { points: 100 }, inc: { bonuses: 1 }}
|
|
260
|
+
* )
|
|
261
|
+
*/
|
|
262
|
+
if (isMutationSelection) {
|
|
263
|
+
const patch = new Patch(patchOrDocumentId, patchOps || {}, this.#client)
|
|
264
|
+
return this._add({patch: patch.serialize()})
|
|
265
|
+
}
|
|
266
|
+
|
|
242
267
|
return this._add({patch: {id: patchOrDocumentId, ...patchOps}})
|
|
243
268
|
}
|
|
244
269
|
}
|
|
@@ -4,6 +4,17 @@ import {defineHttpRequest} from './http/request'
|
|
|
4
4
|
import type {Any, ClientConfig, HttpRequest} from './types'
|
|
5
5
|
|
|
6
6
|
export {validateApiPerspective} from './config'
|
|
7
|
+
export {
|
|
8
|
+
ChannelError,
|
|
9
|
+
connectEventSource,
|
|
10
|
+
ConnectionFailedError,
|
|
11
|
+
DisconnectError,
|
|
12
|
+
type EventSourceEvent,
|
|
13
|
+
type EventSourceInstance,
|
|
14
|
+
MessageError,
|
|
15
|
+
MessageParseError,
|
|
16
|
+
type ServerSentEvent,
|
|
17
|
+
} from './data/eventsource'
|
|
7
18
|
export * from './data/patch'
|
|
8
19
|
export * from './data/transaction'
|
|
9
20
|
export {ClientError, CorsOriginError, ServerError} from './http/errors'
|
package/src/types.ts
CHANGED
|
@@ -854,6 +854,15 @@ export type ReconnectEvent = {
|
|
|
854
854
|
type: 'reconnect'
|
|
855
855
|
}
|
|
856
856
|
|
|
857
|
+
/**
|
|
858
|
+
* The listener connection has been established
|
|
859
|
+
* note: it's usually a better option to use the 'welcome' event
|
|
860
|
+
* @public
|
|
861
|
+
*/
|
|
862
|
+
export type OpenEvent = {
|
|
863
|
+
type: 'open'
|
|
864
|
+
}
|
|
865
|
+
|
|
857
866
|
/**
|
|
858
867
|
* The listener has been established, and will start receiving events.
|
|
859
868
|
* Note that this is also emitted upon _reconnection_.
|
|
@@ -872,6 +881,7 @@ export type ListenEvent<R extends Record<string, Any>> =
|
|
|
872
881
|
| DisconnectEvent
|
|
873
882
|
| ReconnectEvent
|
|
874
883
|
| WelcomeEvent
|
|
884
|
+
| OpenEvent
|
|
875
885
|
|
|
876
886
|
/** @public */
|
|
877
887
|
export type ListenEventName =
|