From 59974edf80c11211a851d8081d2a739ad18aaf81 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 18 Oct 2020 21:24:07 -0500 Subject: [PATCH] feat:add code generator for Shell with HTTPie (#1290) Co-authored-by: Liyas Thomas --- .../__snapshots__/codegen.spec.js.snap | 13 ++++ helpers/codegen/codegen.js | 2 + helpers/codegen/generators/shell-httpie.js | 60 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 helpers/codegen/generators/shell-httpie.js diff --git a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap index ec7b93a46..6bb4d55b1 100644 --- a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap +++ b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap @@ -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 $' + + +' | 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`] = ` "curl -X GET \\\\ 'https://httpbin.org/path/to?a=b' \\\\ diff --git a/helpers/codegen/codegen.js b/helpers/codegen/codegen.js index a1dfc1882..fce8ae969 100644 --- a/helpers/codegen/codegen.js +++ b/helpers/codegen/codegen.js @@ -14,6 +14,7 @@ import { WgetCodegen } from "./generators/wget" import { CsRestSharpCodegen } from "./generators/cs-restsharp" import { RubyNetHttpCodeGen } from "./generators/ruby-net-http" import { SalesforceApexCodegen } from "./generators/salesforce-apex" +import { ShellHTTPie } from "./generators/shell-httpie" /* Register code generators here. * A code generator is defined as an object with the following structure. @@ -39,6 +40,7 @@ export const codegens = [ PythonHttpClientCodegen, RubyNetHttpCodeGen, SalesforceApexCodegen, + ShellHTTPie, WgetCodegen, ] diff --git a/helpers/codegen/generators/shell-httpie.js b/helpers/codegen/generators/shell-httpie.js new file mode 100644 index 000000000..18cce7113 --- /dev/null +++ b/helpers/codegen/generators/shell-httpie.js @@ -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("") + }, +}