Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Allow extending numeric keypaths when extending Solana RPC API #3212

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-flies-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/rpc-api': patch
---

Allow extending numeric keypaths when extending Solana RPC API
48 changes: 48 additions & 0 deletions packages/rpc-api/src/__tests__/create-solana-rpc-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getDefaultResponseTransformerForSolanaRpc } from '@solana/rpc-transformers';

import { createSolanaRpcApi, SolanaRpcApi } from '..';

jest.mock('@solana/rpc-transformers');

describe('createSolanaRpcApi', () => {
describe('when extending allowedNumericKeyPaths', () => {
it('should create a response transformer including the new paths', () => {
type TestApi = SolanaRpcApi & {
someNewFunction(): void;
someOtherFunction(): void;
};

createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {
someNewFunction: [['someNewKeyPath']],
someOtherFunction: [['someOtherKeyPath']],
},
});

expect(getDefaultResponseTransformerForSolanaRpc as jest.Mock).toHaveBeenCalledWith({
allowedNumericKeyPaths: expect.objectContaining({
someNewFunction: [['someNewKeyPath']],
someOtherFunction: [['someOtherKeyPath']],
}),
});
});

it('should include the default paths', () => {
type TestApi = SolanaRpcApi & {
someNewFunction(): void;
};

createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {
someNewFunction: [['someNewKeyPath']],
},
});

expect(getDefaultResponseTransformerForSolanaRpc as jest.Mock).toHaveBeenCalledWith({
allowedNumericKeyPaths: expect.objectContaining({
getAccountInfo: expect.any(Array),
}),
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createSolanaRpcApi, SolanaRpcApi } from '..';

type TestApi = SolanaRpcApi & {
someNewFunction(): void;
someOtherFunction(): void;
};

createSolanaRpcApi<TestApi>();
createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {},
});
createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {
someNewFunction: [['someNewKeyPath']],
},
});
createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {
someNewFunction: [['someNewKeyPath']],
someOtherFunction: [['someOtherKeyPath']],
},
});

createSolanaRpcApi<TestApi>({
extendAllowedNumericKeypaths: {
// @ts-expect-error getAccountInfo is a SolanaRpcApi method
getAccountInfo: [],
},
});

createSolanaRpcApi<SolanaRpcApi>();
createSolanaRpcApi<SolanaRpcApi>({
extendAllowedNumericKeypaths: {},
});
createSolanaRpcApi<SolanaRpcApi>({
extendAllowedNumericKeypaths: {
// @ts-expect-error getAccountInfo is a SolanaRpcApi method
getAccountInfo: [],
},
});
createSolanaRpcApi<SolanaRpcApi>({
extendAllowedNumericKeypaths: {
// @ts-expect-error someNewMethod is not a method on the RPC API
someNewMethod: [['someNewKeyPath']],
},
});
15 changes: 12 additions & 3 deletions packages/rpc-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,24 @@ export type {
SimulateTransactionApi,
};

type Config = RequestTransformerConfig;
// This is required because `AllowedKeyPaths<never>` allows any method
// So we instead force it to be an empty object in that case
type StrictAllowedNumericKeypaths<T> = [keyof T] extends [never] ? Record<string, never> : AllowedNumericKeypaths<T>;

type Config<
TRpcMethods extends SolanaRpcApi | SolanaRpcApiDevnet | SolanaRpcApiMainnet | SolanaRpcApiTestnet = SolanaRpcApi,
> = Readonly<{
extendAllowedNumericKeypaths?: StrictAllowedNumericKeypaths<Omit<TRpcMethods, keyof SolanaRpcApi>>;
}> &
RequestTransformerConfig;

export function createSolanaRpcApi<
TRpcMethods extends SolanaRpcApi | SolanaRpcApiDevnet | SolanaRpcApiMainnet | SolanaRpcApiTestnet = SolanaRpcApi,
>(config?: Config): RpcApi<TRpcMethods> {
>(config?: Config<TRpcMethods>): RpcApi<TRpcMethods> {
return createRpcApi<TRpcMethods>({
requestTransformer: getDefaultRequestTransformerForSolanaRpc(config),
responseTransformer: getDefaultResponseTransformerForSolanaRpc({
allowedNumericKeyPaths: getAllowedNumericKeypaths(),
allowedNumericKeyPaths: { ...(config?.extendAllowedNumericKeypaths ?? {}), ...getAllowedNumericKeypaths() },
}),
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc/src/rpc-default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { createSolanaRpcApi } from '@solana/rpc-api';

import { createSolanaJsonRpcIntegerOverflowError } from './rpc-integer-overflow-error';

export const DEFAULT_RPC_CONFIG: Partial<NonNullable<Parameters<typeof createSolanaRpcApi>[0]>> = {
export const DEFAULT_RPC_CONFIG = {
defaultCommitment: 'confirmed',
onIntegerOverflow(request, keyPath, value) {
throw createSolanaJsonRpcIntegerOverflowError(request.methodName, keyPath, value);
},
};
} as const satisfies Partial<NonNullable<Parameters<typeof createSolanaRpcApi>[0]>>;