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

101 lines
2.9 KiB
TypeScript
Raw Normal View History

import {
Controller,
Request,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
} from "tsoa";
import axios from "axios";
import HttpError from "./http-error";
import { CreateProfileSalary, ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { RequestWithUser } from "../middlewares/user";
import HttpStatus from "./http-status";
import HttpSuccess from "./http-success";
import permission from "./permission";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
class CallAPI {
private profileRepo = AppDataSource.getRepository(Profile);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
//Get
public async GetData(request: any, @Path() path: any) {
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: {
Authorization: `${token}`.includes("Bearer "),
"Content-Type": "application/json",
2024-08-15 13:28:06 +07:00
api_key: process.env.API_KEY,
},
});
return response.data.result;
} catch (error) {
throw error;
}
}
2024-09-23 11:48:10 +07:00
//Get (response.data)
public async GetData2(request: any, @Path() path: any) {
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
2024-09-23 11:48:10 +07:00
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`.includes("Bearer "),
2024-09-23 11:48:10 +07:00
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
});
return response.data;
} catch (error) {
throw error;
}
}
//Post
public async PostData(request: any, @Path() path: any, sendData: any) {
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,
},
});
return response.data.result;
} catch (error) {
console.log(error);
throw error;
}
}
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;
}
}
}
export default CallAPI;