web-humas-fe/service/third-party-service.ts

83 lines
2.0 KiB
TypeScript

import axios from "axios";
const tbnInstance = axios.create({
baseURL: "https://portal.humas.polri.go.id/v1/api",
headers: {
"content-type": "application/json",
},
});
const inpInstance = axios.create({
baseURL: "https://inp.demoaplikasi.web.id/api",
headers: {
"content-type": "application/json",
},
});
async function tbnGetNews(pathUrl: any, headers: any) {
const response = await tbnInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
async function inpGetNews(pathUrl: any, headers: any) {
const response = await inpInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function topNewsTbn() {
const headers = {
"content-type": "application/json",
};
return await tbnGetNews(
`/public/articles?page=1&limit=10&order_by=terkini&source=tbnews`,
headers
);
}
export async function topNewsInp() {
const headers = {
"content-type": "application/json",
};
return await inpGetNews(`/artikel/data?per_page=10&page=1`, headers);
}
export async function getImageInp(id: string) {
const headers = {
"content-type": "application/json",
};
return await inpGetNews(`/media?parent=${id}`, headers);
}