2 releases

new 0.0.2 Dec 1, 2024
0.0.1 Dec 1, 2024

#510 in WebAssembly

Apache-2.0

10KB
182 lines

extension_api

使用

依赖 extension_api

实现 api

cargo create --lib demo_plugin

src/lib.rs

use extension_api::{
    register_plugin,
    wit::{
        get_settings,
        workoss::extension::{
            extension_http::{fetch, HttpRequest},
            platform::current_platform,
        },
        PluginInput, PluginOutput,
    },
    Plugin,
};

struct DemoPlugin;

impl Plugin for DemoPlugin {
    fn new() -> Self
    where
        Self: Sized,
    {
        Self
    }

    fn run_plugin(&self, input: PluginInput) -> Result<PluginOutput, String> {
        let (os, arch) = current_platform()?;
        println!("host os:{os:?},arch:{arch:?}");
        let input = String::from_utf8(input.body.unwrap()).unwrap();
        println!("demo plugin input:{:#?}", input);
        let settings = get_settings(Some("plugin_key"))?;
        println!("settings: {:#?}", settings);
        let req = HttpRequest::builder()
            .method(extension_api::wit::workoss::extension::http_client::HttpMethod::Get)
            .url("https://www.baidu.com")
            .build()?;
        let resp = fetch(&req).map_err(|e| format!("{e:?}"))?;
        let body = String::from_utf8(resp.body).unwrap();
        println!("resp body:{:?}", &body);

        Ok(PluginOutput {
            body: Some(body.into_bytes()),
            mime_type: extension_api::wit::MimeType::Json,
        })
    }
}

register_plugin!(DemoPlugin);

Dependencies

~0.8–1.7MB
~37K SLoC