@sanity/client 0.0.0-dev.2 → 0.0.0-dev.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.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
+ encodeStegaSourceMap?: 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.2";
7
+ var version = "0.0.0-dev.4";
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
  };
@@ -792,16 +813,10 @@ function _requestObservable(client, httpRequest, options) {
792
813
  ...options.query
793
814
  };
794
815
  }
795
- if (config.unstable_overlayDrafts) {
796
- if (config.apiVersion !== "X") {
797
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
798
- }
816
+ if (config.encodeStegaSourceMap) {
799
817
  options.query = {
800
- ...options.query,
801
- params: {
802
- ...(options.query.params || {}),
803
- draftMode: true
804
- }
818
+ encodeHackySourceMap: true,
819
+ ...options.query
805
820
  };
806
821
  }
807
822
  const reqOptions = requestOptions(config, Object.assign({}, options, {
@@ -948,7 +963,7 @@ function _upload(client, httpRequest, assetType, body) {
948
963
  });
949
964
  }
950
965
  function optionsFromFile(opts, file) {
951
- if (typeof window === "undefined" || !(file instanceof window.File)) {
966
+ if (typeof File === "undefined" || !(file instanceof File)) {
952
967
  return opts;
953
968
  }
954
969
  return Object.assign({
@@ -1061,7 +1076,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1061
1076
  return selection;
1062
1077
  }, {});
1063
1078
  const MAX_URL_LENGTH = 16e3 - 1200;
1064
- const EventSource = polyfilledEventSource;
1065
1079
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1066
1080
  const defaultOptions = {
1067
1081
  includeResult: true
@@ -1104,7 +1118,13 @@ function _listen(query, params) {
1104
1118
  };
1105
1119
  }
1106
1120
  return new Observable(observer => {
1107
- let es = getEventSource();
1121
+ let es;
1122
+ getEventSource().then(eventSource => {
1123
+ es = eventSource;
1124
+ }).catch(reason => {
1125
+ observer.error(reason);
1126
+ stop();
1127
+ });
1108
1128
  let reconnectTimer;
1109
1129
  let stopped = false;
1110
1130
  function onError() {
@@ -1115,7 +1135,7 @@ function _listen(query, params) {
1115
1135
  if (stopped) {
1116
1136
  return;
1117
1137
  }
1118
- if (es.readyState === EventSource.CLOSED) {
1138
+ if (es.readyState === es.CLOSED) {
1119
1139
  unsubscribe();
1120
1140
  clearTimeout(reconnectTimer);
1121
1141
  reconnectTimer = setTimeout(open, 100);
@@ -1134,10 +1154,11 @@ function _listen(query, params) {
1134
1154
  observer.complete();
1135
1155
  }
1136
1156
  function unsubscribe() {
1137
- es.removeEventListener("error", onError, false);
1138
- es.removeEventListener("channelError", onChannelError, false);
1139
- es.removeEventListener("disconnect", onDisconnect, false);
1140
- 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));
1141
1162
  es.close();
1142
1163
  }
1143
1164
  function emitReconnect() {
@@ -1147,16 +1168,24 @@ function _listen(query, params) {
1147
1168
  });
1148
1169
  }
1149
1170
  }
1150
- function getEventSource() {
1171
+ async function getEventSource() {
1172
+ const {
1173
+ default: EventSource
1174
+ } = await import('@sanity/eventsource');
1151
1175
  const evs = new EventSource(uri, esOptions);
1152
- evs.addEventListener("error", onError, false);
1153
- evs.addEventListener("channelError", onChannelError, false);
1154
- evs.addEventListener("disconnect", onDisconnect, false);
1155
- 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));
1156
1180
  return evs;
1157
1181
  }
1158
1182
  function open() {
1159
- es = getEventSource();
1183
+ getEventSource().then(eventSource => {
1184
+ es = eventSource;
1185
+ }).catch(reason => {
1186
+ observer.error(reason);
1187
+ stop();
1188
+ });
1160
1189
  }
1161
1190
  function stop() {
1162
1191
  stopped = true;