hirosystems / stacks-blockchain-api

API for the Stacks blockchain
https://stacks-blockchain-api.vercel.app
GNU General Public License v3.0
174 stars 111 forks source link

Wrong types on the API client #1632

Open hugocaillard opened 1 year ago

hugocaillard commented 1 year ago

It seems like I have outdated types with the @stacks/blockchain-api-client For example, AccountsApi.getAccountStxBalance returns Promise<object> instead of Promise<StxBalance> I believe.

I have this kind of issues in multiple places. Is it something wrong with the build or did I mess up somewhere?

Code here https://github.com/hirosystems/subnets-nft-demo/blob/606cb1477d9fa8bc75a8a34b50ccf243d1cb9a9e/src/stacks/apiCalls.ts#L53-L59

zone117x commented 1 year ago

The OpenAPI-to-typescript-client tooling isn't able to represent certain schemas as types. For many of them it will fallback to the object type.

To workaround this, we also publish this library: https://www.npmjs.com/package/@stacks/stacks-blockchain-api-types. This uses tool that is able to generate more useful/accurate types for schemas.

Currently you need to explicitly cast the object types you get from the @stacks/blockchain-api-client lib as the corresponding type from the @stacks/stacks-blockchain-api-types lib.

hugocaillard commented 1 year ago

Ok I see, thanks for clarifying @zone117x .

Do you know in which case the limitation occurs? For example, why does it impact

getAccountStxBalance(requestParameters: GetAccountStxBalanceRequest, initOverrides?: RequestInit): Promise<object>;

But others similar function have the right type:

getAccountBalance(requestParameters: GetAccountBalanceRequest, initOverrides?: RequestInit): Promise<AddressBalanceResponse>;

I'd be happy to investigate a bit if we think there's room for improvement here

zone117x commented 1 year ago

Do you know in which case the limitation occurs?

IIRC the "json schema to typescript" tooling mentions some known limitations. You could take a look at the npm build scripts ran inside the /client and /docs directory to see the various tools used, for example the generate-openapi command in /client/package.json. In some cases it might be possible to modify the json schema in way to make the tooling happy.