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