UNPKG

4.41 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var globals = require('../../utilities/globals');
6var tslib = require('tslib');
7var utilities = require('../../utilities');
8var utils = require('../utils');
9
10function passthrough(op, forward) {
11 return (forward ? forward(op) : utilities.Observable.of());
12}
13function toLink(handler) {
14 return typeof handler === 'function' ? new ApolloLink(handler) : handler;
15}
16function isTerminating(link) {
17 return link.request.length <= 1;
18}
19var LinkError = (function (_super) {
20 tslib.__extends(LinkError, _super);
21 function LinkError(message, link) {
22 var _this = _super.call(this, message) || this;
23 _this.link = link;
24 return _this;
25 }
26 return LinkError;
27}(Error));
28var ApolloLink = (function () {
29 function ApolloLink(request) {
30 if (request)
31 this.request = request;
32 }
33 ApolloLink.empty = function () {
34 return new ApolloLink(function () { return utilities.Observable.of(); });
35 };
36 ApolloLink.from = function (links) {
37 if (links.length === 0)
38 return ApolloLink.empty();
39 return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
40 };
41 ApolloLink.split = function (test, left, right) {
42 var leftLink = toLink(left);
43 var rightLink = toLink(right || new ApolloLink(passthrough));
44 if (isTerminating(leftLink) && isTerminating(rightLink)) {
45 return new ApolloLink(function (operation) {
46 return test(operation)
47 ? leftLink.request(operation) || utilities.Observable.of()
48 : rightLink.request(operation) || utilities.Observable.of();
49 });
50 }
51 else {
52 return new ApolloLink(function (operation, forward) {
53 return test(operation)
54 ? leftLink.request(operation, forward) || utilities.Observable.of()
55 : rightLink.request(operation, forward) || utilities.Observable.of();
56 });
57 }
58 };
59 ApolloLink.execute = function (link, operation) {
60 return (link.request(utils.createOperation(operation.context, utils.transformOperation(utils.validateOperation(operation)))) || utilities.Observable.of());
61 };
62 ApolloLink.concat = function (first, second) {
63 var firstLink = toLink(first);
64 if (isTerminating(firstLink)) {
65 __DEV__ && globals.invariant.warn(new LinkError("You are calling concat on a terminating link, which will have no effect", firstLink));
66 return firstLink;
67 }
68 var nextLink = toLink(second);
69 if (isTerminating(nextLink)) {
70 return new ApolloLink(function (operation) {
71 return firstLink.request(operation, function (op) { return nextLink.request(op) || utilities.Observable.of(); }) || utilities.Observable.of();
72 });
73 }
74 else {
75 return new ApolloLink(function (operation, forward) {
76 return (firstLink.request(operation, function (op) {
77 return nextLink.request(op, forward) || utilities.Observable.of();
78 }) || utilities.Observable.of());
79 });
80 }
81 };
82 ApolloLink.prototype.split = function (test, left, right) {
83 return this.concat(ApolloLink.split(test, left, right || new ApolloLink(passthrough)));
84 };
85 ApolloLink.prototype.concat = function (next) {
86 return ApolloLink.concat(this, next);
87 };
88 ApolloLink.prototype.request = function (operation, forward) {
89 throw __DEV__ ? new globals.InvariantError('request is not implemented') : new globals.InvariantError(22);
90 };
91 ApolloLink.prototype.onError = function (error, observer) {
92 if (observer && observer.error) {
93 observer.error(error);
94 return false;
95 }
96 throw error;
97 };
98 ApolloLink.prototype.setOnError = function (fn) {
99 this.onError = fn;
100 return this;
101 };
102 return ApolloLink;
103}());
104
105var empty = ApolloLink.empty;
106
107var from = ApolloLink.from;
108
109var split = ApolloLink.split;
110
111var concat = ApolloLink.concat;
112
113var execute = ApolloLink.execute;
114
115exports.ApolloLink = ApolloLink;
116exports.concat = concat;
117exports.empty = empty;
118exports.execute = execute;
119exports.from = from;
120exports.split = split;
121//# sourceMappingURL=core.cjs.map