Incluindo lista de marcas de carro do backend
This commit is contained in:
25
src/services/api/cars.ts
Normal file
25
src/services/api/cars.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import instance from './api';
|
||||
|
||||
import carsRoutes from '../../constants/routes/carsRoutes';
|
||||
import { AxiosRequestHeaders } from 'axios';
|
||||
import LocalStorage from '../../LocalStorage';
|
||||
|
||||
let token: string;
|
||||
let header: AxiosRequestHeaders;
|
||||
|
||||
function updateHeader() {
|
||||
token = LocalStorage.getToken();
|
||||
|
||||
header = {
|
||||
"Accept": 'application/json',
|
||||
"Content-Type": 'application/json',
|
||||
"Authorization": 'Bearer ' + token
|
||||
}
|
||||
}
|
||||
export async function list() {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(carsRoutes.list.url, { headers: header });
|
||||
|
||||
return response.data;
|
||||
}
|
||||
51
src/services/functions/carsService.ts
Normal file
51
src/services/functions/carsService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import * as carsRoutes from "../api/cars";
|
||||
|
||||
interface getAllCarModelsReturn {
|
||||
data?: {
|
||||
id_model: string;
|
||||
name: string;
|
||||
}[];
|
||||
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
interface getAllCarModelsRes {
|
||||
status?: string;
|
||||
|
||||
message: string
|
||||
|
||||
data?: {
|
||||
id_model: string;
|
||||
name: string;
|
||||
}[];
|
||||
|
||||
|
||||
}
|
||||
|
||||
const getAllCarModels = async (): Promise<getAllCarModelsReturn> => {
|
||||
try {
|
||||
let res: getAllCarModelsRes = await carsRoutes.list();
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: res.message,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
data: res.data,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export default { getAllCarModels };
|
||||
Reference in New Issue
Block a user