22
33Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
44
5+ function isCurlFileBody ( body ) {
6+ return typeof body === "object" && body !== null && "type" in body && body . type === "file" && "fileName" in body ;
7+ }
8+ function fileBodyToString ( body ) {
9+ return "@" + body . fileName ;
10+ }
11+ function fileBodyToCommand ( body ) {
12+ return "--data-binary " + fileBodyToString ( body ) ;
13+ }
14+
15+ function isCurlRawBody ( body ) {
16+ return typeof body === "object" && body !== null && "type" in body && body . type === "raw" && "content" in body ;
17+ }
18+ function rawBodyToString ( body ) {
19+ return body . content ;
20+ }
21+ function rawBodyToCommand ( body ) {
22+ return "-d \"" + rawBodyToString ( body ) + "\"" ;
23+ }
24+
25+ function isCurlJsonBody ( body ) {
26+ return typeof body === "object" && body !== null && "type" in body && body . type === "json" && "content" in body ;
27+ }
28+ function jsonContentToString ( content ) {
29+ return JSON . stringify ( content ) . replace ( / ( [ \\ " ] ) / g, "\\$1" ) ;
30+ }
31+ function jsonBodyToString ( body ) {
32+ return jsonContentToString ( body . content ) ;
33+ }
34+ function jsonBodyToCommand ( body ) {
35+ return "-d \"" + jsonBodyToString ( body ) + "\"" ;
36+ }
37+
38+ function isCurlFormBody ( body ) {
39+ return typeof body === "object" && body !== null && "type" in body && body . type === "form" && "content" in body ;
40+ }
41+ function formBodyToCommand ( body ) {
42+ if ( body . content instanceof URLSearchParams ) {
43+ return "-d \"" + body . content . toString ( ) + "\"" ;
44+ }
45+ return Object . entries ( body . content )
46+ . map ( function ( _a ) {
47+ var key = _a [ 0 ] , value = _a [ 1 ] ;
48+ if ( typeof value === "string" ) {
49+ return "-F " + key + "=" + value ;
50+ }
51+ if ( value . type === "file" ) {
52+ return "-F " + key + "=@" + value . fileName ;
53+ }
54+ if ( value . type === "raw" ) {
55+ return "-F " + key + "=" + value . content ;
56+ }
57+ throw new Error ( "Invalid form body value type: " + value ) ;
58+ } )
59+ . join ( " \\\n " ) ;
60+ }
61+
62+ function bodyToCommand ( body ) {
63+ if ( typeof body === "string" ) {
64+ return "-d \"" + body + "\"" ;
65+ }
66+ else if ( body instanceof URLSearchParams ) {
67+ return "-d \"" + body . toString ( ) + "\"" ;
68+ }
69+ else if ( isCurlFileBody ( body ) ) {
70+ return fileBodyToCommand ( body ) ;
71+ }
72+ else if ( isCurlRawBody ( body ) ) {
73+ return rawBodyToCommand ( body ) ;
74+ }
75+ else if ( isCurlJsonBody ( body ) ) {
76+ return jsonBodyToCommand ( body ) ;
77+ }
78+ else if ( isCurlFormBody ( body ) ) {
79+ return formBodyToCommand ( body ) ;
80+ }
81+ else if ( typeof body === "object" ) {
82+ return "-d \"" + jsonContentToString ( body ) + "\"" ;
83+ }
84+ throw new Error ( "Invalid body type: " + body ) ;
85+ }
86+
587// slash for connecting previous breakup line to current line for running cURL directly in Command Prompt
688var slash = " \\" ;
789var newLine = "\n" ;
@@ -31,19 +113,19 @@ var getCurlHeaders = function (headers) {
31113 var result = "" ;
32114 if ( headers ) {
33115 Object . keys ( headers ) . map ( function ( val ) {
34- result += "" + slash + newLine + "-H \"" + val + ": " + headers [ val ] . replace ( / ( \\ | " ) / g, "\\$1" ) + "\"" ;
116+ result += "" + slash + newLine + " -H \"" + val + ": " + headers [ val ] . replace ( / ( \\ | " ) / g, "\\$1" ) + "\"" ;
35117 } ) ;
36118 }
37119 return result ;
38120} ;
39121/**
40- * @param {Object } body
122+ * @param {CurlBody } body
41123 * @returns {string }
42124 */
43125var getCurlBody = function ( body ) {
44126 var result = "" ;
45127 if ( body ) {
46- result += "" + slash + newLine + "-d \"" + JSON . stringify ( body ) . replace ( / ( \\ | " ) / g , "\\$1" ) + "\"" ;
128+ result += "" + slash + newLine + " " + bodyToCommand ( body ) ;
47129 }
48130 return result ;
49131} ;
@@ -62,11 +144,11 @@ var getCurlOptions = function (options) {
62144 }
63145 else if ( typeof options [ key ] === "boolean" && options [ key ] ) {
64146 // boolean option, we just add --opt
65- result += "--" + kebabKey + " " ;
147+ result += " --" + kebabKey ;
66148 }
67149 else if ( typeof options [ key ] === "string" ) {
68150 // string option, we have to add --opt=value
69- result += "--" + kebabKey + " " + options [ key ] + " " ;
151+ result += " --" + kebabKey + " " + options [ key ] ;
70152 }
71153 } ) ;
72154 }
0 commit comments