From 4750601e3b2b2298290dd5b9c72565012229b6f5 Mon Sep 17 00:00:00 2001 From: rooyca Date: Sun, 16 Jun 2024 15:21:21 -0500 Subject: [PATCH] update(fix): toDocument function wasnt working --- manifest.json | 2 +- package.json | 2 +- src/functions.ts | 18 +++++++----------- src/main.ts | 29 ++++++++++++++++++----------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/manifest.json b/manifest.json index 830c913..84d6941 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index 77e5d63..b8d7f87 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/functions.ts b/src/functions.ts index 091df01..03c8770 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -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++]; @@ -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(); @@ -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("\n" + `${data.text}\n` + "\n"); + editor.replaceSelection("
\n" + `${data.text}\n` + "
\n"); } }) .catch((error: Error) => { diff --git a/src/main.ts b/src/main.ts index 95c8265..623d54f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); } }); @@ -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); } }); } @@ -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(); @@ -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); } }); }); @@ -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();