edgee-sdk 1.4.0

The full-stack edge platform for your edge oriented applications
Documentation
/// 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.4.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> {
    let content = get_sdk_content(url)?;
    Ok(dynamize_sdk(content, host))
}

pub fn get_sdk_for_unsafe_user(url: &str) -> Result<String, &'static str> {
    let content = get_sdk_content(url)?;
    Ok(content.replace("/_edgee/unsafe", "y"))
}

fn get_sdk_content(url: &str) -> Result<&str, &'static str> {
    if url.ends_with("sdk.js") {
        return Ok(include_str!("../release/edgee.v1.4.0.js").trim());
    }

    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.3.2" => include_str!("../release/edgee.v1.3.2.js").trim(),
        "1.4.0" => include_str!("../release/edgee.v1.4.0.js").trim(),
        // Add more versions as needed
        _ => return Err("Failed to read the JS SDK file"),
    };

    Ok(content)
}

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.4.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");
    }
}