Skip to content
Tauri

core

Invoke your custom commands.

This package is also accessible with window.__TAURI__.core when app.withGlobalTauri in tauri.conf.json is set to true.

Classes

Channel<T>

Type Parameters

Type ParameterDefault type
Tunknown

Constructors

new Channel()
new Channel<T>(): Channel<T>
Returns

Channel<T>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L37

Properties

PropertyTypeDefined in
idnumberSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L28

Accessors

onmessage
get onmessage(): (response) => void
set onmessage(handler): void
Parameters
ParameterType
handler(response) => void
Returns

Function

Parameters
ParameterType
responseT
Returns

void

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L79

Methods

toJSON()
toJSON(): string
Returns

string

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L83


PluginListener

Constructors

new PluginListener()
new PluginListener(
plugin,
event,
channelId): PluginListener
Parameters
ParameterType
pluginstring
eventstring
channelIdnumber
Returns

PluginListener

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L93

Properties

PropertyTypeDefined in
channelIdnumberSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L91
eventstringSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L90
pluginstringSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L89

Methods

unregister()
unregister(): Promise<void>
Returns

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L99


Resource

A rust-backed resource stored through tauri::Manager::resources_table API.

The resource lives in the main process and does not exist in the Javascript world, and thus will not be cleaned up automatiacally except on application exit. If you want to clean it up early, call Resource.close

Example

import { Resource, invoke } from '@tauri-apps/api/core';
export class DatabaseHandle extends Resource {
static async open(path: string): Promise<DatabaseHandle> {
const rid: number = await invoke('open_db', { path });
return new DatabaseHandle(rid);
}
async execute(sql: string): Promise<void> {
await invoke('execute_sql', { rid: this.rid, sql });
}
}

Extended by

Constructors

new Resource()
new Resource(rid): Resource
Parameters
ParameterType
ridnumber
Returns

Resource

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L246

Accessors

rid
get rid(): number
Returns

number

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L242

Methods

close()
close(): Promise<void>

Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.

Returns

Promise<void>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L254

Interfaces

InvokeOptions

Since

2.0.0

Properties

PropertyTypeDefined in
headersRecord<string, string> | HeadersSource: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L157

Type Aliases

InvokeArgs

type InvokeArgs: Record<string, unknown> | number[] | ArrayBuffer | Uint8Array;

Command arguments.

Since

1.0.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L151


PermissionState

type PermissionState: "granted" | "denied" | "prompt" | "prompt-with-rationale";

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L126

Functions

addPluginListener()

function addPluginListener<T>(
plugin,
event,
cb): Promise<PluginListener>

Adds a listener to a plugin event.

Type Parameters

Type Parameter
T

Parameters

ParameterType
pluginstring
eventstring
cb(payload) => void

Returns

Promise<PluginListener>

The listener object to stop listening to the events.

Since

2.0.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L114


checkPermissions()

function checkPermissions<T>(plugin): Promise<T>

Get permission state for a plugin.

This should be used by plugin authors to wrap their actual implementation.

Type Parameters

Type Parameter
T

Parameters

ParameterType
pluginstring

Returns

Promise<T>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L133


convertFileSrc()

function convertFileSrc(filePath, protocol): string

Convert a device file path to an URL that can be loaded by the webview. Note that asset: and http://asset.localhost must be added to app.security.csp in tauri.conf.json. Example CSP value: "csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost" to use the asset protocol on image sources.

Additionally, "enable" : "true" must be added to app.security.assetProtocol in tauri.conf.json and its access scope must be defined on the scope array on the same assetProtocol object.

Parameters

ParameterTypeDefault valueDescription
filePathstringundefinedThe file path.
protocolstring'asset'The protocol to use. Defaults to asset. You only need to set this when using a custom protocol.

Returns

string

the URL that can be used as source on the webview.

Example

import { appDataDir, join } from '@tauri-apps/api/path';
import { convertFileSrc } from '@tauri-apps/api/core';
const appDataDirPath = await appDataDir();
const filePath = await join(appDataDirPath, 'assets/video.mp4');
const assetUrl = convertFileSrc(filePath);
const video = document.getElementById('my-video');
const source = document.createElement('source');
source.type = 'video/mp4';
source.src = assetUrl;
video.appendChild(source);
video.load();

Since

1.0.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L213


invoke()

function invoke<T>(
cmd,
args,
options?): Promise<T>

Sends a message to the backend.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
cmdstringThe command name.
argsInvokeArgsThe optional arguments to pass to the command.
options?InvokeOptionsThe request options.

Returns

Promise<T>

A promise resolving or rejecting to the backend response.

Example

import { invoke } from '@tauri-apps/api/core';
await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });

Since

1.0.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L175


isTauri()

function isTauri(): boolean

Returns

boolean

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L261


requestPermissions()

function requestPermissions<T>(plugin): Promise<T>

Request permissions.

This should be used by plugin authors to wrap their actual implementation.

Type Parameters

Type Parameter
T

Parameters

ParameterType
pluginstring

Returns

Promise<T>

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L142


transformCallback()

function transformCallback<T>(callback?, once?): number

Transforms a callback function to a string identifier that can be passed to the backend. The backend uses the identifier to eval() the callback.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterTypeDefault value
callback?(response) => voidundefined
once?booleanfalse

Returns

number

A unique identifier associated with the callback function.

Since

1.0.0

Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/core.ts#L20


© 2024 Tauri Contributors. CC-BY / MIT