feat: Implement app state store & fix authentication for tvs

This commit is contained in:
Aleksi Lassila
2024-03-27 17:22:19 +02:00
parent a574b718f0
commit da2b4ee6d5
10 changed files with 153 additions and 74 deletions

View File

@@ -0,0 +1,25 @@
import createClient from 'openapi-fetch';
import type { paths } from './reiverr.generated';
import { get } from 'svelte/store';
import { appState } from '../../stores/app-state.store';
interface ApiInterface<Paths extends NonNullable<unknown>> {
getClient(): ReturnType<typeof createClient<Paths>>;
}
export class ReiverrApi implements ApiInterface<paths> {
getClient(basePath?: string) {
const token = get(appState).token;
return createClient<paths>({
baseUrl: (basePath || get(appState).serverBaseUrl) + '/api',
...(token && {
headers: {
Authorization: 'Bearer ' + token
}
})
});
}
}
export const reiverrApi = new ReiverrApi();
export const getReiverrApiClient = reiverrApi.getClient;

View File

@@ -1,29 +0,0 @@
import createClient from 'openapi-fetch';
import type { paths } from './reiverr.generated';
import { Api } from '../api.interface';
import { authenticationToken } from '../../stores/localstorage.store';
import { get } from 'svelte/store';
class ReiverrApi<asd extends NonNullable<unknown>> extends Api<asd> {
protected baseUrl: string;
protected client: ReturnType<typeof createClient<paths>>;
protected isLoggedIn = false;
constructor(baseUrl: string) {
super();
this.baseUrl = baseUrl;
const token = get(authenticationToken);
this.client = createClient<paths>({
baseUrl: this.baseUrl,
...(token && {
headers: {
Authorization: 'Bearer ' + token
}
})
});
}
}
export const reiverrApi = new ReiverrApi<paths>('http://localhost:3000/api');