feat(plugins): inject API on window.__TAURI__ (#383)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-23 10:20:14 -07:00
committed by GitHub
parent 3c8577bc9a
commit b131bc8f7c
78 changed files with 754 additions and 337 deletions
+15 -12
View File
@@ -2,7 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { invoke } from "@tauri-apps/api/tauri";
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
}
}
export interface QueryResult {
/** The number of rows affected by the query. */
@@ -46,7 +50,7 @@ export default class Database {
* ```
*/
static async load(path: string): Promise<Database> {
const _path = await invoke<string>("plugin:sql|load", {
const _path = await window.__TAURI_INVOKE__<string>("plugin:sql|load", {
db: path,
});
@@ -87,14 +91,13 @@ export default class Database {
* ```
*/
async execute(query: string, bindValues?: unknown[]): Promise<QueryResult> {
const [rowsAffected, lastInsertId] = await invoke<[number, number]>(
"plugin:sql|execute",
{
db: this.path,
query,
values: bindValues ?? [],
}
);
const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__<
[number, number]
>("plugin:sql|execute", {
db: this.path,
query,
values: bindValues ?? [],
});
return {
lastInsertId,
@@ -115,7 +118,7 @@ export default class Database {
* ```
*/
async select<T>(query: string, bindValues?: unknown[]): Promise<T> {
const result = await invoke<T>("plugin:sql|select", {
const result = await window.__TAURI_INVOKE__<T>("plugin:sql|select", {
db: this.path,
query,
values: bindValues ?? [],
@@ -136,7 +139,7 @@ export default class Database {
* @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
*/
async close(db?: string): Promise<boolean> {
const success = await invoke<boolean>("plugin:sql|close", {
const success = await window.__TAURI_INVOKE__<boolean>("plugin:sql|close", {
db,
});
return success;
+1
View File
@@ -0,0 +1 @@
if("__TAURI__"in window){var __TAURI_SQL__=function(){"use strict";class _{constructor(_){this.path=_}static async load(t){const e=await window.__TAURI_INVOKE__("plugin:sql|load",{db:t});return new _(e)}static get(t){return new _(t)}async execute(_,t){const[e,n]=await window.__TAURI_INVOKE__("plugin:sql|execute",{db:this.path,query:_,values:null!=t?t:[]});return{lastInsertId:n,rowsAffected:e}}async select(_,t){return await window.__TAURI_INVOKE__("plugin:sql|select",{db:this.path,query:_,values:null!=t?t:[]})}async close(_){return await window.__TAURI_INVOKE__("plugin:sql|close",{db:_})}}return _}();Object.defineProperty(window.__TAURI__,"sql",{value:__TAURI_SQL__})}
+1
View File
@@ -280,6 +280,7 @@ impl Builder {
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R, Option<PluginConfig>> {
PluginBuilder::<R, Option<PluginConfig>>::new("sql")
.js_init_script(include_str!("api-iife.js").to_string())
.invoke_handler(tauri::generate_handler![load, execute, select, close])
.setup(|app, api| {
let config = api.config().clone().unwrap_or_default();