2020-10-13 08:08:41 -04:00
|
|
|
|
- return unless page_startup_api_calls.present? || page_startup_graphql_calls.present?
|
2020-06-19 14:08:39 -04:00
|
|
|
|
|
2020-11-26 13:09:18 -05:00
|
|
|
|
= javascript_tag do
|
2020-06-19 14:08:39 -04:00
|
|
|
|
:plain
|
|
|
|
|
var gl = window.gl || {};
|
|
|
|
|
gl.startup_calls = #{page_startup_api_calls.to_json};
|
2020-10-13 08:08:41 -04:00
|
|
|
|
gl.startup_graphql_calls = #{page_startup_graphql_calls.to_json};
|
|
|
|
|
|
2020-06-19 14:08:39 -04:00
|
|
|
|
if (gl.startup_calls && window.fetch) {
|
|
|
|
|
Object.keys(gl.startup_calls).forEach(apiCall => {
|
2021-10-18 14:11:13 -04:00
|
|
|
|
gl.startup_calls[apiCall] = {
|
|
|
|
|
fetchCall: fetch(apiCall, {
|
|
|
|
|
// Emulate XHR for Rails AJAX request checks
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
|
},
|
|
|
|
|
// fetch won’t send cookies in older browsers, unless you set the credentials init option.
|
|
|
|
|
// We set to `same-origin` which is default value in modern browsers.
|
|
|
|
|
// See https://github.com/whatwg/fetch/pull/585 for more information.
|
|
|
|
|
credentials: 'same-origin'
|
|
|
|
|
})
|
2020-06-19 14:08:39 -04:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-10-13 08:08:41 -04:00
|
|
|
|
if (gl.startup_graphql_calls && window.fetch) {
|
2021-10-08 17:09:48 -04:00
|
|
|
|
const headers = #{page_startup_graphql_headers.to_json};
|
2020-10-13 08:08:41 -04:00
|
|
|
|
const url = `#{api_graphql_url}`
|
|
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
|
method: "POST",
|
2021-10-08 17:09:48 -04:00
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
...headers,
|
2021-10-11 02:13:09 -04:00
|
|
|
|
}
|
2020-10-13 08:08:41 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
gl.startup_graphql_calls = gl.startup_graphql_calls.map(call => ({
|
2020-10-26 08:08:44 -04:00
|
|
|
|
...call,
|
2020-10-13 08:08:41 -04:00
|
|
|
|
fetchCall: fetch(url, {
|
|
|
|
|
...opts,
|
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
body: JSON.stringify(call)
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|