Skip to content

Instantly share code, notes, and snippets.

@brantje
Last active April 1, 2019 07:00
Show Gist options
  • Save brantje/13a42f13120807a9070997eab77c5d2d to your computer and use it in GitHub Desktop.
Save brantje/13a42f13120807a9070997eab77c5d2d to your computer and use it in GitHub Desktop.
Simple wrapper for NimiqX.
const axios = require('axios')
const instance = axios.create({
baseURL: 'https://api.nimiqx.com/',
params: {
api_key: process.env.NIMIQX_KEY
},
})
let NimiqX = new Proxy({}, {
get(target, name) {
return Object.assign(
{},
[
'get',
'head'
].reduce(
(o, method) => Object.assign({}, o, {
[method](url = '', params = {}) {
if (typeof url === 'string') {
url = '/' + url
}
if (typeof url === 'object') {
params = url
url = ''
}
name = name.replace(/[A-Z]/g, '-$&').toLowerCase()
return new Promise((resolve, reject) => {
instance[method](name + url, {params}).then((response) => {
resolve(response.data)
}).catch((error) => {
reject(error.response.data)
})
})
}
}), {}),
[
'post',
'put',
'patch'
].reduce(
(o, method) => Object.assign({}, o, {
[method](url = '', body = {}, params = {}) {
if (typeof url === 'object') {
params = body
body = url
url = ''
}
name = name.replace(/[A-Z]/g, '-$&').toLowerCase()
return new Promise((resolve, reject) => {
instance[method](name + url, body, {params}).then((response) => {
resolve(response.data)
}).catch((error) => {
reject(error.response.data)
})
})
}
}), {})
)
},
set() {
throw Error('Unable to set variables!')
return false
}
})
module.exports = NimiqX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment