@sanity/client 6.20.2-beta.2 → 6.21.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/dist/index.browser.cjs +5 -56
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +60 -32
- package/dist/index.browser.d.ts +60 -32
- package/dist/index.browser.js +5 -56
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +5 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -32
- package/dist/index.d.ts +60 -32
- package/dist/index.js +5 -57
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/SanityClient.ts +60 -29
- package/src/data/listen.ts +1 -13
- package/src/http/browserMiddleware.ts +1 -3
- package/src/http/nodeMiddleware.ts +0 -3
- package/src/http/requestOptions.ts +0 -1
- package/src/types.ts +9 -8
- package/umd/sanityClient.js +5 -56
- package/umd/sanityClient.min.js +3 -3
- package/src/http/domainSharding.ts +0 -101
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.21.0",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -118,12 +118,12 @@
|
|
|
118
118
|
},
|
|
119
119
|
"dependencies": {
|
|
120
120
|
"@sanity/eventsource": "^5.0.2",
|
|
121
|
-
"get-it": "^8.6.
|
|
121
|
+
"get-it": "^8.6.3",
|
|
122
122
|
"rxjs": "^7.0.0"
|
|
123
123
|
},
|
|
124
124
|
"devDependencies": {
|
|
125
|
-
"@edge-runtime/types": "^
|
|
126
|
-
"@edge-runtime/vm": "^
|
|
125
|
+
"@edge-runtime/types": "^3.0.0",
|
|
126
|
+
"@edge-runtime/vm": "^4.0.0",
|
|
127
127
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
128
128
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
129
129
|
"@sanity/pkg-utils": "^6.9.3",
|
package/src/SanityClient.ts
CHANGED
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
BaseActionOptions,
|
|
18
18
|
BaseMutationOptions,
|
|
19
19
|
ClientConfig,
|
|
20
|
+
ClientReturn,
|
|
20
21
|
FilteredResponseQueryOptions,
|
|
21
22
|
FirstDocumentIdMutationOptions,
|
|
22
23
|
FirstDocumentMutationOptions,
|
|
@@ -142,10 +143,11 @@ export class ObservableSanityClient {
|
|
|
142
143
|
*
|
|
143
144
|
* @param query - GROQ-query to perform
|
|
144
145
|
*/
|
|
145
|
-
fetch<
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
fetch<
|
|
147
|
+
R = Any,
|
|
148
|
+
Q extends QueryWithoutParams = QueryWithoutParams,
|
|
149
|
+
const G extends string = string,
|
|
150
|
+
>(query: G, params?: Q | QueryWithoutParams): Observable<ClientReturn<G, R>>
|
|
149
151
|
/**
|
|
150
152
|
* Perform a GROQ-query against the configured dataset.
|
|
151
153
|
*
|
|
@@ -153,11 +155,15 @@ export class ObservableSanityClient {
|
|
|
153
155
|
* @param params - Optional query parameters
|
|
154
156
|
* @param options - Optional request options
|
|
155
157
|
*/
|
|
156
|
-
fetch<
|
|
157
|
-
|
|
158
|
+
fetch<
|
|
159
|
+
R = Any,
|
|
160
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
161
|
+
const G extends string = string,
|
|
162
|
+
>(
|
|
163
|
+
query: G,
|
|
158
164
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
159
165
|
options?: FilteredResponseQueryOptions,
|
|
160
|
-
): Observable<R
|
|
166
|
+
): Observable<ClientReturn<G, R>>
|
|
161
167
|
/**
|
|
162
168
|
* Perform a GROQ-query against the configured dataset.
|
|
163
169
|
*
|
|
@@ -165,11 +171,15 @@ export class ObservableSanityClient {
|
|
|
165
171
|
* @param params - Optional query parameters
|
|
166
172
|
* @param options - Request options
|
|
167
173
|
*/
|
|
168
|
-
fetch<
|
|
174
|
+
fetch<
|
|
175
|
+
R = Any,
|
|
176
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
177
|
+
const G extends string = string,
|
|
178
|
+
>(
|
|
169
179
|
query: string,
|
|
170
180
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
171
181
|
options: UnfilteredResponseQueryOptions,
|
|
172
|
-
): Observable<RawQueryResponse<R
|
|
182
|
+
): Observable<RawQueryResponse<ClientReturn<G, R>>>
|
|
173
183
|
/**
|
|
174
184
|
* Perform a GROQ-query against the configured dataset.
|
|
175
185
|
*
|
|
@@ -177,13 +187,17 @@ export class ObservableSanityClient {
|
|
|
177
187
|
* @param params - Optional query parameters
|
|
178
188
|
* @param options - Request options
|
|
179
189
|
*/
|
|
180
|
-
fetch<
|
|
181
|
-
|
|
190
|
+
fetch<
|
|
191
|
+
R = Any,
|
|
192
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
193
|
+
const G extends string = string,
|
|
194
|
+
>(
|
|
195
|
+
query: G,
|
|
182
196
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
183
197
|
options: UnfilteredResponseWithoutQuery,
|
|
184
|
-
): Observable<RawQuerylessQueryResponse<R
|
|
185
|
-
fetch<R, Q>(
|
|
186
|
-
query:
|
|
198
|
+
): Observable<RawQuerylessQueryResponse<ClientReturn<G, R>>>
|
|
199
|
+
fetch<R, Q, const G extends string>(
|
|
200
|
+
query: G,
|
|
187
201
|
params?: Q,
|
|
188
202
|
options?: QueryOptions,
|
|
189
203
|
): Observable<RawQueryResponse<R> | R> {
|
|
@@ -808,10 +822,11 @@ export class SanityClient {
|
|
|
808
822
|
*
|
|
809
823
|
* @param query - GROQ-query to perform
|
|
810
824
|
*/
|
|
811
|
-
fetch<
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
825
|
+
fetch<
|
|
826
|
+
R = Any,
|
|
827
|
+
Q extends QueryWithoutParams = QueryWithoutParams,
|
|
828
|
+
const G extends string = string,
|
|
829
|
+
>(query: G, params?: Q | QueryWithoutParams): Promise<ClientReturn<G, R>>
|
|
815
830
|
/**
|
|
816
831
|
* Perform a GROQ-query against the configured dataset.
|
|
817
832
|
*
|
|
@@ -819,11 +834,15 @@ export class SanityClient {
|
|
|
819
834
|
* @param params - Optional query parameters
|
|
820
835
|
* @param options - Optional request options
|
|
821
836
|
*/
|
|
822
|
-
fetch<
|
|
823
|
-
|
|
837
|
+
fetch<
|
|
838
|
+
R = Any,
|
|
839
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
840
|
+
const G extends string = string,
|
|
841
|
+
>(
|
|
842
|
+
query: G,
|
|
824
843
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
825
844
|
options?: FilteredResponseQueryOptions,
|
|
826
|
-
): Promise<R
|
|
845
|
+
): Promise<ClientReturn<G, R>>
|
|
827
846
|
/**
|
|
828
847
|
* Perform a GROQ-query against the configured dataset.
|
|
829
848
|
*
|
|
@@ -831,11 +850,15 @@ export class SanityClient {
|
|
|
831
850
|
* @param params - Optional query parameters
|
|
832
851
|
* @param options - Request options
|
|
833
852
|
*/
|
|
834
|
-
fetch<
|
|
835
|
-
|
|
853
|
+
fetch<
|
|
854
|
+
R = Any,
|
|
855
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
856
|
+
const G extends string = string,
|
|
857
|
+
>(
|
|
858
|
+
query: G,
|
|
836
859
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
837
860
|
options: UnfilteredResponseQueryOptions,
|
|
838
|
-
): Promise<RawQueryResponse<R
|
|
861
|
+
): Promise<RawQueryResponse<ClientReturn<G, R>>>
|
|
839
862
|
/**
|
|
840
863
|
* Perform a GROQ-query against the configured dataset.
|
|
841
864
|
*
|
|
@@ -843,14 +866,22 @@ export class SanityClient {
|
|
|
843
866
|
* @param params - Optional query parameters
|
|
844
867
|
* @param options - Request options
|
|
845
868
|
*/
|
|
846
|
-
fetch<
|
|
847
|
-
|
|
869
|
+
fetch<
|
|
870
|
+
R = Any,
|
|
871
|
+
Q extends QueryWithoutParams | QueryParams = QueryParams,
|
|
872
|
+
const G extends string = string,
|
|
873
|
+
>(
|
|
874
|
+
query: G,
|
|
848
875
|
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
|
|
849
876
|
options: UnfilteredResponseWithoutQuery,
|
|
850
|
-
): Promise<RawQuerylessQueryResponse<R
|
|
851
|
-
fetch<R, Q
|
|
877
|
+
): Promise<RawQuerylessQueryResponse<ClientReturn<G, R>>>
|
|
878
|
+
fetch<R, Q, const G extends string>(
|
|
879
|
+
query: G,
|
|
880
|
+
params?: Q,
|
|
881
|
+
options?: QueryOptions,
|
|
882
|
+
): Promise<RawQueryResponse<ClientReturn<G, R>> | ClientReturn<G, R>> {
|
|
852
883
|
return lastValueFrom(
|
|
853
|
-
dataMethods._fetch<
|
|
884
|
+
dataMethods._fetch<ClientReturn<G, R>, Q>(
|
|
854
885
|
this,
|
|
855
886
|
this.#httpRequest,
|
|
856
887
|
this.#clientConfig.stega,
|
package/src/data/listen.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {domainSharder as sharder} from '../http/domainSharding'
|
|
4
3
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
4
|
import type {Any, ListenEvent, ListenOptions, ListenParams, MutationEvent} from '../types'
|
|
6
5
|
import defaults from '../util/defaults'
|
|
@@ -65,11 +64,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
65
64
|
const listenOpts = pick(options, possibleOptions)
|
|
66
65
|
const qs = encodeQueryString({query, params, options: {tag, ...listenOpts}})
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
if (this.config().useDomainSharding) {
|
|
70
|
-
uri = sharder.getShardedUrl(uri)
|
|
71
|
-
}
|
|
72
|
-
|
|
67
|
+
const uri = `${url}${_getDataUrl(this, 'listen', qs)}`
|
|
73
68
|
if (uri.length > MAX_URL_LENGTH) {
|
|
74
69
|
return new Observable((observer) => observer.error(new Error('Query too large for listener')))
|
|
75
70
|
}
|
|
@@ -96,12 +91,6 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
96
91
|
// Once it is`true`, it will never be `false` again.
|
|
97
92
|
let unsubscribed = false
|
|
98
93
|
|
|
99
|
-
// We're about to connect, and will reuse the same shard/bucket for every reconnect henceforth.
|
|
100
|
-
// This may seem inoptimal, but once connected we should just consider this as a "permanent"
|
|
101
|
-
// connection, since we'll automatically retry on failures/disconnects. Once we explicitly
|
|
102
|
-
// unsubsccribe, we can decrement the bucket and free up the shard.
|
|
103
|
-
sharder.incrementBucketForUrl(uri)
|
|
104
|
-
|
|
105
94
|
open()
|
|
106
95
|
|
|
107
96
|
function onError() {
|
|
@@ -198,7 +187,6 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
198
187
|
stopped = true
|
|
199
188
|
unsubscribe()
|
|
200
189
|
unsubscribed = true
|
|
201
|
-
sharder.decrementBucketForUrl(uri)
|
|
202
190
|
}
|
|
203
191
|
|
|
204
192
|
return stop
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import {agent, debug, headers} from 'get-it/middleware'
|
|
2
2
|
|
|
3
3
|
import {name, version} from '../../package.json'
|
|
4
|
-
import {domainSharder} from './domainSharding'
|
|
5
4
|
|
|
6
5
|
const middleware = [
|
|
7
|
-
domainSharder.middleware,
|
|
8
|
-
|
|
9
6
|
debug({verbose: true, namespace: 'sanity:client'}),
|
|
10
7
|
headers({'User-Agent': `${name} ${version}`}),
|
|
11
8
|
|
|
@@ -29,7 +29,6 @@ export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOp
|
|
|
29
29
|
proxy: overrides.proxy || config.proxy,
|
|
30
30
|
json: true,
|
|
31
31
|
withCredentials,
|
|
32
|
-
useDomainSharding: config.useDomainSharding,
|
|
33
32
|
fetch:
|
|
34
33
|
typeof overrides.fetch === 'object' && typeof config.fetch === 'object'
|
|
35
34
|
? {...config.fetch, ...overrides.fetch}
|
package/src/types.ts
CHANGED
|
@@ -46,14 +46,6 @@ export interface ClientConfig {
|
|
|
46
46
|
apiVersion?: string
|
|
47
47
|
proxy?: string
|
|
48
48
|
|
|
49
|
-
/**
|
|
50
|
-
* Spread the requests over a number of hostnames to work around HTTP/1.1 limitations.
|
|
51
|
-
* Only applicable in browsers, and for certain allowed projects.
|
|
52
|
-
*
|
|
53
|
-
* @alpha
|
|
54
|
-
*/
|
|
55
|
-
useDomainSharding?: boolean
|
|
56
|
-
|
|
57
49
|
/**
|
|
58
50
|
* Optional request tag prefix for all request tags
|
|
59
51
|
*/
|
|
@@ -1244,6 +1236,15 @@ export interface LiveEventMessage {
|
|
|
1244
1236
|
tags: SyncTag[]
|
|
1245
1237
|
}
|
|
1246
1238
|
|
|
1239
|
+
/** @public */
|
|
1240
|
+
export interface SanityQueries {}
|
|
1241
|
+
|
|
1242
|
+
/** @public */
|
|
1243
|
+
export type ClientReturn<
|
|
1244
|
+
GroqString extends string,
|
|
1245
|
+
Fallback = Any,
|
|
1246
|
+
> = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
|
|
1247
|
+
|
|
1247
1248
|
export type {
|
|
1248
1249
|
ContentSourceMapParsedPath,
|
|
1249
1250
|
ContentSourceMapParsedPathKeyedSegment,
|
package/umd/sanityClient.js
CHANGED
|
@@ -2393,7 +2393,6 @@ ${selectionOpts}`);
|
|
|
2393
2393
|
proxy: overrides.proxy || config.proxy,
|
|
2394
2394
|
json: !0,
|
|
2395
2395
|
withCredentials,
|
|
2396
|
-
useDomainSharding: config.useDomainSharding,
|
|
2397
2396
|
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
|
|
2398
2397
|
});
|
|
2399
2398
|
}
|
|
@@ -2663,55 +2662,6 @@ ${selectionOpts}`);
|
|
|
2663
2662
|
opts
|
|
2664
2663
|
);
|
|
2665
2664
|
}
|
|
2666
|
-
const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.r(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
|
|
2667
|
-
function getDomainSharder(initialBuckets) {
|
|
2668
|
-
const buckets = new Array(3).fill(0, 0);
|
|
2669
|
-
function incrementBucketForUrl(url) {
|
|
2670
|
-
const shard = getShardFromUrl(url);
|
|
2671
|
-
shard !== null && buckets[shard]++;
|
|
2672
|
-
}
|
|
2673
|
-
function decrementBucketForUrl(url) {
|
|
2674
|
-
const shard = getShardFromUrl(url);
|
|
2675
|
-
shard !== null && buckets[shard]--;
|
|
2676
|
-
}
|
|
2677
|
-
function getShardedUrl(url) {
|
|
2678
|
-
const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
|
|
2679
|
-
if (!isMatch)
|
|
2680
|
-
return url;
|
|
2681
|
-
const bucket = buckets.reduce(
|
|
2682
|
-
(smallest, count, index) => count < buckets[smallest] ? index : smallest,
|
|
2683
|
-
0
|
|
2684
|
-
);
|
|
2685
|
-
return `https://${projectId2}.api.r${bucket + 1}.${rest}`;
|
|
2686
|
-
}
|
|
2687
|
-
function getShardFromUrl(url) {
|
|
2688
|
-
const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
|
|
2689
|
-
return isMatch ? parseInt(shard, 10) - 1 : null;
|
|
2690
|
-
}
|
|
2691
|
-
return {
|
|
2692
|
-
middleware: {
|
|
2693
|
-
processOptions: (options) => {
|
|
2694
|
-
if (!useDomainSharding(options))
|
|
2695
|
-
return options;
|
|
2696
|
-
const url = getShardedUrl(options.url);
|
|
2697
|
-
return options.url = url, options;
|
|
2698
|
-
},
|
|
2699
|
-
onRequest(req) {
|
|
2700
|
-
return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
|
|
2701
|
-
},
|
|
2702
|
-
onResponse(res, context) {
|
|
2703
|
-
return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
|
|
2704
|
-
}
|
|
2705
|
-
},
|
|
2706
|
-
incrementBucketForUrl,
|
|
2707
|
-
decrementBucketForUrl,
|
|
2708
|
-
getShardedUrl,
|
|
2709
|
-
getBuckets: () => buckets
|
|
2710
|
-
};
|
|
2711
|
-
}
|
|
2712
|
-
function useDomainSharding(options) {
|
|
2713
|
-
return "useDomainSharding" in options && options.useDomainSharding === !0;
|
|
2714
|
-
}
|
|
2715
2665
|
var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
|
|
2716
2666
|
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
2717
2667
|
"includePreviousRevision",
|
|
@@ -2723,16 +2673,15 @@ ${selectionOpts}`);
|
|
|
2723
2673
|
includeResult: !0
|
|
2724
2674
|
};
|
|
2725
2675
|
function _listen(query, params, opts = {}) {
|
|
2726
|
-
const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } })
|
|
2727
|
-
|
|
2728
|
-
if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
|
|
2676
|
+
const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } }), uri = `${url}${_getDataUrl(this, "listen", qs)}`;
|
|
2677
|
+
if (uri.length > MAX_URL_LENGTH)
|
|
2729
2678
|
return new Observable((observer) => observer.error(new Error("Query too large for listener")));
|
|
2730
2679
|
const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
|
|
2731
2680
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
2732
2681
|
Authorization: `Bearer ${token}`
|
|
2733
2682
|
}), new Observable((observer) => {
|
|
2734
2683
|
let es, reconnectTimer, stopped = !1, unsubscribed = !1;
|
|
2735
|
-
|
|
2684
|
+
open();
|
|
2736
2685
|
function onError() {
|
|
2737
2686
|
stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
|
|
2738
2687
|
}
|
|
@@ -2767,7 +2716,7 @@ ${selectionOpts}`);
|
|
|
2767
2716
|
});
|
|
2768
2717
|
}
|
|
2769
2718
|
function stop() {
|
|
2770
|
-
stopped = !0, unsubscribe(), unsubscribed = !0
|
|
2719
|
+
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
2771
2720
|
}
|
|
2772
2721
|
return stop;
|
|
2773
2722
|
});
|
|
@@ -3388,7 +3337,7 @@ ${selectionOpts}`);
|
|
|
3388
3337
|
return printNoDefaultExport(), createClient2(config);
|
|
3389
3338
|
};
|
|
3390
3339
|
}
|
|
3391
|
-
var envMiddleware = [
|
|
3340
|
+
var envMiddleware = [];
|
|
3392
3341
|
const exp = defineCreateClientExports(envMiddleware, SanityClient), requester = exp.requester, createClient = exp.createClient, deprecatedCreateClient = defineDeprecatedCreateClient(createClient);
|
|
3393
3342
|
|
|
3394
3343
|
const reKeySegment = /_key\s*==\s*['"](.*)['"]/;
|