Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/get-weather-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
21 changes: 11 additions & 10 deletions examples/get-weather-js/weather.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { get } from "wasi:config/[email protected]";

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";
}

}
2 changes: 1 addition & 1 deletion examples/get-weather-js/wit/mcp.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package mossaka:[email protected];

world weather-mcp {
import wasi:config/[email protected];
export get-weather: func(city: string) -> string;
export get-weather: func(city: string) -> result<string, string>;
}