From b965d83516d3e21cfe436bcb7dd184b04627fbfb Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Thu, 17 Oct 2024 16:43:01 +0100 Subject: [PATCH] Move RpcRequest and RpcResponse types to rpc-spec-types --- .changeset/giant-seals-mix.md | 6 +++++ packages/library/package.json | 1 + packages/library/src/index.ts | 1 + packages/rpc-spec-types/README.md | 23 ++++++++++++++++++- packages/rpc-spec-types/src/index.ts | 1 + .../src/rpc-request.ts} | 6 ----- packages/rpc-spec-types/src/rpc-response.ts | 8 +++++++ packages/rpc-spec/README.md | 19 --------------- .../rpc-spec/src/__tests__/rpc-api-test.ts | 3 ++- packages/rpc-spec/src/index.ts | 1 - packages/rpc-spec/src/rpc-api.ts | 10 +++++--- packages/rpc-spec/src/rpc-transport.ts | 2 +- packages/rpc-transformers/package.json | 2 +- .../src/__tests__/request-transformer-test.ts | 2 +- .../__tests__/response-transformer-test.ts | 2 +- .../request-transformer-default-commitment.ts | 2 +- .../request-transformer-integer-overflow.ts | 2 +- .../src/request-transformer.ts | 2 +- .../src/response-transformer-result.ts | 2 +- ...response-transformer-throw-solana-error.ts | 2 +- .../src/response-transformer.ts | 2 +- .../rpc-transformers/src/tree-traversal.ts | 2 +- packages/rpc-transport-http/README.md | 12 ++++++---- packages/rpc-transport-http/package.json | 1 + .../rpc-transport-http/src/http-transport.ts | 3 ++- packages/rpc/package.json | 1 + .../__tests__/rpc-request-coalescer-test.ts | 3 ++- packages/rpc/src/rpc-request-coalescer.ts | 3 ++- pnpm-lock.yaml | 13 +++++++++-- 29 files changed, 86 insertions(+), 51 deletions(-) create mode 100644 .changeset/giant-seals-mix.md rename packages/{rpc-spec/src/rpc-shared.ts => rpc-spec-types/src/rpc-request.ts} (51%) diff --git a/.changeset/giant-seals-mix.md b/.changeset/giant-seals-mix.md new file mode 100644 index 000000000000..ca9f52324a69 --- /dev/null +++ b/.changeset/giant-seals-mix.md @@ -0,0 +1,6 @@ +--- +'@solana/rpc-spec-types': patch +'@solana/rpc-spec': patch +--- + +Move RpcRequest and RpcResponse types to rpc-spec-types diff --git a/packages/library/package.json b/packages/library/package.json index 3f30e5575ae9..1784270869eb 100644 --- a/packages/library/package.json +++ b/packages/library/package.json @@ -86,6 +86,7 @@ "@solana/rpc-parsed-types": "workspace:*", "@solana/rpc-subscriptions": "workspace:*", "@solana/rpc-types": "workspace:*", + "@solana/rpc-spec-types": "workspace:*", "@solana/signers": "workspace:*", "@solana/sysvars": "workspace:*", "@solana/transaction-confirmation": "workspace:*", diff --git a/packages/library/src/index.ts b/packages/library/src/index.ts index 0890f50df356..251b57e839da 100644 --- a/packages/library/src/index.ts +++ b/packages/library/src/index.ts @@ -10,6 +10,7 @@ export * from '@solana/rpc'; export * from '@solana/rpc-parsed-types'; export * from '@solana/rpc-subscriptions'; export * from '@solana/rpc-types'; +export * from '@solana/rpc-spec-types'; export * from '@solana/signers'; export * from '@solana/transaction-messages'; export * from '@solana/transactions'; diff --git a/packages/rpc-spec-types/README.md b/packages/rpc-spec-types/README.md index 8db2a17b155c..d9e812a1c11a 100644 --- a/packages/rpc-spec-types/README.md +++ b/packages/rpc-spec-types/README.md @@ -14,4 +14,25 @@ # @solana/rpc-spec-types -TODO +This package contains core types that can be used on both RPC and RPC Subscriptions specifications. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@rc`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library). + +## Types + +### `RpcRequest` + +An object that describes the elements of an RPC or RPC Subscriptions request. It consists of the following properties: + +- `methodName`: The name of the RPC method or subscription requested. +- `params`: The parameters to be passed to the RPC server. + +### `RpcRequestTransformer` + +A function that accepts an `RpcRequest` and returns another `RpcRequest`. This allows the `RpcApi` to transform the request before it is sent to the RPC server. + +### `RpcResponse` + +A type that represents the response from an RPC server. This could be any sort of data which is why `RpcResponse` defaults to `unknown`. You may use a type parameter to specify the shape of the response — e.g. `RpcResponse<{ result: number }>`. + +### `RpcResponseTransformer` + +A function that accepts an `RpcResponse` and returns another `RpcResponse`. This allows the `RpcApi` to transform the response before it is returned to the caller. diff --git a/packages/rpc-spec-types/src/index.ts b/packages/rpc-spec-types/src/index.ts index a151cd974c6f..878f73d707a4 100644 --- a/packages/rpc-spec-types/src/index.ts +++ b/packages/rpc-spec-types/src/index.ts @@ -1,4 +1,5 @@ export * from './overloads'; export * from './rpc-message'; +export * from './rpc-request'; export * from './rpc-response'; export * from './type-helpers'; diff --git a/packages/rpc-spec/src/rpc-shared.ts b/packages/rpc-spec-types/src/rpc-request.ts similarity index 51% rename from packages/rpc-spec/src/rpc-shared.ts rename to packages/rpc-spec-types/src/rpc-request.ts index 7e4b1e74c02c..1b15caade124 100644 --- a/packages/rpc-spec/src/rpc-shared.ts +++ b/packages/rpc-spec-types/src/rpc-request.ts @@ -3,12 +3,6 @@ export type RpcRequest = { readonly params: TParams; }; -export type RpcResponse = TResponse; - export type RpcRequestTransformer = { (request: RpcRequest): RpcRequest; }; - -export type RpcResponseTransformer = { - (response: RpcResponse, request: RpcRequest): RpcResponse; -}; diff --git a/packages/rpc-spec-types/src/rpc-response.ts b/packages/rpc-spec-types/src/rpc-response.ts index 485323271f71..7efba62a60a7 100644 --- a/packages/rpc-spec-types/src/rpc-response.ts +++ b/packages/rpc-spec-types/src/rpc-response.ts @@ -1,3 +1,11 @@ +import type { RpcRequest } from './rpc-request'; + +export type RpcResponse = TResponse; + +export type RpcResponseTransformer = { + (response: RpcResponse, request: RpcRequest): RpcResponse; +}; + interface IHasIdentifier { readonly id: number; } diff --git a/packages/rpc-spec/README.md b/packages/rpc-spec/README.md index 40695b85b7db..ce76acf51233 100644 --- a/packages/rpc-spec/README.md +++ b/packages/rpc-spec/README.md @@ -41,25 +41,6 @@ Calling the `send(options)` method on a `PendingRpcRequest` will trigger the req An object that exposes all of the functions described by `TRpcMethods`, and fulfils them using `TRpcTransport`. Calling each method returns a `PendingRpcRequest` where `TResponse` is that method's response type. -### `RpcRequest` - -An object that describes the elements of a JSON RPC request. It consists of the following properties: - -- `methodName`: The name of the JSON RPC method to be called. -- `params`: The parameters to be passed to the JSON RPC method. - -### `RpcRequestTransformer` - -A function that accepts an `RpcRequest` and returns another `RpcRequest`. This allows the `RpcApi` to transform the request before it is sent to the JSON RPC server. - -### `RpcResponse` - -A type that represents the response from a JSON RPC server. This could be any sort of data which is why `RpcResponse` defaults to `unknown`. You may use a type parameter to specify the shape of the response — e.g. `RpcResponse<{ result: number }>`. - -### `RpcResponseTransformer` - -A function that accepts an `RpcResponse` and returns another `RpcResponse`. This allows the `RpcApi` to transform the response before it is returned to the caller. - ### `RpcApi` For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcApiRequestPlan` that describes how to prepare a JSON RPC request to fetch `TResponse`. diff --git a/packages/rpc-spec/src/__tests__/rpc-api-test.ts b/packages/rpc-spec/src/__tests__/rpc-api-test.ts index 43d2f0874c35..3be092b15600 100644 --- a/packages/rpc-spec/src/__tests__/rpc-api-test.ts +++ b/packages/rpc-spec/src/__tests__/rpc-api-test.ts @@ -1,7 +1,8 @@ import '@solana/test-matchers/toBeFrozenObject'; +import type { RpcRequest, RpcResponse } from '@solana/rpc-spec-types'; + import { createJsonRpcApi } from '../rpc-api'; -import { RpcRequest, RpcResponse } from '../rpc-shared'; type DummyApi = { someMethod(...args: unknown[]): unknown; diff --git a/packages/rpc-spec/src/index.ts b/packages/rpc-spec/src/index.ts index dec5b1a2316d..70976a6f746c 100644 --- a/packages/rpc-spec/src/index.ts +++ b/packages/rpc-spec/src/index.ts @@ -1,4 +1,3 @@ export * from './rpc'; export * from './rpc-api'; -export * from './rpc-shared'; export * from './rpc-transport'; diff --git a/packages/rpc-spec/src/rpc-api.ts b/packages/rpc-spec/src/rpc-api.ts index cce38ee7c367..92ac2f759924 100644 --- a/packages/rpc-spec/src/rpc-api.ts +++ b/packages/rpc-spec/src/rpc-api.ts @@ -1,6 +1,10 @@ -import { Callable, createRpcMessage } from '@solana/rpc-spec-types'; - -import { RpcRequestTransformer, RpcResponse, RpcResponseTransformer } from './rpc-shared'; +import { + Callable, + createRpcMessage, + RpcRequestTransformer, + RpcResponse, + RpcResponseTransformer, +} from '@solana/rpc-spec-types'; export type RpcApiConfig = Readonly<{ requestTransformer?: RpcRequestTransformer; diff --git a/packages/rpc-spec/src/rpc-transport.ts b/packages/rpc-spec/src/rpc-transport.ts index 04b2b5eda08c..ed7ff6deb02c 100644 --- a/packages/rpc-spec/src/rpc-transport.ts +++ b/packages/rpc-spec/src/rpc-transport.ts @@ -1,4 +1,4 @@ -import { RpcResponse } from './rpc-shared'; +import { RpcResponse } from '@solana/rpc-spec-types'; type RpcTransportRequest = Readonly<{ payload: unknown; diff --git a/packages/rpc-transformers/package.json b/packages/rpc-transformers/package.json index e61b909d6e13..40312e1fe106 100644 --- a/packages/rpc-transformers/package.json +++ b/packages/rpc-transformers/package.json @@ -73,7 +73,7 @@ "dependencies": { "@solana/errors": "workspace:*", "@solana/functional": "workspace:*", - "@solana/rpc-spec": "workspace:*", + "@solana/rpc-spec-types": "workspace:*", "@solana/rpc-subscriptions-spec": "workspace:*", "@solana/rpc-types": "workspace:*" }, diff --git a/packages/rpc-transformers/src/__tests__/request-transformer-test.ts b/packages/rpc-transformers/src/__tests__/request-transformer-test.ts index 5cb5202349e9..08c00e6ec47d 100644 --- a/packages/rpc-transformers/src/__tests__/request-transformer-test.ts +++ b/packages/rpc-transformers/src/__tests__/request-transformer-test.ts @@ -1,4 +1,4 @@ -import type { RpcRequestTransformer } from '@solana/rpc-spec'; +import type { RpcRequestTransformer } from '@solana/rpc-spec-types'; import type { Commitment } from '@solana/rpc-types'; import { getDefaultRequestTransformerForSolanaRpc } from '../request-transformer'; diff --git a/packages/rpc-transformers/src/__tests__/response-transformer-test.ts b/packages/rpc-transformers/src/__tests__/response-transformer-test.ts index f8aae00ee5af..c1d3275c35e6 100644 --- a/packages/rpc-transformers/src/__tests__/response-transformer-test.ts +++ b/packages/rpc-transformers/src/__tests__/response-transformer-test.ts @@ -1,5 +1,5 @@ import { SOLANA_ERROR__JSON_RPC__PARSE_ERROR, SolanaError } from '@solana/errors'; -import { RpcRequest } from '@solana/rpc-spec'; +import { RpcRequest } from '@solana/rpc-spec-types'; import { getDefaultResponseTransformerForSolanaRpc } from '../response-transformer'; import { KEYPATH_WILDCARD } from '../tree-traversal'; diff --git a/packages/rpc-transformers/src/request-transformer-default-commitment.ts b/packages/rpc-transformers/src/request-transformer-default-commitment.ts index ec6a4652e3c1..32340f6d2cf9 100644 --- a/packages/rpc-transformers/src/request-transformer-default-commitment.ts +++ b/packages/rpc-transformers/src/request-transformer-default-commitment.ts @@ -1,4 +1,4 @@ -import type { RpcRequest, RpcRequestTransformer } from '@solana/rpc-spec'; +import type { RpcRequest, RpcRequestTransformer } from '@solana/rpc-spec-types'; import type { Commitment } from '@solana/rpc-types'; export function getDefaultCommitmentRequestTransformer({ diff --git a/packages/rpc-transformers/src/request-transformer-integer-overflow.ts b/packages/rpc-transformers/src/request-transformer-integer-overflow.ts index dd2aeda186b1..c85dac8b4529 100644 --- a/packages/rpc-transformers/src/request-transformer-integer-overflow.ts +++ b/packages/rpc-transformers/src/request-transformer-integer-overflow.ts @@ -1,4 +1,4 @@ -import { RpcRequest } from '@solana/rpc-spec'; +import { RpcRequest } from '@solana/rpc-spec-types'; import { getTreeWalkerRequestTransformer, KeyPath, TraversalState } from './tree-traversal'; diff --git a/packages/rpc-transformers/src/request-transformer.ts b/packages/rpc-transformers/src/request-transformer.ts index 418fc6260814..76ca8cd53756 100644 --- a/packages/rpc-transformers/src/request-transformer.ts +++ b/packages/rpc-transformers/src/request-transformer.ts @@ -1,5 +1,5 @@ import { pipe } from '@solana/functional'; -import { RpcRequest, RpcRequestTransformer } from '@solana/rpc-spec'; +import { RpcRequest, RpcRequestTransformer } from '@solana/rpc-spec-types'; import { Commitment } from '@solana/rpc-types'; import { getBigIntDowncastRequestTransformer } from './request-transformer-bigint-downcast'; diff --git a/packages/rpc-transformers/src/response-transformer-result.ts b/packages/rpc-transformers/src/response-transformer-result.ts index 6a074dc9355a..d3aedd51db86 100644 --- a/packages/rpc-transformers/src/response-transformer-result.ts +++ b/packages/rpc-transformers/src/response-transformer-result.ts @@ -1,4 +1,4 @@ -import { RpcResponseTransformer } from '@solana/rpc-spec'; +import { RpcResponseTransformer } from '@solana/rpc-spec-types'; type JsonRpcResponse = { result: unknown }; diff --git a/packages/rpc-transformers/src/response-transformer-throw-solana-error.ts b/packages/rpc-transformers/src/response-transformer-throw-solana-error.ts index 0190bb816141..b9767b4c29c7 100644 --- a/packages/rpc-transformers/src/response-transformer-throw-solana-error.ts +++ b/packages/rpc-transformers/src/response-transformer-throw-solana-error.ts @@ -1,5 +1,5 @@ import { getSolanaErrorFromJsonRpcError } from '@solana/errors'; -import { RpcResponseTransformer } from '@solana/rpc-spec'; +import { RpcResponseTransformer } from '@solana/rpc-spec-types'; type JsonRpcResponse = { error: Parameters[0] } | { result: unknown }; diff --git a/packages/rpc-transformers/src/response-transformer.ts b/packages/rpc-transformers/src/response-transformer.ts index 40a86d59816f..5a904ab1a609 100644 --- a/packages/rpc-transformers/src/response-transformer.ts +++ b/packages/rpc-transformers/src/response-transformer.ts @@ -1,5 +1,5 @@ import { pipe } from '@solana/functional'; -import { RpcRequest, RpcResponse, RpcResponseTransformer } from '@solana/rpc-spec'; +import { RpcRequest, RpcResponse, RpcResponseTransformer } from '@solana/rpc-spec-types'; import { AllowedNumericKeypaths } from './response-transformer-allowed-numeric-values'; import { getBigIntUpcastResponseTransformer, getBigIntUpcastVisitor } from './response-transformer-bigint-upcast'; diff --git a/packages/rpc-transformers/src/tree-traversal.ts b/packages/rpc-transformers/src/tree-traversal.ts index c9c704250ee1..b4389fb7b753 100644 --- a/packages/rpc-transformers/src/tree-traversal.ts +++ b/packages/rpc-transformers/src/tree-traversal.ts @@ -1,4 +1,4 @@ -import { RpcRequest, RpcRequestTransformer, RpcResponseTransformer } from '@solana/rpc-spec'; +import { RpcRequest, RpcRequestTransformer, RpcResponseTransformer } from '@solana/rpc-spec-types'; export type KeyPathWildcard = { readonly __brand: unique symbol }; export type KeyPath = ReadonlyArray; diff --git a/packages/rpc-transport-http/README.md b/packages/rpc-transport-http/README.md index 43252c02f7c0..0275848505ae 100644 --- a/packages/rpc-transport-http/README.md +++ b/packages/rpc-transport-http/README.md @@ -127,7 +127,8 @@ Using this core transport, you can implement specialized functionality for lever Here’s an example of how someone might implement a “round robin” approach to distribute requests to multiple transports: ```ts -import { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import { RpcTransport } from '@solana/rpc-spec'; +import { RpcResponse } from '@solana/rpc-spec-types'; import { createHttpTransport } from '@solana/rpc-transport-http'; // Create a transport for each RPC server @@ -153,7 +154,8 @@ Another example of a possible customization for a transport is to shard requests Perhaps your application needs to make a large number of requests, or needs to fan request for different methods out to different servers. Here’s an example of an implementation that does the latter: ```ts -import { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import { RpcTransport } from '@solana/rpc-spec'; +import { RpcResponse } from '@solana/rpc-spec-types'; import { createHttpTransport } from '@solana/rpc-transport-http'; // Create multiple transports @@ -190,7 +192,8 @@ async function shardingTransport(...args: Parameters): The transport library can also be used to implement custom retry logic on any request: ```ts -import { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import { RpcTransport } from '@solana/rpc-spec'; +import { RpcResponse } from '@solana/rpc-spec-types'; import { createHttpTransport } from '@solana/rpc-transport-http'; // Set the maximum number of attempts to retry a request @@ -234,7 +237,8 @@ async function retryingTransport(...args: Parameters): Here’s an example of some failover logic integrated into a transport: ```ts -import { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import { RpcTransport } from '@solana/rpc-spec'; +import { RpcResponse } from '@solana/rpc-spec-types'; import { createHttpTransport } from '@solana/rpc-transport-http'; // Create a transport for each RPC server diff --git a/packages/rpc-transport-http/package.json b/packages/rpc-transport-http/package.json index 0817072e84de..c1ce995efed0 100644 --- a/packages/rpc-transport-http/package.json +++ b/packages/rpc-transport-http/package.json @@ -74,6 +74,7 @@ "dependencies": { "@solana/errors": "workspace:*", "@solana/rpc-spec": "workspace:*", + "@solana/rpc-spec-types": "workspace:*", "undici-types": "^6.20.0" }, "devDependencies": { diff --git a/packages/rpc-transport-http/src/http-transport.ts b/packages/rpc-transport-http/src/http-transport.ts index d9accae97c38..e06d48fce8eb 100644 --- a/packages/rpc-transport-http/src/http-transport.ts +++ b/packages/rpc-transport-http/src/http-transport.ts @@ -1,5 +1,6 @@ import { SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR, SolanaError } from '@solana/errors'; -import { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import type { RpcTransport } from '@solana/rpc-spec'; +import type { RpcResponse } from '@solana/rpc-spec-types'; import type Dispatcher from 'undici-types/dispatcher'; import { diff --git a/packages/rpc/package.json b/packages/rpc/package.json index 8ed80f47f068..9a2d9ec48193 100644 --- a/packages/rpc/package.json +++ b/packages/rpc/package.json @@ -76,6 +76,7 @@ "@solana/functional": "workspace:*", "@solana/rpc-api": "workspace:*", "@solana/rpc-spec": "workspace:*", + "@solana/rpc-spec-types": "workspace:*", "@solana/rpc-transformers": "workspace:*", "@solana/rpc-transport-http": "workspace:*", "@solana/rpc-types": "workspace:*" diff --git a/packages/rpc/src/__tests__/rpc-request-coalescer-test.ts b/packages/rpc/src/__tests__/rpc-request-coalescer-test.ts index aa5b1072b819..217005be4e95 100644 --- a/packages/rpc/src/__tests__/rpc-request-coalescer-test.ts +++ b/packages/rpc/src/__tests__/rpc-request-coalescer-test.ts @@ -1,4 +1,5 @@ -import type { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import type { RpcTransport } from '@solana/rpc-spec'; +import type { RpcResponse } from '@solana/rpc-spec-types'; import { getRpcTransportWithRequestCoalescing } from '../rpc-request-coalescer'; diff --git a/packages/rpc/src/rpc-request-coalescer.ts b/packages/rpc/src/rpc-request-coalescer.ts index 4ba9eb757cb8..98e14f479ef7 100644 --- a/packages/rpc/src/rpc-request-coalescer.ts +++ b/packages/rpc/src/rpc-request-coalescer.ts @@ -1,4 +1,5 @@ -import type { RpcResponse, RpcTransport } from '@solana/rpc-spec'; +import type { RpcTransport } from '@solana/rpc-spec'; +import type { RpcResponse } from '@solana/rpc-spec-types'; type CoalescedRequest = { readonly abortController: AbortController; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 604a563c35b8..26c35eef525f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -584,6 +584,9 @@ importers: '@solana/rpc-parsed-types': specifier: workspace:* version: link:../rpc-parsed-types + '@solana/rpc-spec-types': + specifier: workspace:* + version: link:../rpc-spec-types '@solana/rpc-subscriptions': specifier: workspace:* version: link:../rpc-subscriptions @@ -727,6 +730,9 @@ importers: '@solana/rpc-spec': specifier: workspace:* version: link:../rpc-spec + '@solana/rpc-spec-types': + specifier: workspace:* + version: link:../rpc-spec-types '@solana/rpc-transformers': specifier: workspace:* version: link:../rpc-transformers @@ -975,9 +981,9 @@ importers: '@solana/functional': specifier: workspace:* version: link:../functional - '@solana/rpc-spec': + '@solana/rpc-spec-types': specifier: workspace:* - version: link:../rpc-spec + version: link:../rpc-spec-types '@solana/rpc-subscriptions-spec': specifier: workspace:* version: link:../rpc-subscriptions-spec @@ -996,6 +1002,9 @@ importers: '@solana/rpc-spec': specifier: workspace:* version: link:../rpc-spec + '@solana/rpc-spec-types': + specifier: workspace:* + version: link:../rpc-spec-types typescript: specifier: '>=5' version: 5.5.2