import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';

const serviceHost = process.env.SERVICE_HOST || 'http://localhost';
const servicePort = process.env.SERVICE_PORT || 5000;

export async function getRequest(
  url: string,
  params: AxiosRequestConfig<any>
): Promise<AxiosResponse<any, any> | undefined> {
  const response = await axios
    .get(`${serviceHost}:${servicePort}/${url}`, { params })
    .catch((error) => console.error(error));
  return response || undefined;
}

export async function postRequest(
  url: string,
  data?: any,
  config?: AxiosRequestConfig<any>
): Promise<AxiosResponse<any, any> | undefined> {
  const response = await axios
    .post(`${serviceHost}:${servicePort}/${url}`, data, config)
    .catch((error) => console.error(error));
  return response || undefined;
}

export interface Metadata {
  limit: number;
  page: number;
  pages: number;
  total: number;
}
