UNPKG

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