feat(http): enhance scope config to accept string as well (#1025)

This commit is contained in:
Amr Bashir
2024-03-07 00:10:45 -03:00
committed by GitHub
parent cacf544d51
commit 0f67dfc9b4
4 changed files with 99 additions and 41 deletions
+29 -6
View File
@@ -9,8 +9,10 @@ mod scope;
const COMMANDS: &[&str] = &["fetch", "fetch_cancel", "fetch_send", "fetch_read_body"];
/// HTTP scope entry object definition.
#[allow(unused)]
#[derive(schemars::JsonSchema)]
struct ScopeEntry {
#[serde(untagged)]
enum ScopeEntry {
/// A URL that can be accessed by the webview when using the HTTP APIs.
/// Wildcards can be used following the URL pattern standard.
///
@@ -25,18 +27,39 @@ struct ScopeEntry {
/// - "https://*.github.com/tauri-apps/tauri": allows any subdomain of "github.com" with the "tauri-apps/api" path
///
/// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/"
url: String,
Value(String),
Object {
/// A URL that can be accessed by the webview when using the HTTP APIs.
/// Wildcards can be used following the URL pattern standard.
///
/// See [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information.
///
/// Examples:
///
/// - "https://*" : allows all HTTPS origin on port 443
///
/// - "https://*:*" : allows all HTTPS origin on any port
///
/// - "https://*.github.com/tauri-apps/tauri": allows any subdomain of "github.com" with the "tauri-apps/api" path
///
/// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/"
url: String,
},
}
// ensure scope entry is up to date
impl From<ScopeEntry> for scope::Entry {
fn from(value: ScopeEntry) -> Self {
let url = match value {
ScopeEntry::Value(url) => url,
ScopeEntry::Object { url } => url,
};
scope::Entry {
url: urlpattern::UrlPattern::parse(
urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(
&value.url, None,
)
.unwrap(),
urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(&url, None)
.unwrap(),
)
.unwrap(),
}