mirror of
https://github.com/aleksilassila/reiverr.git
synced 2026-04-22 00:35:12 +02:00
feat: Implement app state store & fix authentication for tvs
This commit is contained in:
25
src/lib/apis/reiverr/reiverr-api.ts
Normal file
25
src/lib/apis/reiverr/reiverr-api.ts
Normal 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;
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user