Skip to content

Backend

Your Taurify frontend has full access to the Taurify API. To extend its functionality and access native operating system interfaces not available in the API, you can run backend code using JavaScript levaraging Deno.

import { runEventLoop } from 'npm:@crabnebula/taurify-api/deno'
import { appConfigDir } from 'npm:@crabnebula/taurify-api/path'
import { join } from 'node:path'
const configDir = await appConfigDir()
const path = join(configDir, 'taurify.txt')
await Deno.mkdir(configDir, { recursive: true })
await Deno.writeTextFile(path, "Hello World")
// poll the Taurify app, waiting for events
runEventLoop()

API

The backend can run arbitrary JavaScript code, and Taurify provides primitives to access the Taurify API and communicate with your frontend.

Taurify API

The JavaScript backend have full access to the Taurify API, just like your frontend. This enables you to have consistency between your frontend and backend code while leveraging all features of the Taurify API.

For example, you can easily create a tray icon from your backend:

import { runEventLoop } from 'npm:@crabnebula/taurify-api/deno'
import { defaultWindowIcon } from 'npm:@crabnebula/taurify-api/app'
import { TrayIcon } from 'npm:@crabnebula/taurify-api/tray'
import { Menu } from 'npm:@crabnebula/taurify-api/menu'
async function createTrayIcon() {
await TrayIcon.new({
icon: await defaultWindowIcon(),
menu: await Menu.new({
items: [
{
text: 'App',
action: (e) => {
console.log('item clicked', e)
}
}
]
}),
action: (e) => {
console.log('tray event', e)
}
})
}
await createTrayIcon()
runEventLoop()

Communicating with the Frontend

To send messages between frontend and backend you can either use the event system or define commands.

A command is a backend function that can be called by the frontend:

import { registerCommand, runEventLoop } from 'npm:@crabnebula/taurify-api/deno'
type LoginRequest = { user: string, password: string }
registerCommand<LoginRequest>('login' /* command name */, async (args) => {
const { user, password } = args;
return {
status: 200,
message: 'authenticated'
}
})
runEventLoop()

To execute the command, use the [invoke] API on your frontend:

import { invoke } from '@crabnebula-dev/taurify-api'
type LoginResponse = { status: number, message: string }
const response = await invoke<LoginResponse>('login', {
user: 'taurify',
password: 'taurify-app'
})
assert(response.status, 200)