Skip to content

Commit

Permalink
update(feat): loop over specific indexes or range
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed May 11, 2024
1 parent 693f5b9 commit 8986798
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
18 changes: 18 additions & 0 deletions docs/docs/codeblock/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions docs/docs/settings.md
Original file line number Diff line number Diff line change
@@ -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`.
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ nav:
- Getting started: 'index.md'
- codeblock:
- flags: 'codeblock/flags.md'
- settings: 'settings.md'
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.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",
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.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": {
Expand Down
45 changes: 35 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8986798

Please sign in to comment.