Skip to content

Commit

Permalink
Merge pull request #37 from blockfrost/fix-all
Browse files Browse the repository at this point in the history
fix: addressesTxsAll and addressesUtxosAll pagination
  • Loading branch information
mmahut authored May 18, 2021
2 parents 15a24ba + 5672e7a commit b1aba8c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
8 changes: 7 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.4] - 2021-18-06

### Fixed

- addressesTxsAll pagination
- addressesUtxosAll pagination

## [0.2.3] - 2021-05-06

### Fixed

- array missing in some `/txs` types, see: https://github.com/blockfrost/openapi/releases/tag/v0.1.13


## [0.2.2] - 2021-05-02

### Fixed
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": "@blockfrost/blockfrost-js",
"version": "0.2.3",
"version": "0.2.4",
"description": "A JavaScript/TypeScript SDK for interacting with the https://blockfrost.io API",
"files": [
"lib/**/*.js",
Expand Down
65 changes: 26 additions & 39 deletions src/endpoints/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function addressesTxsAll(
batchSize = 10,
): Promise<components['schemas']['address_txs_content']> {
let page = 1;
let res: components['schemas']['address_txs_content'] = [];
const res: components['schemas']['address_txs_content'] = [];
let shouldRun = true;
const promisesBundle: Promise<
components['schemas']['address_txs_content']
Expand All @@ -87,23 +87,17 @@ export async function addressesTxsAll(
page++;
}

await Promise.all(
promisesBundle.map(p =>
p
.then(data => {
res = res.concat(data);
// some page is not full end search
if (data.length < DEFAULT_PAGINATION_PAGE_ITEMS_COUNT) {
shouldRun = false;
}
})
.catch(error => {
console.log(error);
shouldRun = false;
return error;
}),
),
);
const result = await Promise.all(promisesBundle).then(values => {
values.map(batch => {
if (batch.length < DEFAULT_PAGINATION_PAGE_ITEMS_COUNT) {
shouldRun = false;
}
});

return values.flat();
});

if (!shouldRun) return result;
}

return res;
Expand All @@ -120,9 +114,7 @@ export async function addressesUtxos(
axios
.get(
`${this.apiUrl}/addresses/${address}/utxos?page=${page}&count=${count}&order=${order}`,
{
headers: getHeaders(this.projectId),
},
{ headers: getHeaders(this.projectId) },
)
.then(resp => {
resolve(resp.data);
Expand All @@ -138,7 +130,7 @@ export async function addressesUtxosAll(
batchSize = 10,
): Promise<components['schemas']['address_utxo_content']> {
let page = 1;
let res: components['schemas']['address_utxo_content'] = [];
const res: components['schemas']['address_utxo_content'] = [];
let shouldRun = true;
const promisesBundle: Promise<
components['schemas']['address_utxo_content']
Expand All @@ -152,27 +144,22 @@ export async function addressesUtxosAll(
DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
order,
);

promisesBundle.push(promise);
page++;
}

await Promise.all(
promisesBundle.map(p =>
p
.then(data => {
res = res.concat(data);
// some page is not full end search
if (data.length < DEFAULT_PAGINATION_PAGE_ITEMS_COUNT) {
shouldRun = false;
}
})
.catch(error => {
console.log(error);
shouldRun = false;
return error;
}),
),
);
const result = await Promise.all(promisesBundle).then(values => {
values.map(batch => {
if (batch.length < DEFAULT_PAGINATION_PAGE_ITEMS_COUNT) {
shouldRun = false;
}
});

return values.flat();
});

if (!shouldRun) return result;
}

return res;
Expand Down

0 comments on commit b1aba8c

Please sign in to comment.