Skip to content

Commit

Permalink
Change to requestUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jun 2, 2023
1 parent 105c073 commit 0150ea8
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,17 @@ export default class MainAPIR extends Plugin {
id: 'response-in-document',
name: 'Paste response in current document',
editorCallback: (editor: Editor, view: MarkdownView) => {
fetch(this.settings.URL, {
requestUrl({
url: this.settings.URL,
method: this.settings.MethodRequest,
body: this.settings.DataRequest,
})
.then(response => {
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
return response.json();
})
.then(data => {
if (this.settings.DataResponse !== "") {
const DataResponseArray = this.settings.DataResponse.split(",");
for (let i = 0; i < DataResponseArray.length; i++) {
const key = DataResponseArray[i];
const value = JSON.stringify(data[key]);
const value = JSON.stringify(data.json[key]);

if (this.settings.FormatOut === "variable") {
editor.replaceSelection(`json:: ${key} : ${value}\n`);
Expand All @@ -61,10 +56,10 @@ export default class MainAPIR extends Plugin {
}
} else {
if (this.settings.FormatOut === "variable") {
editor.replaceSelection(`json:: ${JSON.stringify(data)}\n`);
editor.replaceSelection(`json:: ${JSON.stringify(data.json)}\n`);
}
if (this.settings.FormatOut === "json") {
editor.replaceSelection("```json\n" + `${JSON.stringify(data)}\n` + "```\n");
editor.replaceSelection("```json\n" + `${JSON.stringify(data.json)}\n` + "```\n");
}
}
})
Expand Down Expand Up @@ -108,54 +103,44 @@ onOpen() {
const { URL, MethodRequest, DataRequest, DataResponse } = this.props;

if (MethodRequest === "GET") {
fetch(URL, {
requestUrl({
url: URL,
method: MethodRequest,
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
return response.json();
})
.then(data => {
if (DataResponse !== "") {
const DataResponseArray = DataResponse.split(",");
for (let i = 0; i < DataResponseArray.length; i++) {
contentEl.createEl('b', { text: DataResponseArray[i] + " : " + `${JSON.stringify(data[DataResponseArray[i]])}` });
contentEl.createEl('b', { text: DataResponseArray[i] + " : " + `${JSON.stringify(data.json[DataResponseArray[i]])}` });
}
} else {
contentEl.createEl('b', { text: `${JSON.stringify(data)}` });
contentEl.createEl('b', { text: `${JSON.stringify(data.json)}` });
}
})
.catch(error => {
console.error(error);
contentEl.createEl('b', { text: "Error: " + error.message });
});
} else {
fetch(URL, {
requestUrl({
url: URL,
method: MethodRequest,
headers: {
'Content-Type': 'application/json'
},
body: DataRequest
})
.then(response => {
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
return response.json();
})
.then(data => {
if (DataResponse !== "") {
const DataResponseArray = DataResponse.split(",");
for (let i = 0; i < DataResponseArray.length; i++) {
contentEl.createEl('b', { text: DataResponseArray[i] + " : " + `${JSON.stringify(data[DataResponseArray[i]])}` });
contentEl.createEl('b', { text: DataResponseArray[i] + " : " + `${JSON.stringify(data.json[DataResponseArray[i]])}` });
}
} else {
contentEl.createEl('b', { text: `${JSON.stringify(data)}` });
contentEl.createEl('b', { text: `${JSON.stringify(data.json)}` });
}
})
.catch(error => {
Expand Down

0 comments on commit 0150ea8

Please sign in to comment.