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;