1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-14 14:49:01 +09:00
YuruToot/app/javascript/flavours/glitch/api.ts
Renaud Chaput 3e9298370d [Glitch] Remove the access token from Redux & context
Port 2c5ab8f647 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
2024-05-22 21:14:54 +02:00

61 lines
1.3 KiB
TypeScript

import type { AxiosResponse, RawAxiosRequestHeaders } from 'axios';
import axios from 'axios';
import LinkHeader from 'http-link-header';
import { getAccessToken } from './initial_state';
import ready from './ready';
export const getLinks = (response: AxiosResponse) => {
const value = response.headers.link as string | undefined;
if (!value) {
return new LinkHeader();
}
return LinkHeader.parse(value);
};
const csrfHeader: RawAxiosRequestHeaders = {};
const setCSRFHeader = () => {
const csrfToken = document.querySelector<HTMLMetaElement>(
'meta[name=csrf-token]',
);
if (csrfToken) {
csrfHeader['X-CSRF-Token'] = csrfToken.content;
}
};
void ready(setCSRFHeader);
const authorizationTokenFromInitialState = (): RawAxiosRequestHeaders => {
const accessToken = getAccessToken();
if (!accessToken) return {};
return {
Authorization: `Bearer ${accessToken}`,
};
};
// eslint-disable-next-line import/no-default-export
export default function api() {
return axios.create({
headers: {
...csrfHeader,
...authorizationTokenFromInitialState(),
},
transformResponse: [
function (data: unknown) {
try {
return JSON.parse(data as string) as unknown;
} catch {
return data;
}
},
],
});
}