tauri_plugin_iap/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(target_os = "linux")]
9mod desktop;
10#[cfg(target_os = "macos")]
11mod macos;
12#[cfg(mobile)]
13mod mobile;
14#[cfg(target_os = "windows")]
15mod windows;
16
17mod commands;
18mod error;
19mod models;
20
21pub use error::{Error, Result};
22
23#[cfg(target_os = "linux")]
24use desktop::Iap;
25#[cfg(target_os = "macos")]
26use macos::Iap;
27#[cfg(mobile)]
28use mobile::Iap;
29#[cfg(target_os = "windows")]
30use windows::Iap;
31
32/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the iap APIs.
33pub trait IapExt<R: Runtime> {
34    fn iap(&self) -> &Iap<R>;
35}
36
37impl<R: Runtime, T: Manager<R>> crate::IapExt<R> for T {
38    fn iap(&self) -> &Iap<R> {
39        self.state::<Iap<R>>().inner()
40    }
41}
42
43/// Initializes the plugin.
44pub fn init<R: Runtime>() -> TauriPlugin<R> {
45    Builder::new("iap")
46        .invoke_handler(tauri::generate_handler![
47            commands::initialize,
48            commands::get_products,
49            commands::purchase,
50            commands::restore_purchases,
51            commands::acknowledge_purchase,
52            commands::get_product_status,
53        ])
54        .setup(|app, api| {
55            #[cfg(target_os = "macos")]
56            let iap = macos::init(app, api)?;
57            #[cfg(mobile)]
58            let iap = mobile::init(app, api)?;
59            #[cfg(target_os = "windows")]
60            let iap = windows::init(app, api)?;
61            #[cfg(target_os = "linux")]
62            let iap = desktop::init(app, api)?;
63            app.manage(iap);
64            Ok(())
65        })
66        .build()
67}