@sanity/client 6.1.3-perspective.0 → 6.2.0-fetch.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "6.1.3-perspective.0",
3
+ "version": "6.2.0-fetch.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -71,6 +71,7 @@
71
71
  "prepublishOnly": "npm run build",
72
72
  "rollup": "NODE_ENV=production rollup -c rollup.config.cjs",
73
73
  "test": "vitest",
74
+ "type-check": "tsc --noEmit",
74
75
  "test:browser": "npm test -- --config ./vitest.browser.config.ts",
75
76
  "test:bun": "(cd runtimes/bun && bun wiptest)",
76
77
  "test:deno": "deno test --allow-read --allow-net --allow-env --fail-fast --import-map=runtimes/deno/import_map.json runtimes/deno",
@@ -91,7 +92,7 @@
91
92
  },
92
93
  "dependencies": {
93
94
  "@sanity/eventsource": "^5.0.0",
94
- "get-it": "^8.1.0",
95
+ "get-it": "8.2.0-fetch.0",
95
96
  "rxjs": "^7.0.0"
96
97
  },
97
98
  "devDependencies": {
@@ -69,8 +69,13 @@ export function _fetch<R, Q extends QueryParams>(
69
69
  ): Observable<RawQueryResponse<R> | R> {
70
70
  const mapResponse =
71
71
  options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
72
+ const {cache, next, ...opts} = options
73
+ const reqOpts =
74
+ typeof cache !== 'undefined' || typeof next !== 'undefined'
75
+ ? {...opts, fetch: {cache, next}}
76
+ : opts
72
77
 
73
- return _dataRequest(client, httpRequest, 'query', {query, params}, options).pipe(map(mapResponse))
78
+ return _dataRequest(client, httpRequest, 'query', {query, params}, reqOpts).pipe(map(mapResponse))
74
79
  }
75
80
 
76
81
  /** @internal */
@@ -226,6 +231,7 @@ export function _dataRequest(
226
231
  tag,
227
232
  canUseCdn: isQuery,
228
233
  signal: options.signal,
234
+ fetch: options.fetch,
229
235
  }
230
236
 
231
237
  return _requestObservable(client, httpRequest, reqOptions).pipe(
package/src/types.ts CHANGED
@@ -28,6 +28,9 @@ export interface RequestOptions {
28
28
  signal?: AbortSignal
29
29
  }
30
30
 
31
+ /** @public */
32
+ export type ClientPerspective = 'previewDrafts' | 'published' | 'raw'
33
+
31
34
  /** @public */
32
35
  export interface ClientConfig {
33
36
  projectId?: string
@@ -35,8 +38,7 @@ export interface ClientConfig {
35
38
  /** @defaultValue true */
36
39
  useCdn?: boolean
37
40
  token?: string
38
- /** @defaultValue 'raw' */
39
- perspective?: 'previewDrafts' | 'published' | 'raw'
41
+ perspective?: ClientPerspective
40
42
  apiHost?: string
41
43
  apiVersion?: string
42
44
  proxy?: string
@@ -483,12 +485,18 @@ export interface ListenOptions {
483
485
  }
484
486
 
485
487
  /** @public */
486
- export type FilteredResponseQueryOptions = RequestOptions & {
488
+ export type ResponseQueryOptions<T = 'next'> = RequestOptions & {
489
+ cache?: RequestInit['cache']
490
+ next?: T extends keyof RequestInit ? RequestInit[T] : never
491
+ }
492
+
493
+ /** @public */
494
+ export type FilteredResponseQueryOptions = ResponseQueryOptions & {
487
495
  filterResponse?: true
488
496
  }
489
497
 
490
498
  /** @public */
491
- export type UnfilteredResponseQueryOptions = RequestOptions & {
499
+ export type UnfilteredResponseQueryOptions = ResponseQueryOptions & {
492
500
  filterResponse: false
493
501
  }
494
502
 
@@ -8,10 +8,13 @@
8
8
  const defaultOptions$1 = {
9
9
  timeout: isReactNative ? 6e4 : 12e4
10
10
  };
11
- function processOptions(opts) {
12
- const options = typeof opts === "string" ? Object.assign({
13
- url: opts
14
- }, defaultOptions$1) : Object.assign({}, defaultOptions$1, opts);
11
+ const processOptions = function processOptions2(opts) {
12
+ const options = {
13
+ ...defaultOptions$1,
14
+ ...(typeof opts === "string" ? {
15
+ url: opts
16
+ } : opts)
17
+ };
15
18
  const url = new URL(options.url, "http://localhost");
16
19
  options.timeout = normalizeTimeout(options.timeout);
17
20
  if (options.query) {
@@ -30,7 +33,7 @@
30
33
  options.method = options.body && !options.method ? "POST" : (options.method || "GET").toUpperCase();
31
34
  options.url = url.origin === "http://localhost" ? "".concat(url.pathname, "?").concat(url.searchParams) : url.toString();
32
35
  return options;
33
- }
36
+ };
34
37
  function normalizeTimeout(time) {
35
38
  if (time === false || time === 0) {
36
39
  return false;
@@ -48,11 +51,11 @@
48
51
  };
49
52
  }
50
53
  const validUrl = /^https?:\/\//i;
51
- function validateOptions(options) {
54
+ const validateOptions = function validateOptions2(options) {
52
55
  if (!validUrl.test(options.url)) {
53
56
  throw new Error("\"".concat(options.url, "\" is not a valid URL"));
54
57
  }
55
- }
58
+ };
56
59
 
57
60
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
58
61
 
@@ -95,23 +98,20 @@
95
98
 
96
99
  var parseHeaders$1 = /*@__PURE__*/getDefaultExportFromCjs(parseHeaders);
97
100
 
98
- var middlewareReducer = middleware => {
99
- const applyMiddleware = function (hook, defaultValue) {
100
- const bailEarly = hook === "onError";
101
- let value = defaultValue;
102
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
103
- args[_key - 2] = arguments[_key];
104
- }
105
- for (let i = 0; i < middleware[hook].length; i++) {
106
- const handler = middleware[hook][i];
107
- value = handler(value, ...args);
108
- if (bailEarly && !value) {
109
- break;
110
- }
101
+ const middlewareReducer = middleware => function applyMiddleware(hook, defaultValue) {
102
+ const bailEarly = hook === "onError";
103
+ let value = defaultValue;
104
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
105
+ args[_key - 2] = arguments[_key];
106
+ }
107
+ for (let i = 0; i < middleware[hook].length; i++) {
108
+ const handler = middleware[hook][i];
109
+ value = handler(value, ...args);
110
+ if (bailEarly && !value) {
111
+ break;
111
112
  }
112
- return value;
113
- };
114
- return applyMiddleware;
113
+ }
114
+ return value;
115
115
  };
116
116
  function createPubSub() {
117
117
  const subscribers = /* @__PURE__ */Object.create(null);
@@ -143,8 +143,28 @@
143
143
  }, {
144
144
  processOptions: [processOptions],
145
145
  validateOptions: [validateOptions]
146
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
147
  });
148
+
147
149
  function request(opts) {
150
+ const onResponse = (reqErr, res, ctx) => {
151
+ let error = reqErr;
152
+ let response = res;
153
+ if (!error) {
154
+ try {
155
+ response = applyMiddleware("onResponse", res, ctx);
156
+ } catch (err) {
157
+ response = null;
158
+ error = err;
159
+ }
160
+ }
161
+ error = error && applyMiddleware("onError", error, ctx);
162
+ if (error) {
163
+ channels.error.publish(error);
164
+ } else if (response) {
165
+ channels.response.publish(response);
166
+ }
167
+ };
148
168
  const channels = channelNames.reduce((target, name) => {
149
169
  target[name] = createPubSub();
150
170
  return target;
@@ -157,7 +177,7 @@
157
177
  channels,
158
178
  applyMiddleware
159
179
  };
160
- let ongoingRequest = null;
180
+ let ongoingRequest;
161
181
  const unsubscribe = channels.request.subscribe(ctx => {
162
182
  ongoingRequest = httpRequest(ctx, (err, res) => onResponse(err, res, ctx));
163
183
  });
@@ -172,24 +192,6 @@
172
192
  channels.request.publish(context);
173
193
  }
174
194
  return returnValue;
175
- function onResponse(reqErr, res, ctx) {
176
- let error = reqErr;
177
- let response = res;
178
- if (!error) {
179
- try {
180
- response = applyMiddleware("onResponse", res, ctx);
181
- } catch (err) {
182
- response = null;
183
- error = err;
184
- }
185
- }
186
- error = error && applyMiddleware("onError", error, ctx);
187
- if (error) {
188
- channels.error.publish(error);
189
- } else if (response) {
190
- channels.response.publish(response);
191
- }
192
- }
193
195
  }
194
196
  request.use = function use(newMiddleware) {
195
197
  if (!newMiddleware) {
@@ -209,9 +211,7 @@
209
211
  loadedMiddleware.push(newMiddleware);
210
212
  return request;
211
213
  };
212
- request.clone = function clone() {
213
- return createRequester(loadedMiddleware, httpRequest);
214
- };
214
+ request.clone = () => createRequester(loadedMiddleware, httpRequest);
215
215
  initMiddleware.forEach(request.use);
216
216
  return request;
217
217
  }
@@ -317,7 +317,7 @@
317
317
  _controller = new WeakMap();
318
318
  const adapter = typeof XMLHttpRequest === "function" ? "xhr" : "fetch";
319
319
  const XmlHttpRequest = adapter === "xhr" ? XMLHttpRequest : FetchXhr;
320
- var httpRequester = (context, callback) => {
320
+ const httpRequester = (context, callback) => {
321
321
  const opts = context.options;
322
322
  const options = context.applyMiddleware("finalizeOptions", opts);
323
323
  const timers = {};
@@ -1258,7 +1258,7 @@
1258
1258
  }
1259
1259
  }
1260
1260
  }
1261
- let actualGlobal;
1261
+ let actualGlobal = {};
1262
1262
  if (typeof globalThis !== "undefined") {
1263
1263
  actualGlobal = globalThis;
1264
1264
  } else if (typeof window !== "undefined") {
@@ -1267,13 +1267,13 @@
1267
1267
  actualGlobal = global;
1268
1268
  } else if (typeof self !== "undefined") {
1269
1269
  actualGlobal = self;
1270
- } else {
1271
- actualGlobal = {};
1272
1270
  }
1273
1271
  var global$1 = actualGlobal;
1274
1272
  function observable$1() {
1275
1273
  let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1276
- const Observable = opts.implementation || global$1.Observable;
1274
+ const Observable =
1275
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- @TODO consider dropping checking for a global Observable since it's not on a standards track
1276
+ opts.implementation || global$1.Observable;
1277
1277
  if (!Observable) {
1278
1278
  throw new Error("`Observable` is not available in global scope, and no implementation was passed");
1279
1279
  }
@@ -1296,7 +1296,6 @@
1296
1296
  }
1297
1297
  function progress() {
1298
1298
  return {
1299
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1300
1299
  onRequest: evt => {
1301
1300
  if (evt.adapter !== "xhr") {
1302
1301
  return;
@@ -2090,12 +2089,26 @@
2090
2089
  }
2091
2090
 
2092
2091
  var envMiddleware = [];
2092
+ var __defProp$3 = Object.defineProperty;
2093
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, {
2094
+ enumerable: true,
2095
+ configurable: true,
2096
+ writable: true,
2097
+ value
2098
+ }) : obj[key] = value;
2099
+ var __publicField$3 = (obj, key, value) => {
2100
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
2101
+ return value;
2102
+ };
2093
2103
  const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
2094
2104
  class ClientError extends Error {
2095
2105
  constructor(res) {
2096
2106
  const props = extractErrorProps(res);
2097
2107
  super(props.message);
2098
- this.statusCode = 400;
2108
+ __publicField$3(this, "response");
2109
+ __publicField$3(this, "statusCode", 400);
2110
+ __publicField$3(this, "responseBody");
2111
+ __publicField$3(this, "details");
2099
2112
  Object.assign(this, props);
2100
2113
  }
2101
2114
  }
@@ -2103,7 +2116,10 @@
2103
2116
  constructor(res) {
2104
2117
  const props = extractErrorProps(res);
2105
2118
  super(props.message);
2106
- this.statusCode = 500;
2119
+ __publicField$3(this, "response");
2120
+ __publicField$3(this, "statusCode", 500);
2121
+ __publicField$3(this, "responseBody");
2122
+ __publicField$3(this, "details");
2107
2123
  Object.assign(this, props);
2108
2124
  }
2109
2125
  }
@@ -2324,6 +2340,17 @@
2324
2340
  }
2325
2341
  return "?".concat(searchParams);
2326
2342
  };
2343
+ var __defProp$2 = Object.defineProperty;
2344
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, {
2345
+ enumerable: true,
2346
+ configurable: true,
2347
+ writable: true,
2348
+ value
2349
+ }) : obj[key] = value;
2350
+ var __publicField$2 = (obj, key, value) => {
2351
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
2352
+ return value;
2353
+ };
2327
2354
  var __accessCheck$6 = (obj, member, msg) => {
2328
2355
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
2329
2356
  };
@@ -2344,6 +2371,8 @@
2344
2371
  class BasePatch {
2345
2372
  constructor(selection) {
2346
2373
  let operations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2374
+ __publicField$2(this, "selection");
2375
+ __publicField$2(this, "operations");
2347
2376
  this.selection = selection;
2348
2377
  this.operations = operations;
2349
2378
  }
@@ -2497,7 +2526,7 @@
2497
2526
  return this._assign(op, props, false);
2498
2527
  }
2499
2528
  }
2500
- const _ObservablePatch = class extends BasePatch {
2529
+ const _ObservablePatch = class _ObservablePatch extends BasePatch {
2501
2530
  constructor(selection, operations, client) {
2502
2531
  super(selection, operations);
2503
2532
  __privateAdd$6(this, _client$5, void 0);
@@ -2525,9 +2554,9 @@
2525
2554
  }, opts);
2526
2555
  }
2527
2556
  };
2528
- let ObservablePatch = _ObservablePatch;
2529
2557
  _client$5 = new WeakMap();
2530
- const _Patch = class extends BasePatch {
2558
+ let ObservablePatch = _ObservablePatch;
2559
+ const _Patch = class _Patch extends BasePatch {
2531
2560
  constructor(selection, operations, client) {
2532
2561
  super(selection, operations);
2533
2562
  __privateAdd$6(this, _client2$5, void 0);
@@ -2555,8 +2584,19 @@
2555
2584
  }, opts);
2556
2585
  }
2557
2586
  };
2558
- let Patch = _Patch;
2559
2587
  _client2$5 = new WeakMap();
2588
+ let Patch = _Patch;
2589
+ var __defProp$1 = Object.defineProperty;
2590
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, {
2591
+ enumerable: true,
2592
+ configurable: true,
2593
+ writable: true,
2594
+ value
2595
+ }) : obj[key] = value;
2596
+ var __publicField$1 = (obj, key, value) => {
2597
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
2598
+ return value;
2599
+ };
2560
2600
  var __accessCheck$5 = (obj, member, msg) => {
2561
2601
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
2562
2602
  };
@@ -2581,6 +2621,8 @@
2581
2621
  constructor() {
2582
2622
  let operations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2583
2623
  let transactionId = arguments.length > 1 ? arguments[1] : undefined;
2624
+ __publicField$1(this, "operations");
2625
+ __publicField$1(this, "trxId");
2584
2626
  this.operations = operations;
2585
2627
  this.trxId = transactionId;
2586
2628
  }
@@ -2669,7 +2711,7 @@
2669
2711
  return this;
2670
2712
  }
2671
2713
  }
2672
- const _Transaction = class extends BaseTransaction {
2714
+ const _Transaction = class _Transaction extends BaseTransaction {
2673
2715
  constructor(operations, client, transactionId) {
2674
2716
  super(operations, transactionId);
2675
2717
  __privateAdd$5(this, _client$4, void 0);
@@ -2714,9 +2756,9 @@
2714
2756
  });
2715
2757
  }
2716
2758
  };
2717
- let Transaction = _Transaction;
2718
2759
  _client$4 = new WeakMap();
2719
- const _ObservableTransaction = class extends BaseTransaction {
2760
+ let Transaction = _Transaction;
2761
+ const _ObservableTransaction = class _ObservableTransaction extends BaseTransaction {
2720
2762
  constructor(operations, client, transactionId) {
2721
2763
  super(operations, transactionId);
2722
2764
  __privateAdd$5(this, _client2$4, void 0);
@@ -2761,8 +2803,8 @@
2761
2803
  });
2762
2804
  }
2763
2805
  };
2764
- let ObservableTransaction = _ObservableTransaction;
2765
2806
  _client2$4 = new WeakMap();
2807
+ let ObservableTransaction = _ObservableTransaction;
2766
2808
  const excludeFalsey = (param, defValue) => {
2767
2809
  const value = typeof param === "undefined" ? defValue : param;
2768
2810
  return param === false ? void 0 : value;
@@ -2788,10 +2830,22 @@
2788
2830
  function _fetch(client, httpRequest, query, params) {
2789
2831
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
2790
2832
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
2833
+ const {
2834
+ cache,
2835
+ next,
2836
+ ...opts
2837
+ } = options;
2838
+ const reqOpts = typeof cache !== "undefined" || typeof next !== "undefined" ? {
2839
+ ...opts,
2840
+ fetch: {
2841
+ cache,
2842
+ next
2843
+ }
2844
+ } : opts;
2791
2845
  return _dataRequest(client, httpRequest, "query", {
2792
2846
  query,
2793
2847
  params
2794
- }, options).pipe(map(mapResponse));
2848
+ }, reqOpts).pipe(map(mapResponse));
2795
2849
  }
2796
2850
  function _getDocument(client, httpRequest, id) {
2797
2851
  let opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
@@ -2873,7 +2927,8 @@
2873
2927
  token,
2874
2928
  tag,
2875
2929
  canUseCdn: isQuery,
2876
- signal: options.signal
2930
+ signal: options.signal,
2931
+ fetch: options.fetch
2877
2932
  };
2878
2933
  return _requestObservable(client, httpRequest, reqOptions).pipe(filter(isResponse), map(getBody), map(res => {
2879
2934
  if (!isMutation) {
@@ -3575,6 +3630,17 @@
3575
3630
  }
3576
3631
  _client2 = new WeakMap();
3577
3632
  _httpRequest2$1 = new WeakMap();
3633
+ var __defProp = Object.defineProperty;
3634
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
3635
+ enumerable: true,
3636
+ configurable: true,
3637
+ writable: true,
3638
+ value
3639
+ }) : obj[key] = value;
3640
+ var __publicField = (obj, key, value) => {
3641
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
3642
+ return value;
3643
+ };
3578
3644
  var __accessCheck = (obj, member, msg) => {
3579
3645
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
3580
3646
  };
@@ -3592,9 +3658,13 @@
3592
3658
  return value;
3593
3659
  };
3594
3660
  var _clientConfig, _httpRequest, _clientConfig2, _httpRequest2;
3595
- const _ObservableSanityClient = class {
3661
+ const _ObservableSanityClient = class _ObservableSanityClient {
3596
3662
  constructor(httpRequest) {
3597
3663
  let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
3664
+ __publicField(this, "assets");
3665
+ __publicField(this, "datasets");
3666
+ __publicField(this, "projects");
3667
+ __publicField(this, "users");
3598
3668
  /**
3599
3669
  * Private properties
3600
3670
  */
@@ -3603,7 +3673,7 @@
3603
3673
  /**
3604
3674
  * Instance properties
3605
3675
  */
3606
- this.listen = _listen;
3676
+ __publicField(this, "listen", _listen);
3607
3677
  this.config(config);
3608
3678
  __privateSet(this, _httpRequest, httpRequest);
3609
3679
  this.assets = new ObservableAssetsClient(this, __privateGet(this, _httpRequest));
@@ -3725,12 +3795,20 @@
3725
3795
  return _getDataUrl(this, operation, path);
3726
3796
  }
3727
3797
  };
3728
- let ObservableSanityClient = _ObservableSanityClient;
3729
3798
  _clientConfig = new WeakMap();
3730
3799
  _httpRequest = new WeakMap();
3731
- const _SanityClient = class {
3800
+ let ObservableSanityClient = _ObservableSanityClient;
3801
+ const _SanityClient = class _SanityClient {
3732
3802
  constructor(httpRequest) {
3733
3803
  let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
3804
+ __publicField(this, "assets");
3805
+ __publicField(this, "datasets");
3806
+ __publicField(this, "projects");
3807
+ __publicField(this, "users");
3808
+ /**
3809
+ * Observable version of the Sanity client, with the same configuration as the promise-based one
3810
+ */
3811
+ __publicField(this, "observable");
3734
3812
  /**
3735
3813
  * Private properties
3736
3814
  */
@@ -3739,7 +3817,7 @@
3739
3817
  /**
3740
3818
  * Instance properties
3741
3819
  */
3742
- this.listen = _listen;
3820
+ __publicField(this, "listen", _listen);
3743
3821
  this.config(config);
3744
3822
  __privateSet(this, _httpRequest2, httpRequest);
3745
3823
  this.assets = new AssetsClient(this, __privateGet(this, _httpRequest2));
@@ -3879,9 +3957,9 @@
3879
3957
  return _getDataUrl(this, operation, path);
3880
3958
  }
3881
3959
  };
3882
- let SanityClient = _SanityClient;
3883
3960
  _clientConfig2 = new WeakMap();
3884
3961
  _httpRequest2 = new WeakMap();
3962
+ let SanityClient = _SanityClient;
3885
3963
  const httpRequest = defineHttpRequest(envMiddleware, {});
3886
3964
  const requester = httpRequest.defaultRequester;
3887
3965
  const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {