diff --git a/docs/docs/codeblock/flags.md b/docs/docs/codeblock/flags.md index 024e78e..132737a 100644 --- a/docs/docs/codeblock/flags.md +++ b/docs/docs/codeblock/flags.md @@ -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. diff --git a/docs/docs/settings.md b/docs/docs/settings.md new file mode 100644 index 0000000..71274d4 --- /dev/null +++ b/docs/docs/settings.md @@ -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`. \ No newline at end of file diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 61b31fc..a578aff 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -25,3 +25,4 @@ nav: - Getting started: 'index.md' - codeblock: - flags: 'codeblock/flags.md' + - settings: 'settings.md' diff --git a/manifest.json b/manifest.json index 9c759fb..9434939 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index 84c5fe0..1b4d0eb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/main.ts b/src/main.ts index f385690..7ec06aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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);