@sanity/client 3.3.0 → 4.0.0-alpha.esm.3

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.
Files changed (62) hide show
  1. package/README.md +10 -10
  2. package/dist/browser/sanityClient.js +2243 -0
  3. package/dist/node/sanityClient.js +7075 -0
  4. package/lib/assets/assetsClient.js +132 -106
  5. package/lib/auth/authClient.js +38 -19
  6. package/lib/config.js +25 -13
  7. package/lib/data/dataMethods.js +32 -24
  8. package/lib/data/encodeQueryString.js +8 -2
  9. package/lib/data/listen.js +25 -21
  10. package/lib/data/patch.js +169 -118
  11. package/lib/data/transaction.js +134 -88
  12. package/lib/datasets/datasetsClient.js +63 -32
  13. package/lib/http/browserMiddleware.js +6 -1
  14. package/lib/http/errors.js +12 -8
  15. package/lib/http/nodeMiddleware.js +16 -9
  16. package/lib/http/queryString.js +9 -2
  17. package/lib/http/request.js +27 -24
  18. package/lib/http/requestOptions.js +10 -6
  19. package/lib/projects/projectsClient.js +37 -18
  20. package/lib/sanityClient.js +143 -105
  21. package/lib/users/usersClient.js +28 -11
  22. package/lib/util/defaults.js +12 -4
  23. package/lib/util/getSelection.js +7 -2
  24. package/lib/util/observable.js +24 -13
  25. package/lib/util/once.js +9 -2
  26. package/lib/util/pick.js +9 -2
  27. package/lib/validators.js +38 -15
  28. package/lib/warnings.js +14 -6
  29. package/package.json +22 -3
  30. package/sanityClient.d.ts +1 -13
  31. package/src/assets/assetsClient.js +132 -0
  32. package/src/auth/authClient.js +13 -0
  33. package/src/config.js +93 -0
  34. package/src/data/dataMethods.js +182 -0
  35. package/src/data/encodeQueryString.js +18 -0
  36. package/src/data/listen.js +159 -0
  37. package/src/data/patch.js +119 -0
  38. package/src/data/transaction.js +103 -0
  39. package/src/datasets/datasetsClient.js +28 -0
  40. package/src/http/browserMiddleware.js +1 -0
  41. package/src/http/errors.js +53 -0
  42. package/src/http/nodeMiddleware.js +11 -0
  43. package/src/http/queryString.js +10 -0
  44. package/src/http/request.js +50 -0
  45. package/src/http/requestOptions.js +29 -0
  46. package/src/projects/projectsClient.js +13 -0
  47. package/src/sanityClient.js +124 -0
  48. package/src/users/usersClient.js +9 -0
  49. package/src/util/defaults.js +9 -0
  50. package/src/util/getSelection.js +17 -0
  51. package/src/util/observable.js +6 -0
  52. package/src/util/once.js +12 -0
  53. package/src/util/pick.js +9 -0
  54. package/src/validators.js +76 -0
  55. package/src/warnings.js +25 -0
  56. package/test/client.test.js +37 -37
  57. package/test/encodeQueryString.test.js +3 -1
  58. package/test/helpers/sseServer.js +2 -1
  59. package/test/listen.test.js +4 -2
  60. package/test/warnings.test.disabled.js +8 -4
  61. package/umd/sanityClient.js +38 -38
  62. package/umd/sanityClient.min.js +1 -1
@@ -1,35 +1,39 @@
1
1
  "use strict";
2
2
 
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.listen = listen;
4
7
 
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
8
+ var _observable = require("../util/observable");
6
9
 
7
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+ var _eventsource = _interopRequireDefault(require("@sanity/eventsource"));
8
11
 
9
- var assign = require('object-assign');
12
+ var _pick = require("../util/pick");
10
13
 
11
- var _require = require('../util/observable'),
12
- Observable = _require.Observable;
14
+ var _defaults = require("../util/defaults");
13
15
 
14
- var polyfilledEventSource = require('@sanity/eventsource');
16
+ var _encodeQueryString = require("./encodeQueryString");
15
17
 
16
- var pick = require('../util/pick');
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
19
 
18
- var defaults = require('../util/defaults');
20
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
19
21
 
20
- var encodeQueryString = require('./encodeQueryString'); // Limit is 16K for a _request_, eg including headers. Have to account for an
21
- // unknown range of headers, but an average EventSource request from Chrome seems
22
- // to have around 700 bytes of cruft, so let us account for 1.2K to be "safe"
22
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
23
 
24
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
24
25
 
26
+ // Limit is 16K for a _request_, eg including headers. Have to account for an
27
+ // unknown range of headers, but an average EventSource request from Chrome seems
28
+ // to have around 700 bytes of cruft, so let us account for 1.2K to be "safe"
25
29
  var MAX_URL_LENGTH = 16000 - 1200;
26
- var EventSource = polyfilledEventSource;
30
+ var EventSource = _eventsource.default;
27
31
  var possibleOptions = ['includePreviousRevision', 'includeResult', 'visibility', 'effectFormat', 'tag'];
28
32
  var defaultOptions = {
29
33
  includeResult: true
30
34
  };
31
35
 
32
- module.exports = function listen(query, params) {
36
+ function listen(query, params) {
33
37
  var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
34
38
  var _this$clientConfig = this.clientConfig,
35
39
  url = _this$clientConfig.url,
@@ -38,12 +42,12 @@ module.exports = function listen(query, params) {
38
42
  requestTagPrefix = _this$clientConfig.requestTagPrefix;
39
43
  var tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join('.') : opts.tag;
40
44
 
41
- var options = _objectSpread(_objectSpread({}, defaults(opts, defaultOptions)), {}, {
45
+ var options = _objectSpread(_objectSpread({}, (0, _defaults.defaults)(opts, defaultOptions)), {}, {
42
46
  tag: tag
43
47
  });
44
48
 
45
- var listenOpts = pick(options, possibleOptions);
46
- var qs = encodeQueryString({
49
+ var listenOpts = (0, _pick.pick)(options, possibleOptions);
50
+ var qs = (0, _encodeQueryString.encodeQueryString)({
47
51
  query: query,
48
52
  params: params,
49
53
  options: listenOpts,
@@ -52,7 +56,7 @@ module.exports = function listen(query, params) {
52
56
  var uri = "".concat(url).concat(this.getDataUrl('listen', qs));
53
57
 
54
58
  if (uri.length > MAX_URL_LENGTH) {
55
- return new Observable(function (observer) {
59
+ return new _observable.Observable(function (observer) {
56
60
  return observer.error(new Error('Query too large for listener'));
57
61
  });
58
62
  }
@@ -71,7 +75,7 @@ module.exports = function listen(query, params) {
71
75
  };
72
76
  }
73
77
 
74
- return new Observable(function (observer) {
78
+ return new _observable.Observable(function (observer) {
75
79
  var es = getEventSource();
76
80
  var reconnectTimer;
77
81
  var stopped = false;
@@ -154,12 +158,12 @@ module.exports = function listen(query, params) {
154
158
 
155
159
  return stop;
156
160
  });
157
- };
161
+ }
158
162
 
159
163
  function parseEvent(event) {
160
164
  try {
161
165
  var data = event.data && JSON.parse(event.data) || {};
162
- return assign({
166
+ return Object.assign({
163
167
  type: event.type
164
168
  }, data);
165
169
  } catch (err) {
package/lib/data/patch.js CHANGED
@@ -1,123 +1,174 @@
1
1
  "use strict";
2
2
 
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Patch = void 0;
7
+
8
+ var _getSelection = require("../util/getSelection");
9
+
10
+ var _validators = require("../validators");
11
+
3
12
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
13
 
5
- var assign = require('object-assign');
6
-
7
- var getSelection = require('../util/getSelection');
8
-
9
- var validate = require('../validators');
10
-
11
- var validateObject = validate.validateObject;
12
- var validateInsert = validate.validateInsert;
13
-
14
- function Patch(selection) {
15
- var operations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
16
- var client = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
17
- this.selection = selection;
18
- this.operations = assign({}, operations);
19
- this.client = client;
20
- }
21
-
22
- assign(Patch.prototype, {
23
- clone: function clone() {
24
- return new Patch(this.selection, assign({}, this.operations), this.client);
25
- },
26
- set: function set(props) {
27
- return this._assign('set', props);
28
- },
29
- diffMatchPatch: function diffMatchPatch(props) {
30
- validateObject('diffMatchPatch', props);
31
- return this._assign('diffMatchPatch', props);
32
- },
33
- unset: function unset(attrs) {
34
- if (!Array.isArray(attrs)) {
35
- throw new Error('unset(attrs) takes an array of attributes to unset, non-array given');
36
- }
37
-
38
- this.operations = assign({}, this.operations, {
39
- unset: attrs
40
- });
41
- return this;
42
- },
43
- setIfMissing: function setIfMissing(props) {
44
- return this._assign('setIfMissing', props);
45
- },
46
- replace: function replace(props) {
47
- validateObject('replace', props);
48
- return this._set('set', {
49
- $: props
50
- }); // eslint-disable-line id-length
51
- },
52
- inc: function inc(props) {
53
- return this._assign('inc', props);
54
- },
55
- dec: function dec(props) {
56
- return this._assign('dec', props);
57
- },
58
- insert: function insert(at, selector, items) {
59
- var _this$_assign;
60
-
61
- validateInsert(at, selector, items);
62
- return this._assign('insert', (_this$_assign = {}, _defineProperty(_this$_assign, at, selector), _defineProperty(_this$_assign, "items", items), _this$_assign));
63
- },
64
- append: function append(selector, items) {
65
- return this.insert('after', "".concat(selector, "[-1]"), items);
66
- },
67
- prepend: function prepend(selector, items) {
68
- return this.insert('before', "".concat(selector, "[0]"), items);
69
- },
70
- splice: function splice(selector, start, deleteCount, items) {
71
- // Negative indexes doesn't mean the same in Sanity as they do in JS;
72
- // -1 means "actually at the end of the array", which allows inserting
73
- // at the end of the array without knowing its length. We therefore have
74
- // to substract negative indexes by one to match JS. If you want Sanity-
75
- // behaviour, just use `insert('replace', selector, items)` directly
76
- var delAll = typeof deleteCount === 'undefined' || deleteCount === -1;
77
- var startIndex = start < 0 ? start - 1 : start;
78
- var delCount = delAll ? -1 : Math.max(0, start + deleteCount);
79
- var delRange = startIndex < 0 && delCount >= 0 ? '' : delCount;
80
- var rangeSelector = "".concat(selector, "[").concat(startIndex, ":").concat(delRange, "]");
81
- return this.insert('replace', rangeSelector, items || []);
82
- },
83
- ifRevisionId: function ifRevisionId(rev) {
84
- this.operations.ifRevisionID = rev;
85
- return this;
86
- },
87
- serialize: function serialize() {
88
- return assign(getSelection(this.selection), this.operations);
89
- },
90
- toJSON: function toJSON() {
91
- return this.serialize();
92
- },
93
- commit: function commit() {
94
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
95
-
96
- if (!this.client) {
97
- throw new Error('No `client` passed to patch, either provide one or pass the ' + 'patch to a clients `mutate()` method');
98
- }
99
-
100
- var returnFirst = typeof this.selection === 'string';
101
- var opts = assign({
102
- returnFirst: returnFirst,
103
- returnDocuments: true
104
- }, options);
105
- return this.client.mutate({
106
- patch: this.serialize()
107
- }, opts);
108
- },
109
- reset: function reset() {
110
- this.operations = {};
111
- return this;
112
- },
113
- _set: function _set(op, props) {
114
- return this._assign(op, props, false);
115
- },
116
- _assign: function _assign(op, props) {
117
- var merge = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
118
- validateObject(op, props);
119
- this.operations = assign({}, this.operations, _defineProperty({}, op, assign({}, merge && this.operations[op] || {}, props)));
120
- return this;
14
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
+
16
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
+
18
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
19
+
20
+ var Patch = /*#__PURE__*/function () {
21
+ function Patch(selection) {
22
+ var operations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23
+ var client = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
24
+
25
+ _classCallCheck(this, Patch);
26
+
27
+ this.selection = selection;
28
+ this.operations = Object.assign({}, operations);
29
+ this.client = client;
121
30
  }
122
- });
123
- module.exports = Patch;
31
+
32
+ _createClass(Patch, [{
33
+ key: "clone",
34
+ value: function clone() {
35
+ return new Patch(this.selection, Object.assign({}, this.operations), this.client);
36
+ }
37
+ }, {
38
+ key: "set",
39
+ value: function set(props) {
40
+ return this._assign('set', props);
41
+ }
42
+ }, {
43
+ key: "diffMatchPatch",
44
+ value: function diffMatchPatch(props) {
45
+ (0, _validators.validateObject)('diffMatchPatch', props);
46
+ return this._assign('diffMatchPatch', props);
47
+ }
48
+ }, {
49
+ key: "unset",
50
+ value: function unset(attrs) {
51
+ if (!Array.isArray(attrs)) {
52
+ throw new Error('unset(attrs) takes an array of attributes to unset, non-array given');
53
+ }
54
+
55
+ this.operations = Object.assign({}, this.operations, {
56
+ unset: attrs
57
+ });
58
+ return this;
59
+ }
60
+ }, {
61
+ key: "setIfMissing",
62
+ value: function setIfMissing(props) {
63
+ return this._assign('setIfMissing', props);
64
+ }
65
+ }, {
66
+ key: "replace",
67
+ value: function replace(props) {
68
+ (0, _validators.validateObject)('replace', props);
69
+ return this._set('set', {
70
+ $: props
71
+ }); // eslint-disable-line id-length
72
+ }
73
+ }, {
74
+ key: "inc",
75
+ value: function inc(props) {
76
+ return this._assign('inc', props);
77
+ }
78
+ }, {
79
+ key: "dec",
80
+ value: function dec(props) {
81
+ return this._assign('dec', props);
82
+ }
83
+ }, {
84
+ key: "insert",
85
+ value: function insert(at, selector, items) {
86
+ var _this$_assign;
87
+
88
+ (0, _validators.validateInsert)(at, selector, items);
89
+ return this._assign('insert', (_this$_assign = {}, _defineProperty(_this$_assign, at, selector), _defineProperty(_this$_assign, "items", items), _this$_assign));
90
+ }
91
+ }, {
92
+ key: "append",
93
+ value: function append(selector, items) {
94
+ return this.insert('after', "".concat(selector, "[-1]"), items);
95
+ }
96
+ }, {
97
+ key: "prepend",
98
+ value: function prepend(selector, items) {
99
+ return this.insert('before', "".concat(selector, "[0]"), items);
100
+ }
101
+ }, {
102
+ key: "splice",
103
+ value: function splice(selector, start, deleteCount, items) {
104
+ // Negative indexes doesn't mean the same in Sanity as they do in JS;
105
+ // -1 means "actually at the end of the array", which allows inserting
106
+ // at the end of the array without knowing its length. We therefore have
107
+ // to substract negative indexes by one to match JS. If you want Sanity-
108
+ // behaviour, just use `insert('replace', selector, items)` directly
109
+ var delAll = typeof deleteCount === 'undefined' || deleteCount === -1;
110
+ var startIndex = start < 0 ? start - 1 : start;
111
+ var delCount = delAll ? -1 : Math.max(0, start + deleteCount);
112
+ var delRange = startIndex < 0 && delCount >= 0 ? '' : delCount;
113
+ var rangeSelector = "".concat(selector, "[").concat(startIndex, ":").concat(delRange, "]");
114
+ return this.insert('replace', rangeSelector, items || []);
115
+ }
116
+ }, {
117
+ key: "ifRevisionId",
118
+ value: function ifRevisionId(rev) {
119
+ this.operations.ifRevisionID = rev;
120
+ return this;
121
+ }
122
+ }, {
123
+ key: "serialize",
124
+ value: function serialize() {
125
+ return Object.assign((0, _getSelection.getSelection)(this.selection), this.operations);
126
+ }
127
+ }, {
128
+ key: "toJSON",
129
+ value: function toJSON() {
130
+ return this.serialize();
131
+ }
132
+ }, {
133
+ key: "commit",
134
+ value: function commit() {
135
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
136
+
137
+ if (!this.client) {
138
+ throw new Error('No `client` passed to patch, either provide one or pass the ' + 'patch to a clients `mutate()` method');
139
+ }
140
+
141
+ var returnFirst = typeof this.selection === 'string';
142
+ var opts = Object.assign({
143
+ returnFirst: returnFirst,
144
+ returnDocuments: true
145
+ }, options);
146
+ return this.client.mutate({
147
+ patch: this.serialize()
148
+ }, opts);
149
+ }
150
+ }, {
151
+ key: "reset",
152
+ value: function reset() {
153
+ this.operations = {};
154
+ return this;
155
+ }
156
+ }, {
157
+ key: "_set",
158
+ value: function _set(op, props) {
159
+ return this._assign(op, props, false);
160
+ }
161
+ }, {
162
+ key: "_assign",
163
+ value: function _assign(op, props) {
164
+ var merge = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
165
+ (0, _validators.validateObject)(op, props);
166
+ this.operations = Object.assign({}, this.operations, _defineProperty({}, op, Object.assign({}, merge && this.operations[op] || {}, props)));
167
+ return this;
168
+ }
169
+ }]);
170
+
171
+ return Patch;
172
+ }();
173
+
174
+ exports.Patch = Patch;
@@ -1,115 +1,161 @@
1
1
  "use strict";
2
2
 
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Transaction = void 0;
9
+
10
+ var validators = _interopRequireWildcard(require("../validators"));
11
+
12
+ var _patch = require("./patch");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
3
18
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
19
 
5
- var assign = require('object-assign');
20
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
21
 
7
- var validators = require('../validators');
22
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
8
23
 
9
- var Patch = require('./patch');
24
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
25
 
11
26
  var defaultMutateOptions = {
12
27
  returnDocuments: false
13
28
  };
14
29
 
15
- function Transaction() {
16
- var operations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
17
- var client = arguments.length > 1 ? arguments[1] : undefined;
18
- var transactionId = arguments.length > 2 ? arguments[2] : undefined;
19
- this.trxId = transactionId;
20
- this.operations = operations;
21
- this.client = client;
22
- }
23
-
24
- assign(Transaction.prototype, {
25
- clone: function clone() {
26
- return new Transaction(this.operations.slice(0), this.client, this.trxId);
27
- },
28
- create: function create(doc) {
29
- validators.validateObject('create', doc);
30
- return this._add({
31
- create: doc
32
- });
33
- },
34
- createIfNotExists: function createIfNotExists(doc) {
35
- var op = 'createIfNotExists';
36
- validators.validateObject(op, doc);
37
- validators.requireDocumentId(op, doc);
38
- return this._add(_defineProperty({}, op, doc));
39
- },
40
- createOrReplace: function createOrReplace(doc) {
41
- var op = 'createOrReplace';
42
- validators.validateObject(op, doc);
43
- validators.requireDocumentId(op, doc);
44
- return this._add(_defineProperty({}, op, doc));
45
- },
46
- delete: function _delete(documentId) {
47
- validators.validateDocumentId('delete', documentId);
48
- return this._add({
49
- delete: {
50
- id: documentId
51
- }
52
- });
53
- },
54
- patch: function patch(documentId, patchOps) {
55
- var isBuilder = typeof patchOps === 'function';
56
- var isPatch = documentId instanceof Patch; // transaction.patch(client.patch('documentId').inc({visits: 1}))
30
+ var Transaction = /*#__PURE__*/function () {
31
+ function Transaction() {
32
+ var operations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
33
+ var client = arguments.length > 1 ? arguments[1] : undefined;
34
+ var transactionId = arguments.length > 2 ? arguments[2] : undefined;
57
35
 
58
- if (isPatch) {
36
+ _classCallCheck(this, Transaction);
37
+
38
+ this.trxId = transactionId;
39
+ this.operations = operations;
40
+ this.client = client;
41
+ }
42
+
43
+ _createClass(Transaction, [{
44
+ key: "clone",
45
+ value: function clone() {
46
+ return new Transaction(this.operations.slice(0), this.client, this.trxId);
47
+ }
48
+ }, {
49
+ key: "create",
50
+ value: function create(doc) {
51
+ validators.validateObject('create', doc);
59
52
  return this._add({
60
- patch: documentId.serialize()
53
+ create: doc
61
54
  });
62
- } // patch => patch.inc({visits: 1}).set({foo: 'bar'})
55
+ }
56
+ }, {
57
+ key: "createIfNotExists",
58
+ value: function createIfNotExists(doc) {
59
+ var op = 'createIfNotExists';
60
+ validators.validateObject(op, doc);
61
+ validators.requireDocumentId(op, doc);
62
+ return this._add(_defineProperty({}, op, doc));
63
+ }
64
+ }, {
65
+ key: "createOrReplace",
66
+ value: function createOrReplace(doc) {
67
+ var op = 'createOrReplace';
68
+ validators.validateObject(op, doc);
69
+ validators.requireDocumentId(op, doc);
70
+ return this._add(_defineProperty({}, op, doc));
71
+ }
72
+ }, {
73
+ key: "delete",
74
+ value: function _delete(documentId) {
75
+ validators.validateDocumentId('delete', documentId);
76
+ return this._add({
77
+ delete: {
78
+ id: documentId
79
+ }
80
+ });
81
+ }
82
+ }, {
83
+ key: "patch",
84
+ value: function patch(documentId, patchOps) {
85
+ var isBuilder = typeof patchOps === 'function';
86
+ var isPatch = documentId instanceof _patch.Patch; // transaction.patch(client.patch('documentId').inc({visits: 1}))
87
+
88
+ if (isPatch) {
89
+ return this._add({
90
+ patch: documentId.serialize()
91
+ });
92
+ } // patch => patch.inc({visits: 1}).set({foo: 'bar'})
63
93
 
64
94
 
65
- if (isBuilder) {
66
- var patch = patchOps(new Patch(documentId, {}, this.client));
95
+ if (isBuilder) {
96
+ var patch = patchOps(new _patch.Patch(documentId, {}, this.client));
67
97
 
68
- if (!(patch instanceof Patch)) {
69
- throw new Error('function passed to `patch()` must return the patch');
98
+ if (!(patch instanceof _patch.Patch)) {
99
+ throw new Error('function passed to `patch()` must return the patch');
100
+ }
101
+
102
+ return this._add({
103
+ patch: patch.serialize()
104
+ });
70
105
  }
71
106
 
72
107
  return this._add({
73
- patch: patch.serialize()
108
+ patch: Object.assign({
109
+ id: documentId
110
+ }, patchOps)
74
111
  });
75
112
  }
113
+ }, {
114
+ key: "transactionId",
115
+ value: function transactionId(id) {
116
+ if (!id) {
117
+ return this.trxId;
118
+ }
76
119
 
77
- return this._add({
78
- patch: assign({
79
- id: documentId
80
- }, patchOps)
81
- });
82
- },
83
- transactionId: function transactionId(id) {
84
- if (!id) {
85
- return this.trxId;
120
+ this.trxId = id;
121
+ return this;
122
+ }
123
+ }, {
124
+ key: "serialize",
125
+ value: function serialize() {
126
+ return this.operations.slice();
86
127
  }
128
+ }, {
129
+ key: "toJSON",
130
+ value: function toJSON() {
131
+ return this.serialize();
132
+ }
133
+ }, {
134
+ key: "commit",
135
+ value: function commit(options) {
136
+ if (!this.client) {
137
+ throw new Error('No `client` passed to transaction, either provide one or pass the ' + 'transaction to a clients `mutate()` method');
138
+ }
87
139
 
88
- this.trxId = id;
89
- return this;
90
- },
91
- serialize: function serialize() {
92
- return this.operations.slice();
93
- },
94
- toJSON: function toJSON() {
95
- return this.serialize();
96
- },
97
- commit: function commit(options) {
98
- if (!this.client) {
99
- throw new Error('No `client` passed to transaction, either provide one or pass the ' + 'transaction to a clients `mutate()` method');
140
+ return this.client.mutate(this.serialize(), Object.assign({
141
+ transactionId: this.trxId
142
+ }, defaultMutateOptions, options || {}));
143
+ }
144
+ }, {
145
+ key: "reset",
146
+ value: function reset() {
147
+ this.operations = [];
148
+ return this;
149
+ }
150
+ }, {
151
+ key: "_add",
152
+ value: function _add(mut) {
153
+ this.operations.push(mut);
154
+ return this;
100
155
  }
156
+ }]);
101
157
 
102
- return this.client.mutate(this.serialize(), assign({
103
- transactionId: this.trxId
104
- }, defaultMutateOptions, options || {}));
105
- },
106
- reset: function reset() {
107
- this.operations = [];
108
- return this;
109
- },
110
- _add: function _add(mut) {
111
- this.operations.push(mut);
112
- return this;
113
- }
114
- });
115
- module.exports = Transaction;
158
+ return Transaction;
159
+ }();
160
+
161
+ exports.Transaction = Transaction;