typescipt:为cryptocompare coinlist API

我试图做一个打字稿接口,为cryptocompare coinlist API创建界面(https://www.cryptocompare.com/api/data/coinlist/)typescipt:为cryptocompare coinlist API

我已经尝试过目前这样的:

interface CoinListResponse { 

Response: string,

Message: string,

BaseImageUrl: string,

BaseLinkUrl: string,

Type: number,

Data: Array<Coin>

}

现在,这部分作品但Data: Array<Coin>存在问题,因为Data不是数组而是对象。你可以在这里看到documenation:https://www.cryptocompare.com/api/#-api-data-coinlist-

这是我想解决什么:

这是硬币接口:

export interface Coin { 

Id: number,

Url: string,

Name: string,

CoinName: string,

FullName: string,

Algorithm: string,

ProofType: string,

SortOrder: number

}

这可能吗?

非常感谢。

回答:

根据API JSON响应定义:

"Data": { 

"LTC": {

"Id": "3808",

"Url": "/coins/ltc/overview",

"ImageUrl": "/media/19782/ltc.png",

"Name": "LTC",

"CoinName": "Litecoin",

"FullName": "Litecoin (LTC)",

"Algorithm": "Scrypt",

"ProofType": "PoW",

"SortOrder": "2"

}

...

},

Data属性可以被建模为一个键 - 值对类型,其中关键是货币名称(string),值是​​类型的。

export interface CoinData { 

[key: string]: Coin

};

interface CoinListResponse {

Response: string,

Message: string,

BaseImageUrl: string,

BaseLinkUrl: string,

Type: number,

Data: CoinData

}

回答:

解决方案不必须中将sortOrder either.I认为这将是内部data.And另一个对象[ts] Operator '<=' cannot be applied to types 'Coin' and 'number'.

export function coins$(): Observable<any> { 

const response = WebRequest.json<CoinListResponse>(`${API_BASE}/data/all/coinlist`);

return Observable.fromPromise(response)

.map(response => response.Data)

.map(coins => Object.values(coins).filter(coin => coin.SortOrder <= COIN_LIMIT));

};

以上是 typescipt:为cryptocompare coinlist API 的全部内容, 来源链接: utcz.com/qa/265377.html

回到顶部