Preview
Open Original
Duck by Example
Typesafe fetching from JSON APIs
fn main() {
const response_json = match std::web::fetch("https://dummyjson.com/test").send() {
String @str => str,
.http_error => return,
};
const parsed = match parse_json<{status: String, method: String,}>(response_json) {
{ status: String, method: String } @response => response,
else => return,
};
std::io::println(parsed.status);
}
$ dargo runok
Server Side Rendering
component Counter(props: { initial_value: Int }) jsx {
const [value, setValue] = useState(props.initial_value);
return (
<>
<div>
<div>Current value: {value}</div>
<button onClick={() => setValue((v) => v + 1)}>Increment</button>
</div>
</>
);
}
template Page(props: { param: String }) duckx {
<html lang="en">
<body>
<p>You sent: {props.param}</p>
<...
Duck by Example
Typesafe fetching from JSON APIs
fn main() {
const response_json = match std::web::fetch("https://dummyjson.com/test").send() {
String @str => str,
.http_error => return,
};
const parsed = match parse_json<{status: String, method: String,}>(response_json) {
{ status: String, method: String } @response => response,
else => return,
};
std::io::println(parsed.status);
}
$ dargo runok
Server Side Rendering
component Counter(props: { initial_value: Int }) jsx {
const [value, setValue] = useState(props.initial_value);
return (
<>
<div>
<div>Current value: {value}</div>
<button onClick={() => setValue((v) => v + 1)}>Increment</button>
</div>
</>
);
}
template Page(props: { param: String }) duckx {
<html lang="en">
<body>
<p>You sent: {props.param}</p>
<Counter initial_value={10}/>
</body>
</html>
}
fn main() {
std::web::HttpServer::new(.none)
.handle("GET /{param}", fn(req, res) {
const param = match req.param("param") { String @s => s, else => return, };
res.duckx(Page({param: param}));
})
.listen(":3000");
}
See more examples in our Tour of Duck