สร้างฟังก์ชันกลาง

This commit is contained in:
kittapath 2024-09-27 23:44:47 +07:00
parent 90100e3460
commit 32b50c905c
3 changed files with 77 additions and 10 deletions

View file

@ -12,16 +12,28 @@ import {
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 = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`,
Authorization: `${token}`.includes("Bearer "),
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
@ -33,12 +45,12 @@ class CallAPI {
}
//Get (response.data)
public async GetData2(request: any, @Path() path: any) {
const token = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`,
Authorization: `${token}`.includes("Bearer "),
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
@ -50,7 +62,7 @@ class CallAPI {
}
//Post
public async PostData(request: any, @Path() path: any, sendData: any) {
const token = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.post(url, sendData, {
@ -62,6 +74,7 @@ class CallAPI {
});
return response.data.result;
} catch (error) {
console.log(error);
throw error;
}
}