Skip to content

Commit

Permalink
fix: pagination and query param parsing bugs
Browse files Browse the repository at this point in the history
* 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 <rafael@rafaelcr.com>
  • Loading branch information
zone117x and rafaelcr authored Aug 15, 2024
1 parent 183ddd0 commit a382d2b
Show file tree
Hide file tree
Showing 19 changed files with 421 additions and 313 deletions.
73 changes: 73 additions & 0 deletions client/MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## @stacks/blockchain-api-client (&lt;=7.x.x) → (8.x.x)

## Breaking Changes

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.


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.

#### Configuration & Middleware

```ts
// old:
import { TransactionsApi, Configuration } from '@stacks/blockchain-api-client';
const client = new TransactionsApi(new Configuration({
basePath: 'https://api.mainnet.hiro.so',
middleware: [{
pre({url, init}) {
init.headers = new Headers(init.headers);
init.headers.set('x-custom-header', 'custom-value');
return Promise.resolve({ url, init });
}
}]
}));


// new:
import { createClient } from '@stacks/blockchain-api-client';
const client = createClient({
baseUrl: 'https://api.mainnet.hiro.so'
});
client.use({
onRequest({request}) {
request.headers.set('x-custom-header', 'custom-value');
return request;
}
});
```

#### Performing Requests

```ts
// old:
const blockTxs = await client.getTransactionsByBlock({
heightOrHash: 2000,
limit: 20,
offset: 100
});
console.log('Block transactions:', blockTxs);

// new:
const { data: blockTxs } = await client.GET('/extended/v2/blocks/{height_or_hash}/transactions', {
params: {
path: { height_or_hash: 2000 },
query: { limit: 20, offset: 100 },
}
});
console.log('Block transactions:', blockTxs);
```

#### Referencing Types

```ts
// old:
import { MempoolTransactionStatsResponse } from '@stacks/blockchain-api-client';
let response: MempoolTransactionStatsResponse;
response = await client.getMempoolTransactionStats();

// new:
import { OperationResponse } from '@stacks/blockchain-api-client';
let response: OperationResponse['/extended/v1/tx/mempool/stats'];
response = (await client.GET('/extended/v1/tx/mempool/stats')).data;
```
4 changes: 4 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

A JS Client for the Stacks Blockchain API

## Breaking changes from (&lt;=7.x.x) → (8.x.x)

See [MIGRATION.md](./MIGRATION.md) for details.

## Features

This package provides the ability to:
Expand Down
14 changes: 7 additions & 7 deletions client/src/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ export interface operations {
offset?: number;
/** @description Results per page */
limit?: number;
type?: (("coinbase" | "token_transfer" | "smart_contract" | "contract_call" | "poison_microblock" | "tenure_change") | string)[];
type?: ("coinbase" | "token_transfer" | "smart_contract" | "contract_call" | "poison_microblock" | "tenure_change")[];
/**
* @description Include data from unanchored (i.e. unconfirmed) microblocks
* @example true
Expand Down Expand Up @@ -3177,7 +3177,7 @@ export interface operations {
get_tx_list_details: {
parameters: {
query: {
tx_id: (string)[];
tx_id: string[];
/** @description Results per page */
event_limit?: number;
/** @description Result offset */
Expand Down Expand Up @@ -6496,7 +6496,7 @@ export interface operations {
*/
tx_id?: string;
address?: string;
type?: (("smart_contract_log" | "stx_lock" | "stx_asset" | "fungible_token_asset" | "non_fungible_token_asset") | string)[];
type?: ("smart_contract_log" | "stx_lock" | "stx_asset" | "fungible_token_asset" | "non_fungible_token_asset")[];
/** @description Result offset */
offset?: number;
/** @description Results per page */
Expand Down Expand Up @@ -27255,7 +27255,7 @@ export interface operations {
query?: never;
header?: never;
path: {
height_or_hash: "latest" | string;
height_or_hash: "latest" | string | number;
};
cookie?: never;
};
Expand Down Expand Up @@ -27321,7 +27321,7 @@ export interface operations {
};
header?: never;
path: {
height_or_hash: "latest" | string;
height_or_hash: "latest" | string | number;
};
cookie?: never;
};
Expand Down Expand Up @@ -28697,7 +28697,7 @@ export interface operations {
query?: never;
header?: never;
path: {
height_or_hash: "latest" | string;
height_or_hash: "latest" | string | number;
};
cookie?: never;
};
Expand Down Expand Up @@ -28739,7 +28739,7 @@ export interface operations {
};
header?: never;
path: {
height_or_hash: "latest" | string;
height_or_hash: "latest" | string | number;
};
cookie?: never;
};
Expand Down
1 change: 1 addition & 0 deletions client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './common';
export * from './socket-io';
export * from './ws';
export type * from './types';
export * from 'openapi-fetch';
31 changes: 21 additions & 10 deletions client/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import type { operations } from "./generated/schema";
import type { operations, paths } from './generated/schema';

type Extract200Response<T> = T extends { 200: infer R } ? R : never;
type ExtractOperationResponse<T extends keyof operations> = Extract200Response<operations[T]['responses']> extends { content: { 'application/json': infer U } } ? U : never;
type PathResponse<T extends keyof paths> = paths[T]['get'] extends { responses: infer R } ? Extract200Response<R> extends { content: { 'application/json': infer U } } ? U : never : never;

export type OperationResponse = {
[K in keyof operations]: ExtractOperationResponse<K>;
} & {
[P in keyof paths]: PathResponse<P>;
};

export type Transaction = OperationResponse['get_transaction_list']['results'][number];
export type MempoolTransaction = OperationResponse['get_mempool_transaction_list']['results'][number];
export type Block = OperationResponse['get_block_by_height'];
export type Microblock = OperationResponse['get_microblock_by_hash'];
export type NakamotoBlock = OperationResponse['get_block'];
export type BurnBlock = OperationResponse['get_burn_blocks']['results'][number];
export type SmartContract = OperationResponse['get_contract_by_id'];
export type AddressTransactionWithTransfers = OperationResponse['get_account_transactions_with_transfers']['results'][number];
export type AddressStxBalanceResponse = OperationResponse['get_account_stx_balance'];

export type Transaction = operations['get_transaction_list']['responses']['200']['content']['application/json']['results'][number];
export type MempoolTransaction = operations['get_mempool_transaction_list']['responses']['200']['content']['application/json']['results'][number];
export type Block = operations['get_block_by_height']['responses']['200']['content']['application/json'];
export type Microblock = operations['get_microblock_by_hash']['responses']['200']['content']['application/json'];
export type NakamotoBlock = operations['get_block']['responses']['200']['content']['application/json'];
export type BurnBlock = operations['get_burn_blocks']['responses']['200']['content']['application/json']['results'][number];
export type SmartContract = operations['get_contract_by_id']['responses']['200']['content']['application/json'];
export type AddressTransactionWithTransfers = operations['get_account_transactions_with_transfers']['responses']['200']['content']['application/json']['results'][number];
export type AddressStxBalanceResponse = operations['get_account_stx_balance']['responses']['200']['content']['application/json'];
export type RpcAddressTxNotificationParams = AddressTransactionWithTransfers & {
address: string;
tx_id: string;
Expand Down
Loading

0 comments on commit a382d2b

Please sign in to comment.