From a756df244b8949ca975105e5cdcf13bebdcecc85 Mon Sep 17 00:00:00 2001 From: Pierre Jacquier Date: Mon, 10 Jun 2024 07:26:39 -0400 Subject: [PATCH] Push greet example to use the kittcad.rs api --- manifest.json | 3 ++- src/chrome/background.ts | 7 ++++--- src/wasm-lib/Cargo.lock | 1 + src/wasm-lib/Cargo.toml | 1 + src/wasm-lib/src/lib.rs | 9 +++++++-- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/manifest.json b/manifest.json index 3eda01d6..a1786ab6 100644 --- a/manifest.json +++ b/manifest.json @@ -19,7 +19,8 @@ "https://github.com/", "https://api.github.com/", "https://media.githubusercontent.com/", - "https://api.kittycad.io/" + "https://api.kittycad.io/", + "https://api.zoo.dev/" ], "content_scripts": [ { diff --git a/src/chrome/background.ts b/src/chrome/background.ts index 0c0da585..7735c5b9 100644 --- a/src/chrome/background.ts +++ b/src/chrome/background.ts @@ -36,7 +36,9 @@ async function initGithubApi() { async function initKittycadApi() { try { - kittycad = new Client(await getStorageKittycadToken()) + const token = await getStorageKittycadToken() + console.log('greet(token)', await greet(token)) + kittycad = new Client(token) const response = await users.get_user_self({ client: kittycad }) if ('error_code' in response) throw response const { email } = response @@ -67,7 +69,6 @@ async function initialiseWasm() { const buffer = await input.arrayBuffer() const output = await init(buffer) console.log('Wasm loaded: ', output) - console.log(greet()) return output } catch (e) { console.log('Error initialising WASM', e) @@ -78,9 +79,9 @@ async function initialiseWasm() { ; (async () => { // Delay to allow for external storage sets before auth, like in e2e await new Promise(resolve => setTimeout(resolve, 1000)) + await initialiseWasm() await initKittycadApi() await initGithubApi() - await initialiseWasm() })() const noClientError = new Error('API client is undefined') diff --git a/src/wasm-lib/Cargo.lock b/src/wasm-lib/Cargo.lock index fe4d8a81..c3c634d0 100644 --- a/src/wasm-lib/Cargo.lock +++ b/src/wasm-lib/Cargo.lock @@ -2792,6 +2792,7 @@ dependencies = [ "kcl-lib", "kittycad", "wasm-bindgen", + "wasm-bindgen-futures", "wasm-bindgen-test", ] diff --git a/src/wasm-lib/Cargo.toml b/src/wasm-lib/Cargo.toml index df3f87f0..5648b7f8 100644 --- a/src/wasm-lib/Cargo.toml +++ b/src/wasm-lib/Cargo.toml @@ -15,6 +15,7 @@ default = ["console_error_panic_hook"] kcl-lib = { git = "https://github.com/kittycad/modeling-app", branch = "main" } kittycad = { version = "0.3.0", default-features = false, features = ["js", "requests"] } wasm-bindgen = "0.2.84" +wasm-bindgen-futures = "0.4.42" # The `console_error_panic_hook` crate provides better debugging of panics by # logging them with `console.error`. This is great for development, but requires diff --git a/src/wasm-lib/src/lib.rs b/src/wasm-lib/src/lib.rs index ebfb6286..7428dd8d 100644 --- a/src/wasm-lib/src/lib.rs +++ b/src/wasm-lib/src/lib.rs @@ -3,6 +3,11 @@ mod utils; use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn greet() -> JsValue { - return JsValue::from_str("hello, world"); +pub async fn greet(token: &str) -> Result { + let client = kittycad::Client::new(token); + let result = client.users().get_self().await; + let Some(email) = result.unwrap().email else { + return Err(JsError::new(&format!("Server returned error"))); + }; + Ok(JsValue::from_str(email.as_str())) }