cardano-api
Advanced tools
Comparing version 0.0.5 to 0.0.6
const request = require('request') | ||
const { api, endpoints } = require('./config') | ||
const { api, endpoints, paths } = require('./config') | ||
const noPathAllowed = 'no address, txid, epoch or hash query allowed' | ||
const noPathProvided = 'please provide an address, txid, epoch or hash' | ||
const paths = ['epoch', 'txid', 'hash', 'address'] | ||
// buildMethod returns a function that returns a promise | ||
const buildMethod = endpoint => query => new Promise((resolve, reject) => { | ||
let path = '', params = '', badKey | ||
if(query) { | ||
// declare path and badKey variables | ||
let path = '', badKey | ||
// if trying to set a path in the query but path is not available for this | ||
// endpoint, then reject | ||
if(!endpoint.path && query.path) return reject(noPathAllowed) | ||
params = Object.keys(query).reduce((str, key) => { | ||
// if this is not a valid param for the endpoint, set the badKey | ||
if(!endpoint.params.includes(key)) { | ||
// check in a query object is passed in | ||
if (query) { | ||
// loop over the query object | ||
for (key in query) { | ||
// check if the key is in the paths array | ||
const match = paths.includes(key) | ||
// if this is not a valid key for the endpoint, set badKey | ||
if (!endpoint.path && match || !endpoint.params.includes(key)) { | ||
badKey = key | ||
} | ||
// if one of the keys matches a path, set the path for the url | ||
if(paths.includes(key)) { | ||
// if one of the keys matches a path, set the path for the uri | ||
// then delete the key to remove from the query string | ||
if (match) { | ||
path = `/${query[key]}` | ||
return str | ||
delete query[key] | ||
} | ||
} | ||
} | ||
// tack on the current ket to the params string | ||
str += `${key}=${query[key]}&` | ||
return str | ||
// reject if a query not supported by the api is sent | ||
if (badKey) { | ||
return reject(`${badKey} is not a valid query to this endpoint`) | ||
} | ||
}, '') | ||
// if there are params add a '?' to the front and remove the trailing '&' | ||
params = params ? `?${params.replace(/\&$/, '')}` : '' | ||
if(badKey) return reject(`${badKey} is not a valid query to this endpoint`) | ||
// reject if the endpoint requires a path but it is not provided | ||
if (endpoint.path && !path) { | ||
return reject(`'${endpoint.path}' key is required to query this endpoint`) | ||
} | ||
// if the endpoint requires a path but it is not provided reject | ||
if(endpoint.path && !path) return reject(noPathProvided) | ||
// build the url | ||
const url = `${api}${endpoint.url}${path}${params}` | ||
// Build up the uri to query | ||
const uri = `${api}/${endpoint.url}${path}` | ||
request(url, (err, res, body) => { | ||
if(err) reject(err) | ||
if(res.statusCode === 404 || res.statusCode === 400) { | ||
reject(`${res.statusCode}: ${res.statusMessage}`) | ||
request({uri, qs: query}, (err, res, body) => { | ||
// if there is an error from the api, reject with that error | ||
if (err) { | ||
return reject(err) | ||
} | ||
if(body) resolve(JSON.parse(body)) | ||
// if there is a 404 or 400 error from the api, reject with that error | ||
if (res.statusCode === 404 || res.statusCode === 400) { | ||
return reject(`${res.statusCode}: ${res.statusMessage}`) | ||
} | ||
// if there is a body in the response, resolve with the body | ||
if (body) { | ||
return resolve(JSON.parse(body)) | ||
} | ||
}) | ||
}) | ||
exports.cardano = endpoints.reduce((obj, endpoint) => { | ||
// reduce the endpoints array to build the methods on the cardano object | ||
// and then export that object | ||
module.exports = endpoints.reduce((obj, endpoint) => { | ||
obj[endpoint.method] = buildMethod(endpoint) | ||
return obj | ||
}, {}) |
@@ -6,3 +6,3 @@ module.exports = { | ||
url: 'api/addresses/summary', | ||
path: true, | ||
path: 'address', | ||
params: ['address'] | ||
@@ -13,7 +13,7 @@ }, | ||
url: 'api/blocks/summary', | ||
path: true, | ||
path: 'hash', | ||
params: ['hash'] | ||
}, | ||
{ | ||
method: 'blockPages', | ||
method: 'blocksPages', | ||
url: 'api/blocks/pages', | ||
@@ -26,7 +26,7 @@ path: false, | ||
url: 'api/blocks/txs', | ||
path: true, | ||
path: 'hash', | ||
params: ['hash', 'limit', 'offset'] | ||
}, | ||
{ | ||
method: 'pagesTotal', | ||
method: 'blocksTotal', | ||
url: 'api/blocks/pages/total', | ||
@@ -39,3 +39,3 @@ path: false, | ||
url: 'api/epochs', | ||
path: true, | ||
path: 'epoch', | ||
params: ['epoch', 'slot'] | ||
@@ -64,3 +64,3 @@ }, | ||
url: 'api/txs/summary', | ||
path: true, | ||
path: 'txid', | ||
params: ['txid'] | ||
@@ -75,3 +75,4 @@ }, | ||
], | ||
api: 'https://cardanoexplorer.com/' | ||
paths: ['epoch', 'txid', 'hash', 'address'], | ||
api: 'https://cardanoexplorer.com' | ||
} |
{ | ||
"name": "cardano-api", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Node wrapper for the Cardano Explorer API", | ||
@@ -5,0 +5,0 @@ "main": "lib/cardano.js", |
# cardano-api | ||
Wrapper for the [Cardano API](https://cardanodocs.com/technical/explorer/api/). | ||
Wrapper for the [Cardano Explorer API](https://cardanodocs.com/technical/explorer/api/). | ||
```js | ||
const { cardano } = require('cardano-api') | ||
const cardano = require('cardano-api') | ||
@@ -63,6 +63,6 @@ const address = 'DdzFFzCqrht8iQ2utWYssBnfGvSqkGfM7fxHXZWoB57ormT17td1CY4Eye7bADF6HpeGC57vwV5ZPzmVjiZRQEkAD9Rc4P8LDF7FfYne' | ||
cardano.blockTransaction(query) | ||
.then(data => console.log('block', data)) | ||
.then(data => console.log('blockTransaction', data)) | ||
``` | ||
#### cardano.pagesTotal() - [/api/blocks/pages/total](https://cardanodocs.com/technical/explorer/api/#path--api-blocks-pages-total) | ||
#### cardano.blocksTotal() - [/api/blocks/pages/total](https://cardanodocs.com/technical/explorer/api/#path--api-blocks-pages-total) | ||
@@ -75,4 +75,4 @@ ```js | ||
cardano.pagesTotal(query) | ||
.then(data => console.log('blockTotal', data)) | ||
cardano.blocksTotal(query) | ||
.then(data => console.log('blocksTotal', data)) | ||
``` | ||
@@ -151,7 +151,2 @@ | ||
``` npm test``` | ||
### Cardano tips | ||
```DdzFFzCqrht8iQ2utWYssBnfGvSqkGfM7fxHXZWoB57ormT17td1CY4Eye7bADF6HpeGC57vwV5ZPzmVjiZRQEkAD9Rc4P8LDF7FfYne``` | ||
##### Thank You | ||
``` npm test``` |
const assert = require('assert') | ||
const { cardano } = require('../lib/cardano') | ||
const cardano = require('../lib/cardano') | ||
describe('cardano error tests', () => { | ||
describe('cardano api error tests', () => { | ||
// Supported Methods | ||
// ## address | ||
// ## blockPages | ||
// ## pagesTotal | ||
// ## blocksPages | ||
// ## blocksTotal | ||
// ## block | ||
@@ -41,3 +41,3 @@ // ## genesisAddress | ||
.catch(err => { | ||
assert.equal('please provide an address, txid, epoch or hash', err) | ||
assert.equal(`'address' key is required to query this endpoint`, err) | ||
}) | ||
@@ -83,3 +83,3 @@ }) | ||
.catch(err => { | ||
assert.equal('please provide an address, txid, epoch or hash', err) | ||
assert.equal(`'hash' key is required to query this endpoint`, err) | ||
}) | ||
@@ -89,3 +89,3 @@ }) | ||
describe('cardano.blockPages()', () => { | ||
describe('cardano.blocksPages()', () => { | ||
it('should return message on bad query', () => { | ||
@@ -96,3 +96,3 @@ const query = { | ||
return cardano.blockPages(query) | ||
return cardano.blocksPages(query) | ||
.then(data => { | ||
@@ -125,3 +125,3 @@ console.log(data) | ||
describe('cardano.pagesTotal()', () => { | ||
describe('cardano.blocksTotal()', () => { | ||
it('should return the total amount of blocks', () => { | ||
@@ -132,3 +132,3 @@ const query = { | ||
return cardano.pagesTotal(query) | ||
return cardano.blocksTotal(query) | ||
.then(data => { | ||
@@ -165,3 +165,3 @@ console.log(data) | ||
.catch(err => { | ||
assert.equal('please provide an address, txid, epoch or hash', err) | ||
assert.equal(`'epoch' key is required to query this endpoint`, err) | ||
}) | ||
@@ -228,3 +228,3 @@ }) | ||
.catch(err => { | ||
assert.equal('please provide an address, txid, epoch or hash', err) | ||
assert.equal(`'txid' key is required to query this endpoint`, err) | ||
}) | ||
@@ -231,0 +231,0 @@ }) |
const assert = require('assert') | ||
const { cardano } = require('../lib/cardano') | ||
const cardano = require('../lib/cardano') | ||
describe('cardano api wrapper', () => { | ||
describe('cardano api success tests', () => { | ||
@@ -9,3 +9,3 @@ // Supported Methods | ||
// ## blockPages | ||
// ## pagesTotal | ||
// ## blocksTotal | ||
// ## block | ||
@@ -53,3 +53,3 @@ // ## genesisAddress | ||
describe('cardano.blockPages()', () => { | ||
describe('cardano.blocksPages()', () => { | ||
it('should return the total amount of blocks', () => { | ||
@@ -61,3 +61,3 @@ const query = { | ||
return cardano.blockPages(query) | ||
return cardano.blocksPages(query) | ||
.then(data => { | ||
@@ -90,3 +90,3 @@ assert.equal('Right', Object.keys(data)[0]) | ||
describe('cardano.pagesTotal()', () => { | ||
describe('cardano.blocksTotal()', () => { | ||
it('should return the total amount of blocks', () => { | ||
@@ -97,3 +97,3 @@ const query = { | ||
return cardano.pagesTotal(query) | ||
return cardano.blocksTotal(query) | ||
.then(data => { | ||
@@ -100,0 +100,0 @@ assert.equal('Right', Object.keys(data)[0]) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19042
508
149