bundle.cjs.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var Observable = _interopDefault(require('zen-observable-ts'));
  5. var tsInvariant = require('ts-invariant');
  6. var tslib = require('tslib');
  7. var apolloUtilities = require('apollo-utilities');
  8. function validateOperation(operation) {
  9. var OPERATION_FIELDS = [
  10. 'query',
  11. 'operationName',
  12. 'variables',
  13. 'extensions',
  14. 'context',
  15. ];
  16. for (var _i = 0, _a = Object.keys(operation); _i < _a.length; _i++) {
  17. var key = _a[_i];
  18. if (OPERATION_FIELDS.indexOf(key) < 0) {
  19. throw process.env.NODE_ENV === "production" ? new tsInvariant.InvariantError(2) : new tsInvariant.InvariantError("illegal argument: " + key);
  20. }
  21. }
  22. return operation;
  23. }
  24. var LinkError = (function (_super) {
  25. tslib.__extends(LinkError, _super);
  26. function LinkError(message, link) {
  27. var _this = _super.call(this, message) || this;
  28. _this.link = link;
  29. return _this;
  30. }
  31. return LinkError;
  32. }(Error));
  33. function isTerminating(link) {
  34. return link.request.length <= 1;
  35. }
  36. function toPromise(observable) {
  37. var completed = false;
  38. return new Promise(function (resolve, reject) {
  39. observable.subscribe({
  40. next: function (data) {
  41. if (completed) {
  42. process.env.NODE_ENV === "production" || tsInvariant.invariant.warn("Promise Wrapper does not support multiple results from Observable");
  43. }
  44. else {
  45. completed = true;
  46. resolve(data);
  47. }
  48. },
  49. error: reject,
  50. });
  51. });
  52. }
  53. var makePromise = toPromise;
  54. function fromPromise(promise) {
  55. return new Observable(function (observer) {
  56. promise
  57. .then(function (value) {
  58. observer.next(value);
  59. observer.complete();
  60. })
  61. .catch(observer.error.bind(observer));
  62. });
  63. }
  64. function fromError(errorValue) {
  65. return new Observable(function (observer) {
  66. observer.error(errorValue);
  67. });
  68. }
  69. function transformOperation(operation) {
  70. var transformedOperation = {
  71. variables: operation.variables || {},
  72. extensions: operation.extensions || {},
  73. operationName: operation.operationName,
  74. query: operation.query,
  75. };
  76. if (!transformedOperation.operationName) {
  77. transformedOperation.operationName =
  78. typeof transformedOperation.query !== 'string'
  79. ? apolloUtilities.getOperationName(transformedOperation.query)
  80. : '';
  81. }
  82. return transformedOperation;
  83. }
  84. function createOperation(starting, operation) {
  85. var context = tslib.__assign({}, starting);
  86. var setContext = function (next) {
  87. if (typeof next === 'function') {
  88. context = tslib.__assign({}, context, next(context));
  89. }
  90. else {
  91. context = tslib.__assign({}, context, next);
  92. }
  93. };
  94. var getContext = function () { return (tslib.__assign({}, context)); };
  95. Object.defineProperty(operation, 'setContext', {
  96. enumerable: false,
  97. value: setContext,
  98. });
  99. Object.defineProperty(operation, 'getContext', {
  100. enumerable: false,
  101. value: getContext,
  102. });
  103. Object.defineProperty(operation, 'toKey', {
  104. enumerable: false,
  105. value: function () { return getKey(operation); },
  106. });
  107. return operation;
  108. }
  109. function getKey(operation) {
  110. var query = operation.query, variables = operation.variables, operationName = operation.operationName;
  111. return JSON.stringify([operationName, query, variables]);
  112. }
  113. function passthrough(op, forward) {
  114. return forward ? forward(op) : Observable.of();
  115. }
  116. function toLink(handler) {
  117. return typeof handler === 'function' ? new ApolloLink(handler) : handler;
  118. }
  119. function empty() {
  120. return new ApolloLink(function () { return Observable.of(); });
  121. }
  122. function from(links) {
  123. if (links.length === 0)
  124. return empty();
  125. return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
  126. }
  127. function split(test, left, right) {
  128. var leftLink = toLink(left);
  129. var rightLink = toLink(right || new ApolloLink(passthrough));
  130. if (isTerminating(leftLink) && isTerminating(rightLink)) {
  131. return new ApolloLink(function (operation) {
  132. return test(operation)
  133. ? leftLink.request(operation) || Observable.of()
  134. : rightLink.request(operation) || Observable.of();
  135. });
  136. }
  137. else {
  138. return new ApolloLink(function (operation, forward) {
  139. return test(operation)
  140. ? leftLink.request(operation, forward) || Observable.of()
  141. : rightLink.request(operation, forward) || Observable.of();
  142. });
  143. }
  144. }
  145. var concat = function (first, second) {
  146. var firstLink = toLink(first);
  147. if (isTerminating(firstLink)) {
  148. process.env.NODE_ENV === "production" || tsInvariant.invariant.warn(new LinkError("You are calling concat on a terminating link, which will have no effect", firstLink));
  149. return firstLink;
  150. }
  151. var nextLink = toLink(second);
  152. if (isTerminating(nextLink)) {
  153. return new ApolloLink(function (operation) {
  154. return firstLink.request(operation, function (op) { return nextLink.request(op) || Observable.of(); }) || Observable.of();
  155. });
  156. }
  157. else {
  158. return new ApolloLink(function (operation, forward) {
  159. return (firstLink.request(operation, function (op) {
  160. return nextLink.request(op, forward) || Observable.of();
  161. }) || Observable.of());
  162. });
  163. }
  164. };
  165. var ApolloLink = (function () {
  166. function ApolloLink(request) {
  167. if (request)
  168. this.request = request;
  169. }
  170. ApolloLink.prototype.split = function (test, left, right) {
  171. return this.concat(split(test, left, right || new ApolloLink(passthrough)));
  172. };
  173. ApolloLink.prototype.concat = function (next) {
  174. return concat(this, next);
  175. };
  176. ApolloLink.prototype.request = function (operation, forward) {
  177. throw process.env.NODE_ENV === "production" ? new tsInvariant.InvariantError(1) : new tsInvariant.InvariantError('request is not implemented');
  178. };
  179. ApolloLink.empty = empty;
  180. ApolloLink.from = from;
  181. ApolloLink.split = split;
  182. ApolloLink.execute = execute;
  183. return ApolloLink;
  184. }());
  185. function execute(link, operation) {
  186. return (link.request(createOperation(operation.context, transformOperation(validateOperation(operation)))) || Observable.of());
  187. }
  188. exports.Observable = Observable;
  189. Object.defineProperty(exports, 'getOperationName', {
  190. enumerable: true,
  191. get: function () {
  192. return apolloUtilities.getOperationName;
  193. }
  194. });
  195. exports.ApolloLink = ApolloLink;
  196. exports.concat = concat;
  197. exports.createOperation = createOperation;
  198. exports.empty = empty;
  199. exports.execute = execute;
  200. exports.from = from;
  201. exports.fromError = fromError;
  202. exports.fromPromise = fromPromise;
  203. exports.makePromise = makePromise;
  204. exports.split = split;
  205. exports.toPromise = toPromise;
  206. //# sourceMappingURL=bundle.cjs.js.map