Skip to content

Commit

Permalink
update: disabled flag added on codeblock (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Apr 29, 2024
1 parent ed7e339 commit 39d3351
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ To use the plugin, create a code block with the language set to `req`. Inside th

| Key| Description| Default|
| ---| -----------|---------|
| disabled | Disables the request| |
| url | The URL to send the request to (You can use variables defined in the frontmatter)| |
| method | Request method (GET, POST, PUT, DELETE)| GET |
| body | Data to send with the request. Data should by in JSON format (You can use variables defined in the frontmatter)| |
Expand Down Expand Up @@ -103,11 +104,11 @@ Select the option that suits your needs. Additionally, you can configure the plu
The plugin has a few settings that you can configure:

- **URL**: The URL to send the request to.
- **Output format**: Choose between JSON block or Obsidian variable.
- **Request Method**: Choose between GET, POST, PUT & DELETE.
- **Request Data**: The data to send with the request. Data should by in JSON format.
- **Header Data**: The header data to send with the request. Data should by in JSON format. (`{"Content-Type": "application/json", "Authorization": "Bearer TOKEN"}`)
- **Response Data**: The response data to display. If empty all data will be display. You can use a right arrow `->` to access nested objects. For example, if you want to show the `title` from the `user` object, you can do that like this: `user -> title`.
- **Format Output**: Just JSON blocks (for now).
- **Method**: Choose between GET, POST, PUT & DELETE.
- **Body**: The data to send with the request. Data should by in JSON format.
- **Headers**: The header data to send with the request. Data should by in JSON format. (`{"Content-Type": "application/json", "Authorization": "Bearer TOKEN"}`)
- **Response**: The response data to display. If empty all data will be display. You can use a right arrow `->` to access nested objects. For example, if you want to show the `title` from the `user` object, you can do that like this: `user -> title`.

## To-do

Expand Down
61 changes: 29 additions & 32 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Editor, MarkdownView, Modal, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, Editor, MarkdownView, Modal, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian';
import { readFrontmatter, parseFrontmatter } from './frontmatterUtils';

export function checkFrontmatter(req_prop: string){
Expand All @@ -16,6 +16,7 @@ export function checkFrontmatter(req_prop: string){
return req_prop;
} catch (e) {
console.error(e.message);
new Notice("Error: " + e.message);
return;
}
}
Expand Down Expand Up @@ -87,29 +88,19 @@ function toDocument(settings: any, editor: Editor) {
if (key.includes("->")) {
value = nestedValue(data, key);
}

if (settings.FormatOut === "variable") {
value = JSON.stringify(value);
editor.replaceSelection(`${key.split("->").pop()} : ${value}\n`);
}
if (settings.FormatOut === "json") {
if (key.includes("->")) {
value = JSON.stringify(value);
}
editor.replaceSelection("```json\n" + `${key.split("->").pop()} : ${value}\n` + "```\n\n");
}

if (key.includes("->")) {
value = JSON.stringify(value);
}
editor.replaceSelection("```json\n" + `${key.split("->").pop()} : ${value}\n` + "```\n\n");
}
} else {
if (settings.FormatOut === "variable") {
editor.replaceSelection(`${JSON.stringify(data.json)}\n`);
}
if (settings.FormatOut === "json") {
editor.replaceSelection("```json\n" + `${JSON.stringify(data.json)}\n` + "```\n");
}
}
}
})
.catch((error: Error) => {
console.error(error);
new Notice("Error: " + error.message);
});
}

Expand Down Expand Up @@ -140,6 +131,11 @@ export default class MainAPIR extends Plugin {
let format: string = "{}";
let responseType: string = "json";

if (sourceLines.includes("disabled")) {
el.innerHTML = "<strong>This request is disabled</strong>";
return;
}

for (const line of sourceLines) {
let lowercaseLine = line.toLowerCase();

Expand Down Expand Up @@ -236,10 +232,13 @@ export default class MainAPIR extends Plugin {
} catch (error) {
console.error(error);
el.innerHTML = "Error: " + error.message;
new Notice("Error: " + error.message);
}
});
} catch (e) {
console.error(e.message);
el.innerHTML = "Error: " + error.message;
new Notice("Error: " + e.message);
}

this.addCommand({
Expand Down Expand Up @@ -292,7 +291,6 @@ class ShowOutputModal extends Modal {
onOpen() {
const { contentEl } = this;
const { URL, MethodRequest, DataRequest, HeaderRequest, DataResponse } = this.props;
console.log(HeaderRequest)

if (MethodRequest === "GET") {
requestUrl({
Expand All @@ -316,7 +314,7 @@ onOpen() {
})
.catch((error: Error) => {
console.error(error);
contentEl.createEl('b', { text: "Error: " + error.message });
new Notice("Error: " + error.message);
});
} else {
requestUrl({
Expand All @@ -337,7 +335,7 @@ onOpen() {
})
.catch((error: Error) => {
console.error(error);
contentEl.createEl('b', { text: "Error: " + error.message });
new Notice("Error: " + error.message);
});
}
}
Expand All @@ -364,7 +362,7 @@ class APRSettings extends PluginSettingTab {

new Setting(containerEl)
.setName('Name')
.setDesc('Name of the request')
.setDesc('Name of request')
.addText(text => text
.setPlaceholder('Name')
.setValue(this.plugin.settings.Name)
Expand All @@ -387,19 +385,18 @@ class APRSettings extends PluginSettingTab {
}));
new Setting(containerEl)
.setName('Response format')
.setDesc("Select the desired response format: JSON (as a code block) or Variable (using '::')")
.setDesc("Select the desired response format (only JSON for now)")
.addDropdown(dropDown => {
dropDown.addOption("json", "JSON");
dropDown.addOption("variable", "Variable");
dropDown.setValue(this.plugin.settings.FormatOut)
dropDown.onChange(async value => {
this.plugin.settings.FormatOut = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName('Request method')
.setDesc("Select the desired request method")
.setName('Method')
.setDesc("Select the desired method")
.addDropdown(dropDown => {
dropDown.addOption("GET", "GET");
dropDown.addOption("POST", "POST");
Expand All @@ -412,8 +409,8 @@ class APRSettings extends PluginSettingTab {
});
});
new Setting(containerEl)
.setName('Data to send')
.setDesc("Data to send in the request")
.setName('Body')
.setDesc("Data to send in the body")
.addTextArea(text => text
.setPlaceholder('{"data":"data"}')
.setValue(this.plugin.settings.DataRequest)
Expand All @@ -423,7 +420,7 @@ class APRSettings extends PluginSettingTab {
}));
new Setting(containerEl)
.setName('Headers')
.setDesc("Headers to send in the request")
.setDesc("The headers of the request")
.addTextArea(text => text
.setPlaceholder('{"Content-Type": "application/json"}')
.setValue(this.plugin.settings.HeaderRequest)
Expand All @@ -432,10 +429,10 @@ class APRSettings extends PluginSettingTab {
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Data to show in modal')
.setDesc("Write the name of the variable to show in the modal (space by comma)")
.setName('What to display')
.setDesc("Write the name of the variables you want to show (spaced by comma)")
.addTextArea(text => text
.setPlaceholder('Variable Name')
.setPlaceholder('varname')
.setValue(this.plugin.settings.DataResponse)
.onChange(async (value) => {
this.plugin.settings.DataResponse = value;
Expand Down
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.1.4.1",
"version": "1.1.6",
"minAppVersion": "0.15.0",
"description": "Request and retrieve data from APIs. The responses are delivered in a JSON format for easy integration with 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.1.4.1",
"version": "1.1.6",
"description": "Request and retrieve data from APIs. The responses are delivered in a JSON format for easy integration with your notes.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 39d3351

Please sign in to comment.