read ip from json file electron react

162 Views Asked by At

I need to read from a json file saved in C:/ the ip address of the API server inside an electron-react app build for prod. This for don't build again the project for production when the ip changes.

Im using axios instance to make the calls to the api

import axios from 'axios';

const apiTest = 'http://localhost:8000/api';
const apiURL = 'http://189.173.109.69:1080/api'; // <- i need to get this from a json file saved in C drive

class AxiosInstance {
  private static instance: any;

  public manager: any;

  constructor() {
    if (AxiosInstance.instance) {
      return AxiosInstance.instance;
    }
    this.manager = axios.create({
      baseURL: apiURL,
    });



    this.manager.defaults.headers = {
      'content-type': 'application/json',
      Accept: 'application/json',
    };

    AxiosInstance.instance = this;
    this.manager.defaults.timeout = 15000;
    this.manager.interceptors.request.use((request: any) =>
      AxiosInstance.requestHandler(request)
    );
    this.manager.interceptors.response.use(
      (response: any) => {
        return response;
      },
      async (error: any) => {
        // Do something with response error
        if (error?.response?.data?.error && error.response.status === 401) {
          const errorText = error?.response?.data?.error;
          console.log(errorText);
          return Promise.reject(error);
        }
        // Do something with response error
        if (error?.response?.data?.error && error.response.status === 422) {
          let errorText = '';
          // eslint-disable-next-line guard-for-in
          for (const key in error?.response?.data?.error) {
            errorText += `${error?.response?.data?.error[key]} \n`;
          }
          console.log(errorText);
        }
        return Promise.reject(error);
      }
    );
  }

  static requestHandler = async (request: any) => {
    // const jwt = await SecureStore.getItemAsync('jwt');
    // if (jwt) {
    request.headers['content-type'] = 'application/json';
    // request.headers.Authorization = `Bearer ${jwt}`;
    // }
    return request;
  };
}

export { apiURL, apiTest, AxiosInstance };

export default new AxiosInstance().manager;

I need to get the IP address from that json file and put it in the axios instance as the apiURL (baseURL) to make api calls.

0

There are 0 best solutions below