Skip to content

Commit

Permalink
update(fix): toDocument function wasnt working
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jun 16, 2024
1 parent da1b264 commit 4750601
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 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.3.2",
"version": "1.3.3",
"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.3.2",
"version": "1.3.3",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"main": "main.js",
"scripts": {
Expand Down
18 changes: 7 additions & 11 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function addBtnCopy(el: HTMLElement, copyThis: string) {

// When more than one {} is defined in "format"
// it will loop through the responses and replace the {} with the respective value
export function replaceOrder(stri: string, val: []) {
export function replaceOrder(stri: string, val) {
let index = 0;
let replaced = stri.replace(/{}/g, function () {
return val[index++];
Expand Down Expand Up @@ -59,15 +59,11 @@ export function nestedValue(data, key: string) {
}

// Paste the response to the editor
export function toDocument(settings: any, editor: Editor) {
requestUrl({
url: settings.URL,
method: settings.MethodRequest,
body: settings.DataRequest,
})
export function toDocument(requestOptions: object, DataResponse: string, editor: Editor) {
requestUrl(requestOptions)
.then((data) => {
if (settings.DataResponse !== "") {
const DataResponseArray = settings.DataResponse.split(",");
if (DataResponse !== "") {
const DataResponseArray = DataResponse.split(",");
for (let i = 0; i < DataResponseArray.length; i++) {
const key = DataResponseArray[i].trim();

Expand All @@ -78,10 +74,10 @@ export function toDocument(settings: any, editor: Editor) {
value = JSON.stringify(value);
}

editor.replaceSelection("```markdown\n" + `${key.split("->").pop()} : ${value}\n` + "```\n\n");
editor.replaceSelection(value);
}
} else {
editor.replaceSelection("<code>\n" + `${data.text}\n` + "</code>\n");
editor.replaceSelection("<div>\n" + `${data.text}\n` + "</div>\n");
}
})
.catch((error: Error) => {
Expand Down
29 changes: 18 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,14 @@ export default class MainAPIR extends Plugin {
id: 'response-in-document',
name: 'Paste response in current document',
editorCallback: (editor: Editor) => {
toDocument(this.settings, editor);
const set = this.settings;
const requestOptions = {
url: set.URL,
method: set.MethodRequest,
headers: JSON.parse(set.HeaderRequest),
...(set.MethodRequest !== "GET" && { body: set.DataRequest })
};
toDocument(requestOptions, set.DataResponse, editor);
}
});

Expand All @@ -342,7 +349,7 @@ export default class MainAPIR extends Plugin {
name: 'Response for api: ' + this.settings.URLs[i]["Name"],
editorCallback: (editor: Editor, view: MarkdownView) => {
const rea = this.settings.URLs[i];
toDocument(rea, editor);
toDocument(rea, this.settings.URLs[i]["DataResponse"], editor);
}
});
}
Expand Down Expand Up @@ -513,11 +520,11 @@ class APRSettings extends PluginSettingTab {
const { URL, MethodRequest, DataResponse, DataRequest, HeaderRequest } = this.plugin.settings;
const { URLs } = this.plugin.settings;
URLs.push({
'URL': URL,
'url': URL,
'Name': Name,
'MethodRequest': MethodRequest,
'DataRequest': DataRequest,
'HeaderRequest': HeaderRequest,
'method': MethodRequest,
'body': DataRequest,
'headers': HeaderRequest,
'DataResponse': DataResponse
});
await this.plugin.saveSettings();
Expand All @@ -527,7 +534,7 @@ class APRSettings extends PluginSettingTab {
name: 'Response for api: ' + Name,
editorCallback: (editor: Editor) => {
const rea = URLs[URLs.length - 1];
toDocument(rea, editor);
toDocument(rea, rea.DataResponse, editor);
}
});
});
Expand Down Expand Up @@ -580,16 +587,16 @@ class APRSettings extends PluginSettingTab {
headerRow.createEl('th', { text: 'URL' });

const tbody = table.createEl('tbody');
URLs.forEach((url) => {
URLs.forEach((u) => {
const row = tbody.createEl('tr');

row.createEl('td', { text: url.Name });
row.createEl('td', { text: u.Name });

const urlCell = row.createEl('td');
urlCell.createEl('a', { text: url.URL.length > 50 ? url.URL.substring(0, 50) + "..." : url.URL, cls: 'api-url' });
urlCell.createEl('a', { text: u.url.length > 50 ? u.url.substring(0, 50) + "..." : u.url, cls: 'api-url' });

urlCell.addEventListener('click', async () => {
const index = URLs.indexOf(url);
const index = URLs.indexOf(u);
URLs.splice(index, 1);
await this.plugin.saveSettings();
this.display();
Expand Down

0 comments on commit 4750601

Please sign in to comment.