ArcherGu / vite-react-nestjs-electron

Vite + React + Electron + Nestjs with esbuild, crazy fast! ⚡
MIT License
25 stars 3 forks source link

How can i create api in this project? #97

Closed saileshkush95 closed 1 month ago

saileshkush95 commented 1 month ago

I want to create api in this project and Im using this approach but this is not working,

import { Controller, Get } from '@nestjs/common'
import { IpcHandle, Window } from '@doubleshot/nest-electron'
import { Payload } from '@nestjs/microservices'
import { type Observable, of } from 'rxjs'
import type { BrowserWindow } from 'electron'
import log from 'electron-log'
import { AppService } from './app.service'

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @Window() private readonly mainWin: BrowserWindow,
  ) { }

  @IpcHandle('msg')
  public handleSendMsg(@Payload() msg: string): Observable<string> {
    const { webContents } = this.mainWin
    webContents.send('reply-msg', 'this is msg from webContents.send')
    return of(`The main process received your message: ${msg} at time: ${this.appService.getTime()}`)
  }

  @Get('/hello')
  getHello(): number {
    log.info('Log from the main process')
    return this.appService.getTime()
  }
}

and Im trying to access api using this endpoint, http://localhost:5173/hello

ArcherGu commented 1 month ago

I'll give you a sample code that you can follow my steps to modify the project.

process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'

async function electronAppInit() { const isDev = !app.isPackaged app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })

if (isDev) { if (process.platform === 'win32') { process.on('message', (data) => { if (data === 'graceful-exit') app.quit() }) } else { process.on('SIGTERM', () => { app.quit() }) } }

await app.whenReady() }

async function bootstrap() { try { await electronAppInit()

const nestApp = await NestFactory.create(AppModule)

nestApp.connectMicroservice<MicroserviceOptions>({
  strategy: new ElectronIpcTransport('IpcTransport'),
})

nestApp.startAllMicroservices()

await nestApp.listen(3000)

} catch (error) { // eslint-disable-next-line no-console console.log(error) app.quit() } }

bootstrap()


- 3.Add http method decorators in controllers, `src/main/app.controller.ts`:
```ts
import { Controller, Get } from '@nestjs/common'
import { IpcHandle, Window } from '@doubleshot/nest-electron'
import { Payload } from '@nestjs/microservices'
import { type Observable, of } from 'rxjs'
import type { BrowserWindow } from 'electron'
import { AppService } from './app.service'

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @Window() private readonly mainWin: BrowserWindow,
  ) { }

  @IpcHandle('msg')
  public handleSendMsg(@Payload() msg: string): Observable<string> {
    const { webContents } = this.mainWin
    webContents.send('reply-msg', 'this is msg from webContents.send')
    return of(`The main process received your message: ${msg} at time: ${this.appService.getTime()}`)
  }

  @Get('/hello')
  getHello(): number {
    return this.appService.getTime()
  }
}

Now you're ready to launch a hybrid app! nest.js offers a hybrid app pattern, and you can check out its documentation: Hybrid application