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

122 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-09-30 09:44:43 +07:00
import { Path } from "tsoa";
import axios from "axios";
import { addLogSequence } from "./utils";
class CallAPI {
//Get
2024-12-18 18:59:06 +07:00
public async GetData(request: any, @Path() path: any, log = true) {
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
2024-07-09 17:03:09 +07:00
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
2024-09-30 09:44:43 +07:00
Authorization: `${token}`,
"Content-Type": "application/json",
2024-08-15 13:28:06 +07:00
api_key: process.env.API_KEY,
},
});
addLogSequence(request, {
action: "request",
status: "success",
description: "connected",
request: {
method: "GET",
url: url,
response: JSON.stringify(response.data.result),
},
});
return response.data.result;
} catch (error) {
addLogSequence(request, {
action: "request",
status: "error",
description: "unconnected",
request: {
method: "GET",
url: url,
response: JSON.stringify(error),
},
});
throw error;
}
}
//Post
2024-10-11 11:05:31 +07:00
public async PostData(request: any, @Path() path: any, sendData: any, log = true) {
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
2024-07-09 17:03:09 +07:00
const url = process.env.API_URL + path;
try {
const response = await axios.post(url, sendData, {
headers: {
Authorization: `${token}`,
"Content-Type": "application/json",
2024-08-15 13:28:06 +07:00
api_key: process.env.API_KEY,
},
});
2024-11-01 19:53:26 +07:00
if (log)
addLogSequence(request, {
action: "request",
status: "success",
description: "connected",
request: {
method: "POST",
url: url,
payload: JSON.stringify(sendData),
response: JSON.stringify(response.data.result),
},
});
return response.data.result;
} catch (error) {
2024-11-01 19:53:26 +07:00
if (log)
addLogSequence(request, {
action: "request",
status: "error",
description: "unconnected",
request: {
method: "POST",
url: url,
payload: JSON.stringify(sendData),
response: JSON.stringify(error),
},
});
throw error;
}
}
2024-10-11 11:05:31 +07:00
2024-08-27 16:37:42 +07:00
//Post
public async PostDataKeycloak(@Path() path: any, sendData: any) {
// const token = request.headers.authorization;
const url = process.env.KC_URL + path;
try {
const response = await axios.post(url, sendData, {
headers: {
// Authorization: `${token}`,
"Content-Type": "application/x-www-form-urlencoded",
api_key: process.env.API_KEY,
},
});
return response.data;
} catch (error) {
throw error;
}
}
2026-01-30 14:47:46 +07:00
//Delete File
public async DeleteFile(request: any, @Path() path: any) {
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + "/salary/file/" + path;
try {
await axios.delete(url, {
headers: {
Authorization: `${token}`,
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
});
} catch (error) {
throw error;
}
}
}
export default CallAPI;