mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
- Pre-filter unknown flags before ArgParser; keep positional args. - Tolerate non-request flags: -v/--verbose, --connect-timeout, --retry, --output, --compressed, -i/--include, --globoff. - Auth: support --oauth2-bearer; map to - Authorization only if absent. - Cookies: parse -b/--cookie; accept -c/--cookie-jar (ignored for request). - URL: prefer first http(s) positional when --url missing; quote cleaning. - Data: merge data-urlencode → data-raw → data-binary → data; default POST when body/form present; HEAD remains HEAD. - Forms: parse -F entries; auto-set multipart Content-Type if missing. - Headers: robust -H parsing for multi-colon values. - toCurlString: deterministic order; fix continuation spacing; emit -d right after headers/form; place -k/-L at end. - Utils: normalize backslash-newlines/CRLF; remove stray '+'; shlex split. - Tests: add unknown flags, oauth2-bearer (and non-override), cookie-jar, verbose/timeout/retry/output tolerance, data merging order, HEAD+data, -A user-agent, -b filename. - Docs: add Dartdoc for utils; class docs present.
28 lines
969 B
Dart
28 lines
969 B
Dart
import 'package:shlex/shlex.dart' as shlex;
|
|
|
|
/// Splits a cURL command into tokens suitable for ArgParser.
|
|
///
|
|
/// - Normalizes backslash-newline continuations and CRLF endings.
|
|
/// - Removes stray '+' concatenation artifacts from some shells.
|
|
/// - Uses shlex to respect quoted strings.
|
|
List<String> splitAsCommandLineArgs(String command) {
|
|
// Normalize common shell continuations: backslash + newline
|
|
var normalized = command
|
|
.replaceAll(RegExp(r"\\\s*\r?\n"), ' ')
|
|
.replaceAll('\r', '')
|
|
.trim();
|
|
// Remove stray '+' line concatenation tokens if present in copied shells
|
|
normalized = normalized.replaceAll(RegExp(r"\s\+\s*\n?"), ' ');
|
|
return shlex.split(normalized);
|
|
}
|
|
|
|
/// Removes surrounding quotes from a url/string token.
|
|
String? clean(String? url) {
|
|
return url?.replaceAll('"', '').replaceAll("'", '');
|
|
}
|
|
|
|
/// Provides `firstOrNull` for lists.
|
|
extension FirstOrNull<T> on List<T> {
|
|
T? get firstOrNull => isEmpty ? null : first;
|
|
}
|