1 | import { ApolloLink, Observable } from 'apollo-link';
|
2 |
|
3 | /*! *****************************************************************************
|
4 | Copyright (c) Microsoft Corporation. All rights reserved.
|
5 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
6 | this file except in compliance with the License. You may obtain a copy of the
|
7 | License at http://www.apache.org/licenses/LICENSE-2.0
|
8 |
|
9 | THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
10 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
11 | WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
12 | MERCHANTABLITY OR NON-INFRINGEMENT.
|
13 |
|
14 | See the Apache Version 2.0 License for specific language governing permissions
|
15 | and limitations under the License.
|
16 | ***************************************************************************** */
|
17 | /* global Reflect, Promise */
|
18 |
|
19 | var extendStatics = function(d, b) {
|
20 | extendStatics = Object.setPrototypeOf ||
|
21 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
22 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
23 | return extendStatics(d, b);
|
24 | };
|
25 |
|
26 | function __extends(d, b) {
|
27 | extendStatics(d, b);
|
28 | function __() { this.constructor = d; }
|
29 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
30 | }
|
31 |
|
32 | var onError = function (errorHandler) {
|
33 | return new ApolloLink(function (operation, forward) {
|
34 | return new Observable(function (observer) {
|
35 | var sub;
|
36 | var retriedSub;
|
37 | var retriedResult;
|
38 | try {
|
39 | sub = forward(operation).subscribe({
|
40 | next: function (result) {
|
41 | if (result.errors) {
|
42 | retriedResult = errorHandler({
|
43 | graphQLErrors: result.errors,
|
44 | response: result,
|
45 | operation: operation,
|
46 | forward: forward,
|
47 | });
|
48 | if (retriedResult) {
|
49 | retriedSub = retriedResult.subscribe({
|
50 | next: observer.next.bind(observer),
|
51 | error: observer.error.bind(observer),
|
52 | complete: observer.complete.bind(observer),
|
53 | });
|
54 | return;
|
55 | }
|
56 | }
|
57 | observer.next(result);
|
58 | },
|
59 | error: function (networkError) {
|
60 | retriedResult = errorHandler({
|
61 | operation: operation,
|
62 | networkError: networkError,
|
63 | graphQLErrors: networkError.result && networkError.result.errors,
|
64 | forward: forward,
|
65 | });
|
66 | if (retriedResult) {
|
67 | retriedSub = retriedResult.subscribe({
|
68 | next: observer.next.bind(observer),
|
69 | error: observer.error.bind(observer),
|
70 | complete: observer.complete.bind(observer),
|
71 | });
|
72 | return;
|
73 | }
|
74 | observer.error(networkError);
|
75 | },
|
76 | complete: function () {
|
77 | if (!retriedResult) {
|
78 | observer.complete.bind(observer)();
|
79 | }
|
80 | },
|
81 | });
|
82 | }
|
83 | catch (e) {
|
84 | errorHandler({ networkError: e, operation: operation, forward: forward });
|
85 | observer.error(e);
|
86 | }
|
87 | return function () {
|
88 | if (sub)
|
89 | sub.unsubscribe();
|
90 | if (retriedSub)
|
91 | sub.unsubscribe();
|
92 | };
|
93 | });
|
94 | });
|
95 | };
|
96 | var ErrorLink = (function (_super) {
|
97 | __extends(ErrorLink, _super);
|
98 | function ErrorLink(errorHandler) {
|
99 | var _this = _super.call(this) || this;
|
100 | _this.link = onError(errorHandler);
|
101 | return _this;
|
102 | }
|
103 | ErrorLink.prototype.request = function (operation, forward) {
|
104 | return this.link.request(operation, forward);
|
105 | };
|
106 | return ErrorLink;
|
107 | }(ApolloLink));
|
108 |
|
109 | export { onError, ErrorLink };
|
110 | //# sourceMappingURL=bundle.esm.js.map
|