Skip to content

Commit

Permalink
add api service with cred
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixNazarov committed May 27, 2024
1 parent e04cbfb commit ebe1390
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
16 changes: 16 additions & 0 deletions client/src/api/ApiService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios, {AxiosRequestConfig} from "axios";

export class ApiService {
private static AXIOS_CONFIG: AxiosRequestConfig = {
withCredentials: true,
}

public static async get<R>(url: string) {
return (await axios.get<R>(url, this.AXIOS_CONFIG)).data
}


public static async post<R>(url: string, data: unknown) {
return (await axios.post<R>(url, data, this.AXIOS_CONFIG)).data
}
}
7 changes: 2 additions & 5 deletions client/src/stores/config/abstractStoreFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import {ApiService} from "../../api/ApiService.ts";

export interface BaseEntity {
id: number
Expand All @@ -8,10 +8,7 @@ export interface BaseEntity {
export function abstractStoreFactory(name: string) {
return {
async getAll() {
const out = await axios.get(
'/api/config/' + name + '/get/all'
)
this.entity = out.data
this.entity = await ApiService.get('/api/config/' + name + '/get/all')
}
}
}
6 changes: 2 additions & 4 deletions client/src/stores/prompt.store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {defineStore} from "pinia";
import axios from 'axios'
import {ApiService} from "../api/ApiService.ts";

export interface Prompt {
mapping_id: number
Expand All @@ -23,10 +24,7 @@ export const usePromptStore = defineStore({
},
actions: {
async loadAll() {
const out = await axios.get<Prompt[]>(
'/api/prompts/load_all'
)
this.prompts = out.data
this.prompts = await ApiService.get<Prompt[]>('/api/prompts/load_all')
}
}
})
9 changes: 3 additions & 6 deletions client/src/stores/user.store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {defineStore} from "pinia";
import axios from 'axios'
import {ApiService} from "../api/ApiService.ts";


export const useAccountStore = defineStore({
Expand All @@ -14,14 +14,11 @@ export const useAccountStore = defineStore({
},
actions: {
async loadMe() {
const out = await axios.get<{ login: string } | null>(
'/api/auth/me'
)
this.auth = out.data != null
this.auth = await ApiService.get<{ login: string } | null>('/api/auth/me') != null
},
async login(login: string, password: string) {
try {
await axios.post<{ login: string } | null>(
await ApiService.post<{ login: string } | null>(
'/api/auth/login',
{
login: login,
Expand Down

0 comments on commit ebe1390

Please sign in to comment.