Skip to content

Instantly share code, notes, and snippets.

@HenriqueMitsuo
Created March 18, 2021 19:35
Show Gist options
  • Save HenriqueMitsuo/284f424c1b53467d81e86967233b9f36 to your computer and use it in GitHub Desktop.
Save HenriqueMitsuo/284f424c1b53467d81e86967233b9f36 to your computer and use it in GitHub Desktop.
// import axios from "axios";
import { axios } from "@/plugins/axios"; // vue
export class ApiService {
constructor(Resource) {
this.Resource = Resource;
this.Token = { Authorization: `Bearer ${localStorage.getItem("token")}` };
}
async queryAll() {
try {
const response = await axios.get(this.Resource, { headers: this.Token });
return await response.data;
} catch (error) {
console.log(`Server error: ${error}`)
}
}
async queryOne(id) {
const response = await axios.get(`${this.Resource}/${id}`, { headers: this.Token });
return await response.data;
}
async queryFilter(filter) {
const response = await axios.get(this.Resource, { headers: this.Token, params: filter });
return await response.data;
}
async createOne(data) {
const response = await axios.post(this.Resource, data, { headers: this.Token });
return await response.data;
}
async updateOne(id, data) {
const response = await axios.put(`${this.Resource}/${id}`, data, { headers: this.Token });
return await response.data;
}
async deleteOne(id) {
const response = await axios.delete(`${this.Resource}/${id}`, { headers: this.Token });
return await response.data;
}
// Controle de usuário (Auth de login, Criação)
async login(data) {
const response = await axios.post(this.Resource, data);
return await response.data;
}
async createUser(data) {
const response = await axios.post("register", data);
return await response.data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment