update(feat): loop over specific indexes or range

This commit is contained in:
rooyca 2024-05-11 11:26:05 -05:00
parent 693f5b9104
commit 8986798a12
6 changed files with 94 additions and 12 deletions

View file

@ -99,6 +99,24 @@ show: {..} -> address -> city
```
~~~
Looping over a specified number of elements of the array is also possible using `{n..n}`.
~~~markdown
```req
url: https://jsonplaceholder.typicode.com/users
show: {0..2} -> address -> city
```
~~~
It's also possible to loop over a specified range of indexes of the array using `{n-n-n}`.
~~~markdown
```req
url: https://jsonplaceholder.typicode.com/users
show: {0-2-1} -> address -> city
```
~~~
## format
Specifies the format in which the response should be displayed. The default value is `{}`. It can be any string (including `markdown` and `html`). If more than one output is specified, more then one format should be specified, otherwise, the same format will be applied to all outputs.

38
docs/docs/settings.md Normal file
View file

@ -0,0 +1,38 @@
# Settings
Using the plugin with this method is not recommended. It is better to use code blocks. This method is very limited and does not support all the features of the code block method.
## name
Specifies the name of the request.
## url (required)
Specifies the URL of the request.
## method
- GET
- POST
- PUT
- DELETE
## body
Specifies the body of the request.
## headers
Specifies the headers of the request.
## what to display
Specifies the output you want.
## Add this APIR
Save the above fields into a new APIR (you can access all APIRs by pressing `Ctrl+P` and searching for `APIR`).
## Clear ID's
Remove all IDs stored in `localStorage`.

View file

@ -25,3 +25,4 @@ nav:
- Getting started: 'index.md'
- codeblock:
- flags: 'codeblock/flags.md'
- settings: 'settings.md'

View file

@ -1,7 +1,7 @@
{
"id": "api-request",
"name": "APIRequest",
"version": "1.2.2",
"version": "1.2.3",
"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",

View file

@ -1,6 +1,6 @@
{
"name": "api-request",
"version": "1.2.2",
"version": "1.2.3",
"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": {

View file

@ -226,17 +226,42 @@ export default class MainAPIR extends Plugin {
saveToID(reqID, el.innerText);
addBtnCopy(el, el.innerText);
} else {
if (show.includes("{..}")) {
if (show.includes(",")) {
el.innerHTML = "Error: can't use {..} and , in the same req";
return;
const first_pattern = /{([^{}]*)}/g;
if (show.match(first_pattern)) {
if (show.includes(",")) {
el.innerHTML = "Error: comma is not allowed when using {}";
return;
}
let temp_show = "";
for (let i = 0; i < responseData.json.length; i++) {
temp_show += show.replace("{..}", i) + ", ";
}
show = temp_show;
}
const pattern = /{(\d+)\.\.(\d+)}/;
let temp_show = "";
if (show.match(pattern)) {
const range = show.match(/\d+/g).map(Number);
if (range[0] > range[1]) {
el.innerHTML = "Error: range is not valid";
return;
}
for (let i = range[0]; i <= range[1]; i++) {
temp_show += show.replace(show.match(pattern)[0], i) + ", ";
}
show = temp_show;
} else if (show.match(/(\d+-)+\d+/)) {
const numbers = show.match(/\d+/g).map(Number);
show = show.replace(/{.*?}/g, "-");
for (let i = 0; i < numbers.length; i++) {
temp_show += show.replace("-", numbers[i]) + ", ";
}
show = temp_show;
} else {
for (let i = 0; i < responseData.json.length; i++) {
temp_show += show.replace("{..}", i) + ", ";
}
show = temp_show;
}
}
const values = show.includes(",") ? show.split(",").map(key => {
let value = JSON.stringify(responseData.json[key.trim()]);
if (key.includes("->")) value = nestedValue(responseData, key);