@@ -10,6 +10,39 @@ function copyText(btn) {
1010 setTimeout ( ( ) => btn . innerText = "Copy" , 1500 ) ;
1111}
1212
13+ function buildCurlCommand ( method , url , headers , body , isWindows ) {
14+ function escapeSingleQuote ( str ) {
15+ if ( ! str ) return "" ;
16+ return str . replace ( / ' / g, "'\\''" ) ;
17+ }
18+
19+ function escapeDoubleQuote ( str ) {
20+ if ( ! str ) return "" ;
21+ return str . replace ( / " / g, '\\"' ) . replace ( / % / g, '%%' ) ;
22+ }
23+
24+ const quote = isWindows ? '"' : "'" ;
25+ const escape = isWindows ? escapeDoubleQuote : escapeSingleQuote ;
26+
27+ let curlCmd = `curl -X ${ method . toUpperCase ( ) } ${ quote } ${ escape ( url ) } ${ quote } ` ;
28+
29+ // Process headers
30+ for ( const [ key , value ] of Object . entries ( headers ) ) {
31+ if ( ! key || ! value ) continue ;
32+ const trimmedVal = value . trim ( ) ;
33+ if ( trimmedVal === "[REDACTED]" || trimmedVal === "" ) continue ;
34+
35+ curlCmd += ` -H ${ quote } ${ escape ( key ) } : ${ escape ( value ) } ${ quote } ` ;
36+ }
37+
38+ // Process body (skip if empty or truncated indicator)
39+ if ( body && body . trim ( ) !== "" && body !== "[Body too large]" ) {
40+ curlCmd += ` -d ${ quote } ${ escape ( body ) } ${ quote } ` ;
41+ }
42+
43+ return curlCmd ;
44+ }
45+
1346function copyAsCurl ( btn ) {
1447 const card = btn . closest ( ".trace-card" ) ;
1548 if ( ! card ) return ;
@@ -27,26 +60,10 @@ function copyAsCurl(btn) {
2760
2861 const body = card . dataset . body ;
2962
30- function escapeSingleQuote ( str ) {
31- if ( ! str ) return "" ;
32- return str . replace ( / ' / g, "'\\''" ) ;
33- }
34-
35- let curlCmd = `curl -X ${ method . toUpperCase ( ) } '${ escapeSingleQuote ( url ) } '` ;
36-
37- // Process headers
38- for ( const [ key , value ] of Object . entries ( headers ) ) {
39- if ( ! key || ! value ) continue ;
40- const trimmedVal = value . trim ( ) ;
41- if ( trimmedVal === "[REDACTED]" || trimmedVal === "" ) continue ;
63+ const isWindows = ( navigator . platform && navigator . platform . indexOf ( 'Win' ) !== - 1 ) ||
64+ ( navigator . userAgent && navigator . userAgent . indexOf ( 'Win' ) !== - 1 ) ;
4265
43- curlCmd += ` -H '${ escapeSingleQuote ( key ) } : ${ escapeSingleQuote ( value ) } '` ;
44- }
45-
46- // Process body (skip if empty or truncated indicator)
47- if ( body && body . trim ( ) !== "" && body !== "[Body too large]" ) {
48- curlCmd += ` -d '${ escapeSingleQuote ( body ) } '` ;
49- }
66+ const curlCmd = buildCurlCommand ( method , url , headers , body , isWindows ) ;
5067
5168 navigator . clipboard . writeText ( curlCmd ) ;
5269
0 commit comments