UNPKG

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