UNPKG

6.56 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('cross-fetch/polyfill')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'cross-fetch/polyfill'], factory) :
4 (factory((global.apolloFetch = {})));
5}(this, (function (exports) { 'use strict';
6
7var __assign = (undefined && undefined.__assign) || Object.assign || function(t) {
8 for (var s, i = 1, n = arguments.length; i < n; i++) {
9 s = arguments[i];
10 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11 t[p] = s[p];
12 }
13 return t;
14};
15function buildWareStack(funcs, modifiedObject, resolve) {
16 var _this = this;
17 var next = function () {
18 if (funcs.length > 0) {
19 var f = funcs.shift();
20 if (f) {
21 f.apply(_this, [modifiedObject, next]);
22 }
23 }
24 else {
25 resolve(modifiedObject);
26 }
27 };
28 next();
29}
30function constructDefaultOptions(requestOrRequests, options) {
31 var body;
32 try {
33 body = JSON.stringify(requestOrRequests);
34 }
35 catch (e) {
36 throw new Error("Network request failed. Payload is not serializable: " + e.message);
37 }
38 return __assign({ body: body, method: 'POST' }, options, { headers: __assign({ Accept: '*/*', 'Content-Type': 'application/json' }, options.headers || []) });
39}
40function throwHttpError(response, error) {
41 var httpError;
42 if (response && response.status >= 300) {
43 httpError = new Error("Network request failed with status " + response.status + " - \"" + response.statusText + "\"");
44 }
45 else {
46 httpError = new Error("Network request failed to return valid JSON");
47 }
48 httpError.response = response;
49 httpError.parseError = error;
50 throw httpError;
51}
52function throwBatchError(response) {
53 var httpError = new Error("A batched Operation of responses for ");
54 httpError.response = response;
55 throw httpError;
56}
57function createApolloFetch(params) {
58 if (params === void 0) { params = {}; }
59 var constructOptions = params.constructOptions, customFetch = params.customFetch;
60 var _uri = params.uri || '/graphql';
61 var middlewares = [];
62 var batchedMiddlewares = [];
63 var afterwares = [];
64 var batchedAfterwares = [];
65 var applyMiddlewares = function (requestAndOptions, batched) {
66 return new Promise(function (resolve, reject) {
67 if (batched) {
68 buildWareStack(batchedMiddlewares.slice(), requestAndOptions, resolve);
69 }
70 else {
71 buildWareStack(middlewares.slice(), requestAndOptions, resolve);
72 }
73 });
74 };
75 var applyAfterwares = function (responseObject, batched) {
76 return new Promise(function (resolve, reject) {
77 if (batched) {
78 buildWareStack(batchedAfterwares.slice(), responseObject, resolve);
79 }
80 else {
81 buildWareStack(afterwares.slice(), responseObject, resolve);
82 }
83 });
84 };
85 var apolloFetch = function (request) {
86 var options = {};
87 var parseError;
88 var batched = Array.isArray(request);
89 var requestObject = (batched
90 ? {
91 requests: request,
92 options: options,
93 }
94 : {
95 request: request,
96 options: options,
97 });
98 return applyMiddlewares(requestObject, batched)
99 .then(function (reqOpts) {
100 var construct = constructOptions || constructDefaultOptions;
101 var requestOrRequests = reqOpts.request ||
102 reqOpts.requests;
103 return construct(requestOrRequests, reqOpts.options);
104 })
105 .then(function (opts) {
106 options = __assign({}, opts);
107 return (customFetch || fetch)(_uri, options);
108 })
109 .then(function (response) {
110 return response.text().then(function (raw) {
111 try {
112 var parsed = JSON.parse(raw);
113 response.raw = raw;
114 response.parsed = parsed;
115 return response;
116 }
117 catch (e) {
118 parseError = e;
119 response.raw = raw;
120 return response;
121 }
122 });
123 })
124 .then(function (response) {
125 return applyAfterwares({
126 response: response,
127 options: options,
128 }, batched);
129 })
130 .then(function (_a) {
131 var response = _a.response;
132 if (response.parsed) {
133 if (batched) {
134 if (Array.isArray(response.parsed)) {
135 return response.parsed;
136 }
137 else {
138 throwBatchError(response);
139 }
140 }
141 else {
142 return __assign({}, response.parsed);
143 }
144 }
145 else {
146 throwHttpError(response, parseError);
147 }
148 });
149 };
150 apolloFetch.use = function (middleware) {
151 if (typeof middleware === 'function') {
152 middlewares.push(middleware);
153 }
154 else {
155 throw new Error('Middleware must be a function');
156 }
157 return apolloFetch;
158 };
159 apolloFetch.useAfter = function (afterware) {
160 if (typeof afterware === 'function') {
161 afterwares.push(afterware);
162 }
163 else {
164 throw new Error('Afterware must be a function');
165 }
166 return apolloFetch;
167 };
168 apolloFetch.batchUse = function (middleware) {
169 if (typeof middleware === 'function') {
170 batchedMiddlewares.push(middleware);
171 }
172 else {
173 throw new Error('Middleware must be a function');
174 }
175 return apolloFetch;
176 };
177 apolloFetch.batchUseAfter = function (afterware) {
178 if (typeof afterware === 'function') {
179 batchedAfterwares.push(afterware);
180 }
181 else {
182 throw new Error('Afterware must be a function');
183 }
184 return apolloFetch;
185 };
186 return apolloFetch;
187}
188
189exports.constructDefaultOptions = constructDefaultOptions;
190exports.createApolloFetch = createApolloFetch;
191
192Object.defineProperty(exports, '__esModule', { value: true });
193
194})));
195//# sourceMappingURL=bundle.umd.js.map