1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var apollo_link_1 = require("apollo-link");
|
17 | exports.onError = function (errorHandler) {
|
18 | return new apollo_link_1.ApolloLink(function (operation, forward) {
|
19 | return new apollo_link_1.Observable(function (observer) {
|
20 | var sub;
|
21 | var retriedSub;
|
22 | var retriedResult;
|
23 | try {
|
24 | sub = forward(operation).subscribe({
|
25 | next: function (result) {
|
26 | if (result.errors) {
|
27 | retriedResult = errorHandler({
|
28 | graphQLErrors: result.errors,
|
29 | response: result,
|
30 | operation: operation,
|
31 | forward: forward,
|
32 | });
|
33 | if (retriedResult) {
|
34 | retriedSub = retriedResult.subscribe({
|
35 | next: observer.next.bind(observer),
|
36 | error: observer.error.bind(observer),
|
37 | complete: observer.complete.bind(observer),
|
38 | });
|
39 | return;
|
40 | }
|
41 | }
|
42 | observer.next(result);
|
43 | },
|
44 | error: function (networkError) {
|
45 | retriedResult = errorHandler({
|
46 | operation: operation,
|
47 | networkError: networkError,
|
48 | graphQLErrors: networkError.result && networkError.result.errors,
|
49 | forward: forward,
|
50 | });
|
51 | if (retriedResult) {
|
52 | retriedSub = retriedResult.subscribe({
|
53 | next: observer.next.bind(observer),
|
54 | error: observer.error.bind(observer),
|
55 | complete: observer.complete.bind(observer),
|
56 | });
|
57 | return;
|
58 | }
|
59 | observer.error(networkError);
|
60 | },
|
61 | complete: function () {
|
62 | if (!retriedResult) {
|
63 | observer.complete.bind(observer)();
|
64 | }
|
65 | },
|
66 | });
|
67 | }
|
68 | catch (e) {
|
69 | errorHandler({ networkError: e, operation: operation, forward: forward });
|
70 | observer.error(e);
|
71 | }
|
72 | return function () {
|
73 | if (sub)
|
74 | sub.unsubscribe();
|
75 | if (retriedSub)
|
76 | sub.unsubscribe();
|
77 | };
|
78 | });
|
79 | });
|
80 | };
|
81 | var ErrorLink = (function (_super) {
|
82 | __extends(ErrorLink, _super);
|
83 | function ErrorLink(errorHandler) {
|
84 | var _this = _super.call(this) || this;
|
85 | _this.link = exports.onError(errorHandler);
|
86 | return _this;
|
87 | }
|
88 | ErrorLink.prototype.request = function (operation, forward) {
|
89 | return this.link.request(operation, forward);
|
90 | };
|
91 | return ErrorLink;
|
92 | }(apollo_link_1.ApolloLink));
|
93 | exports.ErrorLink = ErrorLink;
|
94 |
|
\ | No newline at end of file |