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/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-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/pnpm-lock.yaml b/pnpm-lock.yaml index e46a464cbd6c..9b796810bc5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -996,6 +996,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