Skip to content

Commit 3722962

Browse files
committed
Document @solana/rpc-transformers with TypeDoc
1 parent 889a4c0 commit 3722962

11 files changed

+177
-3
lines changed

Diff for: packages/rpc-transformers/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
# @solana/rpc-transformers
1313

14+
This package contains helpers for transforming Solana JSON RPC and RPC Subscriptions requests, responses, and notifications in various ways appropriate for use in a JavaScript application.
15+
1416
## Request Transformers
1517

1618
### `getDefaultRequestTransformerForSolanaRpc(config)`
@@ -28,14 +30,14 @@ const requestTransformer = getDefaultRequestTransformerForSolanaRpc({
2830
});
2931
```
3032

31-
### `getDefaultCommitmentTransformer(config)`
33+
### `getDefaultCommitmentRequestTransformer(config)`
3234

3335
Creates a transformer that adds the provided default commitment to the configuration object of the request when applicable.
3436

3537
```ts
36-
import { getDefaultCommitmentTransformer, OPTIONS_OBJECT_POSITION_BY_METHOD } from '@solana/rpc-transformers';
38+
import { getDefaultCommitmentRequestTransformer, OPTIONS_OBJECT_POSITION_BY_METHOD } from '@solana/rpc-transformers';
3739

38-
const requestTransformer = getDefaultCommitmentTransformer({
40+
const requestTransformer = getDefaultCommitmentRequestTransformer({
3941
defaultCommitment: 'confirmed',
4042
optionsObjectPositionByMethod: OPTIONS_OBJECT_POSITION_BY_METHOD,
4143
});

Diff for: packages/rpc-transformers/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* This package contains helpers for transforming Solana JSON RPC and RPC Subscriptions requests,
3+
* responses, and notifications in various ways appropriate for use in a JavaScript application.
4+
*
5+
* @packageDocumentation
6+
*/
17
export * from './request-transformer';
28
export * from './request-transformer-bigint-downcast';
39
export * from './request-transformer-default-commitment';
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { downcastNodeToNumberIfBigint } from './request-transformer-bigint-downcast-internal';
22
import { getTreeWalkerRequestTransformer } from './tree-traversal';
33

4+
/**
5+
* Creates a transformer that downcasts all `BigInt` values to `Number`.
6+
*
7+
* @example
8+
* ```ts
9+
* import { getBigIntDowncastRequestTransformer } from '@solana/rpc-transformers';
10+
*
11+
* const requestTransformer = getBigIntDowncastRequestTransformer();
12+
* ```
13+
*
14+
*/
415
export function getBigIntDowncastRequestTransformer() {
516
return getTreeWalkerRequestTransformer([downcastNodeToNumberIfBigint], { keyPath: [] });
617
}

Diff for: packages/rpc-transformers/src/request-transformer-default-commitment.ts

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import type { Commitment } from '@solana/rpc-types';
33

44
import { applyDefaultCommitment } from './request-transformer-default-commitment-internal';
55

6+
/**
7+
* Creates a transformer that adds the provided default commitment to the configuration object of the request when applicable.
8+
*
9+
* @param config
10+
*
11+
* @example
12+
* ```ts
13+
* import { getDefaultCommitmentRequestTransformer, OPTIONS_OBJECT_POSITION_BY_METHOD } from '@solana/rpc-transformers';
14+
*
15+
* const requestTransformer = getDefaultCommitmentRequestTransformer({
16+
* defaultCommitment: 'confirmed',
17+
* optionsObjectPositionByMethod: OPTIONS_OBJECT_POSITION_BY_METHOD,
18+
* });
19+
*/
620
export function getDefaultCommitmentRequestTransformer({
721
defaultCommitment,
822
optionsObjectPositionByMethod,

Diff for: packages/rpc-transformers/src/request-transformer-integer-overflow.ts

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import { getTreeWalkerRequestTransformer, KeyPath } from './tree-traversal';
55

66
export type IntegerOverflowHandler = (request: RpcRequest, keyPath: KeyPath, value: bigint) => void;
77

8+
/**
9+
* Creates a transformer that traverses the request parameters and executes the provided handler
10+
* when an integer overflow is detected.
11+
*
12+
* @example
13+
* ```ts
14+
* import { getIntegerOverflowRequestTransformer } from '@solana/rpc-transformers';
15+
*
16+
* const requestTransformer = getIntegerOverflowRequestTransformer((request, keyPath, value) => {
17+
* throw new Error(`Integer overflow at ${keyPath.join('.')}: ${value}`);
18+
* });
19+
* ```
20+
*/
821
export function getIntegerOverflowRequestTransformer(onIntegerOverflow: IntegerOverflowHandler) {
922
return <TParams>(request: RpcRequest<TParams>): RpcRequest => {
1023
const transformer = getTreeWalkerRequestTransformer(

Diff for: packages/rpc-transformers/src/request-transformer.ts

+31
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,41 @@ import { getIntegerOverflowRequestTransformer, IntegerOverflowHandler } from './
88
import { OPTIONS_OBJECT_POSITION_BY_METHOD } from './request-transformer-options-object-position-config';
99

1010
export type RequestTransformerConfig = Readonly<{
11+
/**
12+
* An optional {@link Commitment} value to use as the default when none is supplied by the
13+
* caller.
14+
*/
1115
defaultCommitment?: Commitment;
16+
/**
17+
* An optional function that will be called whenever a `bigint` input exceeds that which can be
18+
* expressed using JavaScript numbers.
19+
*
20+
* This is used in the default {@link SolanaRpcSubscriptionsApi} to throw an exception rather
21+
* than to allow truncated values to propagate through a program.
22+
*/
1223
onIntegerOverflow?: IntegerOverflowHandler;
1324
}>;
1425

26+
/**
27+
* Returns the default request transformer for the Solana RPC API.
28+
*
29+
* Under the hood, this function composes multiple
30+
* {@link RpcRequestTransformer | RpcRequestTransformers} together such as the
31+
* {@link getDefaultCommitmentTransformer}, the {@link getIntegerOverflowRequestTransformer} and the
32+
* {@link getBigIntDowncastRequestTransformer}.
33+
*
34+
* @example
35+
* ```ts
36+
* import { getDefaultRequestTransformerForSolanaRpc } from '@solana/rpc-transformers';
37+
*
38+
* const requestTransformer = getDefaultRequestTransformerForSolanaRpc({
39+
* defaultCommitment: 'confirmed',
40+
* onIntegerOverflow: (request, keyPath, value) => {
41+
* throw new Error(`Integer overflow at ${keyPath.join('.')}: ${value}`);
42+
* },
43+
* });
44+
* ```
45+
*/
1546
export function getDefaultRequestTransformerForSolanaRpc(config?: RequestTransformerConfig): RpcRequestTransformer {
1647
const handleIntegerOverflow = config?.onIntegerOverflow;
1748
return (request: RpcRequest): RpcRequest => {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
import { getBigIntUpcastVisitor } from './response-transformer-bigint-upcast-internal';
22
import { getTreeWalkerResponseTransformer, KeyPath } from './tree-traversal';
33

4+
/**
5+
* Returns a transformer that upcasts all `Number` values to `BigInts` unless they match within the
6+
* provided {@link KeyPath | KeyPaths}. In other words, the provided {@link KeyPath | KeyPaths} will
7+
* remain as `Number` values, any other numeric value will be upcasted to a `BigInt`.
8+
*
9+
* Note that you can use {@link KEYPATH_WILDCARD} to match any key within a {@link KeyPath}.
10+
*
11+
* @example
12+
* ```ts
13+
* import { getBigIntUpcastResponseTransformer } from '@solana/rpc-transformers';
14+
*
15+
* const responseTransformer = getBigIntUpcastResponseTransformer([
16+
* ['index'],
17+
* ['instructions', KEYPATH_WILDCARD, 'accounts', KEYPATH_WILDCARD],
18+
* ['instructions', KEYPATH_WILDCARD, 'programIdIndex'],
19+
* ['instructions', KEYPATH_WILDCARD, 'stackHeight'],
20+
* ]);
21+
* ```
22+
*/
423
export function getBigIntUpcastResponseTransformer(allowedNumericKeyPaths: readonly KeyPath[]) {
524
return getTreeWalkerResponseTransformer([getBigIntUpcastVisitor(allowedNumericKeyPaths)], { keyPath: [] });
625
}

Diff for: packages/rpc-transformers/src/response-transformer-result.ts

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { RpcResponseTransformer } from '@solana/rpc-spec-types';
22

33
type JsonRpcResponse = { result: unknown };
44

5+
/**
6+
* Returns a transformer that extracts the `result` field from the body of the RPC response.
7+
*
8+
* For instance, we go from `{ jsonrpc: '2.0', result: 'foo', id: 1 }` to `'foo'`.
9+
*
10+
* @example
11+
* ```ts
12+
* import { getResultResponseTransformer } from '@solana/rpc-transformers';
13+
*
14+
* const responseTransformer = getResultResponseTransformer();
15+
* ```
16+
*/
517
export function getResultResponseTransformer(): RpcResponseTransformer {
618
return json => (json as JsonRpcResponse).result;
719
}

Diff for: packages/rpc-transformers/src/response-transformer-throw-solana-error.ts

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ import { RpcResponseTransformer } from '@solana/rpc-spec-types';
33

44
type JsonRpcResponse = { error: Parameters<typeof getSolanaErrorFromJsonRpcError>[0] } | { result: unknown };
55

6+
/**
7+
* Returns a transformer that throws a {@link SolanaError} with the appropriate RPC error code if
8+
* the body of the RPC response contains an error.
9+
*
10+
* @example
11+
* ```ts
12+
* import { getThrowSolanaErrorResponseTransformer } from '@solana/rpc-transformers';
13+
*
14+
* const responseTransformer = getThrowSolanaErrorResponseTransformer();
15+
* ```
16+
*/
617
export function getThrowSolanaErrorResponseTransformer(): RpcResponseTransformer {
718
return json => {
819
const jsonRpcResponse = json as JsonRpcResponse;

Diff for: packages/rpc-transformers/src/response-transformer.ts

+36
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,31 @@ import { getResultResponseTransformer } from './response-transformer-result';
77
import { getThrowSolanaErrorResponseTransformer } from './response-transformer-throw-solana-error';
88

99
export type ResponseTransformerConfig<TApi> = Readonly<{
10+
/**
11+
* An optional map from the name of an API method to an array of {@link KeyPath | KeyPaths}
12+
* pointing to values in the response that should materialize in the application as `Number`
13+
* instead of `BigInt`.
14+
*/
1015
allowedNumericKeyPaths?: AllowedNumericKeypaths<TApi>;
1116
}>;
1217

18+
/**
19+
* Returns the default response transformer for the Solana RPC API.
20+
*
21+
* Under the hood, this function composes multiple
22+
* {@link RpcResponseTransformer | RpcResponseTransformers} together such as the
23+
* {@link getThrowSolanaErrorResponseTransformer}, the {@link getResultResponseTransformer} and the
24+
* {@link getBigIntUpcastResponseTransformer}.
25+
*
26+
* @example
27+
* ```ts
28+
* import { getDefaultResponseTransformerForSolanaRpc } from '@solana/rpc-transformers';
29+
*
30+
* const responseTransformer = getDefaultResponseTransformerForSolanaRpc({
31+
* allowedNumericKeyPaths: getAllowedNumericKeypaths(),
32+
* });
33+
* ```
34+
*/
1335
export function getDefaultResponseTransformerForSolanaRpc<TApi>(
1436
config?: ResponseTransformerConfig<TApi>,
1537
): RpcResponseTransformer {
@@ -26,6 +48,20 @@ export function getDefaultResponseTransformerForSolanaRpc<TApi>(
2648
};
2749
}
2850

51+
/**
52+
* Returns the default response transformer for the Solana RPC Subscriptions API.
53+
*
54+
* Under the hood, this function composes the {@link getBigIntUpcastResponseTransformer}.
55+
*
56+
* @example
57+
* ```ts
58+
* import { getDefaultResponseTransformerForSolanaRpcSubscriptions } from '@solana/rpc-transformers';
59+
*
60+
* const responseTransformer = getDefaultResponseTransformerForSolanaRpcSubscriptions({
61+
* allowedNumericKeyPaths: getAllowedNumericKeypaths(),
62+
* });
63+
* ```
64+
*/
2965
export function getDefaultResponseTransformerForSolanaRpcSubscriptions<TApi>(
3066
config?: ResponseTransformerConfig<TApi>,
3167
): RpcResponseTransformer {

Diff for: packages/rpc-transformers/src/tree-traversal.ts

+19
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ function getTreeWalker(visitors: NodeVisitor[]) {
3939
};
4040
}
4141

42+
/**
43+
* Creates a transformer that traverses the request parameters and executes the provided visitors at
44+
* each node. A custom initial state can be provided but must at least provide `{ keyPath: [] }`.
45+
*
46+
* @example
47+
* ```ts
48+
* import { getTreeWalkerRequestTransformer } from '@solana/rpc-transformers';
49+
*
50+
* const requestTransformer = getTreeWalkerRequestTransformer(
51+
* [
52+
* // Replaces foo.bar with "baz".
53+
* (node, state) => (state.keyPath === ['foo', 'bar'] ? 'baz' : node),
54+
* // Increments all numbers by 1.
55+
* node => (typeof node === number ? node + 1 : node),
56+
* ],
57+
* { keyPath: [] },
58+
* );
59+
* ```
60+
*/
4261
export function getTreeWalkerRequestTransformer<TState extends TraversalState>(
4362
visitors: NodeVisitor[],
4463
initialState: TState,

0 commit comments

Comments
 (0)