diff --git a/paginated_request.js b/paginated_request.js index df9097a..2710f17 100644 --- a/paginated_request.js +++ b/paginated_request.js @@ -2,7 +2,7 @@ export default defineComponent({ name: "Paginated HTTP Request", description: "Creates a HTTP Request until end of paginated data or timeout is reached", key: "paginated_http_request", - version: "0.0.1", + version: "0.0.2", type: "action", props: { @@ -46,22 +46,25 @@ export default defineComponent({ } }, methods: { - get_pagination_index() { - for (const key in this.http_request.params) { - if (this.http_request.params[key].name == this.paginator) { - return key - } + getPaginationIndex() { + let i = this.http_request.params.findIndex(p => p.name == this.paginator) + if (i < 0) { + i = this.http_request.params.length + this.http_request.params.push({ + "name": this.paginator, + "value": "0" + }) } - return null + return i }, - get_response_data(resp) { + getResponseData(resp) { let data = resp for (const field of this.data_field.split(".")) { data = data[field] } return data }, - handle_timeout(start) { + handleTimeout(start) { const duration = Date.now() - start if (this.timeout && duration > this.timeout * 1000) { const msg = `Timeout reached at ${duration / 1000} seconds` @@ -75,18 +78,14 @@ export default defineComponent({ } }, async run({ $ }) { - const i = await this.get_pagination_index() - if (i === null) { - throw new Error("Pagination parameter not set") - } - + const i = await this.getPaginationIndex() const start = Date.now() const results = [] let data = [] let count = 1 do { const resp = await this.http_request.execute() - data = await this.get_response_data(resp) + data = await this.getResponseData(resp) if (!Array.isArray(data)) { throw new Error(`Response data is not an array: ${typeof data}`); @@ -98,7 +97,7 @@ export default defineComponent({ results.push(...data) - if (await this.handle_timeout(start)) { + if (await this.handleTimeout(start)) { break } count++