UNPKG

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