Fixed body and header issues with app creator

This commit is contained in:
frikky
2020-06-01 10:05:20 +02:00
parent 8cfbb39386
commit 4e10981db5
4 changed files with 109 additions and 89 deletions
+69 -27
View File
@@ -60,7 +60,7 @@ const useStyles = makeStyles({
})
function rewrite(args) {
const rewrite = (args) => {
return args.reduce(function(args, a){
if (0 == a.indexOf('-X')) {
args.push('-X')
@@ -73,14 +73,15 @@ function rewrite(args) {
}, [])
}
function parseField(s) {
const parseField = (s) => {
return s.split(/: (.+)/)
}
function isURL(s) {
const isURL = (s) => {
return /^https?:\/\//.test(s)
}
// Parses CURL to a real request
const parseCurl = (s) => {
//console.log("CURL: ", s)
@@ -230,16 +231,16 @@ const AppCreator = (props) => {
const [currentActionMethod, setCurrentActionMethod] = useState(actionNonBodyRequest[0])
const [currentAction, setCurrentAction] = useState({
"name": "",
"description": "",
"url": "",
"headers": "",
"paths": [],
"queries": [],
"body": "",
"errors": [],
"method": actionNonBodyRequest[0],
});
"name": "",
"description": "",
"url": "",
"headers": "",
"paths": [],
"queries": [],
"body": "",
"errors": [],
"method": actionNonBodyRequest[0],
});
@@ -429,6 +430,16 @@ const AppCreator = (props) => {
} else if (parameter.in === "path") {
// FIXME - parse this to the URL too
newaction.paths.push(parameter.name)
// FIXME: This doesn't follow OpenAPI3 exactly.
// https://swagger.io/docs/specification/describing-request-body/
// https://swagger.io/docs/specification/describing-parameters/
// Need to split the data.
} else if (parameter.in === "body") {
console.log("BODY: ", parameter)
newaction.body = parameter.example
} else if (parameter.in === "header") {
newaction.headers += `${parameter.name}=${parameter.example}\n`
}
}
@@ -579,10 +590,42 @@ const AppCreator = (props) => {
},
}
console.log(data.paths[item.url][item.method.toLowerCase()])
data.paths[item.url][item.method.toLowerCase()].parameters.push(newitem)
}
if (item.headers.length > 0) {
const required = false
const headersSplit = item.headers.split("\n")
for (var key in headersSplit) {
const header = headersSplit[key]
var key = ""
var value = ""
if (header.length > 0 && header.includes("=")) {
const headersplit = header.split("=")
key = headersplit[0]
value = headersplit[1]
} else {
continue
}
if (key.length > 0 && value.length > 0) {
newitem = {
"in": "header",
"name": key,
"multiline": false,
"description": "Header generated by shuffler.io OpenAPI",
"required": false,
"example": value,
"schema": {
"type": "string",
},
}
data.paths[item.url][item.method.toLowerCase()].parameters.push(newitem)
}
}
}
}
if (authenticationOption === "API key") {
@@ -604,6 +647,8 @@ const AppCreator = (props) => {
}
}
console.log("ACTIONS: ", data.paths)
fetch(globalUrl+"/api/v1/verify_openapi", {
method: 'POST',
headers: {
@@ -627,7 +672,11 @@ const AppCreator = (props) => {
alert.error("Failed to verify: ")
} else {
alert.success("Successfully uploaded openapi")
//window.location = "/apps"
if (window.location.pathname.includes("/new")) {
if (responseJson.id !== undefined && responseJson.id !== null) {
window.location = `/apps/edit/${responseJson.id}`
}
}
}
})
.catch(error => {
@@ -891,7 +940,7 @@ const AppCreator = (props) => {
const bodyInfo = actionBodyRequest.includes(currentActionMethod) ?
<div>
Body
Body - used as example in action argument
<TextField
required
style={{flex: "1", marginRight: "15px", backgroundColor: inputColor}}
@@ -922,10 +971,7 @@ const AppCreator = (props) => {
currentAction.queries = urlPathQueries
setUrlPathQueries([])
console.log(actions)
console.log(currentAction.name)
const actionIndex = actions.findIndex(data => data.name === currentAction.name)
console.log(actionIndex)
if (actionIndex < 0) {
actions.push(currentAction)
} else {
@@ -953,9 +999,6 @@ const AppCreator = (props) => {
errormessage.push("All queries must have a value")
}
console.log(urlPathParameters)
// const [urlPathParameters, setUrlPathParameters] = useState([]);
return errormessage
}
@@ -1039,7 +1082,6 @@ const AppCreator = (props) => {
setActionField("paths", paths)
}
console.log("QUERIES: ", queries)
var tmpQueries = []
// No overlapping of names
@@ -1204,7 +1246,7 @@ const AppCreator = (props) => {
}
if (request.header !== undefined && request.header !== null) {
var headers = ""
var headers = []
for (let [key, value] of Object.entries(request.header)) {
headers += key+"="+value+"\n"
}
@@ -1249,7 +1291,7 @@ const AppCreator = (props) => {
addPathQuery()
}}>New query</Button>
<div/>
Headers
Headers - static for the action
<TextField
required
style={{flex: "1", marginRight: "15px", marginTop: "5px", backgroundColor: inputColor}}
@@ -1310,7 +1352,7 @@ const AppCreator = (props) => {
"errors": [],
"method": actionNonBodyRequest[0],
})
setCurrentActionMethod(actionNonBodyRequest[0])
setCurrentActionMethod(actionNonBodyRequest[0])
setActionsModalOpen(true)
}}>New action</Button>
</div>