Lint, ES6, popover for codegen
This commit is contained in:
@@ -22,39 +22,43 @@ export const CsRestSharpCodegen = {
|
||||
|
||||
// initial request setup
|
||||
let requestBody = rawInput ? rawParams : rawRequestBody
|
||||
requestBody = requestBody.replace(/"/g,'""'); // escape quotes for C# verbatim string compatibility
|
||||
requestBody = requestBody.replace(/"/g, '""') // escape quotes for C# verbatim string compatibility
|
||||
|
||||
// prepare data
|
||||
let requestDataFormat;
|
||||
let requestContentType;
|
||||
let requestDataFormat
|
||||
let requestContentType
|
||||
|
||||
if(isJSONContentType(contentType)) {
|
||||
requestDataFormat = 'DataFormat.Json'
|
||||
requestContentType = 'text/json'
|
||||
if (isJSONContentType(contentType)) {
|
||||
requestDataFormat = "DataFormat.Json"
|
||||
requestContentType = "text/json"
|
||||
} else {
|
||||
requestDataFormat = 'DataFormat.Xml'
|
||||
requestContentType = 'text/xml'
|
||||
requestDataFormat = "DataFormat.Xml"
|
||||
requestContentType = "text/xml"
|
||||
}
|
||||
|
||||
const verbs = [
|
||||
{ verb: 'GET', csMethod: 'Get' },
|
||||
{ verb: 'POST', csMethod: 'Post' },
|
||||
{ verb: 'PUT', csMethod: 'Put' },
|
||||
{ verb: 'PATCH', csMethod: 'Patch' },
|
||||
{ verb: 'DELETE', csMethod: 'Delete' },
|
||||
{ verb: "GET", csMethod: "Get" },
|
||||
{ verb: "POST", csMethod: "Post" },
|
||||
{ verb: "PUT", csMethod: "Put" },
|
||||
{ verb: "PATCH", csMethod: "Patch" },
|
||||
{ verb: "DELETE", csMethod: "Delete" },
|
||||
]
|
||||
|
||||
// create client and request
|
||||
requestString.push(`var client = new RestClient("${url}");\n\n`)
|
||||
requestString.push(`var request = new RestRequest("${pathName}${queryString}", ${requestDataFormat});\n\n`)
|
||||
|
||||
requestString.push(
|
||||
`var request = new RestRequest("${pathName}${queryString}", ${requestDataFormat});\n\n`
|
||||
)
|
||||
|
||||
// authentification
|
||||
if (auth === "Basic Auth") {
|
||||
requestString.push(`client.Authenticator = new HttpBasicAuthenticator("${httpUser}", "${httpPassword}");\n`)
|
||||
requestString.push(
|
||||
`client.Authenticator = new HttpBasicAuthenticator("${httpUser}", "${httpPassword}");\n`
|
||||
)
|
||||
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
|
||||
requestString.push(`request.AddHeader("Authorization", "Bearer ${bearerToken}");\n`)
|
||||
}
|
||||
|
||||
|
||||
// content type
|
||||
if (contentType) {
|
||||
requestString.push(`request.AddHeader("Content-Type", "${contentType}");\n`)
|
||||
@@ -70,19 +74,23 @@ export const CsRestSharpCodegen = {
|
||||
}
|
||||
|
||||
requestString.push(`\n`)
|
||||
|
||||
|
||||
// set body
|
||||
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
|
||||
requestString.push(`request.AddParameter("${requestContentType}", @"${requestBody}", ParameterType.RequestBody);\n\n`)
|
||||
requestString.push(
|
||||
`request.AddParameter("${requestContentType}", @"${requestBody}", ParameterType.RequestBody);\n\n`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// process
|
||||
const verb = verbs.find(v => v.verb === method)
|
||||
const verb = verbs.find((v) => v.verb === method)
|
||||
requestString.push(`var response = client.${verb.csMethod}(request);\n\n`)
|
||||
|
||||
// analyse result
|
||||
requestString.push(`if (!response.IsSuccessful)\n{\n Console.WriteLine("An error occured " + response.ErrorMessage);\n}\n\n`)
|
||||
|
||||
requestString.push(
|
||||
`if (!response.IsSuccessful)\n{\n Console.WriteLine("An error occured " + response.ErrorMessage);\n}\n\n`
|
||||
)
|
||||
|
||||
requestString.push(`var result = response.Content;\n`)
|
||||
|
||||
return requestString.join("")
|
||||
|
||||
@@ -42,7 +42,7 @@ export const NodeJsRequestCodegen = {
|
||||
reqBodyType = "body"
|
||||
} else if (contentType.includes("x-www-form-urlencoded")) {
|
||||
const formData = []
|
||||
if (requestBody.indexOf("=") > -1) {
|
||||
if (requestBody.includes("=")) {
|
||||
requestBody.split("&").forEach((rq) => {
|
||||
const [key, val] = rq.split("=")
|
||||
formData.push(`"${key}": "${val}"`)
|
||||
|
||||
@@ -56,7 +56,7 @@ export const PhpCurlCodegen = {
|
||||
} else if (isJSONContentType(contentType)) {
|
||||
requestBody = JSON.stringify(requestBody)
|
||||
} else if (contentType.includes("x-www-form-urlencoded")) {
|
||||
if (requestBody.indexOf("=") > -1) {
|
||||
if (requestBody.includes("=")) {
|
||||
requestBody = `"${requestBody}"`
|
||||
} else {
|
||||
const requestObject = JSON.parse(requestBody)
|
||||
|
||||
@@ -18,7 +18,7 @@ export const PowerShellRestMethod = {
|
||||
}) => {
|
||||
const methodsWithBody = ["Put", "Post", "Delete"]
|
||||
const formattedMethod = method[0].toUpperCase() + method.substring(1).toLowerCase()
|
||||
const includeBody = methodsWithBody.indexOf(formattedMethod) >= 0
|
||||
const includeBody = methodsWithBody.includes(formattedMethod)
|
||||
const requestString = []
|
||||
let genHeaders = []
|
||||
let variables = ""
|
||||
|
||||
@@ -72,7 +72,7 @@ export const PythonHttpClientCodegen = {
|
||||
requestString.push(`payload = ${requestBody}\n`)
|
||||
} else if (contentType.includes("x-www-form-urlencoded")) {
|
||||
const formData = []
|
||||
if (requestBody.indexOf("=") > -1) {
|
||||
if (requestBody.includes("=")) {
|
||||
requestBody.split("&").forEach((rq) => {
|
||||
const [key, val] = rq.split("=")
|
||||
formData.push(`('${key}', '${val}')`)
|
||||
|
||||
@@ -66,7 +66,7 @@ export const PythonRequestsCodegen = {
|
||||
requestString.push(`data = ${requestBody}\n`)
|
||||
} else if (contentType.includes("x-www-form-urlencoded")) {
|
||||
const formData = []
|
||||
if (requestBody.indexOf("=") > -1) {
|
||||
if (requestBody.includes("=")) {
|
||||
requestBody.split("&").forEach((rq) => {
|
||||
const [key, val] = rq.split("=")
|
||||
formData.push(`('${key}', '${val}')`)
|
||||
|
||||
@@ -17,7 +17,7 @@ export const ShellHTTPie = {
|
||||
headers,
|
||||
}) => {
|
||||
const methodsWithBody = ["POST", "PUT", "PATCH", "DELETE"]
|
||||
const includeBody = methodsWithBody.indexOf(method) >= 0
|
||||
const includeBody = methodsWithBody.includes(method)
|
||||
const requestString = []
|
||||
|
||||
let requestBody = rawInput ? rawParams : rawRequestBody
|
||||
|
||||
Reference in New Issue
Block a user