hirosystems / stacks-blockchain-api

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

api client types and documentation #2138

Open aulneau opened 3 weeks ago

aulneau commented 3 weeks ago

Hey all :)

The new openapi-fetch based client is pretty hard and confusing to use, could you add some basic documentation? 🙏

Additionally, I'm having a really hard time getting types to work correctly, I have to wrangle them pretty explicitly:

Image Image

Let me know if I'm missing anything or doing this incorrectly btw!

Thanks!!

aryzing commented 1 week ago

I'm with you @aulneau. I even created another API client just to provide myself with a friendlier API.

Perhaps it would be worthwhile to look into trpc or Elysia for the Stacks API. Elysia can also use TypeBox, supports typing of both params and body, and has OpenAPI plugins.

By inferring client types directly from the API definition, these tools are able to provide great DX. No need to wrangle a typed client together from the OpenAPI spec of the original typed API definition.

zone117x commented 1 week ago

Hi @aulneau, the error schema isn't defined for many of the routes which is why you're seeing the error: never type issue. The route definition here needs a status 4xx response schema defined: https://github.com/hirosystems/stacks-blockchain-api/blob/f0176a038b3c4fde35195bbfbf9b6d8d5504c9bb/src/api/routes/bns/addresses.ts#L37-L51

For now you can check the request status like:

Image

Here's an example of a route with correct error status schemas defined: https://github.com/hirosystems/stacks-blockchain-api/blob/f0176a038b3c4fde35195bbfbf9b6d8d5504c9bb/src/api/routes/search.ts#L277-L289

Image