UNPKG

4.5 kBJavaScriptView Raw
1import { __assign, __spreadArray } from "tslib";
2import { print } from "../../utilities/index.js";
3var defaultHttpOptions = {
4 includeQuery: true,
5 includeExtensions: false,
6 preserveHeaderCase: false,
7};
8var defaultHeaders = {
9 // headers are case insensitive (https://stackoverflow.com/a/5259004)
10 accept: "*/*",
11 // The content-type header describes the type of the body of the request, and
12 // so it typically only is sent with requests that actually have bodies. One
13 // could imagine that Apollo Client would remove this header when constructing
14 // a GET request (which has no body), but we historically have not done that.
15 // This means that browsers will preflight all Apollo Client requests (even
16 // GET requests). Apollo Server's CSRF prevention feature (introduced in
17 // AS3.7) takes advantage of this fact and does not block requests with this
18 // header. If you want to drop this header from GET requests, then you should
19 // probably replace it with a `apollo-require-preflight` header, or servers
20 // with CSRF prevention enabled might block your GET request. See
21 // https://www.apollographql.com/docs/apollo-server/security/cors/#preventing-cross-site-request-forgery-csrf
22 // for more details.
23 "content-type": "application/json",
24};
25var defaultOptions = {
26 method: "POST",
27};
28export var fallbackHttpConfig = {
29 http: defaultHttpOptions,
30 headers: defaultHeaders,
31 options: defaultOptions,
32};
33export var defaultPrinter = function (ast, printer) { return printer(ast); };
34export function selectHttpOptionsAndBody(operation, fallbackConfig) {
35 var configs = [];
36 for (var _i = 2; _i < arguments.length; _i++) {
37 configs[_i - 2] = arguments[_i];
38 }
39 configs.unshift(fallbackConfig);
40 return selectHttpOptionsAndBodyInternal.apply(void 0, __spreadArray([operation,
41 defaultPrinter], configs, false));
42}
43export function selectHttpOptionsAndBodyInternal(operation, printer) {
44 var configs = [];
45 for (var _i = 2; _i < arguments.length; _i++) {
46 configs[_i - 2] = arguments[_i];
47 }
48 var options = {};
49 var http = {};
50 configs.forEach(function (config) {
51 options = __assign(__assign(__assign({}, options), config.options), { headers: __assign(__assign({}, options.headers), config.headers) });
52 if (config.credentials) {
53 options.credentials = config.credentials;
54 }
55 http = __assign(__assign({}, http), config.http);
56 });
57 if (options.headers) {
58 options.headers = removeDuplicateHeaders(options.headers, http.preserveHeaderCase);
59 }
60 //The body depends on the http options
61 var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query;
62 var body = { operationName: operationName, variables: variables };
63 if (http.includeExtensions)
64 body.extensions = extensions;
65 // not sending the query (i.e persisted queries)
66 if (http.includeQuery)
67 body.query = printer(query, print);
68 return {
69 options: options,
70 body: body,
71 };
72}
73// Remove potential duplicate header names, preserving last (by insertion order).
74// This is done to prevent unintentionally duplicating a header instead of
75// overwriting it (See #8447 and #8449).
76function removeDuplicateHeaders(headers, preserveHeaderCase) {
77 // If we're not preserving the case, just remove duplicates w/ normalization.
78 if (!preserveHeaderCase) {
79 var normalizedHeaders_1 = Object.create(null);
80 Object.keys(Object(headers)).forEach(function (name) {
81 normalizedHeaders_1[name.toLowerCase()] = headers[name];
82 });
83 return normalizedHeaders_1;
84 }
85 // If we are preserving the case, remove duplicates w/ normalization,
86 // preserving the original name.
87 // This allows for non-http-spec-compliant servers that expect intentionally
88 // capitalized header names (See #6741).
89 var headerData = Object.create(null);
90 Object.keys(Object(headers)).forEach(function (name) {
91 headerData[name.toLowerCase()] = {
92 originalName: name,
93 value: headers[name],
94 };
95 });
96 var normalizedHeaders = Object.create(null);
97 Object.keys(headerData).forEach(function (name) {
98 normalizedHeaders[headerData[name].originalName] = headerData[name].value;
99 });
100 return normalizedHeaders;
101}
102//# sourceMappingURL=selectHttpOptionsAndBody.js.map
\No newline at end of file