UNPKG

6.59 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-link'), require('apollo-link-http-common')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'apollo-link', 'apollo-link-http-common'], factory) :
4 (factory((global.apolloLink = global.apolloLink || {}, global.apolloLink.http = {}),global.apolloLink.core,global.apolloLink.httpCommon));
5}(this, (function (exports,apolloLink,apolloLinkHttpCommon) { 'use strict';
6
7var __extends = (undefined && undefined.__extends) || (function () {
8 var extendStatics = Object.setPrototypeOf ||
9 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
11 return function (d, b) {
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17var __rest = (undefined && undefined.__rest) || function (s, e) {
18 var t = {};
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
20 t[p] = s[p];
21 if (s != null && typeof Object.getOwnPropertySymbols === "function")
22 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
23 t[p[i]] = s[p[i]];
24 return t;
25};
26var createHttpLink = function (linkOptions) {
27 if (linkOptions === void 0) { linkOptions = {}; }
28 var _a = linkOptions.uri, uri = _a === void 0 ? '/graphql' : _a, fetcher = linkOptions.fetch, includeExtensions = linkOptions.includeExtensions, useGETForQueries = linkOptions.useGETForQueries, requestOptions = __rest(linkOptions, ["uri", "fetch", "includeExtensions", "useGETForQueries"]);
29 apolloLinkHttpCommon.checkFetcher(fetcher);
30 if (!fetcher) {
31 fetcher = fetch;
32 }
33 var linkConfig = {
34 http: { includeExtensions: includeExtensions },
35 options: requestOptions.fetchOptions,
36 credentials: requestOptions.credentials,
37 headers: requestOptions.headers,
38 };
39 return new apolloLink.ApolloLink(function (operation) {
40 var chosenURI = apolloLinkHttpCommon.selectURI(operation, uri);
41 var context = operation.getContext();
42 var contextConfig = {
43 http: context.http,
44 options: context.fetchOptions,
45 credentials: context.credentials,
46 headers: context.headers,
47 };
48 var _a = apolloLinkHttpCommon.selectHttpOptionsAndBody(operation, apolloLinkHttpCommon.fallbackHttpConfig, linkConfig, contextConfig), options = _a.options, body = _a.body;
49 var _b = apolloLinkHttpCommon.createSignalIfSupported(), controller = _b.controller, signal = _b.signal;
50 if (controller)
51 options.signal = signal;
52 var definitionIsMutation = function (d) {
53 return d.kind === 'OperationDefinition' && d.operation === 'mutation';
54 };
55 if (useGETForQueries &&
56 !operation.query.definitions.some(definitionIsMutation)) {
57 options.method = 'GET';
58 }
59 if (options.method === 'GET') {
60 var _c = rewriteURIForGET(chosenURI, body), newURI = _c.newURI, parseError = _c.parseError;
61 if (parseError) {
62 return apolloLink.fromError(parseError);
63 }
64 chosenURI = newURI;
65 }
66 else {
67 try {
68 options.body = apolloLinkHttpCommon.serializeFetchParameter(body, 'Payload');
69 }
70 catch (parseError) {
71 return apolloLink.fromError(parseError);
72 }
73 }
74 return new apolloLink.Observable(function (observer) {
75 fetcher(chosenURI, options)
76 .then(function (response) {
77 operation.setContext({ response: response });
78 return response;
79 })
80 .then(apolloLinkHttpCommon.parseAndCheckHttpResponse(operation))
81 .then(function (result) {
82 observer.next(result);
83 observer.complete();
84 return result;
85 })
86 .catch(function (err) {
87 if (err.name === 'AbortError')
88 return;
89 if (err.result && err.result.errors && err.result.data) {
90 observer.next(err.result);
91 }
92 observer.error(err);
93 });
94 return function () {
95 if (controller)
96 controller.abort();
97 };
98 });
99 });
100};
101function rewriteURIForGET(chosenURI, body) {
102 var queryParams = [];
103 var addQueryParam = function (key, value) {
104 queryParams.push(key + "=" + encodeURIComponent(value));
105 };
106 if ('query' in body) {
107 addQueryParam('query', body.query);
108 }
109 if (body.operationName) {
110 addQueryParam('operationName', body.operationName);
111 }
112 if (body.variables) {
113 var serializedVariables = void 0;
114 try {
115 serializedVariables = apolloLinkHttpCommon.serializeFetchParameter(body.variables, 'Variables map');
116 }
117 catch (parseError) {
118 return { parseError: parseError };
119 }
120 addQueryParam('variables', serializedVariables);
121 }
122 if (body.extensions) {
123 var serializedExtensions = void 0;
124 try {
125 serializedExtensions = apolloLinkHttpCommon.serializeFetchParameter(body.extensions, 'Extensions map');
126 }
127 catch (parseError) {
128 return { parseError: parseError };
129 }
130 addQueryParam('extensions', serializedExtensions);
131 }
132 var fragment = '', preFragment = chosenURI;
133 var fragmentStart = chosenURI.indexOf('#');
134 if (fragmentStart !== -1) {
135 fragment = chosenURI.substr(fragmentStart);
136 preFragment = chosenURI.substr(0, fragmentStart);
137 }
138 var queryParamsPrefix = preFragment.indexOf('?') === -1 ? '?' : '&';
139 var newURI = preFragment + queryParamsPrefix + queryParams.join('&') + fragment;
140 return { newURI: newURI };
141}
142var HttpLink = (function (_super) {
143 __extends(HttpLink, _super);
144 function HttpLink(opts) {
145 return _super.call(this, createHttpLink(opts).request) || this;
146 }
147 return HttpLink;
148}(apolloLink.ApolloLink));
149
150exports.createHttpLink = createHttpLink;
151exports.HttpLink = HttpLink;
152
153Object.defineProperty(exports, '__esModule', { value: true });
154
155})));
156//# sourceMappingURL=bundle.umd.js.map