From c285546052f20f675569c9b2c82146ecbcb398ab Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 11 Oct 2022 10:32:31 +0200 Subject: Move flavours/glitch/util/api to flavours/glitch/api --- app/javascript/flavours/glitch/api.js | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 app/javascript/flavours/glitch/api.js (limited to 'app/javascript/flavours/glitch/api.js') diff --git a/app/javascript/flavours/glitch/api.js b/app/javascript/flavours/glitch/api.js new file mode 100644 index 000000000..90d8465ef --- /dev/null +++ b/app/javascript/flavours/glitch/api.js @@ -0,0 +1,52 @@ +import axios from 'axios'; +import ready from './ready'; +import LinkHeader from 'http-link-header'; + +export const getLinks = response => { + const value = response.headers.link; + + if (!value) { + return { refs: [] }; + } + + return LinkHeader.parse(value); +}; + +const csrfHeader = {}; + +const setCSRFHeader = () => { + const csrfToken = document.querySelector('meta[name=csrf-token]'); + + if (csrfToken) { + csrfHeader['X-CSRF-Token'] = csrfToken.content; + } +}; + +ready(setCSRFHeader); + +const authorizationHeaderFromState = getState => { + const accessToken = getState && getState().getIn(['meta', 'access_token'], ''); + + if (!accessToken) { + return {}; + } + + return { + 'Authorization': `Bearer ${accessToken}`, + }; +}; + +export default getState => axios.create({ + headers: { + ...csrfHeader, + ...authorizationHeaderFromState(getState), + }, + + transformResponse: [function (data) { + try { + return JSON.parse(data); + } catch(Exception) { + return data; + } + }], +}); -- cgit From 1be97ce17fa6c5635b0922ec97378360d793db59 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 11 Oct 2022 10:33:46 +0200 Subject: Remove unneeded change in flavours/glitch/api --- app/javascript/flavours/glitch/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/javascript/flavours/glitch/api.js') diff --git a/app/javascript/flavours/glitch/api.js b/app/javascript/flavours/glitch/api.js index 90d8465ef..645ef6500 100644 --- a/app/javascript/flavours/glitch/api.js +++ b/app/javascript/flavours/glitch/api.js @@ -1,6 +1,6 @@ import axios from 'axios'; -import ready from './ready'; import LinkHeader from 'http-link-header'; +import ready from './ready'; export const getLinks = response => { const value = response.headers.link; -- cgit