1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use js_sys::{ArrayBuffer, Promise};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    pub type KV;

    /// The promise returns a Result<T, E> where T is a kind defined in KVGetOptions
    #[wasm_bindgen(method)]
    pub fn get(this: &KV, key: &str) -> Promise;

    #[wasm_bindgen(method, js_name=get)]
    pub fn get_with_options(this: &KV, key: &str, options: KVGetOptions) -> Promise;

    /// returns a Promise<MetadataValue>
    #[wasm_bindgen(method, js_name=getWithMetadata)]
    pub fn get_with_metadata(this: &KV, key: &str) -> Promise;

    /// returns a Promise<MetadataValue>
    #[wasm_bindgen(method, js_name=getWithMetadata)]
    pub fn get_with_options_and_metadata(this: &KV, key: &str, options: KVGetOptions) -> Promise;

    #[wasm_bindgen(method, js_name=put)]
    pub fn put_string(this: &KV, key: &str, value: &str) -> Promise;

    #[wasm_bindgen(method, js_name=put)]
    pub fn put_stream(this: &KV, key: &str, value: super::ReadableStream) -> Promise;

    #[wasm_bindgen(method, js_name=put)]
    pub fn put_buffer(this: &KV, key: &str, value: ArrayBuffer) -> Promise;
}

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_name=Object)]
    pub type MetadataValue;

    /// `value` is null or the request type from KV.get() or KV.getWithMetadata()
    #[wasm_bindgen(getter, method, js_class=Object)]
    pub fn value(this: &MetadataValue) -> JsValue;

    /// `metadata` is null or a `String`
    #[wasm_bindgen(getter, method, js_class=Object)]
    pub fn metadata(this: &MetadataValue) -> JsValue;
}

pub enum GetKind {
    Text,
    Json,
    ArrayBuffer,
    Stream,
}

impl From<GetKind> for KVGetOptions {
    fn from(get_kind: GetKind) -> Self {
        Self { kind: get_kind }
    }
}

#[wasm_bindgen]
pub struct KVGetOptions {
    kind: GetKind,
}

#[wasm_bindgen]
impl KVGetOptions {
    #[wasm_bindgen(getter, method, js_name=type)]
    pub fn kind(&self) -> String {
        match self.kind {
            GetKind::Text => String::from("text"),
            GetKind::Json => String::from("json"),
            GetKind::ArrayBuffer => String::from("arrayBuffer"),
            GetKind::Stream => String::from("stream"),
        }
    }
}