diff --git a/CHANGELOG.md b/CHANGELOG.md index 144ebe1f..0ec7b365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ 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). +## [Unreleased] + +### Added + +- New account methods supported for listing, counting and checking account transfers in `AccountAPI`. + ## [0.5.2] - 2023-10-16 ### Fixed diff --git a/src/api/account.ts b/src/api/account.ts index 9bfd5f0a..7e516f89 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -4,12 +4,17 @@ import { API } from './api'; import { AccountMetadata } from '../types'; enum AccountAPIMethods { - INFO = '/accounts', + LIST = '/accounts/page', + NUM_ACCOUNTS = '/accounts/count', + INFO = '/accounts/{accountId}', SET_INFO = '/accounts', ELECTIONS = '/accounts/{accountId}/elections/page', TRANSFERS = '/accounts/{accountId}/transfers/page', + NUM_TRANSFERS = '/accounts/{accountId}/transfers/count', } +export type IAccountSummary = Pick; + interface IAccountInfoResponse { /** * The address of the account @@ -59,7 +64,7 @@ interface IAccountSetInfoResponse { metadataURL: number; } -interface IAccountTransfersResponse { +interface IAccountTransfer { amount: number; from: string; height: number; @@ -68,6 +73,34 @@ interface IAccountTransfersResponse { to: string; } +interface IAccountTransfersResponse { + transfers: { + received: Array; + sent: Array; + }; +} + +export interface IAccountsListResponse { + /** + * List of accounts + */ + accounts: Array; +} + +export interface IAccountTransfersCountResponse { + /** + * Number of account's transfers + */ + count: number; +} + +export interface IAccountsCountResponse { + /** + * Number of accounts + */ + count: number; +} + export abstract class AccountAPI extends API { /** * Cannot be constructed. @@ -76,16 +109,43 @@ export abstract class AccountAPI extends API { super(); } + /** + * Returns paginated list of accounts + * + * @param {string} url API endpoint URL + * @param {number} page The page number + * @returns {Promise} + */ + public static list(url: string, page: number = 0): Promise { + return axios + .get(url + AccountAPIMethods.LIST + '/' + page) + .then((response) => response.data) + .catch(this.isApiError); + } + + /** + * Returns the number of accounts + * + * @param {string} url API endpoint URL + * @returns {Promise} + */ + public static count(url: string): Promise { + return axios + .get(url + AccountAPIMethods.NUM_ACCOUNTS) + .then((response) => response.data) + .catch(this.isApiError); + } + /** * Fetches an Account information * * @param {string} url API endpoint URL - * @param {string} address The one we want the info from + * @param {string} accountId The account we want the info from * @returns {Promise} */ - public static info(url: string, address: string): Promise { + public static info(url: string, accountId: string): Promise { return axios - .get(url + AccountAPIMethods.INFO + '/' + address) + .get(url + AccountAPIMethods.INFO.replace('{accountId}', accountId)) .then((response) => response.data) .catch(this.isApiError); } @@ -120,6 +180,20 @@ export abstract class AccountAPI extends API { .catch(this.isApiError); } + /** + * Returns the account's transfers count + * + * @param {string} url API endpoint URL + * @param {string} accountId accountId to get the transfers count + * @returns {Promise} + */ + public static transfersCount(url: string, accountId: string): Promise { + return axios + .get(url + AccountAPIMethods.NUM_TRANSFERS.replace('{accountId}', accountId)) + .then((response) => response.data) + .catch(this.isApiError); + } + /** * Returns paginated list of elections for a specific account *