chain33-rpc-api
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -12,13 +12,9 @@ "use strict"; | ||
doFetch(config) { | ||
const { method, httpHeaders = {}, } = config; | ||
let { postdata } = config; | ||
if (method === 'GET' && postdata) { | ||
const queryArr = []; | ||
Object.keys(postdata).forEach((q) => queryArr.push(`${q}=${postdata[q]}`)); | ||
this.url = `${this.url}?${queryArr.join('&')}`; | ||
postdata = false; | ||
} | ||
const headers = new Headers(Object.assign({ 'Content-Type': 'application/json', 'Accept': 'application/json' }, httpHeaders)); | ||
const { httpHeaders = {}, postdata, } = config; | ||
let { url } = config; | ||
if (!url) | ||
url = this.url; | ||
const headers = new Headers(Object.assign({ 'Content-Type': 'text/plain', 'Accept': 'application/json' }, httpHeaders)); | ||
const reqConfig = { | ||
method: method || 'POST', | ||
method: 'POST', | ||
headers, | ||
@@ -28,10 +24,5 @@ redirect: 'follow', | ||
if (postdata) { | ||
if (headers.get('Content-Type') && /json/.test(headers.get('Content-Type'))) { | ||
reqConfig['body'] = JSON.stringify(postdata); | ||
} | ||
else { | ||
reqConfig['body'] = postdata; | ||
} | ||
reqConfig['body'] = JSON.stringify(postdata); | ||
} | ||
return fetch(this.url, reqConfig).then(this.checkStatus) | ||
return fetch(url, reqConfig).then(this.checkStatus) | ||
.then((resp) => resp.json()) | ||
@@ -38,0 +29,0 @@ .catch((e) => { |
@@ -11,5 +11,4 @@ import HttpProvider from './HttpProvider'; | ||
httpProvider: HttpProvider; | ||
constructor(url?: string, errorHandler?: any); | ||
setHttpProvider(url: string): void; | ||
createTransaction(params: any): Promise<any>; | ||
constructor(URL?: string, errorHandler?: any); | ||
createTransaction(params: any, url?: string): Promise<any>; | ||
/** | ||
@@ -28,3 +27,3 @@ * @description 构造交易 | ||
*/ | ||
createRawTransaction(params: any): Promise<any>; | ||
createRawTransaction(params: any, url?: string): Promise<any>; | ||
/** | ||
@@ -39,3 +38,3 @@ * @description 发送签名后的交易 | ||
*/ | ||
sendRawTransaction(unsignTx: string, sign: string, pubkey: string, ty?: number): Promise<any>; | ||
sendRawTransaction(unsignTx: string, sign: string, pubkey: string, ty?: number, url?: string): Promise<any>; | ||
/** | ||
@@ -47,3 +46,3 @@ * @description 发送交易 | ||
*/ | ||
sendTransaction(data: string): Promise<any>; | ||
sendTransaction(data: string, url?: string): Promise<any>; | ||
/** | ||
@@ -56,3 +55,3 @@ * @description 查询地址余额 | ||
*/ | ||
getAddrBalance(addresses: string[], execer?: string): Promise<any>; | ||
getAddrBalance(addresses: string[], execer?: string, url?: string): Promise<any>; | ||
/** | ||
@@ -66,3 +65,3 @@ * @description 查询地址token余额 | ||
*/ | ||
getTokenBalance(addresses: string[], execer: string, tokenSymbol: string): Promise<any>; | ||
getTokenBalance(addresses: string[], execer: string, tokenSymbol: string, url?: string): Promise<any>; | ||
/** | ||
@@ -74,3 +73,3 @@ * @description 查询地址所有合约地址余额 | ||
*/ | ||
getAllExecBalance(addr: string): Promise<any>; | ||
getAllExecBalance(addr: string, url?: string): Promise<any>; | ||
/** | ||
@@ -82,3 +81,3 @@ * @description 获取地址相关摘要信息 | ||
*/ | ||
getAddrOverview(addr: string): Promise<any>; | ||
getAddrOverview(addr: string, url?: string): Promise<any>; | ||
/** | ||
@@ -95,3 +94,3 @@ * @description 根据地址获取交易哈希 | ||
*/ | ||
getTxByAddr(addr: string, flag: number, count: number, direction: number, height: number, index: number): Promise<any>; | ||
getTxByAddr(addr: string, flag: number, count: number, direction: number, height: number, index: number, url?: string): Promise<any>; | ||
/** | ||
@@ -103,3 +102,3 @@ * @description 根据哈希数组批量获取交易信息 | ||
*/ | ||
getTxByHashes(hashes: string[]): Promise<any>; | ||
getTxByHashes(hashes: string[], url?: string): Promise<any>; | ||
/** | ||
@@ -116,3 +115,3 @@ * @description 根据地址获取交易信息 | ||
*/ | ||
getAddrTx(addr: string, flag: number, count: number, direction: number, height: number, index: number): Promise<any>; | ||
getAddrTx(addr: string, flag: number, count: number, direction: number, height: number, index: number, url?: string): Promise<any>; | ||
/** | ||
@@ -124,3 +123,3 @@ * @description 根据哈希获取交易的字符串 | ||
*/ | ||
getHexTxByHash(hash: string): Promise<any>; | ||
getHexTxByHash(hash: string, url?: string): Promise<any>; | ||
/** | ||
@@ -132,3 +131,3 @@ * @description 将合约名转化成chain33中的实际地址 | ||
*/ | ||
convertExectoAddr(execname: string): Promise<any>; | ||
convertExectoAddr(execname: string, url?: string): Promise<any>; | ||
/** | ||
@@ -140,3 +139,3 @@ * @description 根据哈希查询交易信息 | ||
*/ | ||
queryTransaction(hash: string): Promise<any>; | ||
queryTransaction(hash: string, url?: string): Promise<any>; | ||
/** | ||
@@ -147,3 +146,3 @@ * @description 查询所有创建成功的token | ||
*/ | ||
queryAllTokens(): Promise<any>; | ||
queryAllTokens(url?: string): Promise<any>; | ||
/** | ||
@@ -156,3 +155,3 @@ * @description 查询地址下的token/trace合约下的token资产 | ||
*/ | ||
getAddrTokenAssets(address: string, execer: string): Promise<any>; | ||
getAddrTokenAssets(address: string, execer: string, url?: string): Promise<any>; | ||
/** | ||
@@ -170,3 +169,3 @@ * @description 查询token相关的交易 | ||
*/ | ||
getTxByToken(symbol: string, addr: string, count: number, flag: number, height: number, index: number, direction: number): Promise<any>; | ||
getTxByToken(symbol: string, addr: string, count: number, flag: number, height: number, index: number, direction: number, url?: string): Promise<any>; | ||
/** | ||
@@ -178,3 +177,3 @@ * @description 查询指定创建成功的token | ||
*/ | ||
getTokenInfo(tokenSymbol: string): Promise<any>; | ||
getTokenInfo(tokenSymbol: string, url?: string): Promise<any>; | ||
/** | ||
@@ -185,3 +184,3 @@ * @description 获取版本 | ||
*/ | ||
version(): Promise<any>; | ||
version(url?: string): Promise<any>; | ||
/** | ||
@@ -195,3 +194,3 @@ * @description 获取区间区块 | ||
*/ | ||
getBlocks(start: number, end: number, isDetail: boolean): Promise<any>; | ||
getBlocks(start: number, end: number, isDetail: boolean, url?: string): Promise<any>; | ||
/** | ||
@@ -202,3 +201,3 @@ * @description 获取最新的区块头 | ||
*/ | ||
getLastHeader(): Promise<any>; | ||
getLastHeader(url?: string): Promise<any>; | ||
/** | ||
@@ -212,3 +211,3 @@ * @description 获取区间区块头 | ||
*/ | ||
getHeaders(start: number, end: number, isDetail: boolean): Promise<any>; | ||
getHeaders(start: number, end: number, isDetail: boolean, url?: string): Promise<any>; | ||
/** | ||
@@ -220,3 +219,3 @@ * @description 获取区块的 hash 值 | ||
*/ | ||
getBlockHash(height: number): Promise<any>; | ||
getBlockHash(height: number, url?: string): Promise<any>; | ||
/** | ||
@@ -228,3 +227,3 @@ * @description 获取区块的详细信息 | ||
*/ | ||
getBlockOverview(hash: string): Promise<any>; | ||
getBlockOverview(hash: string, url?: string): Promise<any>; | ||
/** | ||
@@ -235,3 +234,3 @@ * @description 获取远程节点列表 | ||
*/ | ||
getPeerInfo(): Promise<any>; | ||
getPeerInfo(url?: string): Promise<any>; | ||
/** | ||
@@ -242,3 +241,3 @@ * @description 查询节点状态 | ||
*/ | ||
getNetInfo(): Promise<any>; | ||
getNetInfo(url?: string): Promise<any>; | ||
/** | ||
@@ -249,3 +248,3 @@ * @description 查询时间状态 | ||
*/ | ||
getTimeStatus(): Promise<any>; | ||
getTimeStatus(url?: string): Promise<any>; | ||
/** | ||
@@ -256,4 +255,4 @@ * @description 查询同步状态 | ||
*/ | ||
isSync(): Promise<any>; | ||
callPromiseAPI(apiName: string, params?: any): Promise<any>; | ||
isSync(url?: string): Promise<any>; | ||
callPromiseAPI(apiName: string, params?: any, url?: string): Promise<any>; | ||
} |
@@ -21,12 +21,8 @@ "use strict"; | ||
} | ||
constructor(url = 'http://114.55.101.159:8801', errorHandler = null) { | ||
this.httpProvider = new HttpProvider_1.default(url, errorHandler); | ||
constructor(URL = 'http://114.55.101.159:8801', errorHandler = null) { | ||
this.httpProvider = new HttpProvider_1.default(URL, errorHandler); | ||
} | ||
// 设置请求节点 | ||
setHttpProvider(url) { | ||
this.httpProvider.setUrl(url); | ||
createTransaction(params, url = '') { | ||
return this.callPromiseAPI('CreateTransaction', params, url); | ||
} | ||
createTransaction(params) { | ||
return this.callPromiseAPI('CreateTransaction', params); | ||
} | ||
/** | ||
@@ -45,4 +41,4 @@ * @description 构造交易 | ||
*/ | ||
createRawTransaction(params) { | ||
return this.callPromiseAPI('CreateRawTransaction', params); | ||
createRawTransaction(params, url = '') { | ||
return this.callPromiseAPI('CreateRawTransaction', params, url); | ||
} | ||
@@ -58,3 +54,3 @@ /** | ||
*/ | ||
sendRawTransaction(unsignTx, sign, pubkey, ty = 1) { | ||
sendRawTransaction(unsignTx, sign, pubkey, ty = 1, url = '') { | ||
return this.callPromiseAPI('SendRawTransaction', { | ||
@@ -65,3 +61,3 @@ unsignTx, | ||
ty, | ||
}); | ||
}, url); | ||
} | ||
@@ -74,6 +70,6 @@ /** | ||
*/ | ||
sendTransaction(data) { | ||
sendTransaction(data, url = '') { | ||
return this.callPromiseAPI('SendTransaction', { | ||
data, | ||
}); | ||
}, url); | ||
} | ||
@@ -87,7 +83,7 @@ /** | ||
*/ | ||
getAddrBalance(addresses, execer = 'coins') { | ||
getAddrBalance(addresses, execer = 'coins', url = '') { | ||
return this.callPromiseAPI('GetBalance', { | ||
addresses, | ||
execer, | ||
}); | ||
}, url); | ||
} | ||
@@ -102,3 +98,3 @@ /** | ||
*/ | ||
getTokenBalance(addresses, execer, tokenSymbol) { | ||
getTokenBalance(addresses, execer, tokenSymbol, url = '') { | ||
return this.callPromiseAPI('GetTokenBalance', { | ||
@@ -108,3 +104,3 @@ addresses, | ||
tokenSymbol, | ||
}); | ||
}, url); | ||
} | ||
@@ -117,6 +113,6 @@ /** | ||
*/ | ||
getAllExecBalance(addr) { | ||
getAllExecBalance(addr, url = '') { | ||
return this.callPromiseAPI('GetAllExecBalance', { | ||
addr, | ||
}); | ||
}, url); | ||
} | ||
@@ -129,6 +125,6 @@ /** | ||
*/ | ||
getAddrOverview(addr) { | ||
getAddrOverview(addr, url = '') { | ||
return this.callPromiseAPI('GetAddrOverview', { | ||
addr, | ||
}); | ||
}, url); | ||
} | ||
@@ -146,3 +142,3 @@ /** | ||
*/ | ||
getTxByAddr(addr, flag, count, direction, height, index) { | ||
getTxByAddr(addr, flag, count, direction, height, index, url = '') { | ||
return this.callPromiseAPI('GetTxByAddr', { | ||
@@ -155,3 +151,3 @@ addr, | ||
index, | ||
}); | ||
}, url); | ||
} | ||
@@ -164,6 +160,6 @@ /** | ||
*/ | ||
getTxByHashes(hashes) { | ||
getTxByHashes(hashes, url = '') { | ||
return this.callPromiseAPI('GetTxByHashes', { | ||
hashes, | ||
}).then((res) => { | ||
}, url).then((res) => { | ||
// 给返回的交易详情增加hash属性 | ||
@@ -189,4 +185,4 @@ if (res && res.result && res.result.txs) { | ||
*/ | ||
getAddrTx(addr, flag, count, direction, height, index) { | ||
return this.getTxByAddr(addr, flag, count, direction, height, index) | ||
getAddrTx(addr, flag, count, direction, height, index, url = '') { | ||
return this.getTxByAddr(addr, flag, count, direction, height, index, url) | ||
.then((res) => { | ||
@@ -211,3 +207,3 @@ if (res.error) { | ||
}).then((hashes) => { | ||
return this.getTxByHashes(hashes); | ||
return this.getTxByHashes(hashes, url); | ||
}); | ||
@@ -221,6 +217,6 @@ } | ||
*/ | ||
getHexTxByHash(hash) { | ||
getHexTxByHash(hash, url = '') { | ||
return this.callPromiseAPI('GetHexTxByHash', { | ||
hash, | ||
}); | ||
}, url); | ||
} | ||
@@ -233,6 +229,6 @@ /** | ||
*/ | ||
convertExectoAddr(execname) { | ||
convertExectoAddr(execname, url = '') { | ||
return this.callPromiseAPI('ConvertExectoAddr', { | ||
execname, | ||
}); | ||
}, url); | ||
} | ||
@@ -245,6 +241,6 @@ /** | ||
*/ | ||
queryTransaction(hash) { | ||
queryTransaction(hash, url = '') { | ||
return this.callPromiseAPI('QueryTransaction', { | ||
hash, | ||
}); | ||
}, url); | ||
} | ||
@@ -256,3 +252,3 @@ /** | ||
*/ | ||
queryAllTokens() { | ||
queryAllTokens(url = '') { | ||
return this.callPromiseAPI('Query', { | ||
@@ -265,3 +261,3 @@ execer: 'token', | ||
}, | ||
}); | ||
}, url); | ||
} | ||
@@ -275,3 +271,3 @@ /** | ||
*/ | ||
getAddrTokenAssets(address, execer) { | ||
getAddrTokenAssets(address, execer, url = '') { | ||
return this.callPromiseAPI('Query', { | ||
@@ -284,3 +280,3 @@ execer: 'token', | ||
}, | ||
}); | ||
}, url); | ||
} | ||
@@ -299,3 +295,3 @@ /** | ||
*/ | ||
getTxByToken(symbol, addr, count, flag, height, index, direction) { | ||
getTxByToken(symbol, addr, count, flag, height, index, direction, url = '') { | ||
return this.callPromiseAPI('Query', { | ||
@@ -313,3 +309,3 @@ execer: 'token', | ||
}, | ||
}); | ||
}, url); | ||
} | ||
@@ -322,3 +318,3 @@ /** | ||
*/ | ||
getTokenInfo(tokenSymbol) { | ||
getTokenInfo(tokenSymbol, url = '') { | ||
return this.callPromiseAPI('Query', { | ||
@@ -330,3 +326,3 @@ execer: 'token', | ||
}, | ||
}); | ||
}, url); | ||
} | ||
@@ -338,4 +334,4 @@ /** | ||
*/ | ||
version() { | ||
return this.callPromiseAPI('Version'); | ||
version(url = '') { | ||
return this.callPromiseAPI('Version', {}, url); | ||
} | ||
@@ -350,3 +346,3 @@ /** | ||
*/ | ||
getBlocks(start, end, isDetail) { | ||
getBlocks(start, end, isDetail, url = '') { | ||
return this.callPromiseAPI('GetBlocks', { | ||
@@ -356,3 +352,3 @@ start, | ||
isDetail, | ||
}); | ||
}, url); | ||
} | ||
@@ -364,4 +360,4 @@ /** | ||
*/ | ||
getLastHeader() { | ||
return this.callPromiseAPI('GetLastHeader'); | ||
getLastHeader(url = '') { | ||
return this.callPromiseAPI('GetLastHeader', {}, url); | ||
} | ||
@@ -376,3 +372,3 @@ /** | ||
*/ | ||
getHeaders(start, end, isDetail) { | ||
getHeaders(start, end, isDetail, url = '') { | ||
return this.callPromiseAPI('GetHeaders', { | ||
@@ -382,3 +378,3 @@ start, | ||
isDetail, | ||
}); | ||
}, url); | ||
} | ||
@@ -391,6 +387,6 @@ /** | ||
*/ | ||
getBlockHash(height) { | ||
getBlockHash(height, url = '') { | ||
return this.callPromiseAPI('GetBlockHash', { | ||
height, | ||
}); | ||
}, url); | ||
} | ||
@@ -403,6 +399,6 @@ /** | ||
*/ | ||
getBlockOverview(hash) { | ||
getBlockOverview(hash, url = '') { | ||
return this.callPromiseAPI('GetBlockOverview', { | ||
hash, | ||
}); | ||
}, url); | ||
} | ||
@@ -414,4 +410,4 @@ /** | ||
*/ | ||
getPeerInfo() { | ||
return this.callPromiseAPI('GetPeerInfo'); | ||
getPeerInfo(url = '') { | ||
return this.callPromiseAPI('GetPeerInfo', {}, url); | ||
} | ||
@@ -423,4 +419,4 @@ /** | ||
*/ | ||
getNetInfo() { | ||
return this.callPromiseAPI('GetNetInfo'); | ||
getNetInfo(url = '') { | ||
return this.callPromiseAPI('GetNetInfo', {}, url); | ||
} | ||
@@ -432,4 +428,4 @@ /** | ||
*/ | ||
getTimeStatus() { | ||
return this.callPromiseAPI('GetTimeStatus'); | ||
getTimeStatus(url = '') { | ||
return this.callPromiseAPI('GetTimeStatus', {}, url); | ||
} | ||
@@ -441,7 +437,8 @@ /** | ||
*/ | ||
isSync() { | ||
return this.callPromiseAPI('IsSync'); | ||
isSync(url = '') { | ||
return this.callPromiseAPI('IsSync', {}, url); | ||
} | ||
callPromiseAPI(apiName, params = {}) { | ||
callPromiseAPI(apiName, params = {}, url = '') { | ||
return this.httpProvider.doFetch({ | ||
url, | ||
postdata: { | ||
@@ -448,0 +445,0 @@ id: +new Date(), |
{ | ||
"name": "chain33-rpc-api", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "chain33 rpc 接口封装", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43398
902