diff --git a/examples/get-weather-js/package.json b/examples/get-weather-js/package.json index 01fd32e4..460583b7 100644 --- a/examples/get-weather-js/package.json +++ b/examples/get-weather-js/package.json @@ -5,7 +5,7 @@ "@bytecodealliance/jco": "^1.11.1" }, "scripts": { - "build:component": "jco componentize -w ./wit weather.js -d stdio -d random -d clocks -o weather.wasm", + "build:component": "jco componentize -w ./wit weather.js -o weather.wasm", "transpile": "jco transpile weather.wasm -o out-dir", "debug": "node use-weather.js" }, diff --git a/examples/get-weather-js/weather.js b/examples/get-weather-js/weather.js index beebae78..d15f5684 100644 --- a/examples/get-weather-js/weather.js +++ b/examples/get-weather-js/weather.js @@ -1,31 +1,32 @@ import { get } from "wasi:config/store@0.2.0-draft"; export async function getWeather(city) { + const apiKey = await get("OPENWEATHER_API_KEY"); + if (apiKey === undefined) { + throw "Error: OPENWEATHER_API_KEY is not set"; + } + try { - const apiKey = await get("OPENWEATHER_API_KEY"); - if (apiKey === undefined) { - return "Error: OPENWEATHER_API_KEY is not set"; - } const geoResponse = await fetch( `https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=${apiKey}` ); if (!geoResponse.ok) { - return "Error: Failed to fetch geo data"; + throw "Error: Failed to fetch geo data"; } const geoData = await geoResponse.json(); const lat = geoData[0].lat; const lon = geoData[0].lon; - const response = await fetch( `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric` - ) + ); if (!response.ok) { - return "Error: Failed to fetch weather data"; + throw "Error: Failed to fetch weather data"; } const data = await response.json(); const weather = data.main.temp.toString(); - return weather; + return weather; } catch (error) { - return "Error fetching weather data"; + throw error.message || "Error fetching weather data"; } + } diff --git a/examples/get-weather-js/wit/mcp.wit b/examples/get-weather-js/wit/mcp.wit index 7ddf16d0..172e50dc 100644 --- a/examples/get-weather-js/wit/mcp.wit +++ b/examples/get-weather-js/wit/mcp.wit @@ -2,5 +2,5 @@ package mossaka:mcp@0.1.0; world weather-mcp { import wasi:config/store@0.2.0-draft; - export get-weather: func(city: string) -> string; + export get-weather: func(city: string) -> result; } \ No newline at end of file