@sanity/client 5.0.0 → 5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "5.0.0",
3
+ "version": "5.2.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -89,7 +89,6 @@
89
89
  },
90
90
  "dependencies": {
91
91
  "@sanity/eventsource": "^4",
92
- "@types/node": "^18.11.18",
93
92
  "get-it": "^8",
94
93
  "rxjs": "^7"
95
94
  },
@@ -100,25 +99,26 @@
100
99
  "@rollup/plugin-node-resolve": "^15.0.1",
101
100
  "@sanity/pkg-utils": "^2.2.3",
102
101
  "@sanity/semantic-release-preset": "^4.0.0",
103
- "@typescript-eslint/eslint-plugin": "^5.50.0",
104
- "@typescript-eslint/parser": "^5.50.0",
105
- "@vitest/coverage-c8": "^0.28.3",
102
+ "@types/node": "^18.13.0",
103
+ "@typescript-eslint/eslint-plugin": "^5.51.0",
104
+ "@typescript-eslint/parser": "^5.51.0",
105
+ "@vitest/coverage-c8": "^0.28.4",
106
106
  "eslint": "^8.33.0",
107
107
  "eslint-config-prettier": "^8.6.0",
108
108
  "eslint-plugin-prettier": "^4.2.1",
109
109
  "eslint-plugin-simple-import-sort": "^10.0.0",
110
- "faucet": "^0.0.3",
111
- "happy-dom": "^8.2.3",
110
+ "faucet": "^0.0.4",
111
+ "happy-dom": "^8.2.6",
112
112
  "ls-engines": "^0.9.0",
113
113
  "nock": "^13.3.0",
114
114
  "prettier": "^2.8.3",
115
115
  "prettier-plugin-packagejson": "^2.4.2",
116
116
  "rimraf": "^4.1.2",
117
- "rollup": "^3.12.1",
117
+ "rollup": "^3.14.0",
118
118
  "sse-channel": "^4.0.0",
119
- "terser": "^5.16.2",
119
+ "terser": "^5.16.3",
120
120
  "typescript": "^4.9.5",
121
- "vitest": "^0.28.3",
121
+ "vitest": "^0.28.4",
122
122
  "vitest-github-actions-reporter": "^0.9.0"
123
123
  },
124
124
  "engines": {
@@ -613,6 +613,26 @@ export class ObservableSanityClient {
613
613
  request<R = FIXME>(options: RawRequestOptions): Observable<R> {
614
614
  return dataMethods._request(this, this.#httpRequest, options)
615
615
  }
616
+
617
+ /**
618
+ * Get a Sanity API URL for the URI provided
619
+ *
620
+ * @param uri - URI/path to build URL for
621
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
622
+ */
623
+ getUrl(uri: string, canUseCdn?: boolean): string {
624
+ return dataMethods._getUrl(this, uri, canUseCdn)
625
+ }
626
+
627
+ /**
628
+ * Get a Sanity API URL for the data operation and path provided
629
+ *
630
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
631
+ * @param path - Path to append after the operation
632
+ */
633
+ getDataUrl(operation: string, path?: string): string {
634
+ return dataMethods._getDataUrl(this, operation, path)
635
+ }
616
636
  }
617
637
 
618
638
  /** @public */
@@ -1210,4 +1230,24 @@ export class SanityClient {
1210
1230
  dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<FIXME> {
1211
1231
  return lastValueFrom(dataMethods._dataRequest(this, this.#httpRequest, endpoint, body, options))
1212
1232
  }
1233
+
1234
+ /**
1235
+ * Get a Sanity API URL for the URI provided
1236
+ *
1237
+ * @param uri - URI/path to build URL for
1238
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
1239
+ */
1240
+ getUrl(uri: string, canUseCdn?: boolean): string {
1241
+ return dataMethods._getUrl(this, uri, canUseCdn)
1242
+ }
1243
+
1244
+ /**
1245
+ * Get a Sanity API URL for the data operation and path provided
1246
+ *
1247
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
1248
+ * @param path - Path to append after the operation
1249
+ */
1250
+ getDataUrl(operation: string, path?: string): string {
1251
+ return dataMethods._getDataUrl(this, operation, path)
1252
+ }
1213
1253
  }
@@ -1,4 +1,4 @@
1
- import {Observable} from 'rxjs'
1
+ import {type MonoTypeOperatorFunction, Observable} from 'rxjs'
2
2
  import {filter, map} from 'rxjs/operators'
3
3
 
4
4
  import getRequestOptions from '../http/requestOptions'
@@ -224,6 +224,7 @@ export function _dataRequest(
224
224
  token,
225
225
  tag,
226
226
  canUseCdn: isQuery,
227
+ signal: options.signal,
227
228
  }
228
229
 
229
230
  return _requestObservable(client, httpRequest, reqOptions).pipe(
@@ -307,10 +308,12 @@ export function _requestObservable<R>(
307
308
  })
308
309
  ) as RequestOptions
309
310
 
310
- return new Observable<HttpRequestEvent<R>>((subscriber) =>
311
+ const request = new Observable<HttpRequestEvent<R>>((subscriber) =>
311
312
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the typings thinks it's optional because it's not required to specify it when calling createClient, but it's always defined in practice since SanityClient provides a default
312
313
  httpRequest(reqOptions, config.requester!).subscribe(subscriber)
313
314
  )
315
+
316
+ return options.signal ? request.pipe(_withAbortSignal(options.signal)) : request
314
317
  }
315
318
 
316
319
  /**
@@ -356,3 +359,50 @@ export function _getUrl(
356
359
  const base = canUseCdn ? cdnUrl : url
357
360
  return `${base}/${uri.replace(/^\//, '')}`
358
361
  }
362
+
363
+ /**
364
+ * @internal
365
+ */
366
+ function _withAbortSignal<T>(signal: AbortSignal): MonoTypeOperatorFunction<T> {
367
+ return (input) => {
368
+ return new Observable((observer) => {
369
+ const abort = () => observer.error(_createAbortError(signal))
370
+
371
+ if (signal && signal.aborted) {
372
+ abort()
373
+ return
374
+ }
375
+ const subscription = input.subscribe(observer)
376
+ signal.addEventListener('abort', abort)
377
+ return () => {
378
+ signal.removeEventListener('abort', abort)
379
+ subscription.unsubscribe()
380
+ }
381
+ })
382
+ }
383
+ }
384
+ // DOMException is supported on most modern browsers and Node.js 18+.
385
+ // @see https://developer.mozilla.org/en-US/docs/Web/API/DOMException#browser_compatibility
386
+ const isDomExceptionSupported = Boolean(globalThis.DOMException)
387
+
388
+ /**
389
+ * @internal
390
+ * @param signal
391
+ * Original source copied from https://github.com/sindresorhus/ky/blob/740732c78aad97e9aec199e9871bdbf0ae29b805/source/errors/DOMException.ts
392
+ * TODO: When targeting Node.js 18, use `signal.throwIfAborted()` (https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/throwIfAborted)
393
+ */
394
+ function _createAbortError(signal?: AbortSignal) {
395
+ /*
396
+ NOTE: Use DomException with AbortError name as specified in MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort)
397
+ > When abort() is called, the fetch() promise rejects with an Error of type DOMException, with name AbortError.
398
+ */
399
+ if (isDomExceptionSupported) {
400
+ return new DOMException(signal?.reason ?? 'The operation was aborted.', 'AbortError')
401
+ }
402
+
403
+ // DOMException not supported. Fall back to use of error and override name.
404
+ const error = new Error(signal?.reason ?? 'The operation was aborted.')
405
+ error.name = 'AbortError'
406
+
407
+ return error
408
+ }
@@ -16,3 +16,5 @@ export const requester = httpRequest.defaultRequester
16
16
 
17
17
  /** @public */
18
18
  export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)
19
+
20
+ export {migrationNotice as default} from './migrationNotice'
package/src/index.ts CHANGED
@@ -16,3 +16,5 @@ export const requester = httpRequest.defaultRequester
16
16
 
17
17
  /** @public */
18
18
  export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)
19
+
20
+ export {migrationNotice as default} from './migrationNotice'
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @public
3
+ * @deprecated Use the named export `createClient` instead of the `default` export
4
+ */
5
+ export function migrationNotice() {
6
+ throw new TypeError(
7
+ 'The default export of @sanity/client has been deprecated. Use the named export `createClient` instead'
8
+ )
9
+ }
package/src/types.ts CHANGED
@@ -15,6 +15,7 @@ export interface RequestOptions {
15
15
  method?: string
16
16
  query?: FIXME
17
17
  body?: FIXME
18
+ signal?: AbortSignal
18
19
  }
19
20
 
20
21
  /** @public */
@@ -3840,7 +3840,8 @@
3840
3840
  headers,
3841
3841
  token,
3842
3842
  tag,
3843
- canUseCdn: isQuery
3843
+ canUseCdn: isQuery,
3844
+ signal: options.signal
3844
3845
  };
3845
3846
  return _requestObservable(client, httpRequest, reqOptions).pipe(filter(isResponse), map(getBody), map(res => {
3846
3847
  if (!isMutation) {
@@ -3887,9 +3888,10 @@
3887
3888
  const reqOptions = getRequestOptions(config, Object.assign({}, options, {
3888
3889
  url: _getUrl(client, uri, useCdn)
3889
3890
  }));
3890
- return new Observable(subscriber =>
3891
+ const request = new Observable(subscriber =>
3891
3892
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the typings thinks it's optional because it's not required to specify it when calling createClient, but it's always defined in practice since SanityClient provides a default
3892
3893
  httpRequest(reqOptions, config.requester).subscribe(subscriber));
3894
+ return options.signal ? request.pipe(_withAbortSignal(options.signal)) : request;
3893
3895
  }
3894
3896
  function _request(client, httpRequest, options) {
3895
3897
  const observable = _requestObservable(client, httpRequest, options).pipe(filter(event => event.type === "response"), map(event => event.body));
@@ -3911,6 +3913,33 @@
3911
3913
  const base = canUseCdn ? cdnUrl : url;
3912
3914
  return "".concat(base, "/").concat(uri.replace(/^\//, ""));
3913
3915
  }
3916
+ function _withAbortSignal(signal) {
3917
+ return input => {
3918
+ return new Observable(observer => {
3919
+ const abort = () => observer.error(_createAbortError(signal));
3920
+ if (signal && signal.aborted) {
3921
+ abort();
3922
+ return;
3923
+ }
3924
+ const subscription = input.subscribe(observer);
3925
+ signal.addEventListener("abort", abort);
3926
+ return () => {
3927
+ signal.removeEventListener("abort", abort);
3928
+ subscription.unsubscribe();
3929
+ };
3930
+ });
3931
+ };
3932
+ }
3933
+ const isDomExceptionSupported = Boolean(globalThis.DOMException);
3934
+ function _createAbortError(signal) {
3935
+ var _a, _b;
3936
+ if (isDomExceptionSupported) {
3937
+ return new DOMException((_a = signal == null ? void 0 : signal.reason) != null ? _a : "The operation was aborted.", "AbortError");
3938
+ }
3939
+ const error = new Error((_b = signal == null ? void 0 : signal.reason) != null ? _b : "The operation was aborted.");
3940
+ error.name = "AbortError";
3941
+ return error;
3942
+ }
3914
3943
  var __accessCheck$4 = (obj, member, msg) => {
3915
3944
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
3916
3945
  };
@@ -4616,6 +4645,24 @@
4616
4645
  request(options) {
4617
4646
  return _request(this, __privateGet(this, _httpRequest), options);
4618
4647
  }
4648
+ /**
4649
+ * Get a Sanity API URL for the URI provided
4650
+ *
4651
+ * @param uri - URI/path to build URL for
4652
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
4653
+ */
4654
+ getUrl(uri, canUseCdn) {
4655
+ return _getUrl(this, uri, canUseCdn);
4656
+ }
4657
+ /**
4658
+ * Get a Sanity API URL for the data operation and path provided
4659
+ *
4660
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
4661
+ * @param path - Path to append after the operation
4662
+ */
4663
+ getDataUrl(operation, path) {
4664
+ return _getDataUrl(this, operation, path);
4665
+ }
4619
4666
  };
4620
4667
  let ObservableSanityClient = _ObservableSanityClient;
4621
4668
  _clientConfig = new WeakMap();
@@ -4749,10 +4796,31 @@
4749
4796
  dataRequest(endpoint, body, options) {
4750
4797
  return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
4751
4798
  }
4799
+ /**
4800
+ * Get a Sanity API URL for the URI provided
4801
+ *
4802
+ * @param uri - URI/path to build URL for
4803
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
4804
+ */
4805
+ getUrl(uri, canUseCdn) {
4806
+ return _getUrl(this, uri, canUseCdn);
4807
+ }
4808
+ /**
4809
+ * Get a Sanity API URL for the data operation and path provided
4810
+ *
4811
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
4812
+ * @param path - Path to append after the operation
4813
+ */
4814
+ getDataUrl(operation, path) {
4815
+ return _getDataUrl(this, operation, path);
4816
+ }
4752
4817
  };
4753
4818
  let SanityClient = _SanityClient;
4754
4819
  _clientConfig2 = new WeakMap();
4755
4820
  _httpRequest2 = new WeakMap();
4821
+ function migrationNotice() {
4822
+ throw new TypeError("The default export of @sanity/client has been deprecated. Use the named export `createClient` instead");
4823
+ }
4756
4824
  const httpRequest = defineHttpRequest(envMiddleware);
4757
4825
  const requester = httpRequest.defaultRequester;
4758
4826
  const createClient = config => new SanityClient(httpRequest, config);
@@ -4768,6 +4836,9 @@
4768
4836
  exports.ServerError = ServerError;
4769
4837
  exports.Transaction = Transaction;
4770
4838
  exports.createClient = createClient;
4839
+ exports.default = migrationNotice;
4771
4840
  exports.requester = requester;
4772
4841
 
4842
+ Object.defineProperty(exports, '__esModule', { value: true });
4843
+
4773
4844
  }));