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