@blockbee/api
Advanced tools
Comparing version 2.0.1 to 2.1.0
153
index.js
@@ -178,11 +178,5 @@ /** | ||
*/ | ||
static async getInfo( coin = null, assoc = false, apiKey ) { | ||
static async getInfo( coin = null, assoc = false, apiKey = '' ) { | ||
const params = {} | ||
if ( !apiKey ) { | ||
throw new Error('API Key is Empty') | ||
} | ||
params['apikey'] = apiKey | ||
if ( !coin ) { | ||
@@ -192,9 +186,3 @@ params['prices'] = 0 | ||
const response = await this.#_request_get(coin, 'info', params) | ||
if ( !coin || response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await this.#_request_get(coin, 'info', params) | ||
} | ||
@@ -210,18 +198,7 @@ | ||
*/ | ||
static async getEstimate( coin, apiKey, addresses = 1, priority = 'default' ) { | ||
if ( !apiKey ) { | ||
throw new Error('API Key is Empty') | ||
} | ||
const response = await BlockBee.#_request_get(coin, 'estimate', { | ||
static async getEstimate( coin, apiKey = '', addresses = 1, priority = 'default' ) { | ||
return await BlockBee.#_request_get(coin, 'estimate', { | ||
addresses, | ||
priority, | ||
apikey: apiKey | ||
priority | ||
}) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
} | ||
@@ -237,18 +214,7 @@ | ||
*/ | ||
static async getConvert( coin, value, from, apiKey ) { | ||
if ( !apiKey ) { | ||
throw new Error('API Key is Empty') | ||
} | ||
const response = await BlockBee.#_request_get(coin, 'convert', { | ||
static async getConvert( coin, value, from, apiKey = '' ) { | ||
return await BlockBee.#_request_get(coin, 'convert', { | ||
value, | ||
from, | ||
apikey: apiKey | ||
from | ||
}) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
} | ||
@@ -271,8 +237,3 @@ | ||
const response = await BlockBee.#_request_post(coin, endpoint, apiKey, body, true) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await BlockBee.#_request_post(coin, endpoint, apiKey, body, true) | ||
} | ||
@@ -297,9 +258,3 @@ | ||
const response = await this.#_request_get(coin, endpoint, {...params, apikey: apiKey}) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await this.#_request_get(coin, endpoint, {...params, apikey: apiKey}) | ||
} | ||
@@ -310,6 +265,2 @@ | ||
if ( wallet.status !== 'success' ) { | ||
return null | ||
} | ||
const output = {address: wallet.address} | ||
@@ -332,9 +283,3 @@ | ||
const response = await this.#_request_post('', 'payout/create', apiKey, {request_ids: ids.join(',')}) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await this.#_request_post('', 'payout/create', apiKey, {request_ids: ids.join(',')}) | ||
} | ||
@@ -353,9 +298,3 @@ | ||
const response = await this.#_request_post('', 'payout/status', apiKey, {payout_id: id}) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await this.#_request_post('', 'payout/status', apiKey, {payout_id: id}) | ||
} | ||
@@ -368,7 +307,12 @@ | ||
static async paymentRequest( redirectUrl, notifyUrl, value, apiKey, params = {}, bbParams = {} ) { | ||
if ( !notifyUrl || !redirectUrl || !value || !apiKey ) { | ||
return null | ||
if (!notifyUrl) { | ||
throw new Error('notifyUrl is required') | ||
} | ||
if (!redirectUrl) { | ||
throw new Error('paymentRequest is required') | ||
} | ||
redirectUrl = new URL(redirectUrl) | ||
notifyUrl = new URL(notifyUrl) | ||
@@ -390,8 +334,3 @@ | ||
const response = await BlockBee.#_request_get('', 'checkout/request', reqParams) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await BlockBee.#_request_get('', 'checkout/request', reqParams) | ||
} | ||
@@ -406,6 +345,2 @@ | ||
static async paymentLogs( token, apiKey ) { | ||
if ( !token ) { | ||
throw new Error('Token is Empty') | ||
} | ||
const params = { | ||
@@ -416,9 +351,3 @@ apikey: apiKey, | ||
const response = await BlockBee.#_request_get('', 'checkout/logs', params) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await BlockBee.#_request_get('', 'checkout/logs', params) | ||
} | ||
@@ -431,4 +360,4 @@ | ||
static async depositRequest( notifyUrl, apiKey, parameters = {}, bbParams = {} ) { | ||
if ( !notifyUrl || !apiKey ) { | ||
return null | ||
if ( !notifyUrl ) { | ||
throw new Error('notifyUrl is required') | ||
} | ||
@@ -449,9 +378,3 @@ | ||
const response = await BlockBee.#_request_get('', 'deposit/request', params) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await BlockBee.#_request_get('', 'deposit/request', params) | ||
} | ||
@@ -475,9 +398,3 @@ | ||
const response = await BlockBee.#_request_get('', 'deposit/logs', params) | ||
if ( response.status === 'success' ) { | ||
return response | ||
} | ||
return null | ||
return await BlockBee.#_request_get('', 'deposit/logs', params) | ||
} | ||
@@ -492,3 +409,3 @@ | ||
*/ | ||
static async #_request_get( coin, endpoint, params = {} ) { | ||
static async #_request_get( coin = null, endpoint = '', params = {} ) { | ||
const url = coin ? new URL(`${this.#baseURL}/${coin.replace('_', '/')}/${endpoint}/`) : new URL(`${this.#baseURL}/${endpoint}/`) | ||
@@ -509,3 +426,10 @@ | ||
const response = await fetch(url, fetchParams) | ||
return await response.json() | ||
const response_obj = await response.json() | ||
if ( response_obj.status === 'error' ) { | ||
throw new Error(response_obj.error) | ||
} | ||
return response_obj | ||
} | ||
@@ -543,3 +467,10 @@ | ||
const response = await fetch(url, fetchParams) | ||
return await response.json() | ||
const response_obj = await response.json() | ||
if ( response_obj.status === 'error' ) { | ||
throw new Error(response_obj.error) | ||
} | ||
return response_obj | ||
} | ||
@@ -546,0 +477,0 @@ } |
{ | ||
"name": "@blockbee/api", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "BlockBee's Library for NodeJS", | ||
@@ -31,3 +31,6 @@ "main": "index.js", | ||
"bep20", | ||
"avalanche-c" | ||
"avalanche-c", | ||
"base", | ||
"optimism", | ||
"arbitrum" | ||
], | ||
@@ -34,0 +37,0 @@ "bugs": { |
@@ -558,2 +558,6 @@ [<img src="https://blockbee.io/static/assets/images/blockbee_logo_nospaces.png" width="300"/>](image.png) | ||
#### 2.1.0 | ||
* Minor bugfixes | ||
* Improve error handling | ||
### Breaking Changes | ||
@@ -560,0 +564,0 @@ |
51
test.js
@@ -6,4 +6,17 @@ const BlockBee = require('./index') | ||
test('Test requesting service info', async (t) => { | ||
const info = await BlockBee.getInfo() | ||
console.log(info) | ||
if (info === null) throw new Error('fail') | ||
}) | ||
test('Test requesting coin info', async (t) => { | ||
const info = await BlockBee.getInfo('btc') | ||
console.log(info) | ||
if (info === null) throw new Error('fail') | ||
}) | ||
test('Test requesting supported cryptocurrencies', async (t) => { | ||
const supportedCoins = await BlockBee.getSupportedCoins(apiKey) | ||
console.log(supportedCoins) | ||
if (supportedCoins === null) throw new Error('fail') | ||
@@ -13,3 +26,3 @@ }) | ||
test('Test generating address', async (t) => { | ||
const bb = new BlockBee('polygon_matic', '', 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5s', { | ||
const bb = new BlockBee('polygon_matic', null, 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5s', { | ||
order_id: 1235, | ||
@@ -22,2 +35,3 @@ }, { | ||
const address = await bb.getAddress() | ||
console.log(address) | ||
if (address === null) throw new Error('fail') | ||
@@ -27,3 +41,3 @@ }) | ||
test('Test getting logs', async (t) => { | ||
const bb = new BlockBee('polygon_matic', '', 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5s', { | ||
const bb = new BlockBee('polygon_matic', null, 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5s', { | ||
order_id: 1235, | ||
@@ -36,2 +50,3 @@ }, { | ||
const logs = await bb.checkLogs() | ||
console.log(logs) | ||
if (logs === null) throw new Error('fail') | ||
@@ -42,3 +57,3 @@ }) | ||
test('Test getting QrCode', async (t) => { | ||
const bb = new BlockBee('polygon_matic', '', 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5f', {}, { | ||
const bb = new BlockBee('polygon_matic', null, 'https://webhook.site/19a994f1-54eb-47dc-8516-4107851f9a5f', {}, { | ||
convert: 1, | ||
@@ -49,2 +64,3 @@ multi_chain: 1, | ||
const qrCode = await bb.getQrcode(2, 300) | ||
console.log(qrCode) | ||
if (qrCode === null) throw new Error('fail') | ||
@@ -66,3 +82,3 @@ }) | ||
const requests = { | ||
'0xA6B78B56ee062185E405a1DDDD18cE8fcBC4395d': 0.5, | ||
'0xA6B78B56ee062185E405a1DDDD18cE8fcBC4395d': 0.2, | ||
'0x18B211A1Ba5880C7d62C250B6441C2400d588589': 0.1 | ||
@@ -72,2 +88,3 @@ } | ||
const payout = await BlockBee.createPayout('polygon_matic', requests, apiKey, false) | ||
console.log(payout) | ||
if (payout === null) throw new Error('fail') | ||
@@ -77,3 +94,4 @@ }) | ||
test('Test fetching Payout list', async (t) => { | ||
const payout = await BlockBee.listPayouts('bep20_usdt', '', 1, apiKey) | ||
const payout = await BlockBee.listPayouts('bep20_usdt', 'all', 1, apiKey, true) | ||
console.log(payout) | ||
if (payout === null) throw new Error('fail') | ||
@@ -84,3 +102,3 @@ }) | ||
const wallet = await BlockBee.getPayoutWallet('polygon_matic', apiKey, true) | ||
console.log(wallet) | ||
if (wallet === null) throw new Error('fail') | ||
@@ -90,6 +108,9 @@ }) | ||
test('Create Payout by IDs', async (t) => { | ||
const ids = [52211, 52212] | ||
const ids = [ | ||
'6f84ae0c-e9fd-45d2-9c42-1c8e02e89551', | ||
'372293c6-e841-4dc0-b80d-a41d1971c136' | ||
] | ||
const payout = await BlockBee.createPayoutByIds(apiKey, ids) | ||
console.log(payout) | ||
if (payout === null) throw new Error('fail') | ||
@@ -99,4 +120,4 @@ }) | ||
test('Process Payout by ID', async (t) => { | ||
const payout = await BlockBee.processPayout(apiKey, 2463) | ||
const payout = await BlockBee.processPayout(apiKey, '3da6bff6-f0f7-4f93-8512-b4611d80077e') | ||
console.log(payout) | ||
if (payout === null) throw new Error('fail') | ||
@@ -106,3 +127,4 @@ }) | ||
test('Check Payout status by ID', async (t) => { | ||
const status = await BlockBee.checkPayoutStatus(apiKey, 2463) | ||
const status = await BlockBee.checkPayoutStatus(apiKey, '3da6bff6-f0f7-4f93-8512-b4611d80077e') | ||
console.log(status) | ||
if (status === null) throw new Error('fail') | ||
@@ -128,2 +150,3 @@ }) | ||
const paymentLogs = await BlockBee.paymentLogs(token, apiKey) | ||
console.log(paymentLogs) | ||
if (paymentLogs === null) throw new Error('fail') | ||
@@ -135,7 +158,6 @@ }) | ||
'https://example.com', | ||
apiKey, | ||
{ | ||
deposit_id: 124, | ||
}, | ||
'', | ||
apiKey | ||
} | ||
) | ||
@@ -149,3 +171,4 @@ console.log(depositLink) | ||
const depositLogs = await BlockBee.depositLogs(token, apiKey) | ||
console.log(depositLogs) | ||
if (depositLogs === null) throw new Error('fail') | ||
}) |
565
42848
496