UNPKG

apollo-client

Version:
155 lines 6.14 kB
var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import 'whatwg-fetch'; import { print } from 'graphql-tag/printer'; export function printRequest(request) { return __assign({}, request, { query: print(request.query) }); } var HTTPFetchNetworkInterface = (function () { function HTTPFetchNetworkInterface(uri, opts) { if (opts === void 0) { opts = {}; } if (!uri) { throw new Error('A remote endpoint is required for a network layer'); } if (typeof uri !== 'string') { throw new Error('Remote endpoint must be a string'); } this._uri = uri; this._opts = __assign({}, opts); this._middlewares = []; this._afterwares = []; } HTTPFetchNetworkInterface.prototype.applyMiddlewares = function (_a) { var _this = this; var request = _a.request, options = _a.options; return new Promise(function (resolve, reject) { var queue = function (funcs, scope) { var next = function () { if (funcs.length > 0) { var f = funcs.shift(); if (f) { f.applyMiddleware.apply(scope, [{ request: request, options: options }, next]); } } else { resolve({ request: request, options: options, }); } }; next(); }; queue(_this._middlewares.slice(), _this); }); }; HTTPFetchNetworkInterface.prototype.applyAfterwares = function (_a) { var _this = this; var response = _a.response, options = _a.options; return new Promise(function (resolve, reject) { var queue = function (funcs, scope) { var next = function () { if (funcs.length > 0) { var f = funcs.shift(); f.applyAfterware.apply(scope, [{ response: response, options: options }, next]); } else { resolve({ response: response, options: options, }); } }; next(); }; queue(_this._afterwares.slice(), _this); }); }; HTTPFetchNetworkInterface.prototype.fetchFromRemoteEndpoint = function (_a) { var request = _a.request, options = _a.options; return fetch(this._uri, __assign({}, this._opts, { body: JSON.stringify(printRequest(request)), method: 'POST' }, options, { headers: __assign({ Accept: '*/*', 'Content-Type': 'application/json' }, options.headers) })); }; ; HTTPFetchNetworkInterface.prototype.query = function (request) { var _this = this; var options = __assign({}, this._opts); return this.applyMiddlewares({ request: request, options: options, }).then(function (rao) { return _this.fetchFromRemoteEndpoint.call(_this, rao); }) .then(function (response) { return _this.applyAfterwares({ response: response, options: options, }); }) .then(function (_a) { var response = _a.response; var httpResponse = response; if (!httpResponse.ok) { var httpError = new Error("Network request failed with status " + response.status + " - \"" + response.statusText + "\""); httpError.response = httpResponse; throw httpError; } return httpResponse.json(); }) .then(function (payload) { if (!payload.hasOwnProperty('data') && !payload.hasOwnProperty('errors')) { throw new Error("Server response was missing for query '" + request.debugName + "'."); } else { return payload; } }); }; ; HTTPFetchNetworkInterface.prototype.use = function (middlewares) { var _this = this; middlewares.map(function (middleware) { if (typeof middleware.applyMiddleware === 'function') { _this._middlewares.push(middleware); } else { throw new Error('Middleware must implement the applyMiddleware function'); } }); return this; }; HTTPFetchNetworkInterface.prototype.useAfter = function (afterwares) { var _this = this; afterwares.map(function (afterware) { if (typeof afterware.applyAfterware === 'function') { _this._afterwares.push(afterware); } else { throw new Error('Afterware must implement the applyAfterware function'); } }); return this; }; return HTTPFetchNetworkInterface; }()); export { HTTPFetchNetworkInterface }; export function createNetworkInterface(uriOrInterfaceOpts, secondArgOpts) { if (secondArgOpts === void 0) { secondArgOpts = {}; } if (!uriOrInterfaceOpts) { throw new Error('You must pass an options argument to createNetworkInterface.'); } var uri; var opts; if (typeof uriOrInterfaceOpts === 'string') { console.warn("Passing the URI as the first argument to createNetworkInterface is deprecated as of Apollo Client 0.5. Please pass it as the \"uri\" property of the network interface options."); opts = secondArgOpts; uri = uriOrInterfaceOpts; } else { opts = uriOrInterfaceOpts.opts; uri = uriOrInterfaceOpts.uri; } return new HTTPFetchNetworkInterface(uri, opts); } //# sourceMappingURL=networkInterface.js.map