1

How can i possible access an REST api call from my backend to Electron-Vite without any security issues?

Im trying to do this but its not working.

import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import axios from 'axios';

// Custom APIs for renderer
const api = {

  fetchPosts: async () => {
    try {
      const response = await axios.get('https://jsonplaceholder.typicode.com/posts');
      return response.data;
    } catch (error) {
      console.error('Error fetching posts:', error);
      return [];
    }
  },
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}


1
  • "not working" is not a sufficient problem statement. What result do you expect and what result do you actually get?
    – Phil
    Commented Jul 8 at 1:49

0

Browse other questions tagged or ask your own question.