UNPKG

6.51 kBJavaScriptView Raw
1"use strict";
2var isString = require('lodash.isstring');
3var assign = require('lodash.assign');
4var mapValues = require('lodash.mapvalues');
5require('whatwg-fetch');
6var printer_1 = require('graphql-tag/printer');
7var queryMerging_1 = require('./batching/queryMerging');
8function addQueryMerging(networkInterface) {
9 return assign(networkInterface, {
10 batchQuery: function (requests) {
11 if (requests.length === 1) {
12 return this.query(requests[0]).then(function (result) {
13 return Promise.resolve([result]);
14 });
15 }
16 var composedRequest = queryMerging_1.mergeRequests(requests);
17 return this.query(composedRequest).then(function (composedResult) {
18 return queryMerging_1.unpackMergedResult(composedResult, requests);
19 });
20 },
21 });
22}
23exports.addQueryMerging = addQueryMerging;
24function printRequest(request) {
25 return mapValues(request, function (val, key) {
26 return key === 'query' ? printer_1.print(val) : val;
27 });
28}
29exports.printRequest = printRequest;
30var HTTPFetchNetworkInterface = (function () {
31 function HTTPFetchNetworkInterface(uri, opts) {
32 if (opts === void 0) { opts = {}; }
33 if (!uri) {
34 throw new Error('A remote enpdoint is required for a network layer');
35 }
36 if (!isString(uri)) {
37 throw new Error('Remote endpoint must be a string');
38 }
39 this._uri = uri;
40 this._opts = assign({}, opts);
41 this._middlewares = [];
42 this._afterwares = [];
43 }
44 HTTPFetchNetworkInterface.prototype.applyMiddlewares = function (_a) {
45 var _this = this;
46 var request = _a.request, options = _a.options;
47 return new Promise(function (resolve, reject) {
48 var queue = function (funcs, scope) {
49 var next = function () {
50 if (funcs.length > 0) {
51 var f = funcs.shift();
52 f.applyMiddleware.apply(scope, [{ request: request, options: options }, next]);
53 }
54 else {
55 resolve({
56 request: request,
57 options: options,
58 });
59 }
60 };
61 next();
62 };
63 queue(_this._middlewares.slice(), _this);
64 });
65 };
66 HTTPFetchNetworkInterface.prototype.applyAfterwares = function (_a) {
67 var _this = this;
68 var response = _a.response, options = _a.options;
69 return new Promise(function (resolve, reject) {
70 var queue = function (funcs, scope) {
71 var next = function () {
72 if (funcs.length > 0) {
73 var f = funcs.shift();
74 f.applyAfterware.apply(scope, [{ response: response, options: options }, next]);
75 }
76 else {
77 resolve({
78 response: response,
79 options: options,
80 });
81 }
82 };
83 next();
84 };
85 queue(_this._afterwares.slice(), _this);
86 });
87 };
88 HTTPFetchNetworkInterface.prototype.fetchFromRemoteEndpoint = function (_a) {
89 var request = _a.request, options = _a.options;
90 return fetch(this._uri, assign({}, this._opts, {
91 body: JSON.stringify(printRequest(request)),
92 method: 'POST',
93 }, options, {
94 headers: assign({}, {
95 Accept: '*/*',
96 'Content-Type': 'application/json',
97 }, options.headers),
98 }));
99 };
100 ;
101 HTTPFetchNetworkInterface.prototype.query = function (request) {
102 var _this = this;
103 var options = assign({}, this._opts);
104 return this.applyMiddlewares({
105 request: request,
106 options: options,
107 }).then(this.fetchFromRemoteEndpoint.bind(this))
108 .then(function (response) {
109 _this.applyAfterwares({
110 response: response,
111 options: options,
112 });
113 return response;
114 })
115 .then(function (result) { return result.json(); })
116 .then(function (payload) {
117 if (!payload.hasOwnProperty('data') && !payload.hasOwnProperty('errors')) {
118 throw new Error("Server response was missing for query '" + request.debugName + "'.");
119 }
120 else {
121 return payload;
122 }
123 });
124 };
125 ;
126 HTTPFetchNetworkInterface.prototype.use = function (middlewares) {
127 var _this = this;
128 middlewares.map(function (middleware) {
129 if (typeof middleware.applyMiddleware === 'function') {
130 _this._middlewares.push(middleware);
131 }
132 else {
133 throw new Error('Middleware must implement the applyMiddleware function');
134 }
135 });
136 };
137 HTTPFetchNetworkInterface.prototype.useAfter = function (afterwares) {
138 var _this = this;
139 afterwares.map(function (afterware) {
140 if (typeof afterware.applyAfterware === 'function') {
141 _this._afterwares.push(afterware);
142 }
143 else {
144 throw new Error('Afterware must implement the applyAfterware function');
145 }
146 });
147 };
148 return HTTPFetchNetworkInterface;
149}());
150exports.HTTPFetchNetworkInterface = HTTPFetchNetworkInterface;
151var batchedNetworkInterface_1 = require('./batchedNetworkInterface');
152function createNetworkInterface(interfaceOpts, backOpts) {
153 if (backOpts === void 0) { backOpts = {}; }
154 if (isString(interfaceOpts) || !interfaceOpts) {
155 var uri = interfaceOpts;
156 return addQueryMerging(new HTTPFetchNetworkInterface(uri, backOpts));
157 }
158 else {
159 var _a = interfaceOpts, _b = _a.transportBatching, transportBatching = _b === void 0 ? false : _b, _c = _a.opts, opts = _c === void 0 ? {} : _c, uri = _a.uri;
160 if (transportBatching) {
161 return new batchedNetworkInterface_1.HTTPBatchedNetworkInterface(uri, opts);
162 }
163 else {
164 return addQueryMerging(new HTTPFetchNetworkInterface(uri, opts));
165 }
166 }
167}
168exports.createNetworkInterface = createNetworkInterface;
169//# sourceMappingURL=networkInterface.js.map
\No newline at end of file