@sanity/client 3.3.3-esm.6 → 3.3.3-esm.9
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/sanityClient.browser.mjs +901 -1162
- package/dist/sanityClient.browser.mjs.map +4 -4
- package/lib/assets/assetsClient.js +25 -12
- package/lib/auth/authClient.js +8 -1
- package/lib/config.js +14 -8
- package/lib/data/dataMethods.js +36 -23
- package/lib/data/encodeQueryString.js +9 -2
- package/lib/data/listen.js +24 -18
- package/lib/data/patch.js +18 -12
- package/lib/data/transaction.js +23 -8
- package/lib/datasets/datasetsClient.js +16 -3
- package/lib/http/browserMiddleware.js +7 -1
- package/lib/http/errors.js +7 -3
- package/lib/http/nodeMiddleware.js +8 -1
- package/lib/http/queryString.js +10 -2
- package/lib/http/request.js +20 -14
- package/lib/http/requestOptions.js +10 -2
- package/lib/projects/projectsClient.js +8 -1
- package/lib/sanityClient.js +49 -43
- package/lib/users/usersClient.js +8 -1
- package/lib/util/defaults.js +10 -2
- package/lib/util/getSelection.js +9 -2
- package/lib/util/once.js +10 -2
- package/lib/util/pick.js +10 -2
- package/lib/validators.js +38 -15
- package/lib/warnings.js +16 -6
- package/package.json +4 -3
- package/umd/sanityClient.js +54 -57
- package/umd/sanityClient.min.js +1 -1
|
@@ -1,1265 +1,1004 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __esm = (fn, res) => function __init() {
|
|
8
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
9
|
-
};
|
|
10
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
11
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
12
|
-
};
|
|
13
|
-
var __export = (target, all) => {
|
|
14
|
-
for (var name in all)
|
|
15
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
-
};
|
|
17
|
-
var __copyProps = (to, from, except, desc) => {
|
|
18
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
-
for (let key of __getOwnPropNames(from))
|
|
20
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// node_modules/object-assign/index.js
|
|
32
|
-
var require_object_assign = __commonJS({
|
|
33
|
-
"node_modules/object-assign/index.js"(exports, module) {
|
|
34
|
-
"use strict";
|
|
35
|
-
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
|
|
36
|
-
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
37
|
-
var propIsEnumerable = Object.prototype.propertyIsEnumerable;
|
|
38
|
-
function toObject(val) {
|
|
39
|
-
if (val === null || val === void 0) {
|
|
40
|
-
throw new TypeError("Object.assign cannot be called with null or undefined");
|
|
41
|
-
}
|
|
42
|
-
return Object(val);
|
|
43
|
-
}
|
|
44
|
-
function shouldUseNative() {
|
|
45
|
-
try {
|
|
46
|
-
if (!Object.assign) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
var test1 = new String("abc");
|
|
50
|
-
test1[5] = "de";
|
|
51
|
-
if (Object.getOwnPropertyNames(test1)[0] === "5") {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
var test2 = {};
|
|
55
|
-
for (var i = 0; i < 10; i++) {
|
|
56
|
-
test2["_" + String.fromCharCode(i)] = i;
|
|
57
|
-
}
|
|
58
|
-
var order2 = Object.getOwnPropertyNames(test2).map(function(n) {
|
|
59
|
-
return test2[n];
|
|
60
|
-
});
|
|
61
|
-
if (order2.join("") !== "0123456789") {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
var test3 = {};
|
|
65
|
-
"abcdefghijklmnopqrst".split("").forEach(function(letter) {
|
|
66
|
-
test3[letter] = letter;
|
|
67
|
-
});
|
|
68
|
-
if (Object.keys(Object.assign({}, test3)).join("") !== "abcdefghijklmnopqrst") {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
} catch (err) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
module.exports = shouldUseNative() ? Object.assign : function(target, source) {
|
|
77
|
-
var from;
|
|
78
|
-
var to = toObject(target);
|
|
79
|
-
var symbols;
|
|
80
|
-
for (var s = 1; s < arguments.length; s++) {
|
|
81
|
-
from = Object(arguments[s]);
|
|
82
|
-
for (var key in from) {
|
|
83
|
-
if (hasOwnProperty.call(from, key)) {
|
|
84
|
-
to[key] = from[key];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (getOwnPropertySymbols) {
|
|
88
|
-
symbols = getOwnPropertySymbols(from);
|
|
89
|
-
for (var i = 0; i < symbols.length; i++) {
|
|
90
|
-
if (propIsEnumerable.call(from, symbols[i])) {
|
|
91
|
-
to[symbols[i]] = from[symbols[i]];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return to;
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
1
|
// src/util/observable-modern.js
|
|
102
|
-
var observable_modern_exports = {};
|
|
103
|
-
__export(observable_modern_exports, {
|
|
104
|
-
Observable: () => Observable,
|
|
105
|
-
filter: () => filter,
|
|
106
|
-
map: () => map
|
|
107
|
-
});
|
|
108
2
|
import { Observable } from "rxjs-v7/internal/Observable";
|
|
109
3
|
import { filter } from "rxjs-v7/internal/operators/filter";
|
|
110
4
|
import { map } from "rxjs-v7/internal/operators/map";
|
|
111
|
-
var init_observable_modern = __esm({
|
|
112
|
-
"src/util/observable-modern.js"() {
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
5
|
|
|
116
6
|
// src/util/getSelection.js
|
|
117
|
-
|
|
118
|
-
"
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
].join("\n");
|
|
131
|
-
throw new Error(`Unknown selection - must be one of:
|
|
7
|
+
function getSelection(sel) {
|
|
8
|
+
if (typeof sel === "string" || Array.isArray(sel)) {
|
|
9
|
+
return { id: sel };
|
|
10
|
+
}
|
|
11
|
+
if (sel && sel.query) {
|
|
12
|
+
return "params" in sel ? { query: sel.query, params: sel.params } : { query: sel.query };
|
|
13
|
+
}
|
|
14
|
+
const selectionOpts = [
|
|
15
|
+
"* Document ID (<docId>)",
|
|
16
|
+
"* Array of document IDs",
|
|
17
|
+
"* Object containing `query`"
|
|
18
|
+
].join("\n");
|
|
19
|
+
throw new Error(`Unknown selection - must be one of:
|
|
132
20
|
|
|
133
21
|
${selectionOpts}`);
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
});
|
|
22
|
+
}
|
|
137
23
|
|
|
138
24
|
// src/validators.js
|
|
139
|
-
var
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
"Datasets can only contain lowercase characters, numbers, underscores and dashes, and start with tilde, and be maximum 64 characters"
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
exports.projectId = (id) => {
|
|
151
|
-
if (!/^[-a-z0-9]+$/i.test(id)) {
|
|
152
|
-
throw new Error("`projectId` can only contain only a-z, 0-9 and dashes");
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
exports.validateAssetType = (type) => {
|
|
156
|
-
if (VALID_ASSET_TYPES.indexOf(type) === -1) {
|
|
157
|
-
throw new Error(`Invalid asset type: ${type}. Must be one of ${VALID_ASSET_TYPES.join(", ")}`);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
exports.validateObject = (op, val) => {
|
|
161
|
-
if (val === null || typeof val !== "object" || Array.isArray(val)) {
|
|
162
|
-
throw new Error(`${op}() takes an object of properties`);
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
exports.requireDocumentId = (op, doc) => {
|
|
166
|
-
if (!doc._id) {
|
|
167
|
-
throw new Error(`${op}() requires that the document contains an ID ("_id" property)`);
|
|
168
|
-
}
|
|
169
|
-
exports.validateDocumentId(op, doc._id);
|
|
170
|
-
};
|
|
171
|
-
exports.validateDocumentId = (op, id) => {
|
|
172
|
-
if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
|
|
173
|
-
throw new Error(`${op}(): "${id}" is not a valid document ID`);
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
exports.validateInsert = (at, selector, items) => {
|
|
177
|
-
const signature = "insert(at, selector, items)";
|
|
178
|
-
if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
|
|
179
|
-
const valid = VALID_INSERT_LOCATIONS.map((loc) => `"${loc}"`).join(", ");
|
|
180
|
-
throw new Error(`${signature} takes an "at"-argument which is one of: ${valid}`);
|
|
181
|
-
}
|
|
182
|
-
if (typeof selector !== "string") {
|
|
183
|
-
throw new Error(`${signature} takes a "selector"-argument which must be a string`);
|
|
184
|
-
}
|
|
185
|
-
if (!Array.isArray(items)) {
|
|
186
|
-
throw new Error(`${signature} takes an "items"-argument which must be an array`);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
exports.hasDataset = (config) => {
|
|
190
|
-
if (!config.dataset) {
|
|
191
|
-
throw new Error("`dataset` must be provided to perform queries");
|
|
192
|
-
}
|
|
193
|
-
return config.dataset || "";
|
|
194
|
-
};
|
|
195
|
-
exports.requestTag = (tag) => {
|
|
196
|
-
if (typeof tag !== "string" || !/^[a-z0-9._-]{1,75}$/i.test(tag)) {
|
|
197
|
-
throw new Error(
|
|
198
|
-
`Tag can only contain alphanumeric characters, underscores, dashes and dots, and be between one and 75 characters long.`
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
return tag;
|
|
202
|
-
};
|
|
25
|
+
var VALID_ASSET_TYPES = ["image", "file"];
|
|
26
|
+
var VALID_INSERT_LOCATIONS = ["before", "after", "replace"];
|
|
27
|
+
var dataset = (name) => {
|
|
28
|
+
if (!/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/.test(name)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
"Datasets can only contain lowercase characters, numbers, underscores and dashes, and start with tilde, and be maximum 64 characters"
|
|
31
|
+
);
|
|
203
32
|
}
|
|
204
|
-
}
|
|
33
|
+
};
|
|
34
|
+
var projectId = (id) => {
|
|
35
|
+
if (!/^[-a-z0-9]+$/i.test(id)) {
|
|
36
|
+
throw new Error("`projectId` can only contain only a-z, 0-9 and dashes");
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var validateAssetType = (type) => {
|
|
40
|
+
if (VALID_ASSET_TYPES.indexOf(type) === -1) {
|
|
41
|
+
throw new Error(`Invalid asset type: ${type}. Must be one of ${VALID_ASSET_TYPES.join(", ")}`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var validateObject = (op, val) => {
|
|
45
|
+
if (val === null || typeof val !== "object" || Array.isArray(val)) {
|
|
46
|
+
throw new Error(`${op}() takes an object of properties`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var validateDocumentId = (op, id) => {
|
|
50
|
+
if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
|
|
51
|
+
throw new Error(`${op}(): "${id}" is not a valid document ID`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var requireDocumentId = (op, doc) => {
|
|
55
|
+
if (!doc._id) {
|
|
56
|
+
throw new Error(`${op}() requires that the document contains an ID ("_id" property)`);
|
|
57
|
+
}
|
|
58
|
+
validateDocumentId(op, doc._id);
|
|
59
|
+
};
|
|
60
|
+
var validateInsert = (at, selector, items) => {
|
|
61
|
+
const signature = "insert(at, selector, items)";
|
|
62
|
+
if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
|
|
63
|
+
const valid = VALID_INSERT_LOCATIONS.map((loc) => `"${loc}"`).join(", ");
|
|
64
|
+
throw new Error(`${signature} takes an "at"-argument which is one of: ${valid}`);
|
|
65
|
+
}
|
|
66
|
+
if (typeof selector !== "string") {
|
|
67
|
+
throw new Error(`${signature} takes a "selector"-argument which must be a string`);
|
|
68
|
+
}
|
|
69
|
+
if (!Array.isArray(items)) {
|
|
70
|
+
throw new Error(`${signature} takes an "items"-argument which must be an array`);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var hasDataset = (config) => {
|
|
74
|
+
if (!config.dataset) {
|
|
75
|
+
throw new Error("`dataset` must be provided to perform queries");
|
|
76
|
+
}
|
|
77
|
+
return config.dataset || "";
|
|
78
|
+
};
|
|
79
|
+
var requestTag = (tag) => {
|
|
80
|
+
if (typeof tag !== "string" || !/^[a-z0-9._-]{1,75}$/i.test(tag)) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Tag can only contain alphanumeric characters, underscores, dashes and dots, and be between one and 75 characters long.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return tag;
|
|
86
|
+
};
|
|
205
87
|
|
|
206
88
|
// src/data/patch.js
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
89
|
+
function Patch(selection, operations = {}, client = null) {
|
|
90
|
+
this.selection = selection;
|
|
91
|
+
this.operations = Object.assign({}, operations);
|
|
92
|
+
this.client = client;
|
|
93
|
+
}
|
|
94
|
+
Object.assign(Patch.prototype, {
|
|
95
|
+
clone() {
|
|
96
|
+
return new Patch(this.selection, Object.assign({}, this.operations), this.client);
|
|
97
|
+
},
|
|
98
|
+
set(props) {
|
|
99
|
+
return this.assign("set", props);
|
|
100
|
+
},
|
|
101
|
+
diffMatchPatch(props) {
|
|
102
|
+
validateObject("diffMatchPatch", props);
|
|
103
|
+
return this.assign("diffMatchPatch", props);
|
|
104
|
+
},
|
|
105
|
+
unset(attrs) {
|
|
106
|
+
if (!Array.isArray(attrs)) {
|
|
107
|
+
throw new Error("unset(attrs) takes an array of attributes to unset, non-array given");
|
|
217
108
|
}
|
|
218
|
-
Object.assign(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const returnFirst = typeof this.selection === "string";
|
|
284
|
-
const opts = Object.assign({ returnFirst, returnDocuments: true }, options);
|
|
285
|
-
return this.client.mutate({ patch: this.serialize() }, opts);
|
|
286
|
-
},
|
|
287
|
-
reset() {
|
|
288
|
-
this.operations = {};
|
|
289
|
-
return this;
|
|
290
|
-
},
|
|
291
|
-
_set(op, props) {
|
|
292
|
-
return this.assign(op, props, false);
|
|
293
|
-
},
|
|
294
|
-
assign(op, props, merge = true) {
|
|
295
|
-
validateObject(op, props);
|
|
296
|
-
this.operations = Object.assign({}, this.operations, {
|
|
297
|
-
[op]: Object.assign({}, merge && this.operations[op] || {}, props)
|
|
298
|
-
});
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
109
|
+
this.operations = Object.assign({}, this.operations, { unset: attrs });
|
|
110
|
+
return this;
|
|
111
|
+
},
|
|
112
|
+
setIfMissing(props) {
|
|
113
|
+
return this.assign("setIfMissing", props);
|
|
114
|
+
},
|
|
115
|
+
replace(props) {
|
|
116
|
+
validateObject("replace", props);
|
|
117
|
+
return this._set("set", { $: props });
|
|
118
|
+
},
|
|
119
|
+
inc(props) {
|
|
120
|
+
return this.assign("inc", props);
|
|
121
|
+
},
|
|
122
|
+
dec(props) {
|
|
123
|
+
return this.assign("dec", props);
|
|
124
|
+
},
|
|
125
|
+
insert(at, selector, items) {
|
|
126
|
+
validateInsert(at, selector, items);
|
|
127
|
+
return this.assign("insert", { [at]: selector, items });
|
|
128
|
+
},
|
|
129
|
+
append(selector, items) {
|
|
130
|
+
return this.insert("after", `${selector}[-1]`, items);
|
|
131
|
+
},
|
|
132
|
+
prepend(selector, items) {
|
|
133
|
+
return this.insert("before", `${selector}[0]`, items);
|
|
134
|
+
},
|
|
135
|
+
splice(selector, start, deleteCount, items) {
|
|
136
|
+
const delAll = typeof deleteCount === "undefined" || deleteCount === -1;
|
|
137
|
+
const startIndex = start < 0 ? start - 1 : start;
|
|
138
|
+
const delCount = delAll ? -1 : Math.max(0, start + deleteCount);
|
|
139
|
+
const delRange = startIndex < 0 && delCount >= 0 ? "" : delCount;
|
|
140
|
+
const rangeSelector = `${selector}[${startIndex}:${delRange}]`;
|
|
141
|
+
return this.insert("replace", rangeSelector, items || []);
|
|
142
|
+
},
|
|
143
|
+
ifRevisionId(rev) {
|
|
144
|
+
this.operations.ifRevisionID = rev;
|
|
145
|
+
return this;
|
|
146
|
+
},
|
|
147
|
+
serialize() {
|
|
148
|
+
return Object.assign(getSelection(this.selection), this.operations);
|
|
149
|
+
},
|
|
150
|
+
toJSON() {
|
|
151
|
+
return this.serialize();
|
|
152
|
+
},
|
|
153
|
+
commit(options = {}) {
|
|
154
|
+
if (!this.client) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
"No `client` passed to patch, either provide one or pass the patch to a clients `mutate()` method"
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
const returnFirst = typeof this.selection === "string";
|
|
160
|
+
const opts = Object.assign({ returnFirst, returnDocuments: true }, options);
|
|
161
|
+
return this.client.mutate({ patch: this.serialize() }, opts);
|
|
162
|
+
},
|
|
163
|
+
reset() {
|
|
164
|
+
this.operations = {};
|
|
165
|
+
return this;
|
|
166
|
+
},
|
|
167
|
+
_set(op, props) {
|
|
168
|
+
return this.assign(op, props, false);
|
|
169
|
+
},
|
|
170
|
+
assign(op, props, merge = true) {
|
|
171
|
+
validateObject(op, props);
|
|
172
|
+
this.operations = Object.assign({}, this.operations, {
|
|
173
|
+
[op]: Object.assign({}, merge && this.operations[op] || {}, props)
|
|
301
174
|
});
|
|
302
|
-
|
|
175
|
+
return this;
|
|
303
176
|
}
|
|
304
177
|
});
|
|
178
|
+
var patch_default = Patch;
|
|
305
179
|
|
|
306
180
|
// src/data/transaction.js
|
|
307
|
-
var
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
181
|
+
var defaultMutateOptions = { returnDocuments: false };
|
|
182
|
+
function Transaction(operations = [], client, transactionId) {
|
|
183
|
+
this.trxId = transactionId;
|
|
184
|
+
this.operations = operations;
|
|
185
|
+
this.client = client;
|
|
186
|
+
}
|
|
187
|
+
Object.assign(Transaction.prototype, {
|
|
188
|
+
clone() {
|
|
189
|
+
return new Transaction(this.operations.slice(0), this.client, this.trxId);
|
|
190
|
+
},
|
|
191
|
+
create(doc) {
|
|
192
|
+
validateObject("create", doc);
|
|
193
|
+
return this._add({ create: doc });
|
|
194
|
+
},
|
|
195
|
+
createIfNotExists(doc) {
|
|
196
|
+
const op = "createIfNotExists";
|
|
197
|
+
validateObject(op, doc);
|
|
198
|
+
requireDocumentId(op, doc);
|
|
199
|
+
return this._add({ [op]: doc });
|
|
200
|
+
},
|
|
201
|
+
createOrReplace(doc) {
|
|
202
|
+
const op = "createOrReplace";
|
|
203
|
+
validateObject(op, doc);
|
|
204
|
+
requireDocumentId(op, doc);
|
|
205
|
+
return this._add({ [op]: doc });
|
|
206
|
+
},
|
|
207
|
+
delete(documentId) {
|
|
208
|
+
validateDocumentId("delete", documentId);
|
|
209
|
+
return this._add({ delete: { id: documentId } });
|
|
210
|
+
},
|
|
211
|
+
patch(documentId, patchOps) {
|
|
212
|
+
const isBuilder = typeof patchOps === "function";
|
|
213
|
+
const isPatch = documentId instanceof patch_default;
|
|
214
|
+
if (isPatch) {
|
|
215
|
+
return this._add({ patch: documentId.serialize() });
|
|
316
216
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
create(doc) {
|
|
322
|
-
validators.validateObject("create", doc);
|
|
323
|
-
return this._add({ create: doc });
|
|
324
|
-
},
|
|
325
|
-
createIfNotExists(doc) {
|
|
326
|
-
const op = "createIfNotExists";
|
|
327
|
-
validators.validateObject(op, doc);
|
|
328
|
-
validators.requireDocumentId(op, doc);
|
|
329
|
-
return this._add({ [op]: doc });
|
|
330
|
-
},
|
|
331
|
-
createOrReplace(doc) {
|
|
332
|
-
const op = "createOrReplace";
|
|
333
|
-
validators.validateObject(op, doc);
|
|
334
|
-
validators.requireDocumentId(op, doc);
|
|
335
|
-
return this._add({ [op]: doc });
|
|
336
|
-
},
|
|
337
|
-
delete(documentId) {
|
|
338
|
-
validators.validateDocumentId("delete", documentId);
|
|
339
|
-
return this._add({ delete: { id: documentId } });
|
|
340
|
-
},
|
|
341
|
-
patch(documentId, patchOps) {
|
|
342
|
-
const isBuilder = typeof patchOps === "function";
|
|
343
|
-
const isPatch = documentId instanceof Patch;
|
|
344
|
-
if (isPatch) {
|
|
345
|
-
return this._add({ patch: documentId.serialize() });
|
|
346
|
-
}
|
|
347
|
-
if (isBuilder) {
|
|
348
|
-
const patch = patchOps(new Patch(documentId, {}, this.client));
|
|
349
|
-
if (!(patch instanceof Patch)) {
|
|
350
|
-
throw new Error("function passed to `patch()` must return the patch");
|
|
351
|
-
}
|
|
352
|
-
return this._add({ patch: patch.serialize() });
|
|
353
|
-
}
|
|
354
|
-
return this._add({ patch: Object.assign({ id: documentId }, patchOps) });
|
|
355
|
-
},
|
|
356
|
-
transactionId(id) {
|
|
357
|
-
if (!id) {
|
|
358
|
-
return this.trxId;
|
|
359
|
-
}
|
|
360
|
-
this.trxId = id;
|
|
361
|
-
return this;
|
|
362
|
-
},
|
|
363
|
-
serialize() {
|
|
364
|
-
return this.operations.slice();
|
|
365
|
-
},
|
|
366
|
-
toJSON() {
|
|
367
|
-
return this.serialize();
|
|
368
|
-
},
|
|
369
|
-
commit(options) {
|
|
370
|
-
if (!this.client) {
|
|
371
|
-
throw new Error(
|
|
372
|
-
"No `client` passed to transaction, either provide one or pass the transaction to a clients `mutate()` method"
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
return this.client.mutate(
|
|
376
|
-
this.serialize(),
|
|
377
|
-
Object.assign({ transactionId: this.trxId }, defaultMutateOptions, options || {})
|
|
378
|
-
);
|
|
379
|
-
},
|
|
380
|
-
reset() {
|
|
381
|
-
this.operations = [];
|
|
382
|
-
return this;
|
|
383
|
-
},
|
|
384
|
-
_add(mut) {
|
|
385
|
-
this.operations.push(mut);
|
|
386
|
-
return this;
|
|
217
|
+
if (isBuilder) {
|
|
218
|
+
const patch = patchOps(new patch_default(documentId, {}, this.client));
|
|
219
|
+
if (!(patch instanceof patch_default)) {
|
|
220
|
+
throw new Error("function passed to `patch()` must return the patch");
|
|
387
221
|
}
|
|
388
|
-
|
|
389
|
-
|
|
222
|
+
return this._add({ patch: patch.serialize() });
|
|
223
|
+
}
|
|
224
|
+
return this._add({ patch: Object.assign({ id: documentId }, patchOps) });
|
|
225
|
+
},
|
|
226
|
+
transactionId(id) {
|
|
227
|
+
if (!id) {
|
|
228
|
+
return this.trxId;
|
|
229
|
+
}
|
|
230
|
+
this.trxId = id;
|
|
231
|
+
return this;
|
|
232
|
+
},
|
|
233
|
+
serialize() {
|
|
234
|
+
return this.operations.slice();
|
|
235
|
+
},
|
|
236
|
+
toJSON() {
|
|
237
|
+
return this.serialize();
|
|
238
|
+
},
|
|
239
|
+
commit(options) {
|
|
240
|
+
if (!this.client) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
"No `client` passed to transaction, either provide one or pass the transaction to a clients `mutate()` method"
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return this.client.mutate(
|
|
246
|
+
this.serialize(),
|
|
247
|
+
Object.assign({ transactionId: this.trxId }, defaultMutateOptions, options || {})
|
|
248
|
+
);
|
|
249
|
+
},
|
|
250
|
+
reset() {
|
|
251
|
+
this.operations = [];
|
|
252
|
+
return this;
|
|
253
|
+
},
|
|
254
|
+
_add(mut) {
|
|
255
|
+
this.operations.push(mut);
|
|
256
|
+
return this;
|
|
390
257
|
}
|
|
391
258
|
});
|
|
259
|
+
var transaction_default = Transaction;
|
|
392
260
|
|
|
393
261
|
// src/data/encodeQueryString.js
|
|
394
|
-
var
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
});
|
|
262
|
+
var enc = encodeURIComponent;
|
|
263
|
+
var encodeQueryString_default = ({ query, params = {}, options = {} }) => {
|
|
264
|
+
const { tag, ...opts } = options;
|
|
265
|
+
const q = `query=${enc(query)}`;
|
|
266
|
+
const base = tag ? `?tag=${enc(tag)}&${q}` : `?${q}`;
|
|
267
|
+
const qString = Object.keys(params).reduce(
|
|
268
|
+
(qs, param) => `${qs}&${enc(`$${param}`)}=${enc(JSON.stringify(params[param]))}`,
|
|
269
|
+
base
|
|
270
|
+
);
|
|
271
|
+
return Object.keys(opts).reduce((qs, option) => {
|
|
272
|
+
return options[option] ? `${qs}&${enc(option)}=${enc(options[option])}` : qs;
|
|
273
|
+
}, qString);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/data/listen.js
|
|
277
|
+
import polyfilledEventSource from "@sanity/eventsource";
|
|
411
278
|
|
|
412
279
|
// src/util/pick.js
|
|
413
|
-
var
|
|
414
|
-
"
|
|
415
|
-
|
|
416
|
-
if (typeof obj[prop] === "undefined") {
|
|
417
|
-
return selection;
|
|
418
|
-
}
|
|
419
|
-
selection[prop] = obj[prop];
|
|
420
|
-
return selection;
|
|
421
|
-
}, {});
|
|
280
|
+
var pick_default = (obj, props) => props.reduce((selection, prop) => {
|
|
281
|
+
if (typeof obj[prop] === "undefined") {
|
|
282
|
+
return selection;
|
|
422
283
|
}
|
|
423
|
-
|
|
284
|
+
selection[prop] = obj[prop];
|
|
285
|
+
return selection;
|
|
286
|
+
}, {});
|
|
424
287
|
|
|
425
288
|
// src/util/defaults.js
|
|
426
|
-
var
|
|
427
|
-
"
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
return target;
|
|
431
|
-
}, {});
|
|
432
|
-
}
|
|
433
|
-
});
|
|
289
|
+
var defaults_default = (obj, defaults) => Object.keys(defaults).concat(Object.keys(obj)).reduce((target, prop) => {
|
|
290
|
+
target[prop] = typeof obj[prop] === "undefined" ? defaults[prop] : obj[prop];
|
|
291
|
+
return target;
|
|
292
|
+
}, {});
|
|
434
293
|
|
|
435
294
|
// src/data/listen.js
|
|
436
|
-
|
|
437
|
-
var
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
295
|
+
var MAX_URL_LENGTH = 16e3 - 1200;
|
|
296
|
+
var EventSource = polyfilledEventSource;
|
|
297
|
+
var possibleOptions = [
|
|
298
|
+
"includePreviousRevision",
|
|
299
|
+
"includeResult",
|
|
300
|
+
"visibility",
|
|
301
|
+
"effectFormat",
|
|
302
|
+
"tag"
|
|
303
|
+
];
|
|
304
|
+
var defaultOptions = {
|
|
305
|
+
includeResult: true
|
|
306
|
+
};
|
|
307
|
+
function listen(query, params, opts = {}) {
|
|
308
|
+
const { url, token, withCredentials, requestTagPrefix } = this.clientConfig;
|
|
309
|
+
const tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag;
|
|
310
|
+
const options = { ...defaults_default(opts, defaultOptions), tag };
|
|
311
|
+
const listenOpts = pick_default(options, possibleOptions);
|
|
312
|
+
const qs = encodeQueryString_default({ query, params, options: listenOpts, tag });
|
|
313
|
+
const uri = `${url}${this.getDataUrl("listen", qs)}`;
|
|
314
|
+
if (uri.length > MAX_URL_LENGTH) {
|
|
315
|
+
return new Observable((observer) => observer.error(new Error("Query too large for listener")));
|
|
316
|
+
}
|
|
317
|
+
const listenFor = options.events ? options.events : ["mutation"];
|
|
318
|
+
const shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1;
|
|
319
|
+
const esOptions = {};
|
|
320
|
+
if (token || withCredentials) {
|
|
321
|
+
esOptions.withCredentials = true;
|
|
322
|
+
}
|
|
323
|
+
if (token) {
|
|
324
|
+
esOptions.headers = {
|
|
325
|
+
Authorization: `Bearer ${token}`
|
|
454
326
|
};
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
return new Observable2((observer) => observer.error(new Error("Query too large for listener")));
|
|
464
|
-
}
|
|
465
|
-
const listenFor = options.events ? options.events : ["mutation"];
|
|
466
|
-
const shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1;
|
|
467
|
-
const esOptions = {};
|
|
468
|
-
if (token || withCredentials) {
|
|
469
|
-
esOptions.withCredentials = true;
|
|
327
|
+
}
|
|
328
|
+
return new Observable((observer) => {
|
|
329
|
+
let es = getEventSource();
|
|
330
|
+
let reconnectTimer;
|
|
331
|
+
let stopped = false;
|
|
332
|
+
function onError() {
|
|
333
|
+
if (stopped) {
|
|
334
|
+
return;
|
|
470
335
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
};
|
|
336
|
+
emitReconnect();
|
|
337
|
+
if (stopped) {
|
|
338
|
+
return;
|
|
475
339
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
function onError() {
|
|
481
|
-
if (stopped) {
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
emitReconnect();
|
|
485
|
-
if (stopped) {
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
if (es.readyState === EventSource.CLOSED) {
|
|
489
|
-
unsubscribe();
|
|
490
|
-
clearTimeout(reconnectTimer);
|
|
491
|
-
reconnectTimer = setTimeout(open, 100);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
function onChannelError(err) {
|
|
495
|
-
observer.error(cooerceError(err));
|
|
496
|
-
}
|
|
497
|
-
function onMessage(evt) {
|
|
498
|
-
const event = parseEvent(evt);
|
|
499
|
-
return event instanceof Error ? observer.error(event) : observer.next(event);
|
|
500
|
-
}
|
|
501
|
-
function onDisconnect(evt) {
|
|
502
|
-
stopped = true;
|
|
503
|
-
unsubscribe();
|
|
504
|
-
observer.complete();
|
|
505
|
-
}
|
|
506
|
-
function unsubscribe() {
|
|
507
|
-
es.removeEventListener("error", onError, false);
|
|
508
|
-
es.removeEventListener("channelError", onChannelError, false);
|
|
509
|
-
es.removeEventListener("disconnect", onDisconnect, false);
|
|
510
|
-
listenFor.forEach((type) => es.removeEventListener(type, onMessage, false));
|
|
511
|
-
es.close();
|
|
512
|
-
}
|
|
513
|
-
function emitReconnect() {
|
|
514
|
-
if (shouldEmitReconnect) {
|
|
515
|
-
observer.next({ type: "reconnect" });
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
function getEventSource() {
|
|
519
|
-
const evs = new EventSource(uri, esOptions);
|
|
520
|
-
evs.addEventListener("error", onError, false);
|
|
521
|
-
evs.addEventListener("channelError", onChannelError, false);
|
|
522
|
-
evs.addEventListener("disconnect", onDisconnect, false);
|
|
523
|
-
listenFor.forEach((type) => evs.addEventListener(type, onMessage, false));
|
|
524
|
-
return evs;
|
|
525
|
-
}
|
|
526
|
-
function open() {
|
|
527
|
-
es = getEventSource();
|
|
528
|
-
}
|
|
529
|
-
function stop() {
|
|
530
|
-
stopped = true;
|
|
531
|
-
unsubscribe();
|
|
532
|
-
}
|
|
533
|
-
return stop;
|
|
534
|
-
});
|
|
535
|
-
};
|
|
536
|
-
function parseEvent(event) {
|
|
537
|
-
try {
|
|
538
|
-
const data = event.data && JSON.parse(event.data) || {};
|
|
539
|
-
return Object.assign({ type: event.type }, data);
|
|
540
|
-
} catch (err) {
|
|
541
|
-
return err;
|
|
340
|
+
if (es.readyState === EventSource.CLOSED) {
|
|
341
|
+
unsubscribe();
|
|
342
|
+
clearTimeout(reconnectTimer);
|
|
343
|
+
reconnectTimer = setTimeout(open, 100);
|
|
542
344
|
}
|
|
543
345
|
}
|
|
544
|
-
function
|
|
545
|
-
|
|
546
|
-
return err;
|
|
547
|
-
}
|
|
548
|
-
const evt = parseEvent(err);
|
|
549
|
-
return evt instanceof Error ? evt : new Error(extractErrorMessage(evt));
|
|
346
|
+
function onChannelError(err) {
|
|
347
|
+
observer.error(cooerceError(err));
|
|
550
348
|
}
|
|
551
|
-
function
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
349
|
+
function onMessage(evt) {
|
|
350
|
+
const event = parseEvent(evt);
|
|
351
|
+
return event instanceof Error ? observer.error(event) : observer.next(event);
|
|
352
|
+
}
|
|
353
|
+
function onDisconnect(evt) {
|
|
354
|
+
stopped = true;
|
|
355
|
+
unsubscribe();
|
|
356
|
+
observer.complete();
|
|
357
|
+
}
|
|
358
|
+
function unsubscribe() {
|
|
359
|
+
es.removeEventListener("error", onError, false);
|
|
360
|
+
es.removeEventListener("channelError", onChannelError, false);
|
|
361
|
+
es.removeEventListener("disconnect", onDisconnect, false);
|
|
362
|
+
listenFor.forEach((type) => es.removeEventListener(type, onMessage, false));
|
|
363
|
+
es.close();
|
|
364
|
+
}
|
|
365
|
+
function emitReconnect() {
|
|
366
|
+
if (shouldEmitReconnect) {
|
|
367
|
+
observer.next({ type: "reconnect" });
|
|
557
368
|
}
|
|
558
|
-
return typeof err.error === "string" ? err.error : JSON.stringify(err.error, null, 2);
|
|
559
369
|
}
|
|
370
|
+
function getEventSource() {
|
|
371
|
+
const evs = new EventSource(uri, esOptions);
|
|
372
|
+
evs.addEventListener("error", onError, false);
|
|
373
|
+
evs.addEventListener("channelError", onChannelError, false);
|
|
374
|
+
evs.addEventListener("disconnect", onDisconnect, false);
|
|
375
|
+
listenFor.forEach((type) => evs.addEventListener(type, onMessage, false));
|
|
376
|
+
return evs;
|
|
377
|
+
}
|
|
378
|
+
function open() {
|
|
379
|
+
es = getEventSource();
|
|
380
|
+
}
|
|
381
|
+
function stop() {
|
|
382
|
+
stopped = true;
|
|
383
|
+
unsubscribe();
|
|
384
|
+
}
|
|
385
|
+
return stop;
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
function parseEvent(event) {
|
|
389
|
+
try {
|
|
390
|
+
const data = event.data && JSON.parse(event.data) || {};
|
|
391
|
+
return Object.assign({ type: event.type }, data);
|
|
392
|
+
} catch (err) {
|
|
393
|
+
return err;
|
|
560
394
|
}
|
|
561
|
-
}
|
|
395
|
+
}
|
|
396
|
+
function cooerceError(err) {
|
|
397
|
+
if (err instanceof Error) {
|
|
398
|
+
return err;
|
|
399
|
+
}
|
|
400
|
+
const evt = parseEvent(err);
|
|
401
|
+
return evt instanceof Error ? evt : new Error(extractErrorMessage(evt));
|
|
402
|
+
}
|
|
403
|
+
function extractErrorMessage(err) {
|
|
404
|
+
if (!err.error) {
|
|
405
|
+
return err.message || "Unknown listener error";
|
|
406
|
+
}
|
|
407
|
+
if (err.error.description) {
|
|
408
|
+
return err.error.description;
|
|
409
|
+
}
|
|
410
|
+
return typeof err.error === "string" ? err.error : JSON.stringify(err.error, null, 2);
|
|
411
|
+
}
|
|
562
412
|
|
|
563
413
|
// src/data/dataMethods.js
|
|
564
|
-
var
|
|
565
|
-
"
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
414
|
+
var excludeFalsey = (param, defValue) => {
|
|
415
|
+
const value = typeof param === "undefined" ? defValue : param;
|
|
416
|
+
return param === false ? void 0 : value;
|
|
417
|
+
};
|
|
418
|
+
var getMutationQuery = (options = {}) => {
|
|
419
|
+
return {
|
|
420
|
+
dryRun: options.dryRun,
|
|
421
|
+
returnIds: true,
|
|
422
|
+
returnDocuments: excludeFalsey(options.returnDocuments, true),
|
|
423
|
+
visibility: options.visibility || "sync",
|
|
424
|
+
autoGenerateArrayKeys: options.autoGenerateArrayKeys,
|
|
425
|
+
skipCrossDatasetReferenceValidation: options.skipCrossDatasetReferenceValidation
|
|
426
|
+
};
|
|
427
|
+
};
|
|
428
|
+
var isResponse = (event) => event.type === "response";
|
|
429
|
+
var getBody = (event) => event.body;
|
|
430
|
+
var indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
|
|
431
|
+
indexed[attr(doc)] = doc;
|
|
432
|
+
return indexed;
|
|
433
|
+
}, /* @__PURE__ */ Object.create(null));
|
|
434
|
+
var toPromise = (observable2) => observable2.toPromise();
|
|
435
|
+
var getQuerySizeLimit = 11264;
|
|
436
|
+
var dataMethods_default = {
|
|
437
|
+
listen,
|
|
438
|
+
getDataUrl(operation, path) {
|
|
439
|
+
const config = this.clientConfig;
|
|
440
|
+
const catalog = hasDataset(config);
|
|
441
|
+
const baseUri = `/${operation}/${catalog}`;
|
|
442
|
+
const uri = path ? `${baseUri}/${path}` : baseUri;
|
|
443
|
+
return `/data${uri}`.replace(/\/($|\?)/, "$1");
|
|
444
|
+
},
|
|
445
|
+
fetch(query, params, options = {}) {
|
|
446
|
+
const mapResponse = options.filterResponse === false ? (res) => res : (res) => res.result;
|
|
447
|
+
const observable2 = this._dataRequest("query", { query, params }, options).pipe(map(mapResponse));
|
|
448
|
+
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
449
|
+
},
|
|
450
|
+
getDocument(id, opts = {}) {
|
|
451
|
+
const options = { uri: this.getDataUrl("doc", id), json: true, tag: opts.tag };
|
|
452
|
+
const observable2 = this._requestObservable(options).pipe(
|
|
453
|
+
filter(isResponse),
|
|
454
|
+
map((event) => event.body.documents && event.body.documents[0])
|
|
455
|
+
);
|
|
456
|
+
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
457
|
+
},
|
|
458
|
+
getDocuments(ids, opts = {}) {
|
|
459
|
+
const options = { uri: this.getDataUrl("doc", ids.join(",")), json: true, tag: opts.tag };
|
|
460
|
+
const observable2 = this._requestObservable(options).pipe(
|
|
461
|
+
filter(isResponse),
|
|
462
|
+
map((event) => {
|
|
463
|
+
const indexed = indexBy(event.body.documents || [], (doc) => doc._id);
|
|
464
|
+
return ids.map((id) => indexed[id] || null);
|
|
465
|
+
})
|
|
466
|
+
);
|
|
467
|
+
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
468
|
+
},
|
|
469
|
+
create(doc, options) {
|
|
470
|
+
return this._create(doc, "create", options);
|
|
471
|
+
},
|
|
472
|
+
createIfNotExists(doc, options) {
|
|
473
|
+
requireDocumentId("createIfNotExists", doc);
|
|
474
|
+
return this._create(doc, "createIfNotExists", options);
|
|
475
|
+
},
|
|
476
|
+
createOrReplace(doc, options) {
|
|
477
|
+
requireDocumentId("createOrReplace", doc);
|
|
478
|
+
return this._create(doc, "createOrReplace", options);
|
|
479
|
+
},
|
|
480
|
+
patch(selector, operations) {
|
|
481
|
+
return new patch_default(selector, operations, this);
|
|
482
|
+
},
|
|
483
|
+
delete(selection, options) {
|
|
484
|
+
return this.dataRequest("mutate", { mutations: [{ delete: getSelection(selection) }] }, options);
|
|
485
|
+
},
|
|
486
|
+
mutate(mutations, options) {
|
|
487
|
+
const mut = mutations instanceof patch_default || mutations instanceof transaction_default ? mutations.serialize() : mutations;
|
|
488
|
+
const muts = Array.isArray(mut) ? mut : [mut];
|
|
489
|
+
const transactionId = options && options.transactionId;
|
|
490
|
+
return this.dataRequest("mutate", { mutations: muts, transactionId }, options);
|
|
491
|
+
},
|
|
492
|
+
transaction(operations) {
|
|
493
|
+
return new transaction_default(operations, this);
|
|
494
|
+
},
|
|
495
|
+
dataRequest(endpoint, body, options = {}) {
|
|
496
|
+
const request2 = this._dataRequest(endpoint, body, options);
|
|
497
|
+
return this.isPromiseAPI() ? toPromise(request2) : request2;
|
|
498
|
+
},
|
|
499
|
+
_dataRequest(endpoint, body, options = {}) {
|
|
500
|
+
const isMutation = endpoint === "mutate";
|
|
501
|
+
const isQuery = endpoint === "query";
|
|
502
|
+
const strQuery = !isMutation && encodeQueryString_default(body);
|
|
503
|
+
const useGet = !isMutation && strQuery.length < getQuerySizeLimit;
|
|
504
|
+
const stringQuery = useGet ? strQuery : "";
|
|
505
|
+
const returnFirst = options.returnFirst;
|
|
506
|
+
const { timeout, token, tag, headers } = options;
|
|
507
|
+
const uri = this.getDataUrl(endpoint, stringQuery);
|
|
508
|
+
const reqOptions = {
|
|
509
|
+
method: useGet ? "GET" : "POST",
|
|
510
|
+
uri,
|
|
511
|
+
json: true,
|
|
512
|
+
body: useGet ? void 0 : body,
|
|
513
|
+
query: isMutation && getMutationQuery(options),
|
|
514
|
+
timeout,
|
|
515
|
+
headers,
|
|
516
|
+
token,
|
|
517
|
+
tag,
|
|
518
|
+
canUseCdn: isQuery
|
|
586
519
|
};
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
const
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
fetch(query, params, options = {}) {
|
|
605
|
-
const mapResponse = options.filterResponse === false ? (res) => res : (res) => res.result;
|
|
606
|
-
const observable2 = this._dataRequest("query", { query, params }, options).pipe(map2(mapResponse));
|
|
607
|
-
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
608
|
-
},
|
|
609
|
-
getDocument(id, opts = {}) {
|
|
610
|
-
const options = { uri: this.getDataUrl("doc", id), json: true, tag: opts.tag };
|
|
611
|
-
const observable2 = this._requestObservable(options).pipe(
|
|
612
|
-
filter2(isResponse),
|
|
613
|
-
map2((event) => event.body.documents && event.body.documents[0])
|
|
614
|
-
);
|
|
615
|
-
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
616
|
-
},
|
|
617
|
-
getDocuments(ids, opts = {}) {
|
|
618
|
-
const options = { uri: this.getDataUrl("doc", ids.join(",")), json: true, tag: opts.tag };
|
|
619
|
-
const observable2 = this._requestObservable(options).pipe(
|
|
620
|
-
filter2(isResponse),
|
|
621
|
-
map2((event) => {
|
|
622
|
-
const indexed = indexBy(event.body.documents || [], (doc) => doc._id);
|
|
623
|
-
return ids.map((id) => indexed[id] || null);
|
|
624
|
-
})
|
|
625
|
-
);
|
|
626
|
-
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
627
|
-
},
|
|
628
|
-
create(doc, options) {
|
|
629
|
-
return this._create(doc, "create", options);
|
|
630
|
-
},
|
|
631
|
-
createIfNotExists(doc, options) {
|
|
632
|
-
validators.requireDocumentId("createIfNotExists", doc);
|
|
633
|
-
return this._create(doc, "createIfNotExists", options);
|
|
634
|
-
},
|
|
635
|
-
createOrReplace(doc, options) {
|
|
636
|
-
validators.requireDocumentId("createOrReplace", doc);
|
|
637
|
-
return this._create(doc, "createOrReplace", options);
|
|
638
|
-
},
|
|
639
|
-
patch(selector, operations) {
|
|
640
|
-
return new Patch(selector, operations, this);
|
|
641
|
-
},
|
|
642
|
-
delete(selection, options) {
|
|
643
|
-
return this.dataRequest("mutate", { mutations: [{ delete: getSelection(selection) }] }, options);
|
|
644
|
-
},
|
|
645
|
-
mutate(mutations, options) {
|
|
646
|
-
const mut = mutations instanceof Patch || mutations instanceof Transaction ? mutations.serialize() : mutations;
|
|
647
|
-
const muts = Array.isArray(mut) ? mut : [mut];
|
|
648
|
-
const transactionId = options && options.transactionId;
|
|
649
|
-
return this.dataRequest("mutate", { mutations: muts, transactionId }, options);
|
|
650
|
-
},
|
|
651
|
-
transaction(operations) {
|
|
652
|
-
return new Transaction(operations, this);
|
|
653
|
-
},
|
|
654
|
-
dataRequest(endpoint, body, options = {}) {
|
|
655
|
-
const request = this._dataRequest(endpoint, body, options);
|
|
656
|
-
return this.isPromiseAPI() ? toPromise(request) : request;
|
|
657
|
-
},
|
|
658
|
-
_dataRequest(endpoint, body, options = {}) {
|
|
659
|
-
const isMutation = endpoint === "mutate";
|
|
660
|
-
const isQuery = endpoint === "query";
|
|
661
|
-
const strQuery = !isMutation && encodeQueryString(body);
|
|
662
|
-
const useGet = !isMutation && strQuery.length < getQuerySizeLimit;
|
|
663
|
-
const stringQuery = useGet ? strQuery : "";
|
|
664
|
-
const returnFirst = options.returnFirst;
|
|
665
|
-
const { timeout, token, tag, headers } = options;
|
|
666
|
-
const uri = this.getDataUrl(endpoint, stringQuery);
|
|
667
|
-
const reqOptions = {
|
|
668
|
-
method: useGet ? "GET" : "POST",
|
|
669
|
-
uri,
|
|
670
|
-
json: true,
|
|
671
|
-
body: useGet ? void 0 : body,
|
|
672
|
-
query: isMutation && getMutationQuery(options),
|
|
673
|
-
timeout,
|
|
674
|
-
headers,
|
|
675
|
-
token,
|
|
676
|
-
tag,
|
|
677
|
-
canUseCdn: isQuery
|
|
520
|
+
return this._requestObservable(reqOptions).pipe(
|
|
521
|
+
filter(isResponse),
|
|
522
|
+
map(getBody),
|
|
523
|
+
map((res) => {
|
|
524
|
+
if (!isMutation) {
|
|
525
|
+
return res;
|
|
526
|
+
}
|
|
527
|
+
const results = res.results || [];
|
|
528
|
+
if (options.returnDocuments) {
|
|
529
|
+
return returnFirst ? results[0] && results[0].document : results.map((mut) => mut.document);
|
|
530
|
+
}
|
|
531
|
+
const key = returnFirst ? "documentId" : "documentIds";
|
|
532
|
+
const ids = returnFirst ? results[0] && results[0].id : results.map((mut) => mut.id);
|
|
533
|
+
return {
|
|
534
|
+
transactionId: res.transactionId,
|
|
535
|
+
results,
|
|
536
|
+
[key]: ids
|
|
678
537
|
};
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
const results = res.results || [];
|
|
687
|
-
if (options.returnDocuments) {
|
|
688
|
-
return returnFirst ? results[0] && results[0].document : results.map((mut) => mut.document);
|
|
689
|
-
}
|
|
690
|
-
const key = returnFirst ? "documentId" : "documentIds";
|
|
691
|
-
const ids = returnFirst ? results[0] && results[0].id : results.map((mut) => mut.id);
|
|
692
|
-
return {
|
|
693
|
-
transactionId: res.transactionId,
|
|
694
|
-
results,
|
|
695
|
-
[key]: ids
|
|
696
|
-
};
|
|
697
|
-
})
|
|
698
|
-
);
|
|
699
|
-
},
|
|
700
|
-
_create(doc, op, options = {}) {
|
|
701
|
-
const mutation = { [op]: doc };
|
|
702
|
-
const opts = Object.assign({ returnFirst: true, returnDocuments: true }, options);
|
|
703
|
-
return this.dataRequest("mutate", { mutations: [mutation] }, opts);
|
|
704
|
-
}
|
|
705
|
-
};
|
|
538
|
+
})
|
|
539
|
+
);
|
|
540
|
+
},
|
|
541
|
+
_create(doc, op, options = {}) {
|
|
542
|
+
const mutation = { [op]: doc };
|
|
543
|
+
const opts = Object.assign({ returnFirst: true, returnDocuments: true }, options);
|
|
544
|
+
return this.dataRequest("mutate", { mutations: [mutation] }, opts);
|
|
706
545
|
}
|
|
707
|
-
}
|
|
546
|
+
};
|
|
708
547
|
|
|
709
548
|
// src/datasets/datasetsClient.js
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
_modify(method, name, body) {
|
|
730
|
-
validate2.dataset(name);
|
|
731
|
-
return this.request({ method, uri: `/datasets/${name}`, body });
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
module.exports = DatasetsClient;
|
|
549
|
+
function DatasetsClient(client) {
|
|
550
|
+
this.request = client.request.bind(client);
|
|
551
|
+
}
|
|
552
|
+
Object.assign(DatasetsClient.prototype, {
|
|
553
|
+
create(name, options) {
|
|
554
|
+
return this._modify("PUT", name, options);
|
|
555
|
+
},
|
|
556
|
+
edit(name, options) {
|
|
557
|
+
return this._modify("PATCH", name, options);
|
|
558
|
+
},
|
|
559
|
+
delete(name) {
|
|
560
|
+
return this._modify("DELETE", name);
|
|
561
|
+
},
|
|
562
|
+
list() {
|
|
563
|
+
return this.request({ uri: "/datasets" });
|
|
564
|
+
},
|
|
565
|
+
_modify(method, name, body) {
|
|
566
|
+
dataset(name);
|
|
567
|
+
return this.request({ method, uri: `/datasets/${name}`, body });
|
|
735
568
|
}
|
|
736
569
|
});
|
|
570
|
+
var datasetsClient_default = DatasetsClient;
|
|
737
571
|
|
|
738
572
|
// src/projects/projectsClient.js
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
getById(id) {
|
|
749
|
-
return this.client.request({ uri: `/projects/${id}` });
|
|
750
|
-
}
|
|
751
|
-
});
|
|
752
|
-
module.exports = ProjectsClient;
|
|
573
|
+
function ProjectsClient(client) {
|
|
574
|
+
this.client = client;
|
|
575
|
+
}
|
|
576
|
+
Object.assign(ProjectsClient.prototype, {
|
|
577
|
+
list() {
|
|
578
|
+
return this.client.request({ uri: "/projects" });
|
|
579
|
+
},
|
|
580
|
+
getById(id) {
|
|
581
|
+
return this.client.request({ uri: `/projects/${id}` });
|
|
753
582
|
}
|
|
754
583
|
});
|
|
584
|
+
var projectsClient_default = ProjectsClient;
|
|
755
585
|
|
|
756
586
|
// src/http/queryString.js
|
|
757
|
-
var
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
return qs.length > 0 ? `?${qs.join("&")}` : "";
|
|
767
|
-
};
|
|
587
|
+
var queryString_default = (params) => {
|
|
588
|
+
const qs = [];
|
|
589
|
+
for (const key in params) {
|
|
590
|
+
if (params.hasOwnProperty(key)) {
|
|
591
|
+
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
|
|
592
|
+
}
|
|
768
593
|
}
|
|
769
|
-
}
|
|
594
|
+
return qs.length > 0 ? `?${qs.join("&")}` : "";
|
|
595
|
+
};
|
|
770
596
|
|
|
771
597
|
// src/assets/assetsClient.js
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
598
|
+
function AssetsClient(client) {
|
|
599
|
+
this.client = client;
|
|
600
|
+
}
|
|
601
|
+
function optionsFromFile(opts, file) {
|
|
602
|
+
if (typeof window === "undefined" || !(file instanceof window.File)) {
|
|
603
|
+
return opts;
|
|
604
|
+
}
|
|
605
|
+
return Object.assign(
|
|
606
|
+
{
|
|
607
|
+
filename: opts.preserveFilename === false ? void 0 : file.name,
|
|
608
|
+
contentType: file.type
|
|
609
|
+
},
|
|
610
|
+
opts
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
Object.assign(AssetsClient.prototype, {
|
|
614
|
+
upload(assetType, body, opts = {}) {
|
|
615
|
+
validateAssetType(assetType);
|
|
616
|
+
let meta = opts.extract || void 0;
|
|
617
|
+
if (meta && !meta.length) {
|
|
618
|
+
meta = ["none"];
|
|
779
619
|
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
620
|
+
const dataset2 = hasDataset(this.client.clientConfig);
|
|
621
|
+
const assetEndpoint = assetType === "image" ? "images" : "files";
|
|
622
|
+
const options = optionsFromFile(opts, body);
|
|
623
|
+
const { tag, label, title, description, creditLine, filename, source } = options;
|
|
624
|
+
const query = {
|
|
625
|
+
label,
|
|
626
|
+
title,
|
|
627
|
+
description,
|
|
628
|
+
filename,
|
|
629
|
+
meta,
|
|
630
|
+
creditLine
|
|
631
|
+
};
|
|
632
|
+
if (source) {
|
|
633
|
+
query.sourceId = source.id;
|
|
634
|
+
query.sourceName = source.name;
|
|
635
|
+
query.sourceUrl = source.url;
|
|
791
636
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
const assetEndpoint = assetType === "image" ? "images" : "files";
|
|
801
|
-
const options = optionsFromFile(opts, body);
|
|
802
|
-
const { tag, label, title, description, creditLine, filename, source } = options;
|
|
803
|
-
const query = {
|
|
804
|
-
label,
|
|
805
|
-
title,
|
|
806
|
-
description,
|
|
807
|
-
filename,
|
|
808
|
-
meta,
|
|
809
|
-
creditLine
|
|
810
|
-
};
|
|
811
|
-
if (source) {
|
|
812
|
-
query.sourceId = source.id;
|
|
813
|
-
query.sourceName = source.name;
|
|
814
|
-
query.sourceUrl = source.url;
|
|
815
|
-
}
|
|
816
|
-
const observable2 = this.client._requestObservable({
|
|
817
|
-
tag,
|
|
818
|
-
method: "POST",
|
|
819
|
-
timeout: options.timeout || 0,
|
|
820
|
-
uri: `/assets/${assetEndpoint}/${dataset}`,
|
|
821
|
-
headers: options.contentType ? { "Content-Type": options.contentType } : {},
|
|
822
|
-
query,
|
|
823
|
-
body
|
|
824
|
-
});
|
|
825
|
-
return this.client.isPromiseAPI() ? observable2.pipe(
|
|
826
|
-
filter2((event) => event.type === "response"),
|
|
827
|
-
map2((event) => event.body.document)
|
|
828
|
-
).toPromise() : observable2;
|
|
829
|
-
},
|
|
830
|
-
delete(type, id) {
|
|
831
|
-
console.warn("client.assets.delete() is deprecated, please use client.delete(<document-id>)");
|
|
832
|
-
let docId = id || "";
|
|
833
|
-
if (!/^(image|file)-/.test(docId)) {
|
|
834
|
-
docId = `${type}-${docId}`;
|
|
835
|
-
} else if (type._id) {
|
|
836
|
-
docId = type._id;
|
|
837
|
-
}
|
|
838
|
-
validators.hasDataset(this.client.clientConfig);
|
|
839
|
-
return this.client.delete(docId);
|
|
840
|
-
},
|
|
841
|
-
getImageUrl(ref, query) {
|
|
842
|
-
const id = ref._ref || ref;
|
|
843
|
-
if (typeof id !== "string") {
|
|
844
|
-
throw new Error(
|
|
845
|
-
"getImageUrl() needs either an object with a _ref, or a string with an asset document ID"
|
|
846
|
-
);
|
|
847
|
-
}
|
|
848
|
-
if (!/^image-[A-Za-z0-9_]+-\d+x\d+-[a-z]{1,5}$/.test(id)) {
|
|
849
|
-
throw new Error(
|
|
850
|
-
`Unsupported asset ID "${id}". URL generation only works for auto-generated IDs.`
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
const [, assetId, size, format] = id.split("-");
|
|
854
|
-
validators.hasDataset(this.client.clientConfig);
|
|
855
|
-
const { projectId, dataset } = this.client.clientConfig;
|
|
856
|
-
const qs = query ? queryString(query) : "";
|
|
857
|
-
return `https://cdn.sanity.io/images/${projectId}/${dataset}/${assetId}-${size}.${format}${qs}`;
|
|
858
|
-
}
|
|
637
|
+
const observable2 = this.client._requestObservable({
|
|
638
|
+
tag,
|
|
639
|
+
method: "POST",
|
|
640
|
+
timeout: options.timeout || 0,
|
|
641
|
+
uri: `/assets/${assetEndpoint}/${dataset2}`,
|
|
642
|
+
headers: options.contentType ? { "Content-Type": options.contentType } : {},
|
|
643
|
+
query,
|
|
644
|
+
body
|
|
859
645
|
});
|
|
860
|
-
|
|
646
|
+
return this.client.isPromiseAPI() ? observable2.pipe(
|
|
647
|
+
filter((event) => event.type === "response"),
|
|
648
|
+
map((event) => event.body.document)
|
|
649
|
+
).toPromise() : observable2;
|
|
650
|
+
},
|
|
651
|
+
delete(type, id) {
|
|
652
|
+
console.warn("client.assets.delete() is deprecated, please use client.delete(<document-id>)");
|
|
653
|
+
let docId = id || "";
|
|
654
|
+
if (!/^(image|file)-/.test(docId)) {
|
|
655
|
+
docId = `${type}-${docId}`;
|
|
656
|
+
} else if (type._id) {
|
|
657
|
+
docId = type._id;
|
|
658
|
+
}
|
|
659
|
+
hasDataset(this.client.clientConfig);
|
|
660
|
+
return this.client.delete(docId);
|
|
661
|
+
},
|
|
662
|
+
getImageUrl(ref, query) {
|
|
663
|
+
const id = ref._ref || ref;
|
|
664
|
+
if (typeof id !== "string") {
|
|
665
|
+
throw new Error(
|
|
666
|
+
"getImageUrl() needs either an object with a _ref, or a string with an asset document ID"
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
if (!/^image-[A-Za-z0-9_]+-\d+x\d+-[a-z]{1,5}$/.test(id)) {
|
|
670
|
+
throw new Error(
|
|
671
|
+
`Unsupported asset ID "${id}". URL generation only works for auto-generated IDs.`
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
const [, assetId, size, format] = id.split("-");
|
|
675
|
+
hasDataset(this.client.clientConfig);
|
|
676
|
+
const { projectId: projectId2, dataset: dataset2 } = this.client.clientConfig;
|
|
677
|
+
const qs = query ? queryString_default(query) : "";
|
|
678
|
+
return `https://cdn.sanity.io/images/${projectId2}/${dataset2}/${assetId}-${size}.${format}${qs}`;
|
|
861
679
|
}
|
|
862
680
|
});
|
|
681
|
+
var assetsClient_default = AssetsClient;
|
|
863
682
|
|
|
864
683
|
// src/users/usersClient.js
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
getById(id) {
|
|
872
|
-
return this.client.request({ uri: `/users/${id}` });
|
|
873
|
-
}
|
|
874
|
-
});
|
|
875
|
-
module.exports = UsersClient;
|
|
684
|
+
function UsersClient(client) {
|
|
685
|
+
this.client = client;
|
|
686
|
+
}
|
|
687
|
+
Object.assign(UsersClient.prototype, {
|
|
688
|
+
getById(id) {
|
|
689
|
+
return this.client.request({ uri: `/users/${id}` });
|
|
876
690
|
}
|
|
877
691
|
});
|
|
692
|
+
var usersClient_default = UsersClient;
|
|
878
693
|
|
|
879
694
|
// src/auth/authClient.js
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
logout() {
|
|
890
|
-
return this.client.request({ uri: "/auth/logout", method: "POST" });
|
|
891
|
-
}
|
|
892
|
-
});
|
|
893
|
-
module.exports = AuthClient;
|
|
695
|
+
function AuthClient(client) {
|
|
696
|
+
this.client = client;
|
|
697
|
+
}
|
|
698
|
+
Object.assign(AuthClient.prototype, {
|
|
699
|
+
getLoginProviders() {
|
|
700
|
+
return this.client.request({ uri: "/auth/providers" });
|
|
701
|
+
},
|
|
702
|
+
logout() {
|
|
703
|
+
return this.client.request({ uri: "/auth/logout", method: "POST" });
|
|
894
704
|
}
|
|
895
705
|
});
|
|
706
|
+
var authClient_default = AuthClient;
|
|
707
|
+
|
|
708
|
+
// src/http/request.js
|
|
709
|
+
import getIt from "get-it";
|
|
710
|
+
import { observable, jsonRequest, jsonResponse, progress } from "get-it/middleware";
|
|
896
711
|
|
|
897
712
|
// src/http/errors.js
|
|
898
713
|
import makeError from "make-error";
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
props.message = `${body.error} - ${body.message}`;
|
|
920
|
-
return props;
|
|
921
|
-
}
|
|
922
|
-
if (body.error && body.error.description) {
|
|
923
|
-
props.message = body.error.description;
|
|
924
|
-
props.details = body.error;
|
|
925
|
-
return props;
|
|
926
|
-
}
|
|
927
|
-
props.message = body.error || body.message || httpErrorMessage(res);
|
|
928
|
-
return props;
|
|
929
|
-
}
|
|
930
|
-
function httpErrorMessage(res) {
|
|
931
|
-
const statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
|
|
932
|
-
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}`;
|
|
933
|
-
}
|
|
934
|
-
function stringifyBody(body, res) {
|
|
935
|
-
const contentType = (res.headers["content-type"] || "").toLowerCase();
|
|
936
|
-
const isJson = contentType.indexOf("application/json") !== -1;
|
|
937
|
-
return isJson ? JSON.stringify(body, null, 2) : body;
|
|
938
|
-
}
|
|
939
|
-
makeError(ClientError);
|
|
940
|
-
makeError(ServerError);
|
|
941
|
-
exports.ClientError = ClientError;
|
|
942
|
-
exports.ServerError = ServerError;
|
|
714
|
+
function ClientError(res) {
|
|
715
|
+
const props = extractErrorProps(res);
|
|
716
|
+
ClientError.super.call(this, props.message);
|
|
717
|
+
Object.assign(this, props);
|
|
718
|
+
}
|
|
719
|
+
function ServerError(res) {
|
|
720
|
+
const props = extractErrorProps(res);
|
|
721
|
+
ServerError.super.call(this, props.message);
|
|
722
|
+
Object.assign(this, props);
|
|
723
|
+
}
|
|
724
|
+
function extractErrorProps(res) {
|
|
725
|
+
const body = res.body;
|
|
726
|
+
const props = {
|
|
727
|
+
response: res,
|
|
728
|
+
statusCode: res.statusCode,
|
|
729
|
+
responseBody: stringifyBody(body, res)
|
|
730
|
+
};
|
|
731
|
+
if (body.error && body.message) {
|
|
732
|
+
props.message = `${body.error} - ${body.message}`;
|
|
733
|
+
return props;
|
|
943
734
|
}
|
|
944
|
-
|
|
735
|
+
if (body.error && body.error.description) {
|
|
736
|
+
props.message = body.error.description;
|
|
737
|
+
props.details = body.error;
|
|
738
|
+
return props;
|
|
739
|
+
}
|
|
740
|
+
props.message = body.error || body.message || httpErrorMessage(res);
|
|
741
|
+
return props;
|
|
742
|
+
}
|
|
743
|
+
function httpErrorMessage(res) {
|
|
744
|
+
const statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
|
|
745
|
+
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}`;
|
|
746
|
+
}
|
|
747
|
+
function stringifyBody(body, res) {
|
|
748
|
+
const contentType = (res.headers["content-type"] || "").toLowerCase();
|
|
749
|
+
const isJson = contentType.indexOf("application/json") !== -1;
|
|
750
|
+
return isJson ? JSON.stringify(body, null, 2) : body;
|
|
751
|
+
}
|
|
752
|
+
makeError(ClientError);
|
|
753
|
+
makeError(ServerError);
|
|
945
754
|
|
|
946
755
|
// src/http/browserMiddleware.js
|
|
947
|
-
var
|
|
948
|
-
"src/http/browserMiddleware.js"(exports, module) {
|
|
949
|
-
module.exports = [];
|
|
950
|
-
}
|
|
951
|
-
});
|
|
756
|
+
var browserMiddleware_default = [];
|
|
952
757
|
|
|
953
758
|
// src/http/request.js
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
var httpError = {
|
|
961
|
-
onResponse: (res) => {
|
|
962
|
-
if (res.statusCode >= 500) {
|
|
963
|
-
throw new ServerError(res);
|
|
964
|
-
} else if (res.statusCode >= 400) {
|
|
965
|
-
throw new ClientError(res);
|
|
966
|
-
}
|
|
967
|
-
return res;
|
|
968
|
-
}
|
|
969
|
-
};
|
|
970
|
-
var printWarnings = {
|
|
971
|
-
onResponse: (res) => {
|
|
972
|
-
const warn = res.headers["x-sanity-warning"];
|
|
973
|
-
const warnings2 = Array.isArray(warn) ? warn : [warn];
|
|
974
|
-
warnings2.filter(Boolean).forEach((msg) => console.warn(msg));
|
|
975
|
-
return res;
|
|
976
|
-
}
|
|
977
|
-
};
|
|
978
|
-
var envSpecific = typeof EdgeRuntime === "string" ? require_browserMiddleware() : require_browserMiddleware();
|
|
979
|
-
var middleware = envSpecific.concat([
|
|
980
|
-
printWarnings,
|
|
981
|
-
jsonRequest(),
|
|
982
|
-
jsonResponse(),
|
|
983
|
-
progress(),
|
|
984
|
-
httpError,
|
|
985
|
-
observable({ implementation: Observable2 })
|
|
986
|
-
]);
|
|
987
|
-
var request = getIt(middleware);
|
|
988
|
-
function httpRequest(options, requester = request) {
|
|
989
|
-
return requester(Object.assign({ maxRedirects: 0 }, options));
|
|
759
|
+
var httpError = {
|
|
760
|
+
onResponse: (res) => {
|
|
761
|
+
if (res.statusCode >= 500) {
|
|
762
|
+
throw new ServerError(res);
|
|
763
|
+
} else if (res.statusCode >= 400) {
|
|
764
|
+
throw new ClientError(res);
|
|
990
765
|
}
|
|
991
|
-
|
|
992
|
-
httpRequest.ClientError = ClientError;
|
|
993
|
-
httpRequest.ServerError = ServerError;
|
|
994
|
-
module.exports = httpRequest;
|
|
766
|
+
return res;
|
|
995
767
|
}
|
|
996
|
-
}
|
|
768
|
+
};
|
|
769
|
+
var printWarnings = {
|
|
770
|
+
onResponse: (res) => {
|
|
771
|
+
const warn = res.headers["x-sanity-warning"];
|
|
772
|
+
const warnings = Array.isArray(warn) ? warn : [warn];
|
|
773
|
+
warnings.filter(Boolean).forEach((msg) => console.warn(msg));
|
|
774
|
+
return res;
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
var envSpecific = browserMiddleware_default;
|
|
778
|
+
var middleware = envSpecific.concat([
|
|
779
|
+
printWarnings,
|
|
780
|
+
jsonRequest(),
|
|
781
|
+
jsonResponse(),
|
|
782
|
+
progress(),
|
|
783
|
+
httpError,
|
|
784
|
+
observable({ implementation: Observable })
|
|
785
|
+
]);
|
|
786
|
+
var request = getIt(middleware);
|
|
787
|
+
function httpRequest(options, requester = request) {
|
|
788
|
+
return requester(Object.assign({ maxRedirects: 0 }, options));
|
|
789
|
+
}
|
|
790
|
+
httpRequest.defaultRequester = request;
|
|
791
|
+
httpRequest.ClientError = ClientError;
|
|
792
|
+
httpRequest.ServerError = ServerError;
|
|
793
|
+
var request_default = httpRequest;
|
|
997
794
|
|
|
998
795
|
// src/http/requestOptions.js
|
|
999
|
-
var
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
if (token) {
|
|
1006
|
-
headers.Authorization = `Bearer ${token}`;
|
|
1007
|
-
}
|
|
1008
|
-
if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
|
|
1009
|
-
headers[projectHeader] = config.projectId;
|
|
1010
|
-
}
|
|
1011
|
-
const withCredentials = Boolean(
|
|
1012
|
-
typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials
|
|
1013
|
-
);
|
|
1014
|
-
const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
|
|
1015
|
-
return Object.assign({}, overrides, {
|
|
1016
|
-
headers: Object.assign({}, headers, overrides.headers || {}),
|
|
1017
|
-
timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
|
|
1018
|
-
proxy: overrides.proxy || config.proxy,
|
|
1019
|
-
json: true,
|
|
1020
|
-
withCredentials
|
|
1021
|
-
});
|
|
1022
|
-
};
|
|
796
|
+
var projectHeader = "X-Sanity-Project-ID";
|
|
797
|
+
var requestOptions_default = (config, overrides = {}) => {
|
|
798
|
+
const headers = {};
|
|
799
|
+
const token = overrides.token || config.token;
|
|
800
|
+
if (token) {
|
|
801
|
+
headers.Authorization = `Bearer ${token}`;
|
|
1023
802
|
}
|
|
1024
|
-
|
|
803
|
+
if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
|
|
804
|
+
headers[projectHeader] = config.projectId;
|
|
805
|
+
}
|
|
806
|
+
const withCredentials = Boolean(
|
|
807
|
+
typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials
|
|
808
|
+
);
|
|
809
|
+
const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
|
|
810
|
+
return Object.assign({}, overrides, {
|
|
811
|
+
headers: Object.assign({}, headers, overrides.headers || {}),
|
|
812
|
+
timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
|
|
813
|
+
proxy: overrides.proxy || config.proxy,
|
|
814
|
+
json: true,
|
|
815
|
+
withCredentials
|
|
816
|
+
});
|
|
817
|
+
};
|
|
1025
818
|
|
|
1026
819
|
// src/generateHelpUrl.js
|
|
1027
|
-
var
|
|
1028
|
-
__export(generateHelpUrl_exports, {
|
|
1029
|
-
default: () => generateHelpUrl
|
|
1030
|
-
});
|
|
820
|
+
var BASE_URL = "https://docs.sanity.io/help/";
|
|
1031
821
|
function generateHelpUrl(slug) {
|
|
1032
822
|
return BASE_URL + slug;
|
|
1033
823
|
}
|
|
1034
|
-
var BASE_URL;
|
|
1035
|
-
var init_generateHelpUrl = __esm({
|
|
1036
|
-
"src/generateHelpUrl.js"() {
|
|
1037
|
-
BASE_URL = "https://docs.sanity.io/help/";
|
|
1038
|
-
}
|
|
1039
|
-
});
|
|
1040
824
|
|
|
1041
825
|
// src/util/once.js
|
|
1042
|
-
var
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
return
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
};
|
|
1055
|
-
};
|
|
1056
|
-
}
|
|
1057
|
-
});
|
|
826
|
+
var once_default = (fn) => {
|
|
827
|
+
let didCall = false;
|
|
828
|
+
let returnValue;
|
|
829
|
+
return (...args) => {
|
|
830
|
+
if (didCall) {
|
|
831
|
+
return returnValue;
|
|
832
|
+
}
|
|
833
|
+
returnValue = fn(...args);
|
|
834
|
+
didCall = true;
|
|
835
|
+
return returnValue;
|
|
836
|
+
};
|
|
837
|
+
};
|
|
1058
838
|
|
|
1059
839
|
// src/warnings.js
|
|
1060
|
-
var
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
exports.printNoApiVersionSpecifiedWarning = createWarningPrinter([
|
|
1078
|
-
"Using the Sanity client without specifying an API version is deprecated.",
|
|
1079
|
-
`See ${generateHelpUrl2("js-client-api-version")}`
|
|
1080
|
-
]);
|
|
1081
|
-
}
|
|
1082
|
-
});
|
|
840
|
+
var createWarningPrinter = (message) => once_default((...args) => console.warn(message.join(" "), ...args));
|
|
841
|
+
var printCdnWarning = createWarningPrinter([
|
|
842
|
+
"You are not using the Sanity CDN. That means your data is always fresh, but the CDN is faster and",
|
|
843
|
+
`cheaper. Think about it! For more info, see ${generateHelpUrl("js-client-cdn-configuration")}.`,
|
|
844
|
+
"To hide this warning, please set the `useCdn` option to either `true` or `false` when creating",
|
|
845
|
+
"the client."
|
|
846
|
+
]);
|
|
847
|
+
var printBrowserTokenWarning = createWarningPrinter([
|
|
848
|
+
"You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
|
|
849
|
+
`See ${generateHelpUrl(
|
|
850
|
+
"js-client-browser-token"
|
|
851
|
+
)} for more information and how to hide this warning.`
|
|
852
|
+
]);
|
|
853
|
+
var printNoApiVersionSpecifiedWarning = createWarningPrinter([
|
|
854
|
+
"Using the Sanity client without specifying an API version is deprecated.",
|
|
855
|
+
`See ${generateHelpUrl("js-client-api-version")}`
|
|
856
|
+
]);
|
|
1083
857
|
|
|
1084
858
|
// src/config.js
|
|
1085
|
-
var
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
var
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
defaultCdnHost = "apicdn.sanity.io";
|
|
1098
|
-
defaultConfig = {
|
|
1099
|
-
apiHost: "https://api.sanity.io",
|
|
1100
|
-
apiVersion: "1",
|
|
1101
|
-
useProjectHostname: true,
|
|
1102
|
-
isPromiseAPI: true
|
|
1103
|
-
};
|
|
1104
|
-
LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
|
|
1105
|
-
isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
|
|
1106
|
-
validateApiVersion = function validateApiVersion2(apiVersion) {
|
|
1107
|
-
if (apiVersion === "1" || apiVersion === "X") {
|
|
1108
|
-
return;
|
|
1109
|
-
}
|
|
1110
|
-
const apiDate = new Date(apiVersion);
|
|
1111
|
-
const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
|
|
1112
|
-
if (!apiVersionValid) {
|
|
1113
|
-
throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
|
|
1114
|
-
}
|
|
1115
|
-
};
|
|
1116
|
-
initConfig = (config, prevConfig) => {
|
|
1117
|
-
const specifiedConfig = Object.assign({}, prevConfig, config);
|
|
1118
|
-
if (!specifiedConfig.apiVersion) {
|
|
1119
|
-
import_warnings.default.printNoApiVersionSpecifiedWarning();
|
|
1120
|
-
}
|
|
1121
|
-
const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
|
|
1122
|
-
const projectBased = newConfig.useProjectHostname;
|
|
1123
|
-
if (typeof Promise === "undefined") {
|
|
1124
|
-
const helpUrl = generateHelpUrl("js-client-promise-polyfill");
|
|
1125
|
-
throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
|
|
1126
|
-
}
|
|
1127
|
-
if (projectBased && !newConfig.projectId) {
|
|
1128
|
-
throw new Error("Configuration must contain `projectId`");
|
|
1129
|
-
}
|
|
1130
|
-
const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
|
|
1131
|
-
const isLocalhost = isBrowser && isLocal(window.location.hostname);
|
|
1132
|
-
if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
|
|
1133
|
-
import_warnings.default.printBrowserTokenWarning();
|
|
1134
|
-
} else if (typeof newConfig.useCdn === "undefined") {
|
|
1135
|
-
import_warnings.default.printCdnWarning();
|
|
1136
|
-
}
|
|
1137
|
-
if (projectBased) {
|
|
1138
|
-
import_validators.default.projectId(newConfig.projectId);
|
|
1139
|
-
}
|
|
1140
|
-
if (newConfig.dataset) {
|
|
1141
|
-
import_validators.default.dataset(newConfig.dataset);
|
|
1142
|
-
}
|
|
1143
|
-
if ("requestTagPrefix" in newConfig) {
|
|
1144
|
-
newConfig.requestTagPrefix = newConfig.requestTagPrefix ? import_validators.default.requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
|
|
1145
|
-
}
|
|
1146
|
-
newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, "");
|
|
1147
|
-
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
|
|
1148
|
-
newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
|
|
1149
|
-
validateApiVersion(newConfig.apiVersion);
|
|
1150
|
-
const hostParts = newConfig.apiHost.split("://", 2);
|
|
1151
|
-
const protocol = hostParts[0];
|
|
1152
|
-
const host = hostParts[1];
|
|
1153
|
-
const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
1154
|
-
if (newConfig.useProjectHostname) {
|
|
1155
|
-
newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`;
|
|
1156
|
-
newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`;
|
|
1157
|
-
} else {
|
|
1158
|
-
newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`;
|
|
1159
|
-
newConfig.cdnUrl = newConfig.url;
|
|
1160
|
-
}
|
|
1161
|
-
return newConfig;
|
|
1162
|
-
};
|
|
859
|
+
var defaultCdnHost = "apicdn.sanity.io";
|
|
860
|
+
var defaultConfig = {
|
|
861
|
+
apiHost: "https://api.sanity.io",
|
|
862
|
+
apiVersion: "1",
|
|
863
|
+
useProjectHostname: true,
|
|
864
|
+
isPromiseAPI: true
|
|
865
|
+
};
|
|
866
|
+
var LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
|
|
867
|
+
var isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
|
|
868
|
+
var validateApiVersion = function validateApiVersion2(apiVersion) {
|
|
869
|
+
if (apiVersion === "1" || apiVersion === "X") {
|
|
870
|
+
return;
|
|
1163
871
|
}
|
|
1164
|
-
|
|
872
|
+
const apiDate = new Date(apiVersion);
|
|
873
|
+
const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
|
|
874
|
+
if (!apiVersionValid) {
|
|
875
|
+
throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
var initConfig = (config, prevConfig) => {
|
|
879
|
+
const specifiedConfig = Object.assign({}, prevConfig, config);
|
|
880
|
+
if (!specifiedConfig.apiVersion) {
|
|
881
|
+
printNoApiVersionSpecifiedWarning();
|
|
882
|
+
}
|
|
883
|
+
const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
|
|
884
|
+
const projectBased = newConfig.useProjectHostname;
|
|
885
|
+
if (typeof Promise === "undefined") {
|
|
886
|
+
const helpUrl = generateHelpUrl("js-client-promise-polyfill");
|
|
887
|
+
throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
|
|
888
|
+
}
|
|
889
|
+
if (projectBased && !newConfig.projectId) {
|
|
890
|
+
throw new Error("Configuration must contain `projectId`");
|
|
891
|
+
}
|
|
892
|
+
const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
|
|
893
|
+
const isLocalhost = isBrowser && isLocal(window.location.hostname);
|
|
894
|
+
if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
|
|
895
|
+
printBrowserTokenWarning();
|
|
896
|
+
} else if (typeof newConfig.useCdn === "undefined") {
|
|
897
|
+
printCdnWarning();
|
|
898
|
+
}
|
|
899
|
+
if (projectBased) {
|
|
900
|
+
projectId(newConfig.projectId);
|
|
901
|
+
}
|
|
902
|
+
if (newConfig.dataset) {
|
|
903
|
+
dataset(newConfig.dataset);
|
|
904
|
+
}
|
|
905
|
+
if ("requestTagPrefix" in newConfig) {
|
|
906
|
+
newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
|
|
907
|
+
}
|
|
908
|
+
newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, "");
|
|
909
|
+
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
|
|
910
|
+
newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
|
|
911
|
+
validateApiVersion(newConfig.apiVersion);
|
|
912
|
+
const hostParts = newConfig.apiHost.split("://", 2);
|
|
913
|
+
const protocol = hostParts[0];
|
|
914
|
+
const host = hostParts[1];
|
|
915
|
+
const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
916
|
+
if (newConfig.useProjectHostname) {
|
|
917
|
+
newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`;
|
|
918
|
+
newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`;
|
|
919
|
+
} else {
|
|
920
|
+
newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`;
|
|
921
|
+
newConfig.cdnUrl = newConfig.url;
|
|
922
|
+
}
|
|
923
|
+
return newConfig;
|
|
924
|
+
};
|
|
1165
925
|
|
|
1166
926
|
// src/sanityClient.js
|
|
1167
|
-
var
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
this.projects = new ProjectsClient(this);
|
|
1192
|
-
this.users = new UsersClient(this);
|
|
1193
|
-
this.auth = new AuthClient(this);
|
|
1194
|
-
if (this.clientConfig.isPromiseAPI) {
|
|
1195
|
-
const observableConfig = Object.assign({}, this.clientConfig, { isPromiseAPI: false });
|
|
1196
|
-
this.observable = new SanityClient(observableConfig);
|
|
1197
|
-
}
|
|
927
|
+
var toPromise2 = (observable2) => observable2.toPromise();
|
|
928
|
+
function SanityClient(config = defaultConfig) {
|
|
929
|
+
if (!(this instanceof SanityClient)) {
|
|
930
|
+
return new SanityClient(config);
|
|
931
|
+
}
|
|
932
|
+
this.config(config);
|
|
933
|
+
this.assets = new assetsClient_default(this);
|
|
934
|
+
this.datasets = new datasetsClient_default(this);
|
|
935
|
+
this.projects = new projectsClient_default(this);
|
|
936
|
+
this.users = new usersClient_default(this);
|
|
937
|
+
this.auth = new authClient_default(this);
|
|
938
|
+
if (this.clientConfig.isPromiseAPI) {
|
|
939
|
+
const observableConfig = Object.assign({}, this.clientConfig, { isPromiseAPI: false });
|
|
940
|
+
this.observable = new SanityClient(observableConfig);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
Object.assign(SanityClient.prototype, dataMethods_default);
|
|
944
|
+
Object.assign(SanityClient.prototype, {
|
|
945
|
+
clone() {
|
|
946
|
+
return new SanityClient(this.config());
|
|
947
|
+
},
|
|
948
|
+
config(newConfig) {
|
|
949
|
+
if (typeof newConfig === "undefined") {
|
|
950
|
+
return Object.assign({}, this.clientConfig);
|
|
1198
951
|
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
(subscriber) => httpRequest(reqOptions, this.clientConfig.requester).subscribe(subscriber)
|
|
1241
|
-
);
|
|
1242
|
-
},
|
|
1243
|
-
request(options) {
|
|
1244
|
-
const observable2 = this._requestObservable(options).pipe(
|
|
1245
|
-
filter2((event) => event.type === "response"),
|
|
1246
|
-
map2((event) => event.body)
|
|
1247
|
-
);
|
|
1248
|
-
return this.isPromiseAPI() ? toPromise(observable2) : observable2;
|
|
1249
|
-
}
|
|
1250
|
-
});
|
|
1251
|
-
SanityClient.Patch = Patch;
|
|
1252
|
-
SanityClient.Transaction = Transaction;
|
|
1253
|
-
SanityClient.ClientError = httpRequest.ClientError;
|
|
1254
|
-
SanityClient.ServerError = httpRequest.ServerError;
|
|
1255
|
-
SanityClient.requester = httpRequest.defaultRequester;
|
|
1256
|
-
module.exports = SanityClient;
|
|
952
|
+
if (this.observable) {
|
|
953
|
+
const observableConfig = Object.assign({}, newConfig, { isPromiseAPI: false });
|
|
954
|
+
this.observable.config(observableConfig);
|
|
955
|
+
}
|
|
956
|
+
this.clientConfig = initConfig(newConfig, this.clientConfig || {});
|
|
957
|
+
return this;
|
|
958
|
+
},
|
|
959
|
+
withConfig(newConfig) {
|
|
960
|
+
return this.clone().config(newConfig);
|
|
961
|
+
},
|
|
962
|
+
getUrl(uri, useCdn = false) {
|
|
963
|
+
const base = useCdn ? this.clientConfig.cdnUrl : this.clientConfig.url;
|
|
964
|
+
return `${base}/${uri.replace(/^\//, "")}`;
|
|
965
|
+
},
|
|
966
|
+
isPromiseAPI() {
|
|
967
|
+
return this.clientConfig.isPromiseAPI;
|
|
968
|
+
},
|
|
969
|
+
_requestObservable(options) {
|
|
970
|
+
const uri = options.url || options.uri;
|
|
971
|
+
const canUseCdn = typeof options.canUseCdn === "undefined" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
|
|
972
|
+
const useCdn = this.clientConfig.useCdn && canUseCdn;
|
|
973
|
+
const tag = options.tag && this.clientConfig.requestTagPrefix ? [this.clientConfig.requestTagPrefix, options.tag].join(".") : options.tag || this.clientConfig.requestTagPrefix;
|
|
974
|
+
if (tag) {
|
|
975
|
+
options.query = { tag: requestTag(tag), ...options.query };
|
|
976
|
+
}
|
|
977
|
+
const reqOptions = requestOptions_default(
|
|
978
|
+
this.clientConfig,
|
|
979
|
+
Object.assign({}, options, {
|
|
980
|
+
url: this.getUrl(uri, useCdn)
|
|
981
|
+
})
|
|
982
|
+
);
|
|
983
|
+
return new Observable(
|
|
984
|
+
(subscriber) => request_default(reqOptions, this.clientConfig.requester).subscribe(subscriber)
|
|
985
|
+
);
|
|
986
|
+
},
|
|
987
|
+
request(options) {
|
|
988
|
+
const observable2 = this._requestObservable(options).pipe(
|
|
989
|
+
filter((event) => event.type === "response"),
|
|
990
|
+
map((event) => event.body)
|
|
991
|
+
);
|
|
992
|
+
return this.isPromiseAPI() ? toPromise2(observable2) : observable2;
|
|
1257
993
|
}
|
|
1258
994
|
});
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
995
|
+
SanityClient.Patch = patch_default;
|
|
996
|
+
SanityClient.Transaction = transaction_default;
|
|
997
|
+
SanityClient.ClientError = request_default.ClientError;
|
|
998
|
+
SanityClient.ServerError = request_default.ServerError;
|
|
999
|
+
SanityClient.requester = request_default.defaultRequester;
|
|
1000
|
+
var sanityClient_default = SanityClient;
|
|
1001
|
+
export {
|
|
1002
|
+
sanityClient_default as default
|
|
1003
|
+
};
|
|
1265
1004
|
//# sourceMappingURL=sanityClient.browser.mjs.map
|