Skip to content

Commit

Permalink
Push greet example to use the kittcad.rs api
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremtb committed Jun 10, 2024
1 parent 4f65a8d commit a756df2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
7 changes: 4 additions & 3 deletions src/chrome/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand Down
1 change: 1 addition & 0 deletions src/wasm-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/wasm-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/wasm-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue, JsError> {
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()))
}

0 comments on commit a756df2

Please sign in to comment.