hrms-api-salary/src/interfaces/call-api.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-02-28 11:27:52 +07:00
import {
Controller,
Request,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
} from "tsoa";
import axios from "axios";
2024-02-28 10:37:48 +07:00
class CallAPI {
2024-02-28 11:27:52 +07:00
//Get
public async GetData(request: any, @Path() path: any) {
const token = request.headers.authorization;
2024-02-28 14:18:07 +07:00
const url = process.env.API + path;
2024-02-28 11:27:52 +07:00
try {
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
return response.data.result;
} catch (error) {
throw error;
2024-02-28 10:37:48 +07:00
}
2024-02-28 11:27:52 +07:00
}
2024-02-28 13:59:03 +07:00
//Post
public async PostData(request: any, @Path() path: any, sendData: any) {
const token = request.headers.authorization;
2024-02-28 14:18:07 +07:00
const url = process.env.API + path;
2024-02-28 13:59:03 +07:00
try {
2024-02-28 14:46:17 +07:00
const response = await axios.post(url, sendData, {
2024-02-28 13:59:03 +07:00
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
return response.data.result;
} catch (error) {
throw error;
}
}
2024-02-28 10:37:48 +07:00
}
2024-02-28 11:27:52 +07:00
export default CallAPI;