UNPKG

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