UNPKG

relay-link-batch-http

Version:

Batch HTTP transport layer for Relay

111 lines (108 loc) 4.78 kB
import { __extends, __rest, __assign } from 'tslib'; import { RelayLink, fromError } from 'relay-link'; import { BatchLink } from 'relay-link-batch'; import { checkFetcher, selectURI, selectHttpOptionsAndBody, fallbackHttpConfig, serializeFetchParameter, createSignalIfSupported, parseAndCheckHttpResponse, readResponseBody } from 'relay-link-http-common'; import { Observable } from 'relay-runtime'; var BatchHttpLink = (function (_super) { __extends(BatchHttpLink, _super); function BatchHttpLink(fetchParams) { var _this = _super.call(this) || this; var _a = fetchParams || {}, _b = _a.uri, uri = _b === void 0 ? '/graphql' : _b, fetcher = _a.fetch, batchInterval = _a.batchInterval, batchMax = _a.batchMax, batchKey = _a.batchKey, requestOptions = __rest(_a, ["uri", "fetch", "batchInterval", "batchMax", "batchKey"]); checkFetcher(fetcher); if (!fetcher) { fetcher = fetch; } var linkConfig = { options: requestOptions.fetchOptions, credentials: requestOptions.credentials, headers: requestOptions.headers, }; _this.batchInterval = batchInterval || 10; _this.batchMax = batchMax || 10; var batchHandler = function (operations) { var chosenURI = selectURI(operations[0], uri); var context = operations[0].getContext(); var contextConfig = { http: context.http, options: context.fetchOptions, credentials: context.credentials, headers: __assign({}, context.headers), }; var optsAndBody = operations.map(function (operation) { return selectHttpOptionsAndBody(operation, fallbackHttpConfig, linkConfig, contextConfig); }); var loadedBody = optsAndBody.map(function (_a) { var body = _a.body; return body; }); var options = optsAndBody[0].options; if (options.method === 'GET') { return fromError(new Error('relay-link-batch-http does not support GET requests')); } try { ; options.body = serializeFetchParameter(loadedBody, 'Payload'); } catch (parseError) { return fromError(parseError); } var controller; if (!options.signal) { var _a = createSignalIfSupported(), _controller = _a.controller, signal = _a.signal; controller = _controller; if (controller) options.signal = signal; } return Observable.create(function (sink) { fetcher(chosenURI, options) .then(function (response) { operations.forEach(function (operation) { return operation.setContext({ response: response }); }); return response; }) .then(parseAndCheckHttpResponse(operations, operations[0].getContext().bodyParser || readResponseBody)) .then(function (result) { sink.next(result); sink.complete(); return result; }) .catch(function (err) { if (err.name === 'AbortError') return; if (err.result && err.result.errors && err.result.data) { sink.next(err.result); } sink.error(err); }); return function () { if (controller) controller.abort(); }; }); }; batchKey = batchKey || (function (operation) { var context = operation.getContext(); var contextConfig = { http: context.http, options: context.fetchOptions, credentials: context.credentials, headers: context.headers, }; return selectURI(operation, uri) + JSON.stringify(contextConfig); }); _this.batcher = new BatchLink({ batchInterval: _this.batchInterval, batchMax: _this.batchMax, batchKey: batchKey, batchHandler: batchHandler, }); return _this; } BatchHttpLink.prototype.request = function (operation) { return this.batcher.request(operation); }; return BatchHttpLink; }(RelayLink)); export { BatchHttpLink }; //# sourceMappingURL=bundle.esm.js.map