@sanity/client 0.0.0-dev.3 → 0.0.0-dev.5

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.d.ts CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  import {Observable} from 'rxjs'
4
4
  import {Requester} from 'get-it'
5
+ import {adapter as unstable__adapter} from 'get-it'
6
+ import {environment as unstable__environment} from 'get-it'
5
7
 
6
8
  /** @internal */
7
9
  export declare type AllDocumentIdsMutationOptions = BaseMutationOptions & {
@@ -21,6 +23,13 @@ export declare type AllDocumentsMutationOptions = BaseMutationOptions & {
21
23
  */
22
24
  export declare type Any = any
23
25
 
26
+ /** @internal */
27
+ export declare interface ApiError {
28
+ error: string
29
+ message: string
30
+ statusCode: number
31
+ }
32
+
24
33
  /** @public */
25
34
  export declare type AssetMetadataType =
26
35
  | 'location'
@@ -44,7 +53,7 @@ export declare class AssetsClient {
44
53
  */
45
54
  upload(
46
55
  assetType: 'file',
47
- body: File | Blob | Buffer | NodeJS.ReadableStream,
56
+ body: UploadBody,
48
57
  options?: UploadClientConfig
49
58
  ): Promise<SanityAssetDocument>
50
59
  /**
@@ -56,7 +65,7 @@ export declare class AssetsClient {
56
65
  */
57
66
  upload(
58
67
  assetType: 'image',
59
- body: File | Blob | Buffer | NodeJS.ReadableStream,
68
+ body: UploadBody,
60
69
  options?: UploadClientConfig
61
70
  ): Promise<SanityImageAssetDocument>
62
71
  /**
@@ -68,7 +77,7 @@ export declare class AssetsClient {
68
77
  */
69
78
  upload(
70
79
  assetType: 'file' | 'image',
71
- body: File | Blob | Buffer | NodeJS.ReadableStream,
80
+ body: UploadBody,
72
81
  options?: UploadClientConfig
73
82
  ): Promise<SanityAssetDocument | SanityImageAssetDocument>
74
83
  }
@@ -288,12 +297,7 @@ export declare interface ClientConfig {
288
297
  * @deprecated Don't use
289
298
  */
290
299
  requester?: Requester
291
- /**
292
- * Experimental, requires apiVersion: 'X' and will overlay drafts on top of published documents
293
- * which lets you preview your query on what it'll look like after every document is published.
294
- * @alpha
295
- */
296
- unstable_overlayDrafts?: boolean
300
+ resultSourceMap?: boolean
297
301
  }
298
302
 
299
303
  /** @public */
@@ -544,6 +548,24 @@ export declare type Mutation<R extends Record<string, Any> = Record<string, Any>
544
548
  patch: PatchMutationOperation
545
549
  }
546
550
 
551
+ /** @internal */
552
+ export declare interface MutationError {
553
+ error: {
554
+ type: 'mutationError'
555
+ description: string
556
+ items?: MutationErrorItem[]
557
+ }
558
+ }
559
+
560
+ /** @internal */
561
+ export declare interface MutationErrorItem {
562
+ error: {
563
+ type: string
564
+ description: string
565
+ value?: unknown
566
+ }
567
+ }
568
+
547
569
  /** @public */
548
570
  declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>> = {
549
571
  type: 'mutation'
@@ -592,7 +614,7 @@ export declare class ObservableAssetsClient {
592
614
  */
593
615
  upload(
594
616
  assetType: 'file',
595
- body: File | Blob | Buffer | NodeJS.ReadableStream,
617
+ body: UploadBody,
596
618
  options?: UploadClientConfig
597
619
  ): Observable<
598
620
  HttpRequestEvent<{
@@ -608,7 +630,7 @@ export declare class ObservableAssetsClient {
608
630
  */
609
631
  upload(
610
632
  assetType: 'image',
611
- body: File | Blob | Buffer | NodeJS.ReadableStream,
633
+ body: UploadBody,
612
634
  options?: UploadClientConfig
613
635
  ): Observable<
614
636
  HttpRequestEvent<{
@@ -624,7 +646,7 @@ export declare class ObservableAssetsClient {
624
646
  */
625
647
  upload(
626
648
  assetType: 'file' | 'image',
627
- body: File | Blob | Buffer | NodeJS.ReadableStream,
649
+ body: UploadBody,
628
650
  options?: UploadClientConfig
629
651
  ): Observable<
630
652
  HttpRequestEvent<{
@@ -2153,6 +2175,13 @@ export declare type UnfilteredResponseQueryOptions = RequestOptions & {
2153
2175
  filterResponse: false
2154
2176
  }
2155
2177
 
2178
+ export {unstable__adapter}
2179
+
2180
+ export {unstable__environment}
2181
+
2182
+ /** @public */
2183
+ export declare type UploadBody = File | Blob | Buffer | NodeJS.ReadableStream
2184
+
2156
2185
  /** @public */
2157
2186
  export declare interface UploadClientConfig {
2158
2187
  /**
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { debug, headers, retry, jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
2
2
  import { getIt } from 'get-it';
3
+ export { adapter as unstable__adapter, environment as unstable__environment } from 'get-it';
3
4
  import { Observable, lastValueFrom } from 'rxjs';
4
5
  import { map, filter } from 'rxjs/operators';
5
- import polyfilledEventSource from '@sanity/eventsource';
6
6
  var name = "@sanity/client";
7
- var version = "0.0.0-dev.3";
7
+ var version = "0.0.0-dev.5";
8
8
  const middleware = [debug({
9
9
  verbose: true,
10
10
  namespace: "sanity:client"
@@ -13,6 +13,7 @@ const middleware = [debug({
13
13
  }), retry({
14
14
  maxRetries: 3
15
15
  })];
16
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
16
17
  class ClientError extends Error {
17
18
  constructor(res) {
18
19
  const props = extractErrorProps(res);
@@ -42,6 +43,20 @@ function extractErrorProps(res) {
42
43
  props.message = "".concat(body.error, " - ").concat(body.message);
43
44
  return props;
44
45
  }
46
+ if (isMutationError(body)) {
47
+ const allItems = body.error.items || [];
48
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
49
+ var _a;
50
+ return (_a = item.error) == null ? void 0 : _a.description;
51
+ }).filter(Boolean);
52
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
53
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
54
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
55
+ }
56
+ props.message = "".concat(body.error.description).concat(itemsStr);
57
+ props.details = body.error;
58
+ return props;
59
+ }
45
60
  if (body.error && body.error.description) {
46
61
  props.message = body.error.description;
47
62
  props.details = body.error;
@@ -50,6 +65,12 @@ function extractErrorProps(res) {
50
65
  props.message = body.error || body.message || httpErrorMessage(res);
51
66
  return props;
52
67
  }
68
+ function isMutationError(body) {
69
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
70
+ }
71
+ function isPlainObject(obj) {
72
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
73
+ }
53
74
  function httpErrorMessage(res) {
54
75
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
55
76
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -152,7 +173,7 @@ const validateObject = (op, val) => {
152
173
  }
153
174
  };
154
175
  const validateDocumentId = (op, id) => {
155
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
176
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
156
177
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
157
178
  }
158
179
  };
@@ -669,23 +690,9 @@ const indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
669
690
  return indexed;
670
691
  }, /* @__PURE__ */Object.create(null));
671
692
  const getQuerySizeLimit = 11264;
672
- function _fetch(client, httpRequest, query, _params) {
693
+ function _fetch(client, httpRequest, query, params) {
673
694
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
674
695
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
675
- let params = _params;
676
- const {
677
- unstable_overlayDrafts,
678
- apiVersion
679
- } = client.config();
680
- if (unstable_overlayDrafts) {
681
- if (apiVersion !== "X") {
682
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
683
- }
684
- params = {
685
- ...params,
686
- draftMode: true
687
- };
688
- }
689
696
  return _dataRequest(client, httpRequest, "query", {
690
697
  query,
691
698
  params
@@ -806,6 +813,12 @@ function _requestObservable(client, httpRequest, options) {
806
813
  ...options.query
807
814
  };
808
815
  }
816
+ if (config.resultSourceMap) {
817
+ options.query = {
818
+ resultSourceMap: true,
819
+ ...options.query
820
+ };
821
+ }
809
822
  const reqOptions = requestOptions(config, Object.assign({}, options, {
810
823
  url: _getUrl(client, uri, useCdn)
811
824
  }));
@@ -950,7 +963,7 @@ function _upload(client, httpRequest, assetType, body) {
950
963
  });
951
964
  }
952
965
  function optionsFromFile(opts, file) {
953
- if (typeof window === "undefined" || !(file instanceof window.File)) {
966
+ if (typeof File === "undefined" || !(file instanceof File)) {
954
967
  return opts;
955
968
  }
956
969
  return Object.assign({
@@ -1063,7 +1076,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1063
1076
  return selection;
1064
1077
  }, {});
1065
1078
  const MAX_URL_LENGTH = 16e3 - 1200;
1066
- const EventSource = polyfilledEventSource;
1067
1079
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1068
1080
  const defaultOptions = {
1069
1081
  includeResult: true
@@ -1106,7 +1118,13 @@ function _listen(query, params) {
1106
1118
  };
1107
1119
  }
1108
1120
  return new Observable(observer => {
1109
- let es = getEventSource();
1121
+ let es;
1122
+ getEventSource().then(eventSource => {
1123
+ es = eventSource;
1124
+ }).catch(reason => {
1125
+ observer.error(reason);
1126
+ stop();
1127
+ });
1110
1128
  let reconnectTimer;
1111
1129
  let stopped = false;
1112
1130
  function onError() {
@@ -1117,7 +1135,7 @@ function _listen(query, params) {
1117
1135
  if (stopped) {
1118
1136
  return;
1119
1137
  }
1120
- if (es.readyState === EventSource.CLOSED) {
1138
+ if (es.readyState === es.CLOSED) {
1121
1139
  unsubscribe();
1122
1140
  clearTimeout(reconnectTimer);
1123
1141
  reconnectTimer = setTimeout(open, 100);
@@ -1136,10 +1154,11 @@ function _listen(query, params) {
1136
1154
  observer.complete();
1137
1155
  }
1138
1156
  function unsubscribe() {
1139
- es.removeEventListener("error", onError, false);
1140
- es.removeEventListener("channelError", onChannelError, false);
1141
- es.removeEventListener("disconnect", onDisconnect, false);
1142
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1157
+ if (!es) return;
1158
+ es.removeEventListener("error", onError);
1159
+ es.removeEventListener("channelError", onChannelError);
1160
+ es.removeEventListener("disconnect", onDisconnect);
1161
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1143
1162
  es.close();
1144
1163
  }
1145
1164
  function emitReconnect() {
@@ -1149,16 +1168,24 @@ function _listen(query, params) {
1149
1168
  });
1150
1169
  }
1151
1170
  }
1152
- function getEventSource() {
1171
+ async function getEventSource() {
1172
+ const {
1173
+ default: EventSource
1174
+ } = await import('@sanity/eventsource');
1153
1175
  const evs = new EventSource(uri, esOptions);
1154
- evs.addEventListener("error", onError, false);
1155
- evs.addEventListener("channelError", onChannelError, false);
1156
- evs.addEventListener("disconnect", onDisconnect, false);
1157
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1176
+ evs.addEventListener("error", onError);
1177
+ evs.addEventListener("channelError", onChannelError);
1178
+ evs.addEventListener("disconnect", onDisconnect);
1179
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1158
1180
  return evs;
1159
1181
  }
1160
1182
  function open() {
1161
- es = getEventSource();
1183
+ getEventSource().then(eventSource => {
1184
+ es = eventSource;
1185
+ }).catch(reason => {
1186
+ observer.error(reason);
1187
+ stop();
1188
+ });
1162
1189
  }
1163
1190
  function stop() {
1164
1191
  stopped = true;