store

Attribute Macro store 

Source
#[store]
Expand description

Wraps a function generating a resource out of parameters, and creates a store.

Accessing the store from different locations uses always the same store as data is kept using LocalKey.

use vertigo::{AutoJsJson, LazyCache, RequestBuilder, store};

#[derive(AutoJsJson)]
struct CommentModel {
    id: i32,
    name: String,
}

#[store]
fn get_post(post_id: &String) -> LazyCache<Vec<CommentModel>> {
    RequestBuilder
        ::get(format!("https://jsonplaceholder.typicode.com/posts/{post_id}/comments"))
        .ttl_minutes(10)
        .lazy_cache(|status, body| {
            if status == 200 {
                Some(body.into::<Vec<CommentModel>>())
            } else {
                None
            }
        })
}