1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var globals = require('../../utilities/globals');
|
6 | var tslib = require('tslib');
|
7 | var utilities = require('../../utilities');
|
8 | var utils = require('../utils');
|
9 |
|
10 | function passthrough(op, forward) {
|
11 | return (forward ? forward(op) : utilities.Observable.of());
|
12 | }
|
13 | function toLink(handler) {
|
14 | return typeof handler === 'function' ? new ApolloLink(handler) : handler;
|
15 | }
|
16 | function isTerminating(link) {
|
17 | return link.request.length <= 1;
|
18 | }
|
19 | var 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));
|
28 | var 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 |
|
105 | var empty = ApolloLink.empty;
|
106 |
|
107 | var from = ApolloLink.from;
|
108 |
|
109 | var split = ApolloLink.split;
|
110 |
|
111 | var concat = ApolloLink.concat;
|
112 |
|
113 | var execute = ApolloLink.execute;
|
114 |
|
115 | exports.ApolloLink = ApolloLink;
|
116 | exports.concat = concat;
|
117 | exports.empty = empty;
|
118 | exports.execute = execute;
|
119 | exports.from = from;
|
120 | exports.split = split;
|
121 |
|