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

95 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-04-02 17:53:45 +07:00
import {
Controller,
Request,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
} from "tsoa";
import axios from "axios";
2024-07-25 17:06:42 +07:00
import { addLogSequence } from "./utils";
2024-04-02 17:53:45 +07:00
class CallAPI {
//Get
public async GetData(request: any, @Path() path: any) {
const token = request.headers.authorization;
2024-07-09 17:03:24 +07:00
const url = process.env.API_URL + path;
2024-04-02 17:53:45 +07:00
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`,
2024-04-02 17:53:45 +07:00
"Content-Type": "application/json",
},
});
2024-07-25 17:06:42 +07:00
addLogSequence(request, {
action: "request",
status: "success",
2024-07-26 16:30:38 +07:00
description: "connected",
2024-07-25 17:06:42 +07:00
request: {
method: "GET",
url: url,
2024-07-26 16:30:38 +07:00
response: JSON.stringify(response.data.result),
2024-07-25 17:06:42 +07:00
},
});
2024-04-02 17:53:45 +07:00
return response.data.result;
} catch (error) {
2024-07-25 17:06:42 +07:00
addLogSequence(request, {
action: "request",
status: "error",
2024-07-26 16:30:38 +07:00
description: "unconnected",
2024-07-25 17:06:42 +07:00
request: {
method: "GET",
url: url,
2024-07-26 16:30:38 +07:00
response: JSON.stringify(error),
2024-07-25 17:06:42 +07:00
},
});
2024-04-02 17:53:45 +07:00
throw error;
}
}
//Post
public async PostData(request: any, @Path() path: any, sendData: any) {
const token = request.headers.authorization;
2024-07-09 17:03:24 +07:00
const url = process.env.API_URL + path;
2024-04-02 17:53:45 +07:00
try {
const response = await axios.post(url, sendData, {
headers: {
Authorization: `${token}`,
2024-04-02 17:53:45 +07:00
"Content-Type": "application/json",
},
});
2024-07-25 17:06:42 +07:00
addLogSequence(request, {
action: "request",
status: "success",
2024-07-26 16:30:38 +07:00
description: "connected",
2024-07-25 17:06:42 +07:00
request: {
method: "POST",
url: url,
2024-07-26 16:30:38 +07:00
payload: JSON.stringify(sendData),
response: JSON.stringify(response.data.result),
2024-07-25 17:06:42 +07:00
},
});
2024-04-02 17:53:45 +07:00
return response.data.result;
} catch (error) {
2024-07-25 17:06:42 +07:00
addLogSequence(request, {
action: "request",
status: "error",
2024-07-26 16:30:38 +07:00
description: "unconnected",
2024-07-25 17:06:42 +07:00
request: {
method: "POST",
url: url,
2024-07-26 16:30:38 +07:00
payload: JSON.stringify(sendData),
response: JSON.stringify(error),
2024-07-25 17:06:42 +07:00
},
});
2024-04-02 17:53:45 +07:00
throw error;
}
}
}
export default CallAPI;