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