feat:add code generator for Shell with HTTPie (#1290)
Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
@@ -1080,6 +1080,19 @@ try {
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`generate request for Shell HTTPie generate GET request 1`] = `"http -a mockUser:mockPassword GET $'https://httpbin.org/path/to?a=b' $'h1:h1v' $'h2:h2v'"`;
|
||||||
|
|
||||||
|
exports[`generate request for Shell HTTPie generate POST request for JSON 1`] = `"echo -n $'{\\"foo\\": \\"bar\\", \\"baz\\": \\"qux\\"}' | http POST $'https://httpbin.org/path/to?a=b' 'Content-Type:application/json; charset=utf-8' $'h1:h1v' $'h2:h2v' 'Authorization:Bearer abcdefghijklmn'"`;
|
||||||
|
|
||||||
|
exports[`generate request for Shell HTTPie generate POST request for XML 1`] = `
|
||||||
|
"echo -n $'<?xml version=\\\\'1.0\\\\' encoding=\\\\'utf-8\\\\'?>
|
||||||
|
<xml>
|
||||||
|
<element foo=\\"bar\\"></element>
|
||||||
|
</xml>' | http POST $'https://httpbin.org/path/to?a=b' 'Content-Type:application/xml; charset=utf-8' $'h1:h1v' $'h2:h2v' 'Authorization:Bearer abcdefghijklmn'"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`generate request for Shell HTTPie generate PUT request for www-form-urlencoded 1`] = `"echo -n $'foo=bar&baz=qux' | http PUT $'https://httpbin.org/path/to?a=b' 'Content-Type:application/x-www-form-urlencoded; charset=utf-8'"`;
|
||||||
|
|
||||||
exports[`generate request for cURL generate GET request 1`] = `
|
exports[`generate request for cURL generate GET request 1`] = `
|
||||||
"curl -X GET \\\\
|
"curl -X GET \\\\
|
||||||
'https://httpbin.org/path/to?a=b' \\\\
|
'https://httpbin.org/path/to?a=b' \\\\
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { WgetCodegen } from "./generators/wget"
|
|||||||
import { CsRestSharpCodegen } from "./generators/cs-restsharp"
|
import { CsRestSharpCodegen } from "./generators/cs-restsharp"
|
||||||
import { RubyNetHttpCodeGen } from "./generators/ruby-net-http"
|
import { RubyNetHttpCodeGen } from "./generators/ruby-net-http"
|
||||||
import { SalesforceApexCodegen } from "./generators/salesforce-apex"
|
import { SalesforceApexCodegen } from "./generators/salesforce-apex"
|
||||||
|
import { ShellHTTPie } from "./generators/shell-httpie"
|
||||||
|
|
||||||
/* Register code generators here.
|
/* Register code generators here.
|
||||||
* A code generator is defined as an object with the following structure.
|
* A code generator is defined as an object with the following structure.
|
||||||
@@ -39,6 +40,7 @@ export const codegens = [
|
|||||||
PythonHttpClientCodegen,
|
PythonHttpClientCodegen,
|
||||||
RubyNetHttpCodeGen,
|
RubyNetHttpCodeGen,
|
||||||
SalesforceApexCodegen,
|
SalesforceApexCodegen,
|
||||||
|
ShellHTTPie,
|
||||||
WgetCodegen,
|
WgetCodegen,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
60
helpers/codegen/generators/shell-httpie.js
Normal file
60
helpers/codegen/generators/shell-httpie.js
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
export const ShellHTTPie = {
|
||||||
|
id: "shell-httpie",
|
||||||
|
name: "Shell HTTPie",
|
||||||
|
generator: ({
|
||||||
|
url,
|
||||||
|
pathName,
|
||||||
|
queryString,
|
||||||
|
auth,
|
||||||
|
httpUser,
|
||||||
|
httpPassword,
|
||||||
|
bearerToken,
|
||||||
|
method,
|
||||||
|
rawInput,
|
||||||
|
rawParams,
|
||||||
|
rawRequestBody,
|
||||||
|
contentType,
|
||||||
|
headers,
|
||||||
|
}) => {
|
||||||
|
const methodsWithBody = ["POST", "PUT", "PATCH", "DELETE"]
|
||||||
|
const includeBody = methodsWithBody.indexOf(method) >= 0
|
||||||
|
const requestString = []
|
||||||
|
|
||||||
|
let requestBody = rawInput ? rawParams : rawRequestBody
|
||||||
|
requestBody = requestBody.replace(/'/g, "\\'")
|
||||||
|
if (requestBody.length !== 0 && includeBody) {
|
||||||
|
// Send request body via redirected input
|
||||||
|
requestString.push(`echo -n $'${requestBody}' | `)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Executable itself
|
||||||
|
requestString.push(`http`)
|
||||||
|
|
||||||
|
// basic authentication
|
||||||
|
if (auth === "Basic Auth") {
|
||||||
|
requestString.push(` -a ${httpUser}:${httpPassword}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL
|
||||||
|
let escapedUrl = `${url}${pathName}${queryString}`
|
||||||
|
escapedUrl = escapedUrl.replace(/'/g, "\\'")
|
||||||
|
requestString.push(` ${method} $'${escapedUrl}'`)
|
||||||
|
|
||||||
|
// All headers
|
||||||
|
if (contentType) {
|
||||||
|
requestString.push(` 'Content-Type:${contentType}; charset=utf-8'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (headers) {
|
||||||
|
headers.forEach(({ key, value }) => {
|
||||||
|
requestString.push(` $'${key.replace(/'/g, "\\'")}:${value.replace(/'/g, "\\'")}'`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auth === "Bearer Token" || auth === "OAuth 2.0") {
|
||||||
|
requestString.push(` 'Authorization:Bearer ${bearerToken}'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestString.join("")
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user