Skip to content

Commit

Permalink
fix: proper response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Sep 14, 2024
1 parent 69a6c7d commit 53326eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "api-request",
"name": "APIRequest",
"version": "1.4.1",
"version": "1.4.2",
"minAppVersion": "0.15.0",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"author": "rooyca",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-request",
"version": "1.4.1",
"version": "1.4.2",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"main": "main.js",
"scripts": {
Expand Down
47 changes: 20 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,8 @@ export default class MainAPIR extends Plugin {

if (!response_disabled) {
responseData = await requestUrl({ url: URL, method, headers, body });
try {
responseData = responseData.json;
} catch(e) {
console.error(e)
}
} else {
responseData = JSON.parse(response_disabled);
}

// Save to a file
if (saveTo) {
try {
await this.app.vault.create(saveTo, responseData.text);
new Notice("Saved to: " + saveTo);
} catch (e) {
console.error(e.message);
new Notice("Error: " + e.message);
}
responseData = { json: JSON.parse(response_disabled) };
}

try {
Expand All @@ -245,7 +229,6 @@ export default class MainAPIR extends Plugin {
el.innerHTML = parser.parse(sanitizer.SanitizeHtml(responseData.text));
} catch (e) {
new Notice("Error: " + e.message);
console.log("Here we are!")
el.innerHTML = "<pre>" + sanitizer.SanitizeHtml(responseData.text) + "</pre>";
}

Expand All @@ -257,13 +240,24 @@ export default class MainAPIR extends Plugin {
console.error(e.message);
}

// Save to a file
if (saveTo) {
try {
await this.app.vault.create(saveTo, responseData.text);
new Notice("Saved to: " + saveTo);
} catch (e) {
console.error(e.message);
new Notice("Error: " + e.message);
}
}

if (notifyIf) {
const jsonPath = notifyIf[0];
const symbol = notifyIf[1];
const value = notifyIf[2]
const int_value = parseInt(value);

const jsonPathValue = jsonPath.split(".").reduce((acc, cv) => acc[cv], responseData);
const jsonPathValue = jsonPath.split(".").reduce((acc, cv) => acc[cv], responseData.json);
const lastValue = jsonPath.split(".").pop();
if (symbol === ">" && jsonPathValue > int_value) {
new Notice("APIR: " + lastValue + " is greater than " + int_value);
Expand All @@ -283,7 +277,7 @@ export default class MainAPIR extends Plugin {
el.createEl("strong", { text: "Error: Properties are not allowed without SHOW" });
return;
}
el.innerHTML = "<pre>" + JSON.stringify(responseData, null, 2) + "</pre>";
el.innerHTML = "<pre>" + JSON.stringify(responseData.json, null, 2) + "</pre>";
if (reqID) saveToID(reqID, el.innerText);
addBtnCopy(el, el.innerText);
} else {
Expand Down Expand Up @@ -313,8 +307,8 @@ export default class MainAPIR extends Plugin {
const numberBracesRegex = show.match(num_braces_regx);

if (!numberBracesRegex) {
if (Array.isArray(responseData)) {
for (let i = 0; i < responseData.length; i++) {
if (Array.isArray(responseData.json)) {
for (let i = 0; i < responseData.json.length; i++) {
temp_show += show.replace(in_braces_regx, i.toString()) + ", ";
}
show = temp_show;
Expand All @@ -329,7 +323,6 @@ export default class MainAPIR extends Plugin {
if (typeof current === "object") {
current = JSON.stringify(current, null, 2);
}
console.log(typeof current)
if (typeof current === 'string') current = encodeURIComponent(current);
result += current + ", ";
return;
Expand Down Expand Up @@ -384,7 +377,7 @@ export default class MainAPIR extends Plugin {
return result;
};

temp_show = processNestedObject(responseData, parts);
temp_show = processNestedObject(responseData.json, parts);
show = temp_show;


Expand Down Expand Up @@ -418,8 +411,8 @@ export default class MainAPIR extends Plugin {

if (trimmedKey.includes("->")) {
val = nestedValue(responseData, trimmedKey);
} else if (responseData && responseData[trimmedKey]) {
val = responseData[trimmedKey];
} else if (responseData.json && responseData.json[trimmedKey]) {
val = responseData.json[trimmedKey];
}

let propertyName = propertiesArray[index].trim();
Expand Down Expand Up @@ -450,7 +443,7 @@ export default class MainAPIR extends Plugin {
const trimmedKey = key.trim();
return trimmedKey.includes("->")
? nestedValue(responseData, trimmedKey)
: JSON.stringify(responseData[trimmedKey]) || trimmedKey;
: JSON.stringify(responseData.json[trimmedKey]) || trimmedKey;
};

const values = show.split(",")
Expand Down

0 comments on commit 53326eb

Please sign in to comment.