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 => {
|
2020-09-11 14:08:42 -04:00
|
|
|
|
// 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.
|
2020-06-19 14:08:39 -04:00
|
|
|
|
gl.startup_calls[apiCall] = {
|
2020-09-11 14:08:42 -04:00
|
|
|
|
fetchCall: fetch(apiCall, { 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) {
|
|
|
|
|
const url = `#{api_graphql_url}`
|
|
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json", 'X-CSRF-Token': "#{form_authenticity_token}" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|