|
| 1 | +## @stacks/blockchain-api-client (<=7.x.x) → (8.x.x) |
| 2 | + |
| 3 | +## Breaking Changes |
| 4 | + |
| 5 | +This library is now generated with [openapi-typescript](https://openapi-ts.dev/openapi-fetch/) rather than [swagger-codegen](https://github.com/swagger-api/swagger-codegen). Several types which previously presented as the `any` type are now fixed, and the `@stacks/stacks-blockchain-api-types` package is no longer needed. |
| 6 | + |
| 7 | + |
| 8 | +This repo no longer includes a schema for the Stacks Blockchain RPC interface. An alternative client library for the RPC interface can be found at https://github.com/hirosystems/stacks.js/pull/1737. |
| 9 | + |
| 10 | +#### Configuration & Middleware |
| 11 | + |
| 12 | +```ts |
| 13 | +// old: |
| 14 | +import { TransactionsApi, Configuration } from '@stacks/blockchain-api-client'; |
| 15 | +const client = new TransactionsApi(new Configuration({ |
| 16 | + basePath: 'https://api.mainnet.hiro.so', |
| 17 | + middleware: [{ |
| 18 | + pre({url, init}) { |
| 19 | + init.headers = new Headers(init.headers); |
| 20 | + init.headers.set('x-custom-header', 'custom-value'); |
| 21 | + return Promise.resolve({ url, init }); |
| 22 | + } |
| 23 | + }] |
| 24 | +})); |
| 25 | + |
| 26 | + |
| 27 | +// new: |
| 28 | +import { createClient } from '@stacks/blockchain-api-client'; |
| 29 | +const client = createClient({ |
| 30 | + baseUrl: 'https://api.mainnet.hiro.so' |
| 31 | +}); |
| 32 | +client.use({ |
| 33 | + onRequest({request}) { |
| 34 | + request.headers.set('x-custom-header', 'custom-value'); |
| 35 | + return request; |
| 36 | + } |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +#### Performing Requests |
| 41 | + |
| 42 | +```ts |
| 43 | +// old: |
| 44 | +const blockTxs = await client.getTransactionsByBlock({ |
| 45 | + heightOrHash: 2000, |
| 46 | + limit: 20, |
| 47 | + offset: 100 |
| 48 | +}); |
| 49 | +console.log('Block transactions:', blockTxs); |
| 50 | + |
| 51 | +// new: |
| 52 | +const { data: blockTxs } = await client.GET('/extended/v2/blocks/{height_or_hash}/transactions', { |
| 53 | + params: { |
| 54 | + path: { height_or_hash: 2000 }, |
| 55 | + query: { limit: 20, offset: 100 }, |
| 56 | + } |
| 57 | +}); |
| 58 | +console.log('Block transactions:', blockTxs); |
| 59 | +``` |
| 60 | + |
| 61 | +#### Referencing Types |
| 62 | + |
| 63 | +```ts |
| 64 | +// old: |
| 65 | +import { MempoolTransactionStatsResponse } from '@stacks/blockchain-api-client'; |
| 66 | +let response: MempoolTransactionStatsResponse; |
| 67 | +response = await client.getMempoolTransactionStats(); |
| 68 | + |
| 69 | +// new: |
| 70 | +import { OperationResponse } from '@stacks/blockchain-api-client'; |
| 71 | +let response: OperationResponse['/extended/v1/tx/mempool/stats']; |
| 72 | +response = (await client.GET('/extended/v1/tx/mempool/stats')).data; |
| 73 | +``` |
0 commit comments