-
-
Save Mondonno/182e12880d609374cd41ea64d054f225 to your computer and use it in GitHub Desktop.
const checkBodyType = (body) => { | |
if(Array.isArray(body)) return 0; | |
else if(typeof body == "object") return 1; | |
else throw new Error("Unknown type of the body"); | |
} | |
const definePropertySettings = { configurable: true, enumerable: true, writable: true } | |
const deleteEmptyValues = (currentBody) => { | |
if(typeof currentBody === "string") currentBody = JSON.parse(currentBody); | |
let bodyType = checkBodyType(currentBody); | |
let newBody; | |
if(bodyType == 1) newBody = {}; | |
else newBody = []; | |
for(const key in currentBody) { | |
if(!currentBody.hasOwnProperty(key)) continue; | |
if(Array.isArray(currentBody[key])) { | |
if(currentBody[key].length === 0) continue; | |
let foundValue = deleteEmptyValues(currentBody[key]); | |
if(foundValue.length === 0) continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: foundValue | |
}); | |
} | |
else { | |
newBody.push(foundValue); | |
} | |
continue; | |
} else if(typeof currentBody[key] == "object") { | |
let foundValue = deleteEmptyValues(currentBody[key]); | |
let objectEntries = Object.entries(foundValue); | |
if(objectEntries.length === 0) continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: foundValue | |
}); | |
} | |
else newBody.push(foundValue); | |
continue; | |
}else if(typeof currentBody[key] == "string") { | |
if(currentBody[key] === "") continue; | |
else if(currentBody[key].startsWith("{{") && currentBody[key].endsWith("}}")) { | |
let firstAttempt = currentBody[key].substring(2, currentBody[key].length-1); | |
let finalAttempt = firstAttempt.substring(0, firstAttempt.length-1); | |
if(pm.variables.has(finalAttempt)) { | |
let value = pm.variables.get(finalAttempt); | |
if(typeof value == "string" && value.length === 0) continue; | |
} | |
} | |
} | |
else if(typeof currentBody[key] == "undefined") continue; | |
else if(typeof currentBody[key] == "function") continue; | |
else if(typeof currentBody[key] == "symbol") continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: currentBody[key] | |
}); | |
} | |
else newBody.push(currentBody[key]); | |
} | |
return newBody; | |
} |
doesn't go well with "variables" (postman variables: {{foo}} )
@mrassili I will fix this soon, thanks for reporting the bug
Hi, is the bug fixed on postman variables?
@qianqian748 Sorry for big delay, it should be fixed now.
Thank you so much for your reply. However this does not seem to fix my issue. I am a total newbie in coding. I am trying to create Shopify orders through API post request. The code requires every item array is filled with item information: title, quantity and price. leaving any blank would return this following error:
{
"errors": {
"line_items": [
"line item price not found, price
must provided."
]
}
}
My body request for your reference:
{
"order": {
"order_number": "#1607",
"email": "andy.famm99@gmail.com",
"financial_status":"paid",
"note":"",
"created_at":"2022-03-10T19:08:54+08:00",
"fulfillment_status":"fulfilled",
"accepts_marketing":"FALSE",
"total_tax":"2.28",
"discount_codes":[
{"code":"hi",
"amount":"0",
"type":"fixed amount"}],
"line_items": [
{
"title": "broccoli",
"quantity": "2",
"price": "10"
},
{"title":"",
"quantity": "",
"price":""
}
],
"shipping_address": {
"first_name": "Andy",
"address1": "783b Hello Road",
"phone": "8342 2341",
"city": "Singapore",
"zip": "732746",
"last_name": "Fam"
}
}
}
Any suggestion would be really appreciated. Thanks!
@qianqian748 Hello again!
I saw your test JSON and i saw that you also want to delete empty values on arrays, not only objects which were treated by my program as a value not an object to check. I just now realized how much more important is analyzing arrays in the objects or analyze objects which are in array. Thank you for reporting this feature leak.
The script should work now as you expected.
Good day/afternoon/night!
@Mondonno Thank you for the quick reply! I tried with the edited code, somehow it still checks on the object and returned the following error:
{
"errors": {
"order": "Required parameter missing or invalid"
}
}
Looks like the object is deleted. This also happens even when i passed in values to the empty item array. Are we able to delete only the
empty item array nested in the line_items array?
Thanks!
Good day/afternoon/night to you too!
Hello @qianqian748 !
I think that my script is doing a good work and this error has nothing to this script.
Output what i'm getting when inputing to script your request body:
{
"order":{
"order_number":"#1607",
"email":"andy.famm99@gmail.com",
"financial_status":"paid",
"created_at":"2022-03-10T19:08:54+08:00",
"fulfillment_status":"fulfilled",
"accepts_marketing":"FALSE",
"total_tax":"2.28",
"discount_codes":[
{
"code":"hi",
"amount":"0",
"type":"fixed amount"
}
],
"line_items":[
{
"title":"broccoli",
"quantity":"2",
"price":"10"
}
],
"shipping_address":{
"first_name":"Andy",
"address1":"783b Hello Road",
"phone":"8342 2341",
"city":"Singapore",
"zip":"732746",
"last_name":"Fam"
}
}
}
I think the problem is that you are using your API uncorrectly.
I do not know shopify API but after i read docs i was able to figure out problems with your request body:
accepts_marketing
needs to beboolean
type
indiscount_codes
needs to befixed_amount
notfixed amount
quantity
inline_items
needs to beinteger
There are probably more mistakes in your request body, so check it and after that try to send request.
I think that from my side it is all what i can do for you, since it is not related to this gist.
Regards, Mondonno
Hello Mondonno! Thank you very much for trying out the request and pointing out some of my errors. Really appreaciate your time and suggestions. I will look deeper into my code. Thanks!! =))
great script. exactly what I needed. Just a note, in your postman usage "converted" might need to be "convertedBody"
Usage with Postman
Normal usage example