UNPKG

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