crypto-bot-api
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -1,3 +0,3 @@ | ||
import { Balances, Currency, Invoice, Invoices, InvoicesPaginated } from '../helpers/casts'; | ||
import { CreateInvoiceOptions, GetInvoicesOptions, GetInvoicesPaginateOptions } from '../helpers/utils'; | ||
import { Balances, Balance, BalancesType, Currency, CurrencyCode, CryptoCurrencyCode, CurrencyType, DetailedCurrencyType, Invoice, Check, Stats, Transfer, InvoiceStatus, CheckStatus, TransferStatus } from '../helpers/casts'; | ||
import { CreateInvoiceOptions, CreateCheckOptions, GetChecksOptions, GetChecksPaginateOptions, GetInvoicesOptions, GetInvoicesPaginateOptions, GetStatsOptions, TransferOptions, GetTransfersOptions, GetTransfersPaginateOptions } from '../helpers/utils'; | ||
import Store from './Store'; | ||
@@ -15,2 +15,26 @@ /** | ||
/** | ||
* Access to {@link CurrencyType} enumeration, used in {@link Invoice} type | ||
*/ | ||
static CurrencyType: typeof CurrencyType; | ||
/** | ||
* Access to {@link DetailedCurrencyType} enumeration, used in {@link Store.getCurrencies} | ||
* and {@link Client.getCurrency} methods results | ||
*/ | ||
static DetailedCurrencyType: typeof DetailedCurrencyType; | ||
/** | ||
* Access to {@link InvoiceStatus} enumeration, used in {@link Invoice} type, | ||
* {@link Client.getInvoices} and {@link Client.getInvoicesPaginate} methods options | ||
*/ | ||
static InvoiceStatus: typeof InvoiceStatus; | ||
/** | ||
* Access to {@link CheckStatus} enumeration, used in {@link Check} type, | ||
* {@link Client.getChecks} and {@link Client.getChecksPaginate} methods options | ||
*/ | ||
static CheckStatus: typeof CheckStatus; | ||
/** | ||
* Access to {@link TransferStatus} enumeration, used in {@link Transfer} type, | ||
* {@link Client.getTransfers} and {@link Client.getTransfersPaginate} methods options | ||
*/ | ||
static TransferStatus: typeof TransferStatus; | ||
/** | ||
* Return count invoices per page for {@link Client.getInvoicesPaginate} method | ||
@@ -22,3 +46,3 @@ */ | ||
* | ||
* @param pageSize - Invoices per page | ||
* @param pageSizeParam - Invoices per page | ||
* | ||
@@ -29,2 +53,14 @@ * @throws Error - If `pageSize` parameter is invalid | ||
/** | ||
* Get associated with passed API key app statistics | ||
* | ||
* Use {@link toStats} backend API result convert function | ||
* | ||
* @param options - New receive statistics options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to associated with passed API key app statistics object | ||
*/ | ||
getStats(options?: GetStatsOptions): Promise<Stats>; | ||
/** | ||
* Get API app balances infomation | ||
@@ -34,14 +70,28 @@ * | ||
* | ||
* Call {@link Store.getCurrencies} method to fetch exchange rates information | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* @param isForce - If true, return fresh data from backend API, not from cache | ||
* @returns Promise, what resolved to API app balances infomation object | ||
*/ | ||
getBalances(): Promise<Balances>; | ||
/** | ||
* Get API app balances infomation | ||
* | ||
* Use {@link toBalances} backend API result convert function | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balances infomation object | ||
* @returns Promise, what resolved to API app available balances infomation object | ||
*/ | ||
getBalances(isReturnInNanos?: boolean, isForce?: boolean): Promise<Balances>; | ||
getBalancesAvailable(): Promise<BalancesType>; | ||
/** | ||
* Get API app balances infomation | ||
* | ||
* Use {@link toBalances} backend API result convert function | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balances on hold infomation object | ||
*/ | ||
getBalancesOnhold(): Promise<BalancesType>; | ||
/** | ||
* Get API app balance value for passed currency | ||
@@ -51,6 +101,3 @@ * | ||
* | ||
* @param currencyCode - Currency code | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* @param isForce - If true, return fresh data from backend API, not from cache | ||
* @param currencyCode - Crypto currency code | ||
* | ||
@@ -61,4 +108,28 @@ * @throws Error - If there is an error sending request to backend API or parsing response | ||
*/ | ||
getBalance(currencyCode: string, isReturnInNanos?: boolean, isForce?: boolean): Promise<string>; | ||
getBalance(currencyCode: CryptoCurrencyCode): Promise<Balance>; | ||
/** | ||
* Get API app balance value for passed currency | ||
* | ||
* Call {@link Client.getBalances} method to fetch balances information | ||
* | ||
* @param currencyCode - Crypto currency code | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app available balance value for passed currency | ||
*/ | ||
getBalanceAvailable(currencyCode: CryptoCurrencyCode): Promise<string>; | ||
/** | ||
* Get API app balance value for passed currency | ||
* | ||
* Call {@link Client.getBalances} method to fetch balances information | ||
* | ||
* @param currencyCode - Crypto currency code | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balance on hold value for passed currency | ||
*/ | ||
getBalanceOnhold(currencyCode: CryptoCurrencyCode): Promise<string>; | ||
/** | ||
* Get currency with passed code infomation | ||
@@ -76,3 +147,3 @@ * | ||
*/ | ||
getCurrency(currencyCode: string, isForce?: boolean): Promise<Currency | null>; | ||
getCurrency(currencyCode: CurrencyCode, isForce?: boolean): Promise<Currency | null>; | ||
/** | ||
@@ -93,4 +164,18 @@ * Get one exchange rate infomation to passed currencies pair | ||
*/ | ||
getExchangeRate(source: string, target: string, isForce?: boolean): Promise<number>; | ||
getExchangeRate(source: string, target: string, isForce?: boolean): Promise<string>; | ||
/** | ||
* Transfer | ||
* | ||
* Use {@link toTransfer} backend API result convert function and | ||
* prepare backend API parameters {@link prepareTransferOptions} function | ||
* | ||
* @param options - Transfer options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to completed transfer information object | ||
*/ | ||
transfer(options: TransferOptions): Promise<Transfer>; | ||
/** | ||
* Create invoice | ||
@@ -110,2 +195,36 @@ * | ||
/** | ||
* Create check | ||
* | ||
* Use {@link toCheck} backend API result convert function and | ||
* prepare backend API parameters {@link prepareCreateCheckOptions} function | ||
* | ||
* @param options - New check options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to created check information object | ||
*/ | ||
createCheck(options: CreateCheckOptions): Promise<Check>; | ||
/** | ||
* Delete invoice | ||
* | ||
* @param id - Invoice identifier | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response error | ||
* | ||
* @returns Promise, what resolved to boolean operation result status | ||
*/ | ||
deleteInvoice(id: number): Promise<boolean>; | ||
/** | ||
* Delete check | ||
* | ||
* @param id - Check identifier | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response error | ||
* | ||
* @returns Promise, what resolved to boolean operation result status | ||
*/ | ||
deleteCheck(id: number): Promise<boolean>; | ||
/** | ||
* Get invoices | ||
@@ -122,3 +241,3 @@ * | ||
*/ | ||
getInvoices(options?: GetInvoicesOptions): Promise<Invoices>; | ||
getInvoices(options?: GetInvoicesOptions): Promise<Invoice[]>; | ||
/** | ||
@@ -131,3 +250,3 @@ * Get invoices paginated | ||
* | ||
* Use {@link toInvoicesPaginated} backend API result convert function and | ||
* Use {@link toInvoices} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetInvoicesPaginateOptions} function | ||
@@ -142,4 +261,66 @@ * | ||
*/ | ||
getInvoicesPaginate(options?: GetInvoicesPaginateOptions): Promise<InvoicesPaginated>; | ||
getInvoicesPaginate(options?: GetInvoicesPaginateOptions): Promise<Invoice[]>; | ||
/** | ||
* Get checks | ||
* | ||
* Use {@link toChecks} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetChecksOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to checks information object | ||
*/ | ||
getChecks(options?: GetChecksOptions): Promise<Check[]>; | ||
/** | ||
* Get checks paginated | ||
* | ||
* Fetch checks with `page` options parameter, except `count` and `offset` | ||
* | ||
* See {@link Client.getPageSize} and {@link Client.setPageSize} | ||
* | ||
* Use {@link toChecks} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetChecksPaginateOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to checks information object | ||
*/ | ||
getChecksPaginate(options?: GetChecksPaginateOptions): Promise<Check[]>; | ||
/** | ||
* Get transfers | ||
* | ||
* Use {@link toTransfers} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetTransfersOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to transfers information object | ||
*/ | ||
getTransfers(options?: GetTransfersOptions): Promise<Transfer[]>; | ||
/** | ||
* Get transfers paginated | ||
* | ||
* Fetch checks with `page` options parameter, except `count` and `offset` | ||
* | ||
* See {@link Client.getPageSize} and {@link Client.setPageSize} | ||
* | ||
* Use {@link toTransfers} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetTransfersOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to transfers information object | ||
*/ | ||
getTransfersPaginate(options?: GetTransfersPaginateOptions): Promise<Transfer[]>; | ||
/** | ||
* Call backend API method directly (types unsafe) | ||
@@ -146,0 +327,0 @@ * |
@@ -6,2 +6,3 @@ "use strict"; | ||
const Store_1 = require("./Store"); | ||
// Because `tsdoc` not support `@category` tag, but `typedoc` support | ||
/* eslint-disable tsdoc/syntax */ | ||
@@ -31,3 +32,3 @@ /** | ||
* | ||
* @param pageSize - Invoices per page | ||
* @param pageSizeParam - Invoices per page | ||
* | ||
@@ -44,2 +45,17 @@ * @throws Error - If `pageSize` parameter is invalid | ||
/** | ||
* Get associated with passed API key app statistics | ||
* | ||
* Use {@link toStats} backend API result convert function | ||
* | ||
* @param options - New receive statistics options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to associated with passed API key app statistics object | ||
*/ | ||
getStats(options = {}) { | ||
return this._transport.call('getStats', (0, utils_1.prepareGetStatsOptions)(options)) | ||
.then((result) => (0, casts_1.toStats)(result)); | ||
} | ||
/** | ||
* Get API app balances infomation | ||
@@ -49,20 +65,48 @@ * | ||
* | ||
* Call {@link Store.getCurrencies} method to fetch exchange rates information | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* @param isForce - If true, return fresh data from backend API, not from cache | ||
* @returns Promise, what resolved to API app balances infomation object | ||
*/ | ||
getBalances() { | ||
return this._transport.call('getBalance').then((result) => (0, casts_1.toBalances)(result)); | ||
} | ||
/** | ||
* Get API app balances infomation | ||
* | ||
* Use {@link toBalances} backend API result convert function | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balances infomation object | ||
* @returns Promise, what resolved to API app available balances infomation object | ||
*/ | ||
getBalances(isReturnInNanos = false, isForce = false) { | ||
return Promise.all([this.getCurrencies(isForce), this._transport.call('getBalance')]) | ||
// eslint-disable-next-line arrow-body-style | ||
.then(([currencies, balancesResponse]) => { | ||
return (0, casts_1.toBalances)(balancesResponse, currencies, isReturnInNanos); | ||
getBalancesAvailable() { | ||
return this.getBalances() | ||
.then((balances) => { | ||
return Object.entries(balances).reduce((accumulator, entry) => { | ||
const [code, balance] = entry; | ||
accumulator[code] = balance.available; | ||
return accumulator; | ||
}, {}); | ||
}); | ||
} | ||
/** | ||
* Get API app balances infomation | ||
* | ||
* Use {@link toBalances} backend API result convert function | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balances on hold infomation object | ||
*/ | ||
getBalancesOnhold() { | ||
return this.getBalances() | ||
.then((balances) => { | ||
return Object.entries(balances).reduce((accumulator, entry) => { | ||
const [code, balance] = entry; | ||
accumulator[code] = balance.onhold; | ||
return accumulator; | ||
}, {}); | ||
}); | ||
} | ||
/** | ||
* Get API app balance value for passed currency | ||
@@ -72,6 +116,3 @@ * | ||
* | ||
* @param currencyCode - Currency code | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* @param isForce - If true, return fresh data from backend API, not from cache | ||
* @param currencyCode - Crypto currency code | ||
* | ||
@@ -82,7 +123,7 @@ * @throws Error - If there is an error sending request to backend API or parsing response | ||
*/ | ||
getBalance(currencyCode, isReturnInNanos = false, isForce = false) { | ||
return this.getBalances(isReturnInNanos, isForce) | ||
getBalance(currencyCode) { | ||
return this.getBalances() | ||
.then((balances) => { | ||
if (balances[currencyCode] === undefined) | ||
return '0'; | ||
return { available: '0', onhold: '0' }; | ||
return balances[currencyCode]; | ||
@@ -92,2 +133,40 @@ }); | ||
/** | ||
* Get API app balance value for passed currency | ||
* | ||
* Call {@link Client.getBalances} method to fetch balances information | ||
* | ||
* @param currencyCode - Crypto currency code | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app available balance value for passed currency | ||
*/ | ||
getBalanceAvailable(currencyCode) { | ||
return this.getBalances() | ||
.then((balances) => { | ||
if (balances[currencyCode] === undefined) | ||
return '0'; | ||
return balances[currencyCode].available; | ||
}); | ||
} | ||
/** | ||
* Get API app balance value for passed currency | ||
* | ||
* Call {@link Client.getBalances} method to fetch balances information | ||
* | ||
* @param currencyCode - Crypto currency code | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to API app balance on hold value for passed currency | ||
*/ | ||
getBalanceOnhold(currencyCode) { | ||
return this.getBalances() | ||
.then((balances) => { | ||
if (balances[currencyCode] === undefined) | ||
return '0'; | ||
return balances[currencyCode].onhold; | ||
}); | ||
} | ||
/** | ||
* Get currency with passed code infomation | ||
@@ -129,9 +208,25 @@ * | ||
getExchangeRate(source, target, isForce = false) { | ||
return Promise.all([this.getCurrencies(isForce), this.getExchangeRates(isForce)]) | ||
// eslint-disable-next-line arrow-body-style | ||
.then(([currencies, exchangeRates]) => { | ||
return (0, utils_1.getExchageRate)(source, target, exchangeRates, currencies); | ||
return this.getExchangeRates(isForce) | ||
.then((exchangeRates) => { | ||
return (0, utils_1.getExchageRate)(source, target, exchangeRates); | ||
}); | ||
} | ||
/** | ||
* Transfer | ||
* | ||
* Use {@link toTransfer} backend API result convert function and | ||
* prepare backend API parameters {@link prepareTransferOptions} function | ||
* | ||
* @param options - Transfer options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to completed transfer information object | ||
*/ | ||
transfer(options) { | ||
return this._transport.call('transfer', (0, utils_1.prepareTransferOptions)(options)) | ||
.then((result) => (0, casts_1.toTransfer)(result)); | ||
} | ||
/** | ||
* Create invoice | ||
@@ -154,2 +249,43 @@ * | ||
/** | ||
* Create check | ||
* | ||
* Use {@link toCheck} backend API result convert function and | ||
* prepare backend API parameters {@link prepareCreateCheckOptions} function | ||
* | ||
* @param options - New check options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to created check information object | ||
*/ | ||
createCheck(options) { | ||
return this._transport.call('createCheck', (0, utils_1.prepareCreateCheckOptions)(options)) | ||
.then((result) => (0, casts_1.toCheck)(result)); | ||
} | ||
/** | ||
* Delete invoice | ||
* | ||
* @param id - Invoice identifier | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response error | ||
* | ||
* @returns Promise, what resolved to boolean operation result status | ||
*/ | ||
deleteInvoice(id) { | ||
return this._transport.call('deleteInvoice', { invoice_id: (0, utils_1.prepareDeleteOptions)(id) }); | ||
} | ||
/** | ||
* Delete check | ||
* | ||
* @param id - Check identifier | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response error | ||
* | ||
* @returns Promise, what resolved to boolean operation result status | ||
*/ | ||
deleteCheck(id) { | ||
return this._transport.call('deleteCheck', { check_id: (0, utils_1.prepareDeleteOptions)(id) }); | ||
} | ||
/** | ||
* Get invoices | ||
@@ -177,3 +313,3 @@ * | ||
* | ||
* Use {@link toInvoicesPaginated} backend API result convert function and | ||
* Use {@link toInvoices} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetInvoicesPaginateOptions} function | ||
@@ -191,8 +327,81 @@ * | ||
return this._transport.call('getInvoices', prepared) | ||
// eslint-disable-next-line arrow-body-style | ||
.then((result) => { | ||
return (0, casts_1.toInvoicesPaginated)(options.page, this._pageSize, result); | ||
}); | ||
.then((result) => (0, casts_1.toInvoices)(result)); | ||
} | ||
/** | ||
* Get checks | ||
* | ||
* Use {@link toChecks} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetChecksOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to checks information object | ||
*/ | ||
getChecks(options = {}) { | ||
return this._transport.call('getChecks', (0, utils_1.prepareGetChecksOptions)(options)) | ||
.then((result) => (0, casts_1.toChecks)(result)); | ||
} | ||
/** | ||
* Get checks paginated | ||
* | ||
* Fetch checks with `page` options parameter, except `count` and `offset` | ||
* | ||
* See {@link Client.getPageSize} and {@link Client.setPageSize} | ||
* | ||
* Use {@link toChecks} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetChecksPaginateOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to checks information object | ||
*/ | ||
getChecksPaginate(options = {}) { | ||
const prepared = (0, utils_1.prepareGetChecksPaginateOptions)(this._pageSize, options); | ||
return this._transport.call('getChecks', prepared) | ||
.then((result) => (0, casts_1.toChecks)(result)); | ||
} | ||
/** | ||
* Get transfers | ||
* | ||
* Use {@link toTransfers} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetTransfersOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API or parsing response | ||
* | ||
* @returns Promise, what resolved to transfers information object | ||
*/ | ||
getTransfers(options = {}) { | ||
return this._transport.call('getTransfers', (0, utils_1.prepareGetTransfersOptions)(options)) | ||
.then((result) => (0, casts_1.toTransfers)(result)); | ||
} | ||
/** | ||
* Get transfers paginated | ||
* | ||
* Fetch checks with `page` options parameter, except `count` and `offset` | ||
* | ||
* See {@link Client.getPageSize} and {@link Client.setPageSize} | ||
* | ||
* Use {@link toTransfers} backend API result convert function and | ||
* prepare backend API parameters {@link prepareGetTransfersOptions} function | ||
* | ||
* @param options - Filters options | ||
* | ||
* @throws Error - If there is an error sending request to backend API, parsing response error | ||
* or options object is invalid | ||
* | ||
* @returns Promise, what resolved to transfers information object | ||
*/ | ||
getTransfersPaginate(options = {}) { | ||
const prepared = (0, utils_1.prepareGetTransfersPaginateOptions)(this._pageSize, options); | ||
return this._transport.call('getTransfers', prepared) | ||
.then((result) => (0, casts_1.toTransfers)(result)); | ||
} | ||
/** | ||
* Call backend API method directly (types unsafe) | ||
@@ -214,2 +423,26 @@ * | ||
} | ||
/** | ||
* Access to {@link CurrencyType} enumeration, used in {@link Invoice} type | ||
*/ | ||
Client.CurrencyType = casts_1.CurrencyType; | ||
/** | ||
* Access to {@link DetailedCurrencyType} enumeration, used in {@link Store.getCurrencies} | ||
* and {@link Client.getCurrency} methods results | ||
*/ | ||
Client.DetailedCurrencyType = casts_1.DetailedCurrencyType; | ||
/** | ||
* Access to {@link InvoiceStatus} enumeration, used in {@link Invoice} type, | ||
* {@link Client.getInvoices} and {@link Client.getInvoicesPaginate} methods options | ||
*/ | ||
Client.InvoiceStatus = casts_1.InvoiceStatus; | ||
/** | ||
* Access to {@link CheckStatus} enumeration, used in {@link Check} type, | ||
* {@link Client.getChecks} and {@link Client.getChecksPaginate} methods options | ||
*/ | ||
Client.CheckStatus = casts_1.CheckStatus; | ||
/** | ||
* Access to {@link TransferStatus} enumeration, used in {@link Transfer} type, | ||
* {@link Client.getTransfers} and {@link Client.getTransfersPaginate} methods options | ||
*/ | ||
Client.TransferStatus = casts_1.TransferStatus; | ||
exports.default = Client; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { ListenOptions } from 'net'; | ||
@@ -40,3 +39,3 @@ import { IncomingMessage } from 'http'; | ||
private _events; | ||
/** @inheritdoc */ | ||
/** {@inheritDoc Client:constructor} */ | ||
constructor(apiKey: string, endpoint?: string); | ||
@@ -46,3 +45,3 @@ /** | ||
* | ||
* Important: at the time of publication of version 0.2.0 (Dec 9, 2021), | ||
* Important: at the time of publication of version 0.3.0 (Sep 16, 2024), | ||
* API servers do not accept self-signed certificates | ||
@@ -49,0 +48,0 @@ * |
@@ -8,3 +8,2 @@ "use strict"; | ||
const casts_1 = require("../helpers/casts"); | ||
/* eslint-disable tsdoc/syntax */ | ||
/** | ||
@@ -19,3 +18,2 @@ * Check webhook data signature | ||
*/ | ||
/* eslint-enable tsdoc/syntax */ | ||
const checkSignature = (apiKey, signature, body) => { | ||
@@ -28,3 +26,3 @@ try { | ||
} | ||
catch (err) { | ||
catch { | ||
return false; | ||
@@ -51,3 +49,3 @@ } | ||
} | ||
catch (err) { | ||
catch { | ||
resolve(null); | ||
@@ -60,2 +58,3 @@ return; | ||
exports.readRequestBody = readRequestBody; | ||
// Because `tsdoc` not support `@category` tag, but `typedoc` support | ||
/* eslint-disable tsdoc/syntax */ | ||
@@ -71,4 +70,6 @@ /** | ||
class ClientEmitter extends Client_1.default { | ||
// Because `tsdoc` throw `tsdoc-reference-selector-missing-parens`, | ||
// but `typedoc` doesn't recognize reference in parentheses | ||
/* eslint-disable tsdoc/syntax */ | ||
/** @inheritdoc */ | ||
/** {@inheritDoc Client:constructor} */ | ||
/* eslint-enable tsdoc/syntax */ | ||
@@ -84,3 +85,3 @@ constructor(apiKey, endpoint = 'mainnet') { | ||
* | ||
* Important: at the time of publication of version 0.2.0 (Dec 9, 2021), | ||
* Important: at the time of publication of version 0.3.0 (Sep 16, 2024), | ||
* API servers do not accept self-signed certificates | ||
@@ -87,0 +88,0 @@ * |
@@ -57,2 +57,3 @@ "use strict"; | ||
exports.createFetchHandler = createFetchHandler; | ||
// Because `tsdoc` not support `@category` tag, but `typedoc` support | ||
/* eslint-disable tsdoc/syntax */ | ||
@@ -130,3 +131,2 @@ /** | ||
} | ||
exports.default = Store; | ||
/** Update period for fetching currencies from backend API in seconds */ | ||
@@ -138,1 +138,2 @@ Store._CURRENCIES_UPDATE_PERIOD = 3600; | ||
Store._ME_UPDATE_PERIOD = 3600; | ||
exports.default = Store; |
@@ -67,3 +67,3 @@ "use strict"; | ||
} | ||
catch (err) { | ||
catch { | ||
throw new Error(`Response parse error, raw reponse:\n${rawResponse}`); | ||
@@ -80,4 +80,4 @@ } | ||
} | ||
exports.default = Transport; | ||
/** RegExp to check API key */ | ||
Transport.KEY_CHECK_REGEXP = /\d{1,}:[a-zA-Z0-9]{35}/; | ||
exports.default = Transport; |
@@ -5,3 +5,7 @@ "use strict"; | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -18,3 +22,2 @@ if (k2 === undefined) k2 = k; | ||
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return https_1.Server; } }); | ||
// eslint-disable-next-line import/prefer-default-export | ||
var Client_1 = require("./classes/Client"); | ||
@@ -21,0 +24,0 @@ Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return Client_1.default; } }); |
@@ -1,8 +0,49 @@ | ||
import { InvoiceCurrency, InvoiceStatus, PaidBtnName } from './utils'; | ||
import { PaidBtnName } from './utils'; | ||
/** Result type value for {@link Client.getBalances} method */ | ||
export type Balance = { | ||
available: string; | ||
onhold: string; | ||
}; | ||
/** Result type for {@link Client.getBalances} method */ | ||
export declare type Balances = { | ||
[key: string]: string; | ||
export type Balances = { | ||
[variant in CryptoCurrencyCode]: Balance; | ||
}; | ||
/** Possible currencies types */ | ||
export declare type CurrencyType = 'blockchain' | 'fiat' | 'stablecoin'; | ||
/** Result type for {@link Client.getBalances} method */ | ||
export type BalancesType = { | ||
[variant in CryptoCurrencyCode]: string; | ||
}; | ||
export type CryptoCurrencyCode = 'USDT' | 'TON' | 'BTC' | 'ETH' | 'LTC' | 'BNB' | 'TRX' | 'USDC' | 'JET'; | ||
export type FiatCurrencyCode = 'USD' | 'EUR' | 'RUB' | 'BYN' | 'UAH' | 'GBP' | 'CNY' | 'KZT' | 'UZS' | 'GEL' | 'TRY' | 'AMD' | 'THB' | 'INR' | 'BRL' | 'IDR' | 'AZN' | 'AED' | 'PLN' | 'ILS'; | ||
export type CurrencyCode = CryptoCurrencyCode | FiatCurrencyCode | 'KGS' | 'TJS'; | ||
/** Possible currency types */ | ||
export declare enum CurrencyType { | ||
Crypto = "crypto", | ||
Fiat = "fiat", | ||
Unknown = "unknown" | ||
} | ||
/** Possible detailed currency types */ | ||
export declare enum DetailedCurrencyType { | ||
Blockchain = "blockchain", | ||
Stablecoin = "stablecoin", | ||
Fiat = "fiat", | ||
Unknown = "unknown" | ||
} | ||
/** Possible invoice statuses */ | ||
export declare enum InvoiceStatus { | ||
Active = "active", | ||
Paid = "paid", | ||
Expired = "expired", | ||
Unknown = "unknown" | ||
} | ||
/** Possible check statuses */ | ||
export declare enum CheckStatus { | ||
Active = "active", | ||
Activated = "activated", | ||
Unknown = "unknown" | ||
} | ||
/** Possible transfer statuses */ | ||
export declare enum TransferStatus { | ||
Completed = "completed", | ||
Unknown = "unknown" | ||
} | ||
/** | ||
@@ -12,3 +53,5 @@ * Currency type object for {@link Store.getCurrencies} | ||
*/ | ||
export declare type Currency = { | ||
export type Currency = { | ||
/** Currency code */ | ||
code: CurrencyCode; | ||
/** Currency name */ | ||
@@ -21,7 +64,7 @@ name: string; | ||
/** Currency type */ | ||
type: CurrencyType; | ||
type: DetailedCurrencyType; | ||
}; | ||
/** Result type for {@link Store.getCurrencies} method */ | ||
export declare type Currencies = { | ||
[key: string]: Currency; | ||
export type Currencies = { | ||
[variant in CurrencyCode]?: Currency; | ||
}; | ||
@@ -31,22 +74,77 @@ /** | ||
* and {@link Client.getExchangeRate} methods results | ||
* | ||
* @remarks | ||
* Used strings for currencies codes instead of {@link InvoiceCurrency}, | ||
* because exchange rates contains fiat currencies, who do not take part in other methods | ||
*/ | ||
export declare type ExchangeRate = { | ||
export type ExchangeRate = { | ||
/** Source currency code */ | ||
source: string; | ||
source: CurrencyCode; | ||
/** Target currency code */ | ||
target: string; | ||
target: CurrencyCode; | ||
/** Source to target exchange rate */ | ||
rate: number; | ||
rate: string; | ||
/** True, if the received rate is up-to-date */ | ||
isValid: boolean; | ||
}; | ||
/** Result type for {@link Store.getExchangeRates} method */ | ||
export declare type ExchangeRates = ExchangeRate[]; | ||
export type ExchangeRates = ExchangeRate[]; | ||
/** | ||
* Transfer type object for {@link Client.getTransfers} and {@link Client.transfer} methods results | ||
*/ | ||
export type Transfer = { | ||
/** Transfer identifier */ | ||
id: number; | ||
/** | ||
* Transfer spend identifier, optional because not returned from `transfer` API method call | ||
*/ | ||
spendId?: string; | ||
/** Telegram user ID the transfer was sent to */ | ||
userId: number; | ||
/** Transfer asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Transfer amount */ | ||
amount: string; | ||
/** Transfer status */ | ||
status: TransferStatus; | ||
/** Transfer completed date */ | ||
completedAt: Date; | ||
/** Check activated date */ | ||
comment?: string; | ||
}; | ||
/** | ||
* Check type object for {@link Client.getChecks}, {@link Client.getChecksPaginate} | ||
* and {@link Client.createCheck} methods results | ||
*/ | ||
export type Check = { | ||
/** Check identifier */ | ||
id: number; | ||
/** Check hash */ | ||
hash: string; | ||
/** Check asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Check amount */ | ||
amount: string; | ||
/** Check receive url for user by bot */ | ||
botCheckUrl: string; | ||
/** Check status */ | ||
status: CheckStatus; | ||
/** Check created date */ | ||
createdAt: Date; | ||
/** Check activated date */ | ||
activatedAt?: Date; | ||
/** | ||
* ID of the user who will be able to activate the check, | ||
* only if passed in check creation, | ||
* if exists, field `pinToUsername` will be absent | ||
*/ | ||
pinToUserId?: number; | ||
/** | ||
* A user with the specified username will be able to activate the check, | ||
* only if passed in check creation, | ||
* if exists, field `pinToUserId` will be absent | ||
*/ | ||
pinToUsername?: string; | ||
}; | ||
/** | ||
* Invoice type object for {@link Client.getInvoices}, {@link Client.getInvoicesPaginate}, | ||
* {@link Client.createInvoice} methods results and {@link ClientEmitter} `paid` event emit | ||
*/ | ||
export declare type Invoice = { | ||
export type Invoice = { | ||
/** Invoice identifier */ | ||
@@ -58,8 +156,14 @@ id: number; | ||
hash: string; | ||
/** Invoice currency type */ | ||
currencyType: CurrencyType; | ||
/** Invoice currency code */ | ||
currency: InvoiceCurrency; | ||
currency: CurrencyCode; | ||
/** Invoice amount */ | ||
amount: number; | ||
/** Invoice pay url for user */ | ||
payUrl: string; | ||
amount: string; | ||
/** Invoice pay url for user by bot */ | ||
botPayUrl: string; | ||
/** Invoice pay url for user by mini app */ | ||
miniAppPayUrl: string; | ||
/** Invoice pay url for user by web app */ | ||
webAppPayUrl: string; | ||
/** Is invoice allow user comment */ | ||
@@ -71,2 +175,4 @@ isAllowComments: boolean; | ||
createdAt: Date; | ||
/** Text of the hidden message, only if set in invoice creation */ | ||
hiddenMessage?: string; | ||
/** Is invoice paid anonymously, only for paid invoice */ | ||
@@ -76,2 +182,4 @@ isPaidAnonymously?: boolean; | ||
paidAt?: Date; | ||
/** Expiration date, only if set pay limit time in invoice creation */ | ||
expirationDate?: Date; | ||
/** Invoice displayed to user description, only if `description` passed in invoice creation */ | ||
@@ -100,26 +208,53 @@ description?: string; | ||
paidBtnUrl?: string; | ||
/** | ||
* Asset of service fees charged when the invoice was paid, only if status is InvoiceStatus.Paid | ||
*/ | ||
feeAsset?: CryptoCurrencyCode; | ||
/** | ||
* Amount of service fees charged when the invoice was paid, only if status is InvoiceStatus.Paid | ||
*/ | ||
fee?: number; | ||
/** | ||
* Price of the asset in USD, only if status is InvoiceStatus.Paid | ||
*/ | ||
usdRate?: number; | ||
/** | ||
* List of assets which can be used to pay the invoice, only if set in invoice creation | ||
*/ | ||
acceptedAssets?: CryptoCurrencyCode[]; | ||
/** | ||
* Cryptocurrency alphabetic code for which the invoice was paid, | ||
* only if currency type is CurrencyType.Fiat and status is InvoiceStatus.Paid | ||
*/ | ||
paidAsset?: CryptoCurrencyCode; | ||
/** | ||
* Amount of the invoice for which the invoice was paid, | ||
* only if currency type is CurrencyType.Fiat and status is InvoiceStatus.Paid | ||
*/ | ||
paidAmount?: number; | ||
/** | ||
* The rate of the paid_asset valued in the fiat currency, | ||
* only if currency type is CurrencyType.Fiat and status is InvoiceStatus.Paid | ||
*/ | ||
paidFiatRate?: number; | ||
}; | ||
/** | ||
* Invoices type object for {@link Client.getInvoices} | ||
* and {@link Client.getInvoicesPaginate} methods results | ||
*/ | ||
export declare type Invoices = { | ||
/** All items count value */ | ||
count: number; | ||
/** Fetched by passed filters items slice */ | ||
items: Invoice[]; | ||
/** Result type object for {@link Client.getStats} method */ | ||
export type Stats = { | ||
/** Total volume of paid invoices in USD */ | ||
volume: string; | ||
/** Conversion of all created invoices */ | ||
conversion: string; | ||
/** The unique number of users who have paid the invoice */ | ||
uniqueUsersCount: number; | ||
/** Total created invoice count */ | ||
createdInvoiceCount: number; | ||
/** Total paid invoice count */ | ||
paidInvoiceCount: number; | ||
/** The date on which the statistics calculation was started */ | ||
startAt: Date; | ||
/** The date on which the statistics calculation was ended */ | ||
endAt: Date; | ||
}; | ||
/** | ||
* Invoices type object for {@link Client.getInvoicesPaginate} methods results | ||
*/ | ||
export declare type InvoicesPaginated = { | ||
/** Pagination page number */ | ||
page: number; | ||
/** Pagination pages count */ | ||
pagesCount: number; | ||
/** Fetched by passed filters items slice */ | ||
items: Invoice[]; | ||
}; | ||
/** Result type object for {@link Client.getMe} method */ | ||
export declare type Me = { | ||
/** Result type object for {@link Store.getMe} method */ | ||
export type Me = { | ||
/** App identifier */ | ||
@@ -137,10 +272,8 @@ id: number; | ||
* @param input - Backend API result | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method, | ||
* need to correct format output in coins by currencies decimals counts | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* | ||
* @throws Error - If input parameter is not array | ||
* | ||
* @returns Converted result | ||
*/ | ||
export declare const toBalances: (input: any, currencies: Currencies, isReturnInNanos: boolean) => Balances; | ||
export declare const toBalances: (input: any) => Balances; | ||
/** | ||
@@ -176,2 +309,20 @@ * Convert backend API result to library result object to return in | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.createCheck} method and {@link toChecks} function | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
export declare const toCheck: (input: any) => Check; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.transfer} method and {@link toTransfers} function | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
export declare const toTransfer: (input: any) => Transfer; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getInvoices} and {@link Client.getInvoicesPaginate} | ||
@@ -184,6 +335,7 @@ * methods | ||
*/ | ||
export declare const toInvoices: (input: any) => Invoices; | ||
export declare const toInvoices: (input: any) => Invoice[]; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getInvoicesPaginate} method | ||
* {@link Client.getChecks} and {@link Client.getChecksPaginate} | ||
* methods | ||
* | ||
@@ -194,5 +346,24 @@ * @param input - Backend API result | ||
*/ | ||
export declare const toInvoicesPaginated: (page: number, pageSize: number, input: any) => InvoicesPaginated; | ||
export declare const toChecks: (input: any) => Check[]; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getTransfers} and {@link Client.getTransfersPaginate} | ||
* methods | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
export declare const toTransfers: (input: any) => Transfer[]; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getStats} method | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
export declare const toStats: (input: any) => Stats; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Store.getMe} method | ||
@@ -199,0 +370,0 @@ * |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toMe = exports.toInvoicesPaginated = exports.toInvoices = exports.toInvoice = exports.toExchangeRates = exports.toCurrencies = exports.toBalances = void 0; | ||
const utils_1 = require("./utils"); | ||
exports.toMe = exports.toStats = exports.toTransfers = exports.toChecks = exports.toInvoices = exports.toTransfer = exports.toCheck = exports.toInvoice = exports.toExchangeRates = exports.toCurrencies = exports.toBalances = exports.TransferStatus = exports.CheckStatus = exports.InvoiceStatus = exports.DetailedCurrencyType = exports.CurrencyType = void 0; | ||
/** Possible currency types */ | ||
var CurrencyType; | ||
(function (CurrencyType) { | ||
CurrencyType["Crypto"] = "crypto"; | ||
CurrencyType["Fiat"] = "fiat"; | ||
CurrencyType["Unknown"] = "unknown"; | ||
})(CurrencyType || (exports.CurrencyType = CurrencyType = {})); | ||
/** Possible detailed currency types */ | ||
var DetailedCurrencyType; | ||
(function (DetailedCurrencyType) { | ||
DetailedCurrencyType["Blockchain"] = "blockchain"; | ||
DetailedCurrencyType["Stablecoin"] = "stablecoin"; | ||
DetailedCurrencyType["Fiat"] = "fiat"; | ||
DetailedCurrencyType["Unknown"] = "unknown"; | ||
})(DetailedCurrencyType || (exports.DetailedCurrencyType = DetailedCurrencyType = {})); | ||
/** Possible invoice statuses */ | ||
var InvoiceStatus; | ||
(function (InvoiceStatus) { | ||
InvoiceStatus["Active"] = "active"; | ||
InvoiceStatus["Paid"] = "paid"; | ||
InvoiceStatus["Expired"] = "expired"; | ||
InvoiceStatus["Unknown"] = "unknown"; | ||
})(InvoiceStatus || (exports.InvoiceStatus = InvoiceStatus = {})); | ||
/** Possible check statuses */ | ||
var CheckStatus; | ||
(function (CheckStatus) { | ||
CheckStatus["Active"] = "active"; | ||
CheckStatus["Activated"] = "activated"; | ||
CheckStatus["Unknown"] = "unknown"; | ||
})(CheckStatus || (exports.CheckStatus = CheckStatus = {})); | ||
/** Possible transfer statuses */ | ||
var TransferStatus; | ||
(function (TransferStatus) { | ||
TransferStatus["Completed"] = "completed"; | ||
TransferStatus["Unknown"] = "unknown"; | ||
})(TransferStatus || (exports.TransferStatus = TransferStatus = {})); | ||
/** | ||
@@ -10,17 +45,13 @@ * Convert backend API result to library result object to return in | ||
* @param input - Backend API result | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method, | ||
* need to correct format output in coins by currencies decimals counts | ||
* @param isReturnInNanos - If true, return raw balances in nanos, | ||
* else return converted to coins balances | ||
* | ||
* @throws Error - If input parameter is not array | ||
* | ||
* @returns Converted result | ||
*/ | ||
const toBalances = (input, currencies, isReturnInNanos) => { | ||
const toBalances = (input) => { | ||
if (!Array.isArray(input)) | ||
return {}; | ||
throw new Error(`Input is not array: ${JSON.stringify(input)}`); | ||
// Conver array to HashMap structure | ||
return input.reduce((accumulator, value) => { | ||
if (value.currency_code && value.available) { | ||
accumulator[value.currency_code] = isReturnInNanos ? value.available : (0, utils_1.nonosToCoins)(value.available, value.currency_code, currencies); | ||
} | ||
accumulator[value.currency_code] = { available: value.available, onhold: value.onhold }; | ||
return accumulator; | ||
@@ -44,10 +75,11 @@ }, {}); | ||
const code = value.code.toString(); | ||
let type; | ||
let type = DetailedCurrencyType.Unknown; | ||
if (value.is_blockchain) | ||
type = 'blockchain'; | ||
type = DetailedCurrencyType.Blockchain; | ||
if (value.is_fiat) | ||
type = 'fiat'; | ||
type = DetailedCurrencyType.Fiat; | ||
if (value.is_stablecoin) | ||
type = 'stablecoin'; | ||
type = DetailedCurrencyType.Stablecoin; | ||
const currency = { | ||
code: code, | ||
name: value.name || '', | ||
@@ -57,2 +89,4 @@ decimals: value.decimals || 0, | ||
}; | ||
if (Object.prototype.hasOwnProperty.call(value, 'url')) | ||
currency.url = value.url; | ||
accumulator[code] = currency; | ||
@@ -78,3 +112,4 @@ } | ||
target: value.target || '', | ||
rate: parseFloat(value.rate), | ||
rate: value.rate, | ||
isValid: value.is_valid, | ||
})); | ||
@@ -95,13 +130,26 @@ }; | ||
id: input.invoice_id || 0, | ||
status: input.status || '', | ||
status: input.status || InvoiceStatus.Unknown, | ||
hash: input.hash || '', | ||
currency: input.asset || '', | ||
amount: parseFloat(input.amount) || 0, | ||
payUrl: input.pay_url || '', | ||
currencyType: input.currency_type || '', | ||
currency: input.asset || input.fiat || '', | ||
amount: input.amount || '0', | ||
isAllowComments: input.allow_comments || false, | ||
isAllowAnonymous: input.allow_anonymous || false, | ||
createdAt: new Date(input.created_at), | ||
botPayUrl: input.bot_invoice_url || '', | ||
miniAppPayUrl: input.mini_app_invoice_url || '', | ||
webAppPayUrl: input.web_app_invoice_url || '', | ||
}; | ||
if (invoice.currencyType === CurrencyType.Crypto) { | ||
invoice.currency = input.asset || ''; | ||
} | ||
if (invoice.currencyType === CurrencyType.Fiat) { | ||
invoice.currency = input.fiat || ''; | ||
} | ||
if (input.hidden_message !== undefined) | ||
invoice.hiddenMessage = input.hidden_message; | ||
if (input.paid_anonymously !== undefined) | ||
invoice.isPaidAnonymously = input.paid_anonymously; | ||
if (input.expiration_date !== undefined) | ||
invoice.expirationDate = new Date(input.expiration_date); | ||
if (input.paid_at !== undefined) | ||
@@ -117,2 +165,16 @@ invoice.paidAt = new Date(input.paid_at); | ||
invoice.comment = input.comment; | ||
if (input.paid_usd_rate !== undefined) | ||
invoice.usdRate = parseFloat(input.paid_usd_rate) || 0; | ||
if (input.fee_asset !== undefined) | ||
invoice.feeAsset = input.fee_asset || ''; | ||
if (input.fee_amount !== undefined) | ||
invoice.fee = input.fee_amount || 0; | ||
if (input.accepted_assets !== undefined) | ||
invoice.acceptedAssets = input.accepted_assets; | ||
if (input.paid_asset !== undefined) | ||
invoice.paidAsset = input.paid_asset || ''; | ||
if (input.paid_amount !== undefined) | ||
invoice.paidAmount = parseFloat(input.paid_amount) || 0; | ||
if (input.paid_fiat_rate !== undefined) | ||
invoice.paidFiatRate = parseFloat(input.paid_fiat_rate) || 0; | ||
if (input.payload !== undefined) { | ||
@@ -123,3 +185,3 @@ let payload; | ||
} | ||
catch (err) { | ||
catch { | ||
payload = input.payload; | ||
@@ -134,2 +196,57 @@ } | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.createCheck} method and {@link toChecks} function | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
const toCheck = (input) => { | ||
const check = { | ||
id: input.check_id || 0, | ||
hash: input.hash || '', | ||
asset: input.asset || '', | ||
amount: input.amount || '0', | ||
botCheckUrl: input.bot_check_url || '', | ||
status: input.status || CheckStatus.Unknown, | ||
createdAt: new Date(input.created_at), | ||
}; | ||
if (input.activated_at !== undefined) | ||
check.activatedAt = new Date(input.activated_at); | ||
if (input.pin_to_user !== undefined && input.pin_to_user.pin_by !== undefined) { | ||
if (input.pin_to_user.pin_by === 'id' && input.pin_to_user.user_id !== undefined) { | ||
check.pinToUserId = input.pin_to_user.user_id; | ||
} | ||
if (input.pin_to_user.pin_by === 'username' && input.pin_to_user.username !== undefined) { | ||
check.pinToUsername = input.pin_to_user.username; | ||
} | ||
} | ||
return check; | ||
}; | ||
exports.toCheck = toCheck; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.transfer} method and {@link toTransfers} function | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
const toTransfer = (input) => { | ||
const transfer = { | ||
id: input.transfer_id || 0, | ||
userId: input.user_id || 0, | ||
asset: input.asset || '', | ||
amount: input.amount || '0', | ||
status: input.status || TransferStatus.Unknown, | ||
completedAt: new Date(input.completed_at), | ||
}; | ||
if (input.spend_id !== undefined) | ||
transfer.spendId = input.spend_id; | ||
if (input.comment !== undefined) | ||
transfer.comment = input.comment; | ||
return transfer; | ||
}; | ||
exports.toTransfer = toTransfer; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getInvoices} and {@link Client.getInvoicesPaginate} | ||
@@ -146,6 +263,3 @@ * methods | ||
items = input.items.map(exports.toInvoice); | ||
return { | ||
count: input.count || 0, | ||
items, | ||
}; | ||
return items; | ||
}; | ||
@@ -155,3 +269,4 @@ exports.toInvoices = toInvoices; | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getInvoicesPaginate} method | ||
* {@link Client.getChecks} and {@link Client.getChecksPaginate} | ||
* methods | ||
* | ||
@@ -162,15 +277,45 @@ * @param input - Backend API result | ||
*/ | ||
const toInvoicesPaginated = (page, pageSize, input) => { | ||
const toChecks = (input) => { | ||
let items = []; | ||
if (Array.isArray(input.items)) | ||
items = input.items.map(exports.toInvoice); | ||
return { | ||
page, | ||
pagesCount: Math.ceil((input.count || 0) / pageSize), | ||
items, | ||
}; | ||
items = input.items.map(exports.toCheck); | ||
return items; | ||
}; | ||
exports.toInvoicesPaginated = toInvoicesPaginated; | ||
exports.toChecks = toChecks; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getTransfers} and {@link Client.getTransfersPaginate} | ||
* methods | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
const toTransfers = (input) => { | ||
let items = []; | ||
if (Array.isArray(input.items)) | ||
items = input.items.map(exports.toTransfer); | ||
return items; | ||
}; | ||
exports.toTransfers = toTransfers; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Client.getStats} method | ||
* | ||
* @param input - Backend API result | ||
* | ||
* @returns Converted result | ||
*/ | ||
const toStats = (input) => ({ | ||
volume: input.volume || '0', | ||
conversion: input.conversion || '0', | ||
uniqueUsersCount: input.unique_users_count || 0, | ||
createdInvoiceCount: input.created_invoice_count || 0, | ||
paidInvoiceCount: input.paid_invoice_count || 0, | ||
startAt: new Date(input.start_at ? input.start_at : 0), | ||
endAt: new Date(input.end_at ? input.end_at : 0), | ||
}); | ||
exports.toStats = toStats; | ||
/** | ||
* Convert backend API result to library result object to return in | ||
* {@link Store.getMe} method | ||
@@ -177,0 +322,0 @@ * |
@@ -1,10 +0,96 @@ | ||
import { Currencies, ExchangeRates } from './casts'; | ||
import { CurrencyType, CryptoCurrencyCode, FiatCurrencyCode, InvoiceStatus, CheckStatus, ExchangeRates } from './casts'; | ||
/** Possible backend API methods names */ | ||
export declare type ApiMethod = 'getMe' | 'createInvoice' | 'getInvoices' | 'getBalance' | 'getExchangeRates' | 'getCurrencies'; | ||
export type ApiMethod = 'getMe' | 'getStats' | 'createInvoice' | 'createCheck' | 'deleteInvoice' | 'deleteCheck' | 'getInvoices' | 'getChecks' | 'getBalance' | 'getExchangeRates' | 'getCurrencies' | 'transfer' | 'getTransfers'; | ||
/** Options object type for {@link Client.getStats} method */ | ||
export type GetStatsOptions = { | ||
/** Date from which start calculating statistics */ | ||
startAt?: Date; | ||
/** The date on which to finish calculating statistics */ | ||
endAt?: Date; | ||
}; | ||
/** Backend options object type for {@link Client.getStats} method */ | ||
export type GetStatsBackendOptions = { | ||
/** Date from which start calculating statistics */ | ||
start_at?: string; | ||
/** The date on which to finish calculating statistics */ | ||
end_at?: string; | ||
}; | ||
/** Options object type for {@link Client.transfer} method */ | ||
export type TransferOptions = { | ||
/** User ID in Telegram */ | ||
userId: number; | ||
/** Transfer asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Transfer amount */ | ||
amount: number | string; | ||
/** | ||
* Random UTF-8 string unique per transfer for idempotent requests. | ||
* The same spend_id can be accepted only once from your app. | ||
* Up to 64 symbols. | ||
*/ | ||
spendId: string; | ||
/** | ||
* Comment for the transfer. | ||
* Users will see this comment in the notification about the transfer | ||
*/ | ||
comment?: string; | ||
/** Pass true to not send to the user the notification about the transfer */ | ||
disableSendNotification?: boolean; | ||
}; | ||
/** Backend options object type for {@link Client.transfer} method */ | ||
export type TransferBackendOptions = { | ||
/** User ID in Telegram */ | ||
user_id: number; | ||
/** Transfer asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Transfer amount */ | ||
amount: number | string; | ||
/** | ||
* Random UTF-8 string unique per transfer for idempotent requests. | ||
* The same spend_id can be accepted only once from your app. | ||
* Up to 64 symbols. | ||
*/ | ||
spend_id: string; | ||
/** | ||
* Comment for the transfer. | ||
* Users will see this comment in the notification about the transfer | ||
*/ | ||
comment?: string; | ||
/** Pass true to not send to the user the notification about the transfer */ | ||
disable_send_notification?: boolean; | ||
}; | ||
/** Options object type for {@link Client.createCheck} method */ | ||
export type CreateCheckOptions = { | ||
/** Check asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Check amount */ | ||
amount: number | string; | ||
/** ID of the user who will be able to activate the check */ | ||
pinToUserId?: number; | ||
/** A user with the specified username will be able to activate the check */ | ||
pinToUsername?: string; | ||
}; | ||
/** Backend options object type for {@link Client.createCheck} method */ | ||
export type CreateCheckBackendOptions = { | ||
/** Invoice asset */ | ||
asset: CryptoCurrencyCode; | ||
/** Invoice amount */ | ||
amount: string; | ||
/** ID of the user who will be able to activate the check */ | ||
pin_to_user_id?: number; | ||
/** A user with the specified username will be able to activate the check */ | ||
pin_to_username?: string; | ||
}; | ||
/** Options object type for {@link Client.createInvoice} method */ | ||
export declare type CreateInvoiceOptions = { | ||
/** Invoice currency */ | ||
currency: InvoiceCurrency; | ||
export type CreateInvoiceOptions = { | ||
/** Invoice amount */ | ||
amount: number | string; | ||
/** Currency type */ | ||
currencyType?: CurrencyType.Crypto | CurrencyType.Fiat; | ||
/** Invoice asset */ | ||
asset?: CryptoCurrencyCode; | ||
/** Invoice fiat */ | ||
fiat?: FiatCurrencyCode; | ||
/** List of cryptocurrency alphabetic codes */ | ||
acceptedAssets?: CryptoCurrencyCode[]; | ||
/** Invoice description, displayed to user, up to 1024 symbols */ | ||
@@ -25,9 +111,19 @@ description?: string; | ||
isAllowAnonymous?: boolean; | ||
/** Text of the message which will be presented to a user after the invoice is paid */ | ||
hiddenMessage?: string; | ||
/** You can set a payment time limit for the invoice in seconds */ | ||
expiresIn?: number; | ||
}; | ||
/** Backend options object type for {@link Client.createInvoice} method */ | ||
export declare type CreateInvoiceBackendOptions = { | ||
/** Invoice currency */ | ||
asset: InvoiceCurrency; | ||
export type CreateInvoiceBackendOptions = { | ||
/** Invoice amount */ | ||
amount: number; | ||
amount: string; | ||
/** Currency type */ | ||
currency_type?: CurrencyType.Crypto | CurrencyType.Fiat; | ||
/** Invoice asset */ | ||
asset?: CryptoCurrencyCode; | ||
/** Invoice fiat */ | ||
fiat?: FiatCurrencyCode; | ||
/** List of cryptocurrency alphabetic codes separated comma */ | ||
accepted_assets?: string; | ||
/** Invoice description, displayed to user */ | ||
@@ -45,11 +141,17 @@ description?: string; | ||
allow_anonymous?: boolean; | ||
/** Text of the message which will be presented to a user after the invoice is paid */ | ||
hidden_message?: string; | ||
/** You can set a payment time limit for the invoice in seconds */ | ||
expires_in?: number; | ||
}; | ||
/** Options object type for {@link Client.getInvoices} method */ | ||
export declare type GetInvoicesOptions = { | ||
/** Invoices currency filter */ | ||
currency?: InvoiceCurrency; | ||
export type GetInvoicesOptions = { | ||
/** Invoices crypto currency filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Invoices fiat currency filter */ | ||
fiat?: FiatCurrencyCode; | ||
/** Invoices identifiers filter */ | ||
ids?: (number | string)[]; | ||
ids?: number[]; | ||
/** Invoices status filter */ | ||
status?: InvoiceStatus; | ||
status?: GetInvoicesStatus; | ||
/** Number of invoices to skip */ | ||
@@ -61,20 +163,27 @@ offset?: number; | ||
/** Options object type for {@link Client.getInvoicesPaginate} method */ | ||
export declare type GetInvoicesPaginateOptions = { | ||
/** Invoices currency filter */ | ||
currency?: InvoiceCurrency; | ||
export type GetInvoicesPaginateOptions = { | ||
/** Invoices crypto currency filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Invoices fiat currency filter */ | ||
fiat?: FiatCurrencyCode; | ||
/** Invoices identifiers filter */ | ||
ids?: (number | string)[]; | ||
ids?: number[]; | ||
/** Invoices status filter */ | ||
status?: InvoiceStatus; | ||
status?: GetInvoicesStatus; | ||
/** Pagination page number */ | ||
page?: number; | ||
}; | ||
/** Backend options object type for {@link Client.getInvoices} method */ | ||
export declare type GetInvoicesBackendOptions = { | ||
/** Invoices currency filter */ | ||
asset?: InvoiceCurrency; | ||
/** | ||
* Backend options object type for {@link Client.getInvoices} | ||
* and {@link Client.getInvoicesPaginate} methods | ||
*/ | ||
export type GetInvoicesBackendOptions = { | ||
/** Invoices crypto currency filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Invoices fiat currency filter */ | ||
fiat?: FiatCurrencyCode; | ||
/** Invoices identifiers filter */ | ||
invoice_ids?: number[]; | ||
invoice_ids?: string; | ||
/** Invoices status filter */ | ||
status?: InvoiceStatus; | ||
status?: GetInvoicesStatus; | ||
/** Number of invoices to skip */ | ||
@@ -85,14 +194,98 @@ offset?: number; | ||
}; | ||
/** Possible invoices currencies */ | ||
export declare type InvoiceCurrency = 'BTC' | 'ETH' | 'TON' | 'BNB' | 'BUSD' | 'USDC' | 'USDT'; | ||
/** Options object type for {@link Client.getChecks} method */ | ||
export type GetChecksOptions = { | ||
/** Checks asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Checks identifiers filter */ | ||
ids?: number[]; | ||
/** Checks status filter */ | ||
status?: GetChecksStatus; | ||
/** Number of checks to skip */ | ||
offset?: number; | ||
/** Number of checks returned */ | ||
count?: number; | ||
}; | ||
/** Options object type for {@link Client.getChecksPaginate} method */ | ||
export type GetChecksPaginateOptions = { | ||
/** Checks asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Checks identifiers filter */ | ||
ids?: number[]; | ||
/** Checks status filter */ | ||
status?: GetChecksStatus; | ||
/** Pagination page number */ | ||
page?: number; | ||
}; | ||
/** | ||
* Backend options object type for {@link Client.getChecks} | ||
* and {@link Client.getChecksPaginate} methods | ||
*/ | ||
export type GetChecksBackendOptions = { | ||
/** Checks asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Checks identifiers filter */ | ||
check_ids?: string; | ||
/** Checks status filter */ | ||
status?: GetChecksStatus; | ||
/** Number of checks to skip */ | ||
offset?: number; | ||
/** Number of checks returned */ | ||
count?: number; | ||
}; | ||
/** Options object type for {@link Client.getTransfers} method */ | ||
export type GetTransfersOptions = { | ||
/** Transfer asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Transfers identifiers filter */ | ||
ids?: number[]; | ||
/** Transfer spend identifier */ | ||
spendId?: string; | ||
/** Number of transfers to skip */ | ||
offset?: number; | ||
/** Number of transfers returned */ | ||
count?: number; | ||
}; | ||
/** Options object type for {@link Client.getTransfersPaginate} method */ | ||
export type GetTransfersPaginateOptions = { | ||
/** Transfer asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Transfers identifiers filter */ | ||
ids?: number[]; | ||
/** Transfer spend identifier */ | ||
spendId?: string; | ||
/** Pagination page number */ | ||
page?: number; | ||
}; | ||
/** | ||
* Backend options object type for {@link Client.getTransfers} | ||
* and {@link Client.getTransfersPaginate} methods | ||
*/ | ||
export type GetTransfersBackendOptions = { | ||
/** Transfer asset filter */ | ||
asset?: CryptoCurrencyCode; | ||
/** Transfers identifiers filter */ | ||
transfer_ids?: string; | ||
/** Transfer spend identifier */ | ||
spend_id?: string; | ||
/** Number of transfers to skip */ | ||
offset?: number; | ||
/** Number of transfers returned */ | ||
count?: number; | ||
}; | ||
/** | ||
* Possible invoices statuses | ||
* - active - Unpaid invoice | ||
* - paid - Paid invoice | ||
* - {@link InvoiceStatus.Active} - Unpaid invoice | ||
* - {@link InvoiceStatus.Paid} - Paid invoice | ||
*/ | ||
export declare type InvoiceStatus = 'active' | 'paid'; | ||
export type GetInvoicesStatus = InvoiceStatus.Active | InvoiceStatus.Paid; | ||
/** | ||
* Possible checks statuses | ||
* - {@link CheckStatus.Active} - Active check | ||
* - {@link CheckStatus.Activated} - Activated check | ||
*/ | ||
export type GetChecksStatus = CheckStatus.Active | CheckStatus.Activated; | ||
/** | ||
* Express.js-like API middleware handler | ||
*/ | ||
export declare type Middleware = (req: any, res: any) => void; | ||
export type Middleware = (req: any, res: any) => void; | ||
/** | ||
@@ -105,3 +298,3 @@ * Paid button types, button text depends on the type | ||
*/ | ||
export declare type PaidBtnName = 'viewItem' | 'openChannel' | 'openBot' | 'callback'; | ||
export type PaidBtnName = 'viewItem' | 'openChannel' | 'openBot' | 'callback'; | ||
/** | ||
@@ -113,7 +306,6 @@ * Return exchange rate to passed currencies pair | ||
* @param exchangeRates - Exchange rates information from {@link Store.getExchangeRates} method | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method | ||
* | ||
* @returns Exchange rate or zero, if currencies pair not exists | ||
*/ | ||
export declare const getExchageRate: (source: string, target: string, exchangeRates: ExchangeRates, currencies: Currencies) => number; | ||
export declare const getExchageRate: (source: string, target: string, exchangeRates: ExchangeRates) => string; | ||
/** | ||
@@ -128,15 +320,35 @@ * Check is string is valid url | ||
/** | ||
* Convert nanos string value to the form of string of whole coins | ||
* Convert {@link GetStatsOptions} object to using backend API method | ||
* parameters {@link GetStatsBackendOptions} object | ||
* | ||
* @remarks | ||
* Currencies need to know how many characters after decimal point are used by currency | ||
* @param options - Library {@link Client.getStats} method options object | ||
* | ||
* @param value - Value in nanos | ||
* @param currencyCode - Currency code | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Representation of amount in coins | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const nonosToCoins: (value: string, currencyCode: InvoiceCurrency, currencies: Currencies) => string; | ||
export declare const prepareGetStatsOptions: (options: GetStatsOptions) => GetStatsBackendOptions; | ||
/** | ||
* Convert {@link CreateCheckOptions} object to using backend API method | ||
* parameters {@link CreateCheckBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.createCheck} method options object | ||
* | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareTransferOptions: (options: TransferOptions) => TransferBackendOptions; | ||
/** | ||
* Convert {@link CreateCheckOptions} object to using backend API method | ||
* parameters {@link CreateCheckBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.createCheck} method options object | ||
* | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareCreateCheckOptions: (options: CreateCheckOptions) => CreateCheckBackendOptions; | ||
/** | ||
* Convert {@link CreateInvoiceOptions} object to using backend API method | ||
@@ -153,2 +365,12 @@ * parameters {@link CreateInvoiceBackendOptions} object | ||
/** | ||
* Convert identifier to using backend API delete methods | ||
* | ||
* @param id - Passed identifier | ||
* | ||
* @throws Error - If options identifier invalid | ||
* | ||
* @returns Identifier number | ||
*/ | ||
export declare const prepareDeleteOptions: (id: any) => number; | ||
/** | ||
* Convert {@link GetInvoicesOptions} object to using backend API method | ||
@@ -166,3 +388,3 @@ * parameters {@link GetInvoicesBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getInvoices} method options object | ||
* @param options - Library {@link Client.getInvoicesPaginate} method options object | ||
* | ||
@@ -172,1 +394,37 @@ * @returns Object with corresponding backend API method parameters | ||
export declare const prepareGetInvoicesPaginateOptions: (pageSize: number, options: GetInvoicesPaginateOptions) => GetInvoicesBackendOptions; | ||
/** | ||
* Convert {@link GetChecksOptions} object to using backend API method | ||
* parameters {@link GetChecksBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getChecks} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareGetChecksOptions: (options: GetChecksOptions) => GetChecksBackendOptions; | ||
/** | ||
* Convert {@link GetChecksPaginateOptions} object to using backend API method | ||
* parameters {@link GetChecksBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getChecksPaginate} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareGetChecksPaginateOptions: (pageSize: number, options: GetChecksPaginateOptions) => GetChecksBackendOptions; | ||
/** | ||
* Convert {@link GetTransfersOptions} object to using backend API method | ||
* parameters {@link GetTransfersBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getTransfers} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareGetTransfersOptions: (options: GetTransfersOptions) => GetTransfersBackendOptions; | ||
/** | ||
* Convert {@link GetTransfersPaginateOptions} object to using backend API method | ||
* parameters {@link GetTransfersBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getTransfersPaginate} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
export declare const prepareGetTransfersPaginateOptions: (pageSize: number, options: GetTransfersPaginateOptions) => GetTransfersBackendOptions; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.prepareGetInvoicesPaginateOptions = exports.prepareGetInvoicesOptions = exports.prepareCreateInvoiceOptions = exports.nonosToCoins = exports.isValidUrl = exports.getExchageRate = void 0; | ||
exports.prepareGetTransfersPaginateOptions = exports.prepareGetTransfersOptions = exports.prepareGetChecksPaginateOptions = exports.prepareGetChecksOptions = exports.prepareGetInvoicesPaginateOptions = exports.prepareGetInvoicesOptions = exports.prepareDeleteOptions = exports.prepareCreateInvoiceOptions = exports.prepareCreateCheckOptions = exports.prepareTransferOptions = exports.prepareGetStatsOptions = exports.isValidUrl = exports.getExchageRate = void 0; | ||
const casts_1 = require("./casts"); | ||
/** | ||
@@ -10,9 +11,7 @@ * Return exchange rate to passed currencies pair | ||
* @param exchangeRates - Exchange rates information from {@link Store.getExchangeRates} method | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method | ||
* | ||
* @returns Exchange rate or zero, if currencies pair not exists | ||
*/ | ||
const getExchageRate = (source, target, exchangeRates, currencies) => { | ||
var _a; | ||
let rate = NaN; | ||
const getExchageRate = (source, target, exchangeRates) => { | ||
let rate = ''; | ||
for (let i = 0, l = exchangeRates.length; i < l; i += 1) { | ||
@@ -22,16 +21,10 @@ const exchangeRate = exchangeRates[i]; | ||
if (exchangeRate.source === source && exchangeRate.target === target) { | ||
rate = exchangeRate.rate; | ||
if (exchangeRate.isValid) | ||
rate = exchangeRate.rate; | ||
break; | ||
} | ||
// If source and target reverse to direction in Store.getExchangeRates method result | ||
if (exchangeRate.source === target && exchangeRate.target === source) { | ||
rate = 1 / exchangeRate.rate; | ||
break; | ||
} | ||
} | ||
// eslint-disable-next-line no-restricted-globals | ||
if (isNaN(rate)) | ||
return 0; | ||
const numberOfNanosSigns = ((_a = currencies[target]) === null || _a === void 0 ? void 0 : _a.decimals) || 8; | ||
return parseFloat(rate.toFixed(numberOfNanosSigns)); | ||
if (rate === '') | ||
return '0'; | ||
return rate; | ||
}; | ||
@@ -53,36 +46,82 @@ exports.getExchageRate = getExchageRate; | ||
/** | ||
* Convert nanos string value to the form of string of whole coins | ||
* Convert {@link GetStatsOptions} object to using backend API method | ||
* parameters {@link GetStatsBackendOptions} object | ||
* | ||
* @remarks | ||
* Currencies need to know how many characters after decimal point are used by currency | ||
* @param options - Library {@link Client.getStats} method options object | ||
* | ||
* @param value - Value in nanos | ||
* @param currencyCode - Currency code | ||
* @param currencies - Currencies information from {@link Store.getCurrencies} method | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Representation of amount in coins | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const nonosToCoins = (value, currencyCode, currencies) => { | ||
var _a; | ||
let result = value; | ||
// Use default value as `8` if decimals property is lost | ||
const numberOfNanosSigns = ((_a = currencies[currencyCode]) === null || _a === void 0 ? void 0 : _a.decimals) || 8; | ||
const zerosNeed = numberOfNanosSigns - result.length; | ||
if (zerosNeed > 0) { | ||
let zeros = ''; | ||
for (let i = 0; i < zerosNeed; i += 1) | ||
zeros += '0'; | ||
result = zeros + result; | ||
const prepareGetStatsOptions = (options) => { | ||
const prepared = {}; | ||
if (options.startAt === undefined && options.endAt === undefined) | ||
return prepared; | ||
if (options.startAt === undefined || !(options.startAt instanceof Date)) { | ||
throw new Error('Field `startAt` must be a Date'); | ||
} | ||
if (result.length === numberOfNanosSigns) | ||
result = `0.${result}`; | ||
else { | ||
const pointPosition = result.length - numberOfNanosSigns; | ||
result = `${result.substr(0, pointPosition)}.${result.substr(pointPosition)}`; | ||
if (options.endAt === undefined || !(options.endAt instanceof Date)) { | ||
throw new Error('Field `endAt` must be a Date'); | ||
} | ||
// Remove trailing zeros | ||
return result.replace(/0+$/, ''); | ||
prepared.start_at = options.startAt.toISOString(); | ||
prepared.end_at = options.endAt.toISOString(); | ||
return prepared; | ||
}; | ||
exports.nonosToCoins = nonosToCoins; | ||
exports.prepareGetStatsOptions = prepareGetStatsOptions; | ||
/** | ||
* Convert {@link CreateCheckOptions} object to using backend API method | ||
* parameters {@link CreateCheckBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.createCheck} method options object | ||
* | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareTransferOptions = (options) => { | ||
if (options.comment !== undefined && options.comment.length > 1024) { | ||
throw new Error('Comment can\'t be longer than 1024 characters'); | ||
} | ||
// Create object with required parameters | ||
const prepared = { | ||
user_id: options.userId, | ||
spend_id: options.spendId, | ||
asset: options.asset, | ||
amount: typeof options.amount === 'number' ? '' + options.amount : options.amount, | ||
}; | ||
if (options.disableSendNotification !== undefined) { | ||
prepared.disable_send_notification = options.disableSendNotification; | ||
} | ||
if (options.comment !== undefined) | ||
prepared.comment = options.comment; | ||
return prepared; | ||
}; | ||
exports.prepareTransferOptions = prepareTransferOptions; | ||
/** | ||
* Convert {@link CreateCheckOptions} object to using backend API method | ||
* parameters {@link CreateCheckBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.createCheck} method options object | ||
* | ||
* @throws Error - If options object invalid | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareCreateCheckOptions = (options) => { | ||
// Create object with required parameters | ||
const prepared = { | ||
asset: options.asset, | ||
amount: typeof options.amount === 'number' ? '' + options.amount : options.amount, | ||
}; | ||
if (options.pinToUserId !== undefined) | ||
prepared.pin_to_user_id = options.pinToUserId; | ||
if (options.pinToUsername !== undefined) | ||
prepared.pin_to_username = options.pinToUsername; | ||
if (options.pinToUserId !== undefined && options.pinToUsername !== undefined) { | ||
throw new Error('Pass only one of `pinToUserId` and `pinToUsername`'); | ||
} | ||
return prepared; | ||
}; | ||
exports.prepareCreateCheckOptions = prepareCreateCheckOptions; | ||
/** | ||
* Convert {@link CreateInvoiceOptions} object to using backend API method | ||
@@ -99,8 +138,17 @@ * parameters {@link CreateInvoiceBackendOptions} object | ||
// Check is options object valid | ||
if (options.description && options.description.length > 1024) { | ||
if (options.description !== undefined && options.description.length > 1024) { | ||
throw new Error('Description can\'t be longer than 1024 characters'); | ||
} | ||
if (options.paidBtnName && !options.paidBtnUrl) { | ||
if (options.paidBtnName !== undefined && !options.paidBtnUrl) { | ||
throw new Error('Require paidBtnUrl parameter if paidBtnName parameter pass'); | ||
} | ||
if (options.hiddenMessage !== undefined && options.hiddenMessage.length > 2048) { | ||
throw new Error('Hidden message can\'t be longer than 2048 characters'); | ||
} | ||
if (options.expiresIn !== undefined | ||
&& (typeof options.expiresIn !== 'number' | ||
|| options.expiresIn < 1 | ||
|| options.expiresIn > 2678400)) { | ||
throw new Error('Expires must be a number between 1-2678400'); | ||
} | ||
let payload; | ||
@@ -118,8 +166,31 @@ if (options.payload !== undefined) { | ||
const prepared = { | ||
asset: options.currency, | ||
amount: +options.amount, | ||
amount: typeof options.amount === 'number' ? '' + options.amount : options.amount, | ||
}; | ||
const currencyType = options.currencyType || casts_1.CurrencyType.Crypto; | ||
prepared.currency_type = currencyType; | ||
if (currencyType === casts_1.CurrencyType.Crypto) { | ||
const asset = options.asset; | ||
if (!asset) | ||
throw new Error('Field `asset` required for crypto currency type'); | ||
prepared.asset = asset; | ||
} | ||
if (currencyType === casts_1.CurrencyType.Fiat) { | ||
const fiat = options.fiat; | ||
if (!fiat) | ||
throw new Error('Field `fiat` required for fiat currency type'); | ||
prepared.fiat = fiat; | ||
if (options.acceptedAssets !== undefined) { | ||
if (!Array.isArray(options.acceptedAssets)) { | ||
throw new Error('Field `acceptedAssets` must be array'); | ||
} | ||
prepared.accepted_assets = options.acceptedAssets.join(','); | ||
} | ||
} | ||
// Same names | ||
if (options.expiresIn !== undefined) | ||
prepared.expires_in = options.expiresIn; | ||
if (options.description !== undefined) | ||
prepared.description = options.description; | ||
if (options.hiddenMessage !== undefined) | ||
prepared.hidden_message = options.hiddenMessage; | ||
if (payload !== undefined) | ||
@@ -140,2 +211,18 @@ prepared.payload = payload; | ||
/** | ||
* Convert identifier to using backend API delete methods | ||
* | ||
* @param id - Passed identifier | ||
* | ||
* @throws Error - If options identifier invalid | ||
* | ||
* @returns Identifier number | ||
*/ | ||
const prepareDeleteOptions = (id) => { | ||
if (typeof id !== 'number' || isNaN(id) || id < 1) { | ||
throw new Error('Identifier must be a valid positive number'); | ||
} | ||
return id; | ||
}; | ||
exports.prepareDeleteOptions = prepareDeleteOptions; | ||
/** | ||
* Convert {@link GetInvoicesOptions} object to using backend API method | ||
@@ -159,7 +246,8 @@ * parameters {@link GetInvoicesBackendOptions} object | ||
// Different names | ||
if (options.currency !== undefined) | ||
prepared.asset = options.currency; | ||
if (options.ids !== undefined) { | ||
prepared.invoice_ids = options.ids.map((value) => +value); | ||
} | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.fiat !== undefined) | ||
prepared.fiat = options.fiat; | ||
if (options.ids !== undefined) | ||
prepared.invoice_ids = options.ids.join(','); | ||
return prepared; | ||
@@ -172,3 +260,3 @@ }; | ||
* | ||
* @param options - Library {@link Client.getInvoices} method options object | ||
* @param options - Library {@link Client.getInvoicesPaginate} method options object | ||
* | ||
@@ -184,7 +272,8 @@ * @returns Object with corresponding backend API method parameters | ||
// Different names | ||
if (options.currency !== undefined) | ||
prepared.asset = options.currency; | ||
if (options.ids !== undefined) { | ||
prepared.invoice_ids = options.ids.map((value) => +value); | ||
} | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.fiat !== undefined) | ||
prepared.fiat = options.fiat; | ||
if (options.ids !== undefined) | ||
prepared.invoice_ids = options.ids.join(','); | ||
// Paginate options | ||
@@ -199,1 +288,108 @@ let page = options.page ? +options.page : 1; | ||
exports.prepareGetInvoicesPaginateOptions = prepareGetInvoicesPaginateOptions; | ||
/** | ||
* Convert {@link GetChecksOptions} object to using backend API method | ||
* parameters {@link GetChecksBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getChecks} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareGetChecksOptions = (options) => { | ||
// Create empty object, method doesn't have required parameters | ||
const prepared = {}; | ||
// Same names | ||
if (options.status !== undefined) | ||
prepared.status = options.status; | ||
if (options.offset !== undefined) | ||
prepared.offset = options.offset; | ||
if (options.count !== undefined) | ||
prepared.count = options.count; | ||
// Different names | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.ids !== undefined) | ||
prepared.check_ids = options.ids.join(','); | ||
return prepared; | ||
}; | ||
exports.prepareGetChecksOptions = prepareGetChecksOptions; | ||
/** | ||
* Convert {@link GetChecksPaginateOptions} object to using backend API method | ||
* parameters {@link GetChecksBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getChecksPaginate} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareGetChecksPaginateOptions = (pageSize, options) => { | ||
// Create empty object, method doesn't have required parameters | ||
const prepared = {}; | ||
// Same names | ||
if (options.status !== undefined) | ||
prepared.status = options.status; | ||
// Different names | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.ids !== undefined) | ||
prepared.check_ids = options.ids.join(','); | ||
// Paginate options | ||
let page = options.page ? +options.page : 1; | ||
if (page < 1) | ||
page = 1; | ||
prepared.count = pageSize; | ||
prepared.offset = pageSize * (page - 1); | ||
return prepared; | ||
}; | ||
exports.prepareGetChecksPaginateOptions = prepareGetChecksPaginateOptions; | ||
/** | ||
* Convert {@link GetTransfersOptions} object to using backend API method | ||
* parameters {@link GetTransfersBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getTransfers} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareGetTransfersOptions = (options) => { | ||
// Create empty object, method doesn't have required parameters | ||
const prepared = {}; | ||
// Same names | ||
if (options.offset !== undefined) | ||
prepared.offset = options.offset; | ||
if (options.count !== undefined) | ||
prepared.count = options.count; | ||
// Different names | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.spendId !== undefined) | ||
prepared.spend_id = options.spendId; | ||
if (options.ids !== undefined) | ||
prepared.transfer_ids = options.ids.join(','); | ||
return prepared; | ||
}; | ||
exports.prepareGetTransfersOptions = prepareGetTransfersOptions; | ||
/** | ||
* Convert {@link GetTransfersPaginateOptions} object to using backend API method | ||
* parameters {@link GetTransfersBackendOptions} object | ||
* | ||
* @param options - Library {@link Client.getTransfersPaginate} method options object | ||
* | ||
* @returns Object with corresponding backend API method parameters | ||
*/ | ||
const prepareGetTransfersPaginateOptions = (pageSize, options) => { | ||
// Create empty object, method doesn't have required parameters | ||
const prepared = {}; | ||
// Different names | ||
if (options.asset !== undefined) | ||
prepared.asset = options.asset; | ||
if (options.spendId !== undefined) | ||
prepared.spend_id = options.spendId; | ||
if (options.ids !== undefined) | ||
prepared.transfer_ids = options.ids.join(','); | ||
// Paginate options | ||
let page = options.page ? +options.page : 1; | ||
if (page < 1) | ||
page = 1; | ||
prepared.count = pageSize; | ||
prepared.offset = pageSize * (page - 1); | ||
return prepared; | ||
}; | ||
exports.prepareGetTransfersPaginateOptions = prepareGetTransfersPaginateOptions; |
{ | ||
"name": "crypto-bot-api", | ||
"version": "0.2.1", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"version": "0.3.0", | ||
"description": "Simple and minimalistic client for Telegram CryptoBot Crypto Pay API", | ||
"author": "Sergei Ivankov <sergeiivankov@pm.me>", | ||
"license": "MIT", | ||
@@ -11,6 +11,13 @@ "repository": { | ||
}, | ||
"files": ["lib/**/*"], | ||
"exports": { | ||
"types": "./lib/index.d.ts", | ||
"require": "./lib/index.js", | ||
"default": "./lib/index.js" | ||
}, | ||
"files": [ | ||
"lib/**/*" | ||
], | ||
"scripts": { | ||
"build": "npm run build-docs && npm run build-lib && npm run build-dist", | ||
"build-docs": "typedoc src/exports.ts", | ||
"build-docs": "typedoc", | ||
"build-lib": "tsc", | ||
@@ -20,26 +27,23 @@ "build-dist": "rollup -c", | ||
"watch-docs": "typedoc --watch --preserveWatchOutput", | ||
"watch-lib": "tsc-watch --noClear", | ||
"watch-lib": "tsc -w", | ||
"watch-dist": "rollup -c -w", | ||
"lint": "eslint src/**/*" | ||
"lint": "eslint" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-replace": "^3.0.0", | ||
"@rollup/plugin-typescript": "^8.3.0", | ||
"@types/node": "^16.11.10", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"eslint": "^8.3.0", | ||
"eslint-config-airbnb": "^19.0.1", | ||
"eslint-config-airbnb-typescript": "^16.0.0", | ||
"eslint-plugin-tsdoc": "^0.2.14", | ||
"nlfurniss-typedoc-plugin-sourcefile-url": "^2.0.0", | ||
"@eslint/js": "^9.10.0", | ||
"@rollup/plugin-replace": "^5.0.7", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@stylistic/eslint-plugin": "^2.8.0", | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/node": "^22.5.5", | ||
"eslint": "^9.10.0", | ||
"eslint-plugin-tsdoc": "^0.3.0", | ||
"npm-run-all": "^4.1.5", | ||
"rollup": "^2.60.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"tsc-watch": "^4.5.0", | ||
"typedoc": "^0.22.10", | ||
"typescript": "^4.5.2" | ||
"rollup": "^4.21.3", | ||
"tslib": "^2.7.0", | ||
"typedoc": "^0.26.7", | ||
"typescript": "^5.6.2", | ||
"typescript-eslint": "^8.5.0" | ||
}, | ||
"author": "Sergei Ivankov <sergeiivankov@pm.me>", | ||
"description": "Simple and minimalistic client for Telegram CryptoBot Crypto Pay API", | ||
"keywords": [ | ||
@@ -46,0 +50,0 @@ "crypto-bot-api", |
@@ -54,3 +54,3 @@ # Crypto Bot API | ||
**Important: at the time of publication of version 0.2.0 (Dec 9, 2021), API servers do not return header Access-Control-Allow-Origin, which allows make requests to API from third-party domains, so client request from website environment won't work (but its work in browser extensions, Electron and similar apps)** | ||
**Important: at the time of publication of version 0.3.0 (Sep 16, 2024), API servers do not return header Access-Control-Allow-Origin, which allows make requests to API from third-party domains, so client request from website environment won't work (but its work in browser extensions, Electron and similar apps)** | ||
@@ -65,3 +65,3 @@ More usage examples see in [examples](https://github.com/sergeiivankov/crypto-bot-api/tree/main/examples) project directory. | ||
**Important: at the time of publication of version 0.2.0 (Dec 9, 2021), API servers do not accept self-signed certificates** | ||
**Important: at the time of publication of version 0.3.0 (Sep 16, 2024), API servers do not accept self-signed certificates** | ||
@@ -101,5 +101,5 @@ ## Documentation | ||
* [Documentation](https://sergeiivankov.github.io/crypto-bot-api/) | ||
* [Examples](https://github.com/sergeiivankov/crypto-bot-api/examples) | ||
* [Changelog](https://github.com/sergeiivankov/crypto-bot-api/blob/main/CHANGELOG.md) | ||
* [Backend CryptoBot API description](https://telegra.ph/Crypto-Pay-API-11-25) | ||
* [Examples](https://github.com/sergeiivankov/crypto-bot-api/tree/main/examples) | ||
* [Changelog](https://github.com/sergeiivankov/crypto-bot-api/blob/main/changelog.md) | ||
* [Backend CryptoBot API description](https://help.crypt.bot/crypto-pay-api) | ||
@@ -116,2 +116,2 @@ ## Code quality | ||
[MIT](LICENSE) | ||
[MIT](https://github.com/sergeiivankov/crypto-bot-api/blob/main/licanse) |
118685
15
3070