nab 0.8.2

Token-optimized HTTP client for LLMs — fetches any URL as clean markdown
Documentation
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Static API Endpoint Discovery
//!
//! Extracts API endpoints from JavaScript bundles without execution.
//! Uses regex patterns to find `fetch()`, axios, GraphQL, and other HTTP calls.
//!
//! ## Strategy
//!
//! 1. **Fast Path**: Extract and try endpoints directly (~50ms)
//! 2. **Fallback**: If no data, fall back to JavaScript execution (~200ms)
//!
//! ## Patterns Detected
//!
//! - `fetch("/api/users")` - Direct fetch calls
//! - `axios.get("/api/data")` - Axios HTTP client
//! - `$.ajax({url: "/api/..."})` - jQuery AJAX
//! - `new XMLHttpRequest()` with `.open("GET", "/api/...")`
//! - `baseURL: "https://api.example.com"` - API base configuration
//! - GraphQL endpoints: `/graphql`, `/__graphql`

use anyhow::Result;
use regex::Regex;
use std::collections::HashSet;

/// Discovered API endpoint
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ApiEndpoint {
    /// URL path (may be relative like "/api/users" or absolute)
    pub url: String,
    /// HTTP method if detected (GET, POST, etc.)
    pub method: Option<String>,
    /// Source pattern that found this (for debugging)
    pub source: String,
}

/// API endpoint discovery engine
pub struct ApiDiscovery {
    /// Regex patterns for finding endpoints
    patterns: Vec<EndpointPattern>,
    /// Optional user-provided URL fragments to prioritize or discover.
    url_hints: Vec<String>,
}

struct EndpointPattern {
    name: String,
    regex: Regex,
    url_group: usize,            // Which capture group contains the URL
    method_group: Option<usize>, // Which capture group contains the method (if any)
}

impl ApiDiscovery {
    /// Create a new API discovery engine with built-in patterns
    pub fn new() -> Result<Self> {
        Self::with_url_hints(&[])
    }

    /// Create a discovery engine with additional user-provided URL hints.
    pub fn with_url_hints(url_hints: &[String]) -> Result<Self> {
        let mut patterns = default_patterns()?;

        let mut normalized_hints = Vec::new();
        for hint in url_hints {
            let trimmed = hint.trim();
            if trimmed.is_empty() {
                continue;
            }

            normalized_hints.push(trimmed.to_string());
            patterns.push(EndpointPattern {
                name: format!("custom_hint:{trimmed}"),
                regex: Regex::new(&format!(
                    r#"["'`]([^"'`]*{}[^"'`]*)["'`]"#,
                    regex::escape(trimmed)
                ))?,
                url_group: 1,
                method_group: None,
            });
        }

        Ok(Self {
            patterns,
            url_hints: normalized_hints,
        })
    }

    /// Discover API endpoints from JavaScript code
    #[must_use]
    pub fn discover(&self, js_code: &str) -> Vec<ApiEndpoint> {
        let mut endpoints = Vec::new();
        let mut seen = HashSet::new();

        for pattern in &self.patterns {
            for cap in pattern.regex.captures_iter(js_code) {
                if let Some(url_match) = cap.get(pattern.url_group) {
                    let url = url_match.as_str().to_string();

                    // Skip template literals with variables (can't resolve statically)
                    if url.contains("${") {
                        continue;
                    }

                    // Skip very short URLs (likely false positives)
                    if url.len() < 4 {
                        continue;
                    }

                    if pattern.name.starts_with("custom_hint:")
                        && !url.starts_with('/')
                        && !url.starts_with("http://")
                        && !url.starts_with("https://")
                    {
                        continue;
                    }

                    let method = pattern
                        .method_group
                        .and_then(|group| cap.get(group))
                        .map(|m| m.as_str().to_uppercase());

                    if seen.insert((url.clone(), method.clone())) {
                        endpoints.push(ApiEndpoint {
                            url,
                            method,
                            source: pattern.name.clone(),
                        });
                    }
                }
            }
        }

        endpoints.sort_by(|a, b| a.url.cmp(&b.url));
        endpoints
    }

    /// Discover endpoints from HTML (extracts inline scripts and external script URLs)
    #[must_use]
    pub fn discover_from_html(&self, html: &str) -> Vec<ApiEndpoint> {
        use scraper::{Html, Selector};

        let mut all_endpoints = Vec::new();
        let document = Html::parse_document(html);

        // Extract inline scripts
        if let Ok(script_selector) = Selector::parse("script") {
            for script in document.select(&script_selector) {
                // Skip external scripts (we can't fetch them... yet)
                if script.value().attr("src").is_some() {
                    continue;
                }

                let script_content = script.text().collect::<String>();
                let endpoints = self.discover(&script_content);
                all_endpoints.extend(endpoints);
            }
        }

        all_endpoints
    }

    /// Score an endpoint for likelihood of containing useful data
    /// Higher score = more likely to be useful
    #[must_use]
    pub fn score_endpoint(endpoint: &ApiEndpoint) -> i32 {
        let mut score = 0;

        // Prefer GET requests (more likely to return data)
        if let Some(ref method) = endpoint.method {
            if method == "GET" {
                score += 10;
            }
        } else {
            // No method specified, might be GET
            score += 5;
        }

        // Prefer /api/ paths
        if endpoint.url.contains("/api/") {
            score += 20;
        }

        // Prefer GraphQL
        if endpoint.url.contains("graphql") {
            score += 15;
        }

        // Prefer data-related keywords
        for keyword in &["data", "list", "get", "fetch", "load", "users", "items"] {
            if endpoint.url.to_lowercase().contains(keyword) {
                score += 5;
            }
        }

        // Penalize very long URLs (likely to be specific queries)
        if endpoint.url.len() > 100 {
            score -= 10;
        }

        // Penalize URLs with query params (might be incomplete)
        if endpoint.url.contains('?') && !endpoint.url.contains('=') {
            score -= 5;
        }

        if endpoint.source.starts_with("custom_hint:") {
            score += 25;
        }

        score
    }

    /// Return true when the endpoint matches a user-provided URL hint.
    #[must_use]
    pub fn matches_hint(&self, endpoint: &ApiEndpoint) -> bool {
        let endpoint_url = endpoint.url.to_lowercase();
        self.url_hints
            .iter()
            .any(|hint| endpoint_url.contains(&hint.to_lowercase()))
    }
}

fn default_patterns() -> Result<Vec<EndpointPattern>> {
    Ok(vec![
        // fetch() calls: fetch("/api/users"), fetch(`/api/${id}`)
        EndpointPattern {
            name: "fetch".to_string(),
            regex: Regex::new(r#"fetch\s*\(\s*["'`]([^"'`]+)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // fetch with method: fetch("/api/users", {method: "POST"})
        EndpointPattern {
            name: "fetch_with_method".to_string(),
            regex: Regex::new(
                r#"fetch\s*\(\s*["'`]([^"'`]+)["'`]\s*,\s*\{[^}]*method:\s*["'](\w+)["']"#,
            )?,
            url_group: 1,
            method_group: Some(2),
        },
        // axios: axios.get("/api/users"), axios.post("/api/users")
        EndpointPattern {
            name: "axios_method".to_string(),
            regex: Regex::new(r#"axios\.(\w+)\s*\(\s*["'`]([^"'`]+)["'`]"#)?,
            url_group: 2,
            method_group: Some(1),
        },
        // axios: axios({url: "/api/users", method: "GET"})
        EndpointPattern {
            name: "axios_config".to_string(),
            regex: Regex::new(
                r#"axios\s*\(\s*\{[^}]*url:\s*["'`]([^"'`]+)["'`][^}]*method:\s*["'](\w+)["']"#,
            )?,
            url_group: 1,
            method_group: Some(2),
        },
        // XMLHttpRequest: xhr.open("GET", "/api/users")
        EndpointPattern {
            name: "xhr_open".to_string(),
            regex: Regex::new(r#"\.open\s*\(\s*["'](\w+)["']\s*,\s*["'`]([^"'`]+)["'`]"#)?,
            url_group: 2,
            method_group: Some(1),
        },
        // jQuery AJAX: $.ajax({url: "/api/users", type: "GET"})
        EndpointPattern {
            name: "jquery_ajax".to_string(),
            regex: Regex::new(
                r#"\$\.ajax\s*\(\s*\{[^}]*url:\s*["'`]([^"'`]+)["'`][^}]*type:\s*["'](\w+)["']"#,
            )?,
            url_group: 1,
            method_group: Some(2),
        },
        // GraphQL: Common GraphQL endpoint patterns
        EndpointPattern {
            name: "graphql_endpoint".to_string(),
            regex: Regex::new(r#"["'`](/graphql|/__graphql|/api/graphql)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // Base URL configuration: baseURL: "https://api.example.com"
        EndpointPattern {
            name: "base_url".to_string(),
            regex: Regex::new(r#"baseURL:\s*["'`](https?://[^"'`]+)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // API endpoint in string: const API_URL = "/api/users"
        EndpointPattern {
            name: "api_constant".to_string(),
            regex: Regex::new(r#"(?:API_URL|ENDPOINT|API_ENDPOINT)\s*=\s*["'`]([^"'`]+)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // Google batchexecute: "/_/FlightSearch/data/batchexecute"
        EndpointPattern {
            name: "google_batchexecute".to_string(),
            regex: Regex::new(r#"["'`](/_/[A-Za-z]+/data/batchexecute)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // Google RPC-style: "https://www.google.com/_/..." or "/_/..."
        EndpointPattern {
            name: "google_rpc".to_string(),
            regex: Regex::new(r#"["'`]((?:https?://[^"'`]+)?/_/[A-Za-z]+[^"'`]*)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // Google Travel/Flights API patterns
        EndpointPattern {
            name: "google_travel".to_string(),
            regex: Regex::new(r#"["'`](/travel/[a-z]+/(?:search|offers|booking)[^"'`]*)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // gRPC-Web endpoints
        EndpointPattern {
            name: "grpc_web".to_string(),
            regex: Regex::new(r#"["'`]([^"'`]+\.grpc\.web[^"'`]*)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
        // Internal data endpoints: /data/, /api/v*, /_ah/
        EndpointPattern {
            name: "internal_data".to_string(),
            regex: Regex::new(r#"["'`](/(?:data|_ah|api/v\d+)/[^"'`]+)["'`]"#)?,
            url_group: 1,
            method_group: None,
        },
    ])
}

impl Default for ApiDiscovery {
    fn default() -> Self {
        Self::new().expect("Failed to create API discovery engine")
    }
}

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

    #[test]
    fn test_fetch_detection() {
        let discovery = ApiDiscovery::new().unwrap();
        let code = r#"
            fetch("/api/users");
            fetch('/api/posts');
            fetch(`/api/comments`);
        "#;

        let endpoints = discovery.discover(code);
        assert_eq!(endpoints.len(), 3);
        assert!(endpoints.iter().any(|e| e.url == "/api/users"));
        assert!(endpoints.iter().any(|e| e.url == "/api/posts"));
        assert!(endpoints.iter().any(|e| e.url == "/api/comments"));
    }

    #[test]
    fn test_axios_detection() {
        let discovery = ApiDiscovery::new().unwrap();
        let code = r#"
            axios.get("/api/users");
            axios.post("/api/users", data);
            axios({url: "/api/settings", method: "GET"});
        "#;

        let endpoints = discovery.discover(code);
        assert!(
            endpoints
                .iter()
                .any(|e| e.url == "/api/users" && e.method == Some("GET".to_string()))
        );
        assert!(
            endpoints
                .iter()
                .any(|e| e.url == "/api/users" && e.method == Some("POST".to_string()))
        );
        assert!(
            endpoints
                .iter()
                .any(|e| e.url == "/api/settings" && e.method == Some("GET".to_string()))
        );
    }

    #[test]
    fn test_skip_template_literals() {
        let discovery = ApiDiscovery::new().unwrap();
        let code = r#"
            fetch(`/api/users/${userId}`);  // Should be skipped
            fetch("/api/users");             // Should be found
        "#;

        let endpoints = discovery.discover(code);
        assert_eq!(endpoints.len(), 1);
        assert_eq!(endpoints[0].url, "/api/users");
    }

    #[test]
    fn test_endpoint_scoring() {
        let ep1 = ApiEndpoint {
            url: "/api/data".to_string(),
            method: Some("GET".to_string()),
            source: "fetch".to_string(),
        };

        let ep2 = ApiEndpoint {
            url: "/some/path".to_string(),
            method: Some("POST".to_string()),
            source: "axios".to_string(),
        };

        assert!(ApiDiscovery::score_endpoint(&ep1) > ApiDiscovery::score_endpoint(&ep2));
    }

    #[test]
    fn test_custom_url_hints_find_matching_strings() {
        let discovery = ApiDiscovery::with_url_hints(&["/custom/data".to_string()]).unwrap();
        let code = r#"
            const endpoint = "/custom/data/items";
            const ignored = "not-an-endpoint";
        "#;

        let endpoints = discovery.discover(code);
        assert!(endpoints.iter().any(|e| e.url == "/custom/data/items"));
        assert!(
            endpoints
                .iter()
                .any(|e| e.source == "custom_hint:/custom/data")
        );
    }

    #[test]
    fn test_custom_hints_do_not_duplicate_existing_discoveries() {
        let discovery = ApiDiscovery::with_url_hints(&["/api/users".to_string()]).unwrap();
        let code = r#"fetch("/api/users");"#;

        let endpoints = discovery.discover(code);
        let users_matches = endpoints.iter().filter(|e| e.url == "/api/users").count();

        assert_eq!(users_matches, 1);
    }
}