edgee_sdk/
lib.rs

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// Retrieves the SDK content from a given URL.
///
/// This function extracts the version of the SDK from the URL using a regular expression.
/// Then, it retrieves the SDK content based on the extracted version.
/// The function returns a `Result` that contains the SDK content wrapped in a `<script>` tag if successful, or an error message if not.
///
/// # Arguments
///
/// * `url` - A string slice that holds the URL.
/// * `host` - A string slice that holds the host.
///
/// # Returns
///
/// * `Result<String, &'static str>` - The SDK content wrapped in a `<script>` tag if successful, or an error message if not.
///
/// # Example
///
/// ```rust
/// let url = "/_edgee/edgee.v1.2.0.js";
/// let host = "example.com";
/// let sdk_content = edgee_sdk::get_sdk_from_url(url, host);
/// assert!(sdk_content.is_ok());
/// ```
pub fn get_sdk_from_url(url: &str, host: &str) -> Result<String, &'static str> {
    if url.ends_with("sdk.js") {
        let sdk = include_str!("../release/edgee.v1.3.1.js").trim();
        return Ok(dynamize_sdk(sdk, host));
    }

    let Some((_, part)) = url.rsplit_once("edgee.v") else {
        return Err("Failed to read the JS SDK file");
    };
    let Some(part) = part.strip_suffix(".js") else {
        return Err("Failed to read the JS SDK file");
    };

    let content = match part {
        "1.2.0" => include_str!("../release/edgee.v1.2.0.js"),
        "1.3.0" => include_str!("../release/edgee.v1.3.0.js"),
        "1.3.1" => include_str!("../release/edgee.v1.3.1.js"),
        // Add more versions as needed
        _ => return Err("Failed to read the JS SDK file"),
    };

    Ok(dynamize_sdk(content, host))
}

fn dynamize_sdk(sdk: &str, host: &str) -> String {
    let new_path = edgee_path::generate(host);
    sdk.replace("/_edgee/event", new_path.as_str())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn retrieves_sdk_content_for_valid_url() {
        let url = "/sdk.js";
        let host = "example.com";
        let result = get_sdk_from_url(url, host);
        assert!(result.is_ok());
        assert!(result.unwrap().contains("/_edgee/side"));
    }

    #[test]
    fn retrieves_versioned_sdk_content() {
        let url = "/edgee.v1.2.0.js";
        let host = "example.com";
        let result = get_sdk_from_url(url, host);
        assert!(result.is_ok());
        assert!(result.unwrap().contains("/_edgee/side"));
    }

    #[test]
    fn returns_error_for_invalid_url_format() {
        let url = "/invalid.js";
        let host = "example.com";
        let result = get_sdk_from_url(url, host);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), "Failed to read the JS SDK file");
    }

    #[test]
    fn returns_error_for_unsupported_version() {
        let url = "/edgee.v2.0.0.js";
        let host = "example.com";
        let result = get_sdk_from_url(url, host);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), "Failed to read the JS SDK file");
    }
}