Client.ts
987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import axios, {AxiosInstance} from 'axios'
import FormRequest from "src/models/FormRequest";
// @ts-ignore
import {Store} from "src/store";
export default class Client<T extends FormRequest> {
private client: AxiosInstance;
constructor() {
// @ts-ignore
const token = Store.getters['login/getToken'];
this.client = axios.create({
baseURL: 'http://localhost:8080',
headers: {
Authorization: 'Bearer ' + token
}
});
}
public async get(uri: string, id: number) {
const resp = await this.client.get(`${uri}/${id}`);
return resp.data;
}
public async list(uri: string) {
const resp = await this.client.get(uri);
return resp.data;
}
public async post(uri: string, data: T) {
const resp = await this.client.post(uri, data.build());
return resp.data;
}
public async put(uri: string, id: number, data: T) {
const resp = await this.client.put(`${uri}/${id}`, data.build())
return resp.data;
}
}