Last active
June 25, 2022 00:14
-
-
Save mark05e/2d03579a3ed3eecf5ce49eef0998f6fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// postman-nice-codegen.js | |
// Mark05E | |
var log = false | |
var version = 0.6 | |
var prefix = 'REQUEST' | |
var pm_request | |
var insidePostman = false | |
var UnsupportedFeatureFoundflag = false | |
var UnsupportedFeatureFoundLocation | |
var sampleCode = '' | |
var finalProtocol, finalHost, finalPath, finalQuery, finalBody, finalHeader, finalAuth, finalMethod | |
// Check if we are inside postman | |
if (typeof pm !== 'undefined') { | |
insidePostman = true | |
loadDataFromPostman() | |
} else { | |
console.clear() // does not work inside postman | |
loadDataFromSample(pm_request) | |
} | |
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZoneName: 'short', hour: 'numeric', minute: 'numeric', second: 'numeric' }; | |
var startTimeStamp = new Date().toLocaleString('en-US', options) | |
// displayDataFromMemory() | |
buildHeader() | |
buildCode() | |
buildFooter() | |
printSampleCode() | |
function loadDataFromSample(request){ | |
console.log('loadDataFromSample:') | |
// if (request.includes('\\')) {request.replaceAll('\\','\\\\\')} | |
request = JSON.parse(request) | |
if (log) {console.log({request})} | |
// request.url | |
// url.protocol - string | |
// console.log(request.url.protocol) | |
finalProtocol = request.url.protocol | |
// method - string | |
finalMethod = request.method | |
// url.host (combine) - array -> string | |
// console.log(request.url.host) | |
finalHost = "" | |
let host = request.url.host | |
host.forEach((h,i) => { | |
if (i < host.length - 1) { finalHost += h.concat(".") } | |
else { finalHost += h } | |
}); | |
// if (log) { console.log({finalHost}) } | |
// url.path (combine) - array -> string | |
// console.log(request.url.path) | |
finalPath = request.url.path | |
// let path = request.url.path | |
// path.forEach((p,i) => { | |
// if(i < path.length - 1) { finalPath += p.concat("/") } | |
// else { finalPath += p } | |
// }); | |
// if (log) { console.log({finalPath})} | |
// url.query (combine) - JsonObject -> string | |
// console.log(request.url.query) | |
if (request.url.query && request.url.query.length > 0) { | |
finalQuery = '' | |
let query = request.url.query | |
query.forEach((q,i) => { | |
finalQuery += q.key | |
finalQuery += '=' | |
finalQuery += q.value | |
if (i < query.length - 1) {finalQuery += '&'} | |
}) | |
finalQuery = finalQuery.split('&') | |
// console.log({finalQuery}) | |
} | |
// request.header - array | |
// console.log(request.header) | |
finalHeader = request.header | |
// console.log({finalHeader}) | |
// request.auth - JsonObject | |
// console.log(request.auth) | |
finalAuth = request.auth | |
// console.log(finalAuth) | |
// request.body - JsonObject | |
// console.log(request.body) | |
finalBody = request.body | |
// console.log({finalBody}) | |
} | |
function loadDataFromPostman(){ | |
try {finalProtocol = pm.request.url.protocol.toString()} | |
catch(e) {console.log(e); finalProtocol = ''} | |
finalHost = pm.request.url.host.join('.') | |
finalPath = pm.request.url.path // array | |
finalQuery = pm.request.url.query.toString().split('&') // array | |
finalHeader = pm.request.headers // object | |
finalMethod = pm.request.method // string | |
try { finalAuth = pm.request.auth.toJSON() // json | |
} catch(e){ console.log(e); finalAuth = '' } | |
finalBody = pm.request.body | |
} | |
function displayDataFromMemory(){ | |
if (log) {console.log({insidePostman})} | |
if (log) {console.log({finalProtocol}, typeof(finalProtocol), '// string')} | |
if (log) {console.log({finalMethod}, typeof(finalMethod), '// string')} | |
if (log) {console.log({finalHost}, typeof(finalHost), '// string')} | |
if (log) {console.log({finalPath}, typeof(finalPath), '// array?')} | |
if (log) {console.log({finalQuery}, typeof(finalQuery), '// array?')} | |
if (log) {console.log({finalHeader}, typeof(finalHeader), '// object?')} | |
if (log) {console.log({finalAuth}, typeof(finalAuth), '// jsonObject?')} | |
if (log) {console.log({finalBody}, typeof(finalBody), '// object?')} | |
} | |
function buildCode(){ | |
// sampleCode = '' | |
// start | |
sampleCode += 'ASSIGN ' + prefix + ' = GetRestProxy()\n' | |
// url | |
sampleCode += '\n// SECTION: URL\n' | |
// -- base | |
let urlBase = prefix + '_URL_BASE' | |
if (finalProtocol && finalHost) { | |
sampleCode += 'ASSIGN ' + urlBase + ' = "' + finalProtocol + '://' + finalHost + '"\n' | |
} else if (finalHost) { sampleCode += 'ASSIGN ' + urlBase + ' = "' + finalHost + '"\n' } | |
// -- path | |
let urlPath = prefix + '_URL_PATH' | |
if (finalPath && finalPath.length > 0) { | |
sampleCode += 'ASSIGN ' + urlPath + ' = "' | |
finalPath.forEach((p,i) => { | |
if (i < finalPath.length - 1 ) { sampleCode += p + '/' } | |
else { sampleCode += p } | |
}) | |
sampleCode += '"\n' | |
} | |
// -- method | |
let urlMethod = prefix + '_URL_METHOD' | |
if (finalProtocol) { | |
sampleCode += 'ASSIGN ' + urlMethod + ' = "' + finalMethod + '"\n' | |
} | |
// -- query | |
// TODO: Rewrite this to add appending multiple lines | |
let urlQuery = prefix + '_URL_QUERY' | |
if (finalQuery && !(Object.keys(finalQuery).length === 0)) { | |
//xx | |
sampleCode += 'ASSIGN ' + urlQuery + ' = $"' | |
finalQuery.forEach((q,i) => { | |
if (i < finalQuery.length - 1 ) { | |
// fix single quote | |
if (q.indexOf('\'') >= 0) { q=q.replaceAll('\'','\\\'') } | |
// TODO: Do I need to fix double quotes and others? | |
sampleCode += q + '&' | |
} | |
else { sampleCode += q } | |
}) | |
sampleCode += '"\n' | |
} | |
// -- final url combine code | |
sampleCode += 'ASSIGN ' + prefix + '_URL = "{' + urlBase + '}/{' + urlPath + '}' | |
if (finalQuery && !(Object.keys(finalQuery).length === 0)) { | |
sampleCode += '?{' + urlQuery + '}' | |
} | |
sampleCode += '"\n' | |
// header | |
// console.log({finalHeader}, 'xx') | |
if (finalHeader && finalHeader.length > 0) { | |
sampleCode += '\n// SECTION: HEADER\n' | |
finalHeader.forEach((h,i) => { | |
// if () | |
// console.log(h) | |
if (h.key.includes('Content-Type')) { | |
sampleCode += prefix + '.ContentType = "' + h.value + '"\n' | |
} | |
else { | |
sampleCode += prefix + '.AddHeader("' + h.key + '","' + h.value + '")\n' | |
} | |
}) | |
} | |
// auth | |
// console.log({finalAuth}) | |
if (finalAuth) { | |
sampleCode += '\n// SECTION: AUTH\n' | |
let username, password | |
if (finalAuth.type === 'basic') { | |
finalAuth.basic.forEach((b,i) => { | |
if (b.key === 'password') { password = b.value} | |
if (b.key === 'username') { username = b.value} | |
}) | |
sampleCode += 'ASSIGN ' + prefix + '_USERNAME = "' + username + '"\n' | |
sampleCode += 'ASSIGN ' + prefix + '_PASSWORD = "' + password + '"\n' | |
// GetRESTProxy().EncodeBase64("{username}:{password}") | |
sampleCode += 'ASSIGN ' + prefix + '_BASICAUTHCREDS = GetRESTProxy().EncodeBase64("{' + prefix + '_USERNAME' + '}' | |
sampleCode += ':' | |
sampleCode += '{' + prefix + '_PASSWORD' + '}' | |
sampleCode += '")\n' | |
sampleCode += prefix + '.AddHeader("Authorization","Basic {' + prefix + '_BASICAUTHCREDS}")' + '\n' | |
} else { UnsupportedFeatureFoundflag = true; UnsupportedFeatureFoundLocation = 'AUTH'} | |
} | |
// body | |
if (finalBody && !(Object.keys(finalBody).length === 0)) { | |
console.log(finalBody) | |
sampleCode += '\n// SECTION: BODY\n' | |
if (finalBody.mode === 'raw') { | |
let finalbodyraw = JSON.stringify(finalBody.raw) | |
finalbodyraw = finalbodyraw.replaceAll('\{','\\{') | |
finalbodyraw = finalbodyraw.replaceAll('\,','\\,') | |
sampleCode += 'ASSIGN ' + prefix + '_BODY = $' | |
sampleCode += finalbodyraw | |
} else { UnsupportedFeatureFoundflag = true; UnsupportedFeatureFoundLocation = 'BODY' } | |
sampleCode += '\n' | |
} | |
// trigger request | |
sampleCode += '\n// SECTION: TRIGGER REQUEST\n' | |
// MakeRestRequest | |
sampleCode += 'ASSIGN RESPONSE = ' + prefix + '.MakeRestRequest(' | |
// url | |
sampleCode += prefix + '_URL' + ',' | |
// body | |
if (finalBody) { | |
sampleCode += prefix + '_BODY' | |
} else { | |
sampleCode += '' | |
} | |
sampleCode += ',' | |
// json or xml | |
// TODO: perform checks here | |
sampleCode += '0' + ',' | |
// http method | |
sampleCode += prefix + '_URL_METHOD' | |
sampleCode += ')\n' | |
// handle response | |
sampleCode += '\n// SECTION: HANDLE RESPONSE\n' | |
// Tag Response code and desc | |
sampleCode += 'ASSIGN ' + 'RESPONSE' + '_STATUSCODE = ' + prefix + '.StatusCode\n' | |
sampleCode += 'ASSIGN ' + 'RESPONSE' + '_STATUSDESC = ' + prefix + '.StatusDescription\n' | |
sampleCode += '\n' | |
sampleCode += 'IF (' + 'RESPONSE' + '_STATUSCODE >= 200 & ' + 'RESPONSE' + '_STATUSCODE < 300) {' + '\n' | |
sampleCode += ' // ' + '\n' | |
sampleCode += ' // Handle Sucessfull Response' + '\n' | |
sampleCode += ' // ' + '\n' | |
sampleCode += '}' + '\n' | |
sampleCode += 'ELSE {' + '\n' | |
sampleCode += ' // ' + '\n' | |
sampleCode += ' // Handle UnSucessfull Response' + '\n' | |
sampleCode += ' // ' + '\n' | |
sampleCode += '}' + '\n' | |
} | |
function printPmRequest(insidePostman){ | |
if (insidePostman){ | |
console.log('////////////////// SEND THIS TO MARK! ////////////////////////////') | |
console.log('//--LOCATION: ' + UnsupportedFeatureFoundLocation) | |
console.log('//--COPY START------------------------------------------------------') | |
console.log(JSON.stringify(pm.request)) | |
console.log('//--COPY END--------------------------------------------------------') | |
} | |
else { | |
console.log('//--UNSUPPORTED ENTRY FOUND -----------------------------------') | |
console.log('//--LOCATION: ' + UnsupportedFeatureFoundLocation) | |
} | |
} | |
function printSampleCode(){ | |
if (!UnsupportedFeatureFoundflag) { console.log(sampleCode) } | |
else { printPmRequest(insidePostman) } | |
} | |
function buildHeader(){ | |
let copyStart = '//----------------------------COPY START----------------------------//' + '\n' | |
let hrLine = '//////////////////////////////////////////////////////////////////////' + '\n' | |
let head = '//▂▃▅▇█▓▒░ Mark\'s Nice Sudio Snippet CodeGen 【ツ】 v' + version + ' ░▒▓█▇▅▃▂' + '\n' | |
sampleCode += copyStart + hrLine + head + hrLine | |
} | |
function buildFooter(){ | |
let copyEnd = '//----------------------------COPY END------------------------------//' + '\n' | |
let hrLine = '//////////////////////////////////////////////////////////////////////' + '\n' | |
let msg = '// Generated on: ' + startTimeStamp + '\n' | |
sampleCode += hrLine + msg + hrLine + copyEnd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment