-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (49 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict'
const EOSAPI = require('eosjs-api')
const snakeCaseKeys = require('snakecase-keys')
const camelCaseKeys = require('camelcase-keys')
const isObject = param => typeof param === 'object'
const isString = param => typeof param === 'string'
const isFunction = param => typeof param === 'function'
const defaultOptions = {httpEndpoint: 'http://jungle.eosio.cr'}
const snakifyObjects = (argumentsObject) =>
Object
.entries(argumentsObject)
.reduce((prev, [ key, value ]) => ({
...prev,
[key]: isObject(value) ? snakeCaseKeys(value) : value
}), {})
const camelizeResponse = (response) => {
if (isObject(response)) {
return camelCaseKeys(response)
} else if (isString(response)) {
try {
return camelCaseKeys(JSON.parse(response))
} catch (error) {
return response
}
}
return response
}
const getInstance = options => {
const eosApi = new EOSAPI(Object.assign({}, defaultOptions, options))
const camelApi = {}
Object.keys(eosApi).forEach(key => {
if (isObject(eosApi[key])) {
camelApi[key] = eosApi[key]
} else if (isFunction(eosApi[key])) {
camelApi[key] = async function () {
const snakedArgs = Object.values(snakifyObjects(arguments))
return eosApi[key].apply(null, snakedArgs).then(camelizeResponse)
}
}
})
//add custom utility function in camel namespace
camelApi.camel = {
getConfig: () => options // the initial options object
}
return camelApi
}
module.exports = {
getInstance
}