Lint, ES6, popover for codegen

This commit is contained in:
Liyas Thomas
2020-10-19 18:02:42 +05:30
parent 59974edf80
commit eaeefafe39
12 changed files with 97 additions and 80 deletions

View File

@@ -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("")