Skip to content

Commit a382d2b

Browse files
zone117xrafaelcr
andauthored
fix: pagination and query param parsing bugs
* feat: route splitting between express and fastify * feat: refactor `/extended/v1/tx/*` endpoints to fastify * feat: refactor `/extended/v1/stx_supply/*` endpoints to fastify * feat: refactor `/extended/v1/info/*` endpoints to fastify * feat: refactor `/extended/v1/tokens/*` endpoints to fastify * feat: refactor `/extended/v1/tokens/*` endpoints to fastify * feat: refactor `/extended/v1/contract/*` endpoints to fastify * feat: refactor `/extended/v1/fee_rate/*` endpoints to fastify * feat: refactor `/extended/v1/microblock/*` endpoints to fastify * feat: refactor `/extended/v1/block/*` endpoints to fastify * feat: refactor `/extended/v1/burnchain/*` endpoints to fastify * feat: refactor `/extended/v1/address/*` endpoints to fastify * feat: refactor `/extended/v1/search/*` endpoints to fastify * feat: refactor `/extended/v1/pox*` endpoints to fastify * feat: refactor `/extended/v1/faucets/*` endpoints to fastify * feat: refactor `/extended/v1/debug/*` endpoints to fastify * feat: refactor `/extended/v2/blocks/*` and `/extended/v2/burn-blocks/*` endpoints to fastify * feat: refactor `/extended/v2/smart-contracts/*` endpoints to fastify * feat: refactor `/extended/v2/mempool/*` endpoints to fastify * feat: refactor `/extended/v2/pox/*` endpoints to fastify * feat: refactor `/extended/v2/addresses/*` endpoints to fastify * feat: refactor `/v1/names/*` endpoints to fastify * feat: refactor `/v1/namespaces/*` endpoints to fastify * feat: refactor `/v1/addresses/*` endpoints to fastify * feat: refactor `/v2/prices/*` endpoints to fastify * feat: refactor core-node RPC proxy (/v2/*) to fastify * chore: remove dead code * feat: openAPI spec generation from fastify routes * chore: remove references to legacy generated types * docs: missing openapi tag on burn-blocks route * docs: update docs and client generation scripts * fix: several query params should be optional * fix: only use a GET route for extended status * chore: simpify typing for TransactionSchema and MempoolTransactionSchema * feat: refactor client library * chore: remove dependencies on old generated types * chore: isolate rosetta json schemas and delete the rest * chore: cleanup prometheus metrics * fix: misc tests * test: misc rosetta fixes * fix: batch insert length assertion (#2042) * fix: batch insert length assertion * build: upgrade docker-compose * build: use docker compose * test: misc bns test fixes * test: misc pox fixes * ci: misc fixes * chore: fix unused exports lint * chore: simplify docs and client package.json scripts * feat: refactor event-server from express to fastify * chore: expose more helper types in the client lib * ci: fix client npm lib building * fix: openapi and client support for comma-separated query params * chore: expose more helper types to client lib * ci: fix lint and tests * fix: ensure height-or-hash params are parsed correctly and have correct openapi and client types * docs: client library migration guide * fix: tx event pagination limit * docs: note RPC client library change --------- Co-authored-by: Rafael Cárdenas <[email protected]>
1 parent 183ddd0 commit a382d2b

19 files changed

+421
-313
lines changed

client/MIGRATION.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## @stacks/blockchain-api-client (&lt;=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+
```

client/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
A JS Client for the Stacks Blockchain API
55

6+
## Breaking changes from (&lt;=7.x.x) → (8.x.x)
7+
8+
See [MIGRATION.md](./MIGRATION.md) for details.
9+
610
## Features
711

812
This package provides the ability to:

client/src/generated/schema.d.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ export interface operations {
18121812
offset?: number;
18131813
/** @description Results per page */
18141814
limit?: number;
1815-
type?: (("coinbase" | "token_transfer" | "smart_contract" | "contract_call" | "poison_microblock" | "tenure_change") | string)[];
1815+
type?: ("coinbase" | "token_transfer" | "smart_contract" | "contract_call" | "poison_microblock" | "tenure_change")[];
18161816
/**
18171817
* @description Include data from unanchored (i.e. unconfirmed) microblocks
18181818
* @example true
@@ -3177,7 +3177,7 @@ export interface operations {
31773177
get_tx_list_details: {
31783178
parameters: {
31793179
query: {
3180-
tx_id: (string)[];
3180+
tx_id: string[];
31813181
/** @description Results per page */
31823182
event_limit?: number;
31833183
/** @description Result offset */
@@ -6496,7 +6496,7 @@ export interface operations {
64966496
*/
64976497
tx_id?: string;
64986498
address?: string;
6499-
type?: (("smart_contract_log" | "stx_lock" | "stx_asset" | "fungible_token_asset" | "non_fungible_token_asset") | string)[];
6499+
type?: ("smart_contract_log" | "stx_lock" | "stx_asset" | "fungible_token_asset" | "non_fungible_token_asset")[];
65006500
/** @description Result offset */
65016501
offset?: number;
65026502
/** @description Results per page */
@@ -27255,7 +27255,7 @@ export interface operations {
2725527255
query?: never;
2725627256
header?: never;
2725727257
path: {
27258-
height_or_hash: "latest" | string;
27258+
height_or_hash: "latest" | string | number;
2725927259
};
2726027260
cookie?: never;
2726127261
};
@@ -27321,7 +27321,7 @@ export interface operations {
2732127321
};
2732227322
header?: never;
2732327323
path: {
27324-
height_or_hash: "latest" | string;
27324+
height_or_hash: "latest" | string | number;
2732527325
};
2732627326
cookie?: never;
2732727327
};
@@ -28697,7 +28697,7 @@ export interface operations {
2869728697
query?: never;
2869828698
header?: never;
2869928699
path: {
28700-
height_or_hash: "latest" | string;
28700+
height_or_hash: "latest" | string | number;
2870128701
};
2870228702
cookie?: never;
2870328703
};
@@ -28739,7 +28739,7 @@ export interface operations {
2873928739
};
2874028740
header?: never;
2874128741
path: {
28742-
height_or_hash: "latest" | string;
28742+
height_or_hash: "latest" | string | number;
2874328743
};
2874428744
cookie?: never;
2874528745
};

client/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './common';
1010
export * from './socket-io';
1111
export * from './ws';
1212
export type * from './types';
13+
export * from 'openapi-fetch';

client/src/types.d.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import type { operations } from "./generated/schema";
1+
import type { operations, paths } from './generated/schema';
2+
3+
type Extract200Response<T> = T extends { 200: infer R } ? R : never;
4+
type ExtractOperationResponse<T extends keyof operations> = Extract200Response<operations[T]['responses']> extends { content: { 'application/json': infer U } } ? U : never;
5+
type PathResponse<T extends keyof paths> = paths[T]['get'] extends { responses: infer R } ? Extract200Response<R> extends { content: { 'application/json': infer U } } ? U : never : never;
6+
7+
export type OperationResponse = {
8+
[K in keyof operations]: ExtractOperationResponse<K>;
9+
} & {
10+
[P in keyof paths]: PathResponse<P>;
11+
};
12+
13+
export type Transaction = OperationResponse['get_transaction_list']['results'][number];
14+
export type MempoolTransaction = OperationResponse['get_mempool_transaction_list']['results'][number];
15+
export type Block = OperationResponse['get_block_by_height'];
16+
export type Microblock = OperationResponse['get_microblock_by_hash'];
17+
export type NakamotoBlock = OperationResponse['get_block'];
18+
export type BurnBlock = OperationResponse['get_burn_blocks']['results'][number];
19+
export type SmartContract = OperationResponse['get_contract_by_id'];
20+
export type AddressTransactionWithTransfers = OperationResponse['get_account_transactions_with_transfers']['results'][number];
21+
export type AddressStxBalanceResponse = OperationResponse['get_account_stx_balance'];
222

3-
export type Transaction = operations['get_transaction_list']['responses']['200']['content']['application/json']['results'][number];
4-
export type MempoolTransaction = operations['get_mempool_transaction_list']['responses']['200']['content']['application/json']['results'][number];
5-
export type Block = operations['get_block_by_height']['responses']['200']['content']['application/json'];
6-
export type Microblock = operations['get_microblock_by_hash']['responses']['200']['content']['application/json'];
7-
export type NakamotoBlock = operations['get_block']['responses']['200']['content']['application/json'];
8-
export type BurnBlock = operations['get_burn_blocks']['responses']['200']['content']['application/json']['results'][number];
9-
export type SmartContract = operations['get_contract_by_id']['responses']['200']['content']['application/json'];
10-
export type AddressTransactionWithTransfers = operations['get_account_transactions_with_transfers']['responses']['200']['content']['application/json']['results'][number];
11-
export type AddressStxBalanceResponse = operations['get_account_stx_balance']['responses']['200']['content']['application/json'];
1223
export type RpcAddressTxNotificationParams = AddressTransactionWithTransfers & {
1324
address: string;
1425
tx_id: string;

0 commit comments

Comments
 (0)