UNPKG

6.48 kBJavaScriptView Raw
1"use strict";
2
3function _extends() {
4 return (_extends = Object.assign || function(a) {
5 var b, c, d;
6 for (b = 1; b < arguments.length; b++) {
7 c = arguments[b];
8 for (d in c) {
9 Object.prototype.hasOwnProperty.call(c, d) && (a[d] = c[d]);
10 }
11 }
12 return a;
13 }).apply(this, arguments);
14}
15
16function _toString() {
17 return this.message;
18}
19
20var wonka = require("wonka"), graphql = require("graphql"), generateErrorMessage = function(a, b) {
21 var d = "";
22 if (void 0 !== a) {
23 return d = "[Network] " + a.message;
24 }
25 void 0 !== b && b.forEach((function c(a) {
26 d += "[GraphQL] " + a.message + "\n";
27 }));
28 return d.trim();
29}, rehydrateGraphQlError = function(a) {
30 return "string" == typeof a ? new graphql.GraphQLError(a) : "object" == typeof a && a.message ? new graphql.GraphQLError(a.message, a.nodes, a.source, a.positions, a.path, a, a.extensions || {}) : a;
31}, CombinedError = function(a) {
32 function b(c) {
33 var f, b = c.networkError, e = c.response;
34 c = (c.graphQLErrors || []).map(rehydrateGraphQlError);
35 f = generateErrorMessage(b, c);
36 a.call(this, f);
37 this.name = "CombinedError";
38 this.message = f;
39 this.graphQLErrors = c;
40 this.networkError = b;
41 this.response = e;
42 }
43 a && (b.__proto__ = a);
44 (b.prototype = Object.create(a && a.prototype)).constructor = b;
45 b.prototype.toString = _toString;
46 return b;
47}(Error), phash = function(a, b) {
48 a |= 0;
49 for (var c = 0, d = 0 | b.length; c < d; c++) {
50 a = (a << 5) + a + b.charCodeAt(c);
51 }
52 return a;
53}, hash = function(a) {
54 return phash(5381, a) >>> 0;
55}, seen = new Set, cache = new WeakMap, stringify = function(a) {
56 var b, c, d, e, f, g;
57 if (null === a || seen.has(a)) {
58 return "null";
59 }
60 if ("object" != typeof a) {
61 return JSON.stringify(a) || "";
62 }
63 if (a.toJSON) {
64 return stringify(a.toJSON());
65 }
66 if (Array.isArray(a)) {
67 for (b = "[", c = 0, d = a.length; c < d; c++) {
68 0 < c && (b += ",");
69 b += 0 < (e = stringify(a[c])).length ? e : "null";
70 }
71 return b + "]";
72 }
73 if (!(b = Object.keys(a).sort()).length && a.constructor && a.constructor !== Object) {
74 return b = cache.get(a) || Math.random().toString(36).slice(2), cache.set(a, b),
75 '{"__key":"' + b + '"}';
76 }
77 seen.add(a);
78 c = "{";
79 d = 0;
80 for (e = b.length; d < e; d++) {
81 (g = stringify(a[f = b[d]])) && (1 < c.length && (c += ","), c += stringify(f) + ":" + g);
82 }
83 seen.delete(a);
84 return c + "}";
85}, stringifyVariables = function(a) {
86 seen.clear();
87 return stringify(a);
88}, hashQuery = function(a) {
89 return hash(a.replace(/([\s,]|#[^\n\r]+)+/g, " ").trim());
90}, docs = Object.create(null), createRequest = function(a, b) {
91 if ("string" == typeof a) {
92 var c = hashQuery(a);
93 a = void 0 !== docs[c] ? docs[c] : graphql.parse(a, {
94 noLocation: !0
95 });
96 } else {
97 void 0 !== a.__key ? c = a.__key : (c = hashQuery(graphql.print(a)), a = void 0 !== docs[c] ? docs[c] : a);
98 }
99 docs[c] = a;
100 a.__key = c;
101 return {
102 key: b ? phash(c, stringifyVariables(b)) >>> 0 : c,
103 query: a,
104 variables: b || {}
105 };
106}, getOperationName = function(a) {
107 var b, c, d;
108 for (b = 0, c = a.definitions.length; b < c; b++) {
109 if ((d = a.definitions[b]).kind === graphql.Kind.OPERATION_DEFINITION && d.name) {
110 return d.name.value;
111 }
112 }
113}, makeResult = function(a, b, c) {
114 return {
115 operation: a,
116 data: b.data,
117 error: Array.isArray(b.errors) ? new CombinedError({
118 graphQLErrors: b.errors,
119 response: c
120 }) : void 0,
121 extensions: "object" == typeof b.extensions && b.extensions || void 0
122 };
123}, makeErrorResult = function(a, b, c) {
124 return {
125 operation: a,
126 data: void 0,
127 error: new CombinedError({
128 networkError: b,
129 response: c
130 }),
131 extensions: void 0
132 };
133}, shouldUseGet = function(a) {
134 return "query" === a.kind && !!a.context.preferGetMethod;
135}, makeFetchBody = function(a) {
136 return {
137 query: graphql.print(a.query),
138 operationName: getOperationName(a.query),
139 variables: a.variables || void 0,
140 extensions: void 0
141 };
142}, makeFetchURL = function(a, b) {
143 var c = shouldUseGet(a);
144 a = a.context.url;
145 if (!c || !b) {
146 return a;
147 }
148 c = [];
149 b.operationName && c.push("operationName=" + encodeURIComponent(b.operationName));
150 b.query && c.push("query=" + encodeURIComponent(b.query.replace(/([\s,]|#[^\n\r]+)+/g, " ").trim()));
151 b.variables && c.push("variables=" + encodeURIComponent(stringifyVariables(b.variables)));
152 b.extensions && c.push("extensions=" + encodeURIComponent(stringifyVariables(b.extensions)));
153 return a + "?" + c.join("&");
154}, makeFetchOptions = function(a, b) {
155 var c = shouldUseGet(a);
156 return _extends({}, a = "function" == typeof a.context.fetchOptions ? a.context.fetchOptions() : a.context.fetchOptions || {}, {
157 body: !c && b ? JSON.stringify(b) : void 0,
158 method: c ? "GET" : "POST",
159 headers: c ? a.headers : _extends({}, {
160 "content-type": "application/json"
161 }, a.headers)
162 });
163}, executeFetch = function(a, b, c) {
164 var e, d = !1;
165 return (a.context.fetch || fetch)(b, c).then((function(a) {
166 e = a;
167 d = 200 > a.status || a.status >= ("manual" === c.redirect ? 400 : 300);
168 return a.json();
169 })).then((function(b) {
170 if (!("data" in b) && !("errors" in b)) {
171 throw Error("No Content");
172 }
173 return makeResult(a, b, e);
174 })).catch((function(b) {
175 if ("AbortError" !== b.name) {
176 return makeErrorResult(a, d ? Error(e.statusText) : b, e);
177 }
178 }));
179}, makeFetchSource = function(a, b, c) {
180 return wonka.make((function(d) {
181 var e = d.next, f = d.complete, g = "undefined" != typeof AbortController ? new AbortController : null, h = !1;
182 Promise.resolve().then((function() {
183 if (!h) {
184 return g && (c.signal = g.signal), executeFetch(a, b, c);
185 }
186 })).then((function(a) {
187 h || (h = !0, a && e(a), f());
188 }));
189 return function() {
190 h = !0;
191 g && g.abort();
192 };
193 }));
194};
195
196exports.CombinedError = CombinedError;
197
198exports._extends = _extends;
199
200exports.createRequest = createRequest;
201
202exports.makeErrorResult = makeErrorResult;
203
204exports.makeFetchBody = makeFetchBody;
205
206exports.makeFetchOptions = makeFetchOptions;
207
208exports.makeFetchSource = makeFetchSource;
209
210exports.makeFetchURL = makeFetchURL;
211
212exports.makeResult = makeResult;
213
214exports.stringifyVariables = stringifyVariables;
215//# sourceMappingURL=3fe74c44.js.map