Skip to content

Commit

Permalink
Combine RpcResponseTransformer and RpcResponseTransformerFor
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Aug 27, 2024
1 parent 26f7290 commit dcb68fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
10 changes: 4 additions & 6 deletions packages/rpc-spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ This allows the `RpcApi` to decide whether they want the parsed JSON object or t

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.

Note that a `RpcResponseTransformerFor<T>` generic function type is also available to ensure the response data returned by the transformer matches the expected type `T`.

### `RpcApi<TRpcMethods>`

For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcApiRequestPlan<TResponse>` that describes how to prepare a JSON RPC request to fetch `TResponse`.
Expand Down Expand Up @@ -143,13 +141,13 @@ A config object with the following properties:
- `requestTransformer<T>(request: RpcRequest<T>): RpcRequest`: An optional function that transforms the `RpcRequest` before it is sent to the JSON RPC server.
- `responseTransformer<T>(response: RpcResponse, request: RpcRequest): RpcResponse<T>`: An optional function that transforms the `RpcResponse` before it is returned to the caller.

### `createJsonRpcResponseTransformer<T>(jsonTransformer)`
### `createJsonRpcResponseTransformer(jsonTransformer)`

Creates an `RpcResponseTransformerFor<T>` function from a function that transforms any JSON value to a value of type `T` by wrapping it in a `json` async function.
Creates an `RpcResponseTransformer<T>` function from a function that transforms any JSON value to a value of type `T` by wrapping it in a `json` async function.

```ts
const getResultTransformer = createJsonRpcResponseTransformer((json: unknown) => {
return (json as { result: TResponse }).result;
const getResultTransformer = createJsonRpcResponseTransformer((json: unknown): unknown => {
return (json as { result: unknown }).result;
});
```

Expand Down
10 changes: 7 additions & 3 deletions packages/rpc-spec/src/rpc-api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Callable } from '@solana/rpc-spec-types';

import { RpcRequest, RpcRequestTransformer, RpcResponseTransformer, RpcResponseTransformerFor } from './rpc-shared';
import { RpcRequest, RpcRequestTransformer, RpcResponseTransformer } from './rpc-shared';

export type RpcApiConfig = Readonly<{
requestTransformer?: RpcRequestTransformer;
responseTransformer?: RpcResponseTransformer;
}>;

export type RpcApiRequestPlan<TResponse> = RpcRequest & {
responseTransformer?: RpcResponseTransformerFor<TResponse>;
responseTransformer?: RpcResponseTransformer<TResponse>;
};

export type RpcApi<TRpcMethods> = {
Expand Down Expand Up @@ -50,7 +50,11 @@ export function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiC
return Object.freeze({
...request,
...(config?.responseTransformer
? { responseTransformer: config.responseTransformer<ReturnType<TRpcMethods[TMethodName]>> }
? {
responseTransformer: config.responseTransformer as RpcResponseTransformer<
ReturnType<TRpcMethods[TMethodName]>
>,
}
: {}),
});
};
Expand Down
10 changes: 3 additions & 7 deletions packages/rpc-spec/src/rpc-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ export type RpcRequestTransformer = {
<TParams>(request: RpcRequest<TParams>): RpcRequest;
};

export type RpcResponseTransformer = {
<TResponse>(response: RpcResponse, request: RpcRequest): RpcResponse<TResponse>;
};

export type RpcResponseTransformerFor<TResponse> = {
export type RpcResponseTransformer<TResponse = unknown> = {
(response: RpcResponse, request: RpcRequest): RpcResponse<TResponse>;
};

export function createJsonRpcResponseTransformer<TResponse>(
export function createJsonRpcResponseTransformer<TResponse = unknown>(
jsonTransformer: (json: unknown, request: RpcRequest) => TResponse,
): RpcResponseTransformerFor<TResponse> {
): RpcResponseTransformer<TResponse> {
return function (response: RpcResponse, request: RpcRequest): RpcResponse<TResponse> {
return Object.freeze({
...response,
Expand Down

0 comments on commit dcb68fd

Please sign in to comment.