init
This commit is contained in:
49
src/lib/pocketbase/client.ts
Normal file
49
src/lib/pocketbase/client.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import PocketBase from 'pocketbase';
|
||||
import { createPlayersService } from './services/players';
|
||||
import { createTournamentsService } from './services/tournaments';
|
||||
import { createTeamsService } from './services/teams';
|
||||
|
||||
class PocketBaseAdminClient {
|
||||
private pb: PocketBase;
|
||||
private authPromise: Promise<void>;
|
||||
|
||||
constructor() {
|
||||
this.pb = new PocketBase(import.meta.env.VITE_POCKETBASE_URL);
|
||||
|
||||
this.pb.beforeSend = (url, options) => {
|
||||
options.cache = 'no-store';
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||
'Pragma': 'no-cache',
|
||||
'Expires': '0'
|
||||
};
|
||||
|
||||
return { url, options };
|
||||
};
|
||||
this.pb.autoCancellation(false);
|
||||
|
||||
this.authPromise = this.authenticate();
|
||||
|
||||
this.authPromise.then(() => {
|
||||
Object.assign(this, createPlayersService(this.pb));
|
||||
Object.assign(this, createTeamsService(this.pb));
|
||||
Object.assign(this, createTournamentsService(this.pb));
|
||||
});
|
||||
}
|
||||
|
||||
private async authenticate() {
|
||||
await this.pb.collection("_superusers").authWithPassword(
|
||||
import.meta.env.VITE_POCKETBASE_ADMIN_EMAIL!,
|
||||
import.meta.env.VITE_POCKETBASE_ADMIN_PASSWORD!
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
interface AdminClient extends
|
||||
PocketBaseAdminClient,
|
||||
ReturnType<typeof createPlayersService>,
|
||||
ReturnType<typeof createTeamsService>,
|
||||
ReturnType<typeof createTournamentsService> {}
|
||||
|
||||
export const pbAdmin = new PocketBaseAdminClient() as AdminClient;
|
||||
Reference in New Issue
Block a user