This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more response transformers (#3163)
This PR refactors the `getDefaultResponseTransformerForSolanaRpc` function by creating four new response transformers and delegating to them: - `getThrowSolanaErrorResponseTransformer`: Throws a `SolanaError` if the RPC response is an error. - `getResultResponseTransformer`: Access the `result` attribute of the RPC response. - `getBigIntUpcastResponseTransformer`: Upcasts all numeric values to bigints unless they are within the provided allowed keypaths. - `getTreeWalkerResponseTransformer`: Helper that creates response transformers from visitors.
- Loading branch information
1 parent
c312964
commit 29d5113
Showing
8 changed files
with
149 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@solana/rpc-transformers': patch | ||
--- | ||
|
||
Add `getThrowSolanaErrorResponseTransformer`, `getResultResponseTransformer`, `getBigIntUpcastResponseTransformer` and `getTreeWalkerResponseTransformer` helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createJsonRpcResponseTransformer } from '@solana/rpc-spec'; | ||
|
||
type JsonRpcResponse = { result: unknown }; | ||
|
||
export function getResultResponseTransformer() { | ||
return createJsonRpcResponseTransformer(json => (json as JsonRpcResponse).result); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/rpc-transformers/src/response-transformer-throw-solana-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { getSolanaErrorFromJsonRpcError } from '@solana/errors'; | ||
import { createJsonRpcResponseTransformer } from '@solana/rpc-spec'; | ||
|
||
type JsonRpcResponse = { error: Parameters<typeof getSolanaErrorFromJsonRpcError>[0] } | { result: unknown }; | ||
|
||
export function getThrowSolanaErrorResponseTransformer() { | ||
return createJsonRpcResponseTransformer(json => { | ||
const jsonRpcResponse = json as JsonRpcResponse; | ||
if ('error' in jsonRpcResponse) { | ||
throw getSolanaErrorFromJsonRpcError(jsonRpcResponse.error); | ||
} | ||
return jsonRpcResponse; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters