bundle.esm.js 6.6 KB

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