UNPKG

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