Skip to content

Commit

Permalink
add: response types in codeblock
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Mar 7, 2024
1 parent 4b98381 commit d7e4252
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Obsidian plugin that allows you to make requests to APIs and receive responses i

> [!IMPORTANT]
>
> At the moment only JSON responses are supported.
> We now support JSON, TEXT and MARKDOWN responses
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/rooyca/obsidian-api-request?logo=github&color=ee8449&style=flat-square)](https://github.com/rooyca/obsidian-api-request/releases/latest)
[![Obsidian plugin release](https://img.shields.io/badge/Obsidian%20plugin%20release-purple?logo=obsidian&style=flat-square)](https://obsidian.md/plugins?id=api-request)
Expand Down Expand Up @@ -39,6 +39,7 @@ To use the plugin, create a code block with the language set to `req`. Inside th
| header | Header(s) for the request. Data should by in JSON format| |
| show | Response data to display. You can use a right arrow `->` to access nested objects| ALL |
| format | Format in which the response should be displayed| {} |
| response-type | The type of response we are getting (json, txt or md)| json |

The plugin will automatically replace the code block with the response from the API. Here are a quick examples:

Expand All @@ -49,12 +50,19 @@ url: https://api.chess.com/pub/player/hikaru/stats
show: chess_daily -> last
```

You can also show the entire response by only specifying the url:
You can show the entire response by only specifying the url:

```req
url: https://jsonplaceholder.typicode.com/todos/1
```

You can specify the response type you are getting:

```req
url: https://raw.githubusercontent.com/Rooyca/Rooyca/main/README.md
response-type: md
```

You can also specify the request method, request data, header data, and the response format:

```req
Expand Down
14 changes: 13 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class MainAPIR extends Plugin {
let header: any = {};
let body: any = {};
let format: string = "{}";
let responseType: string = "json";

for (const line of sourceLines) {
let lowercaseLine = line.toLowerCase();
Expand All @@ -121,6 +122,15 @@ export default class MainAPIR extends Plugin {
URL = line.replace(/url: /i, "");
break;

case lowercaseLine.includes("response-type"):
responseType = line.replace(/response-type: /i, "").toLowerCase();
const allowedResponseTypes = ["json", "txt", "md"];
if (!allowedResponseTypes.includes(responseType)) {
el.innerHTML = "Error: Response type " + responseType + " not supported";
return;
}
break;

case lowercaseLine.includes("show: "):
show = line.replace(/show: /i, "");
break;
Expand Down Expand Up @@ -157,7 +167,9 @@ export default class MainAPIR extends Plugin {
responseData = await requestUrl({ url: URL, method });
}

if (!show) {
if (responseType !== "json") {
el.innerHTML = formatSplit[0] + responseData.text + formatSplit[1];
} else if (!show) {
el.innerHTML = formatSplit[0] + JSON.stringify(responseData.json, null) + formatSplit[1];
} else {
let nesData: any = show.includes("->") ? nestedValue(responseData, show) : responseData.json[show];
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.0",
"version": "1.1.1",
"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.0.4",
"version": "1.1.1",
"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 d7e4252

Please sign in to comment.