From bfe8847d72cd0893230f2e654242658214943e61 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Sat, 2 Oct 2021 09:24:32 +0200 Subject: chore: convert various files from JS to TS (#2010) --- src/api/utils/auth.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/api/utils/auth.ts (limited to 'src/api/utils/auth.ts') diff --git a/src/api/utils/auth.ts b/src/api/utils/auth.ts new file mode 100644 index 000000000..98295d1a4 --- /dev/null +++ b/src/api/utils/auth.ts @@ -0,0 +1,38 @@ +import localStorage from 'mobx-localstorage'; +import { ferdiLocale, ferdiVersion } from '../../environment-remote'; + +export const prepareAuthRequest = ( + // eslint-disable-next-line unicorn/no-object-as-default-parameter + options = { method: 'GET' }, + auth = true, +) => { + const request = Object.assign(options, { + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + 'X-Franz-Source': 'desktop', + 'X-Franz-Version': ferdiVersion, + 'X-Franz-platform': process.platform, + 'X-Franz-Timezone-Offset': new Date().getTimezoneOffset(), + 'X-Franz-System-Locale': ferdiLocale, + // @ts-expect-error Property 'headers' does not exist on type '{ method: string; }'. + ...options.headers, + }, + }); + + if (auth) { + request.headers.Authorization = `Bearer ${localStorage.getItem( + 'authToken', + )}`; + } + + return request; +}; + +export const sendAuthRequest = ( + url: RequestInfo, + options: { method: string } | undefined, + auth?: boolean, +) => + // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. + window.fetch(url, prepareAuthRequest(options, auth)); -- cgit v1.2.3-54-g00ecf