@solana/rpc-spec 2.0.0-canary-20240731173615
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +118 -0
- package/dist/index.browser.cjs +69 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +66 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.native.mjs +66 -0
- package/dist/index.native.mjs.map +1 -0
- package/dist/index.node.cjs +69 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +66 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/rpc-api.d.ts +17 -0
- package/dist/types/rpc-api.d.ts.map +1 -0
- package/dist/types/rpc-request.d.ts +12 -0
- package/dist/types/rpc-request.d.ts.map +1 -0
- package/dist/types/rpc-transport.d.ts +9 -0
- package/dist/types/rpc-transport.d.ts.map +1 -0
- package/dist/types/rpc.d.ts +18 -0
- package/dist/types/rpc.d.ts.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2023 Solana Labs, Inc
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
[![npm][npm-image]][npm-url]
|
|
2
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
3
|
+
[![semantic-release][semantic-release-image]][semantic-release-url]
|
|
4
|
+
<br />
|
|
5
|
+
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
|
|
6
|
+
|
|
7
|
+
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
|
|
8
|
+
[code-style-prettier-url]: https://github.com/prettier/prettier
|
|
9
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/rpc-spec/experimental.svg?style=flat
|
|
10
|
+
[npm-image]: https://img.shields.io/npm/v/@solana/rpc-spec/experimental.svg?style=flat
|
|
11
|
+
[npm-url]: https://www.npmjs.com/package/@solana/rpc-spec/v/experimental
|
|
12
|
+
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
|
13
|
+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
|
|
14
|
+
|
|
15
|
+
# @solana/rpc-spec
|
|
16
|
+
|
|
17
|
+
This package contains types that describe the implementation of the JSON RPC API, as well as methods to create one. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
|
|
18
|
+
|
|
19
|
+
This API is designed to be used as follows:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
const rpc =
|
|
23
|
+
// Step 1 - Create an `Rpc` instance. This may be stateful.
|
|
24
|
+
createSolanaRpc(mainnet('https://api.mainnet-beta.solana.com'));
|
|
25
|
+
const response = await rpc
|
|
26
|
+
// Step 2 - Call supported methods on it to produce `PendingRpcRequest` objects.
|
|
27
|
+
.getLatestBlockhash({ commitment: 'confirmed' })
|
|
28
|
+
// Step 3 - Call the `send()` method on those pending requests to trigger them.
|
|
29
|
+
.send({ abortSignal: AbortSignal.timeout(10_000) });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Types
|
|
33
|
+
|
|
34
|
+
### `PendingRpcRequest<TResponse>`
|
|
35
|
+
|
|
36
|
+
Pending requests are the result of calling a supported method on on `Rpc` object. They encapsulate all of the information necessary to make the request without actually making it.
|
|
37
|
+
|
|
38
|
+
Calling the `send(options)` method on a `PendingRpcRequest` will trigger the request and return a promise for `TResponse`.
|
|
39
|
+
|
|
40
|
+
### `Rpc<TRpcMethods, TRpcTransport>`
|
|
41
|
+
|
|
42
|
+
An object that exposes all of the functions described by `TRpcMethods`, and fulfils them using `TRpcTransport`. Calling each method returns a `PendingRpcRequest<TResponse>` where `TResponse` is that method's response type.
|
|
43
|
+
|
|
44
|
+
### `RpcApi<TRpcMethods>`
|
|
45
|
+
|
|
46
|
+
For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcRequest<TResponse>` that describes how to prepare a JSON RPC request to fetch `TResponse`.
|
|
47
|
+
|
|
48
|
+
### `RpcApiMethods`
|
|
49
|
+
|
|
50
|
+
This is a marker interface that all RPC method definitions must extend to be accepted for use with the `RpcApi` creator.
|
|
51
|
+
|
|
52
|
+
### `RpcRequest`
|
|
53
|
+
|
|
54
|
+
This type describes how a particular request should be issued to the JSON RPC server. Given a function that was called on a `Rpc`, this object gives you the opportunity to:
|
|
55
|
+
|
|
56
|
+
- customize the JSON RPC method name in the case that it's different than the name of that function
|
|
57
|
+
- define the shape of the JSON RPC params in case they are different than the arguments provided to that function
|
|
58
|
+
- provide a function to transform the JSON RPC server's response, in case it does not match the `TResponse` specified by the `PendingRpcRequest<TResponse>` returned from that function.
|
|
59
|
+
|
|
60
|
+
### `RpcSendOptions`
|
|
61
|
+
|
|
62
|
+
A configuration object consisting of the following properties:
|
|
63
|
+
|
|
64
|
+
- `abortSignal`: An optional signal that you can supply when triggering a `PendingRpcRequest` that you might later need to abort.
|
|
65
|
+
|
|
66
|
+
### `RpcTransport`
|
|
67
|
+
|
|
68
|
+
Any function that implements this interface can act as a transport for an `Rpc`. It need only return a promise for a response given the following config:
|
|
69
|
+
|
|
70
|
+
- `payload`: A value of arbitrary type to be sent.
|
|
71
|
+
- `signal`: An optional `AbortSignal` on which the `'abort'` event will be fired if the request should be cancelled.
|
|
72
|
+
|
|
73
|
+
## Functions
|
|
74
|
+
|
|
75
|
+
### `createRpc(config)`
|
|
76
|
+
|
|
77
|
+
Creates an RPC instance given an `RpcApi<TRpcMethods>` and a `RpcTransport` capable of fulfilling them.
|
|
78
|
+
|
|
79
|
+
#### Arguments
|
|
80
|
+
|
|
81
|
+
A config object with the following properties:
|
|
82
|
+
|
|
83
|
+
- `api`: An instance of `RpcApi`
|
|
84
|
+
- `transport`: A function that implements the `RpcTransport` interface
|
|
85
|
+
|
|
86
|
+
### `createRpcApi(config)`
|
|
87
|
+
|
|
88
|
+
Creates a JavaScript proxy that converts _any_ function call called on it to a `RpcRequest` by:
|
|
89
|
+
|
|
90
|
+
- setting `methodName` to the name of the function called
|
|
91
|
+
- setting `params` to the arguments supplied to that function, optionally transformed by `config.parametersTransformer`
|
|
92
|
+
- setting `responseTransformer` to `config.responseTransformer` or the identity function if no such config exists
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// For example, given this `RpcApi`:
|
|
96
|
+
const rpcApi = createRpcApi({
|
|
97
|
+
paramsTransformer: (...rawParams) => rawParams.reverse(),
|
|
98
|
+
responseTransformer: response => response.result,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// ...the following function call:
|
|
102
|
+
rpcApi.foo('bar', { baz: 'bat' });
|
|
103
|
+
|
|
104
|
+
// ...will produce the following `RpcRequest` object:
|
|
105
|
+
//
|
|
106
|
+
// {
|
|
107
|
+
// methodName: 'foo',
|
|
108
|
+
// params: [{ baz: 'bat' }, 'bar'],
|
|
109
|
+
// responseTransformer: (response) => response.result,
|
|
110
|
+
// }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### Arguments
|
|
114
|
+
|
|
115
|
+
A config object with the following properties:
|
|
116
|
+
|
|
117
|
+
- `parametersTransformer<T>(params: T, methodName): unknown`: An optional function that maps between the shape of the arguments an RPC method was called with and the shape of the params expected by the JSON RPC server.
|
|
118
|
+
- `responseTransformer<T>(response, methodName): T`: An optional function that maps between the shape of the JSON RPC server response for a given method and the shape of the response expected by the `RpcApi`.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rpcSpecTypes = require('@solana/rpc-spec-types');
|
|
4
|
+
|
|
5
|
+
// src/rpc.ts
|
|
6
|
+
function createRpc(rpcConfig) {
|
|
7
|
+
return makeProxy(rpcConfig);
|
|
8
|
+
}
|
|
9
|
+
function makeProxy(rpcConfig) {
|
|
10
|
+
return new Proxy(rpcConfig.api, {
|
|
11
|
+
defineProperty() {
|
|
12
|
+
return false;
|
|
13
|
+
},
|
|
14
|
+
deleteProperty() {
|
|
15
|
+
return false;
|
|
16
|
+
},
|
|
17
|
+
get(target, p, receiver) {
|
|
18
|
+
return function(...rawParams) {
|
|
19
|
+
const methodName = p.toString();
|
|
20
|
+
const createRpcRequest = Reflect.get(target, methodName, receiver);
|
|
21
|
+
const newRequest = createRpcRequest ? createRpcRequest(...rawParams) : { methodName, params: rawParams };
|
|
22
|
+
return createPendingRpcRequest(rpcConfig, newRequest);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function createPendingRpcRequest(rpcConfig, pendingRequest) {
|
|
28
|
+
return {
|
|
29
|
+
async send(options) {
|
|
30
|
+
const { methodName, params, responseTransformer } = pendingRequest;
|
|
31
|
+
const payload = rpcSpecTypes.createRpcMessage(methodName, params);
|
|
32
|
+
const response = await rpcConfig.transport({
|
|
33
|
+
payload,
|
|
34
|
+
signal: options?.abortSignal
|
|
35
|
+
});
|
|
36
|
+
return responseTransformer ? responseTransformer(response, methodName) : response;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/rpc-api.ts
|
|
42
|
+
function createRpcApi(config) {
|
|
43
|
+
return new Proxy({}, {
|
|
44
|
+
defineProperty() {
|
|
45
|
+
return false;
|
|
46
|
+
},
|
|
47
|
+
deleteProperty() {
|
|
48
|
+
return false;
|
|
49
|
+
},
|
|
50
|
+
get(...args) {
|
|
51
|
+
const [_, p] = args;
|
|
52
|
+
const methodName = p.toString();
|
|
53
|
+
return function(...rawParams) {
|
|
54
|
+
const params = config?.parametersTransformer ? config?.parametersTransformer(rawParams, methodName) : rawParams;
|
|
55
|
+
const responseTransformer = config?.responseTransformer ? config?.responseTransformer : (rawResponse) => rawResponse;
|
|
56
|
+
return {
|
|
57
|
+
methodName,
|
|
58
|
+
params,
|
|
59
|
+
responseTransformer
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.createRpc = createRpc;
|
|
67
|
+
exports.createRpcApi = createRpcApi;
|
|
68
|
+
//# sourceMappingURL=index.browser.cjs.map
|
|
69
|
+
//# sourceMappingURL=index.browser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts"],"names":["createRpcMessage"],"mappings":";;;;;AAkCO,SAAS,UACZ,SACgB,EAAA;AAChB,EAAA,OAA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CAAA;AAAA,KAC9E;AAAA,GACJ,CAAA;AACJ,CAAA;;;ACvDO,SAAS,aAAgD,MAA4C,EAAA;AACxG,EAAO,OAAA,IAAI,KAAM,CAAA,EAA2B,EAAA;AAAA,IACxC,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,OACO,IACL,EAAA;AACE,MAAM,MAAA,CAAC,CAAG,EAAA,CAAC,CAAI,GAAA,IAAA,CAAA;AACf,MAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,MAAA,OAAO,YACA,SAG6C,EAAA;AAChD,QAAA,MAAM,SAAS,MAAQ,EAAA,qBAAA,GACjB,QAAQ,qBAAsB,CAAA,SAAA,EAAW,UAAU,CACnD,GAAA,SAAA,CAAA;AACN,QAAA,MAAM,sBAAsB,MAAQ,EAAA,mBAAA,GAC9B,MAAQ,EAAA,mBAAA,GACR,CAAC,WAAyB,KAAA,WAAA,CAAA;AAChC,QAAO,OAAA;AAAA,UACH,UAAA;AAAA,UACA,MAAA;AAAA,UACA,mBAAA;AAAA,SACJ,CAAA;AAAA,OACJ,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL","file":"index.browser.cjs","sourcesContent":["import {\n Callable,\n createRpcMessage,\n Flatten,\n OverloadImplementations,\n RpcResponse,\n UnionToIntersection,\n} from '@solana/rpc-spec-types';\n\nimport { RpcApi } from './rpc-api';\nimport { PendingRpcRequest, RpcRequest, RpcSendOptions } from './rpc-request';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{\n api: RpcApi<TRpcMethods>;\n transport: TRpcTransport;\n}>;\n\nexport type Rpc<TRpcMethods> = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;\n};\n\ntype PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper<TMethodImplementation> =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>>\n : never;\n\nexport function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return makeProxy(rpcConfig) as Rpc<TRpcMethods>;\n}\n\nfunction makeProxy<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const createRpcRequest = Reflect.get(target, methodName, receiver);\n const newRequest = createRpcRequest\n ? createRpcRequest(...rawParams)\n : { methodName, params: rawParams };\n return createPendingRpcRequest(rpcConfig, newRequest);\n };\n },\n }) as Rpc<TRpcMethods>;\n}\n\nfunction createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport, TResponse>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n pendingRequest: RpcRequest<TResponse>,\n): PendingRpcRequest<TResponse> {\n return {\n async send(options?: RpcSendOptions): Promise<TResponse> {\n const { methodName, params, responseTransformer } = pendingRequest;\n const payload = createRpcMessage(methodName, params);\n const response = await rpcConfig.transport<RpcResponse<unknown>>({\n payload,\n signal: options?.abortSignal,\n });\n return (responseTransformer ? responseTransformer(response, methodName) : response) as TResponse;\n },\n };\n}\n","import { Callable } from '@solana/rpc-spec-types';\n\nimport { RpcRequest } from './rpc-request';\n\nexport type RpcApiConfig = Readonly<{\n parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;\n responseTransformer?: <T>(response: unknown, methodName?: string) => T;\n}>;\n\nexport type RpcApi<TRpcMethods> = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;\n};\n\ntype RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\nexport interface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\nexport function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods> {\n return new Proxy({} as RpcApi<TRpcMethods>, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get<TMethodName extends keyof RpcApi<TRpcMethods>>(\n ...args: Parameters<NonNullable<ProxyHandler<RpcApi<TRpcMethods>>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcRequest<ReturnType<TRpcMethods[TMethodName]>> {\n const params = config?.parametersTransformer\n ? config?.parametersTransformer(rawParams, methodName)\n : rawParams;\n const responseTransformer = config?.responseTransformer\n ? config?.responseTransformer<ReturnType<TRpcMethods[TMethodName]>>\n : (rawResponse: unknown) => rawResponse as ReturnType<TRpcMethods[TMethodName]>;\n return {\n methodName,\n params,\n responseTransformer,\n };\n };\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createRpcMessage } from '@solana/rpc-spec-types';
|
|
2
|
+
|
|
3
|
+
// src/rpc.ts
|
|
4
|
+
function createRpc(rpcConfig) {
|
|
5
|
+
return makeProxy(rpcConfig);
|
|
6
|
+
}
|
|
7
|
+
function makeProxy(rpcConfig) {
|
|
8
|
+
return new Proxy(rpcConfig.api, {
|
|
9
|
+
defineProperty() {
|
|
10
|
+
return false;
|
|
11
|
+
},
|
|
12
|
+
deleteProperty() {
|
|
13
|
+
return false;
|
|
14
|
+
},
|
|
15
|
+
get(target, p, receiver) {
|
|
16
|
+
return function(...rawParams) {
|
|
17
|
+
const methodName = p.toString();
|
|
18
|
+
const createRpcRequest = Reflect.get(target, methodName, receiver);
|
|
19
|
+
const newRequest = createRpcRequest ? createRpcRequest(...rawParams) : { methodName, params: rawParams };
|
|
20
|
+
return createPendingRpcRequest(rpcConfig, newRequest);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function createPendingRpcRequest(rpcConfig, pendingRequest) {
|
|
26
|
+
return {
|
|
27
|
+
async send(options) {
|
|
28
|
+
const { methodName, params, responseTransformer } = pendingRequest;
|
|
29
|
+
const payload = createRpcMessage(methodName, params);
|
|
30
|
+
const response = await rpcConfig.transport({
|
|
31
|
+
payload,
|
|
32
|
+
signal: options?.abortSignal
|
|
33
|
+
});
|
|
34
|
+
return responseTransformer ? responseTransformer(response, methodName) : response;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/rpc-api.ts
|
|
40
|
+
function createRpcApi(config) {
|
|
41
|
+
return new Proxy({}, {
|
|
42
|
+
defineProperty() {
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
deleteProperty() {
|
|
46
|
+
return false;
|
|
47
|
+
},
|
|
48
|
+
get(...args) {
|
|
49
|
+
const [_, p] = args;
|
|
50
|
+
const methodName = p.toString();
|
|
51
|
+
return function(...rawParams) {
|
|
52
|
+
const params = config?.parametersTransformer ? config?.parametersTransformer(rawParams, methodName) : rawParams;
|
|
53
|
+
const responseTransformer = config?.responseTransformer ? config?.responseTransformer : (rawResponse) => rawResponse;
|
|
54
|
+
return {
|
|
55
|
+
methodName,
|
|
56
|
+
params,
|
|
57
|
+
responseTransformer
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { createRpc, createRpcApi };
|
|
65
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
66
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts"],"names":[],"mappings":";;;AAkCO,SAAS,UACZ,SACgB,EAAA;AAChB,EAAA,OAAO,UAAU,SAAS,CAAA,CAAA;AAC9B,CAAA;AAEA,SAAS,UACL,SACgB,EAAA;AAChB,EAAO,OAAA,IAAI,KAAM,CAAA,SAAA,CAAU,GAAK,EAAA;AAAA,IAC5B,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,GAAA,CAAI,MAAQ,EAAA,CAAA,EAAG,QAAU,EAAA;AACrB,MAAA,OAAO,YAAa,SAAsB,EAAA;AACtC,QAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,QAAA,MAAM,gBAAmB,GAAA,OAAA,CAAQ,GAAI,CAAA,MAAA,EAAQ,YAAY,QAAQ,CAAA,CAAA;AACjE,QAAM,MAAA,UAAA,GAAa,mBACb,gBAAiB,CAAA,GAAG,SAAS,CAC7B,GAAA,EAAE,UAAY,EAAA,MAAA,EAAQ,SAAU,EAAA,CAAA;AACtC,QAAO,OAAA,uBAAA,CAAwB,WAAW,UAAU,CAAA,CAAA;AAAA,OACxD,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL,CAAA;AAEA,SAAS,uBAAA,CACL,WACA,cAC4B,EAAA;AAC5B,EAAO,OAAA;AAAA,IACH,MAAM,KAAK,OAA8C,EAAA;AACrD,MAAA,MAAM,EAAE,UAAA,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\n RpcResponse,\n UnionToIntersection,\n} from '@solana/rpc-spec-types';\n\nimport { RpcApi } from './rpc-api';\nimport { PendingRpcRequest, RpcRequest, RpcSendOptions } from './rpc-request';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{\n api: RpcApi<TRpcMethods>;\n transport: TRpcTransport;\n}>;\n\nexport type Rpc<TRpcMethods> = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;\n};\n\ntype PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper<TMethodImplementation> =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>>\n : never;\n\nexport function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return makeProxy(rpcConfig) as Rpc<TRpcMethods>;\n}\n\nfunction makeProxy<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const createRpcRequest = Reflect.get(target, methodName, receiver);\n const newRequest = createRpcRequest\n ? createRpcRequest(...rawParams)\n : { methodName, params: rawParams };\n return createPendingRpcRequest(rpcConfig, newRequest);\n };\n },\n }) as Rpc<TRpcMethods>;\n}\n\nfunction createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport, TResponse>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n pendingRequest: RpcRequest<TResponse>,\n): PendingRpcRequest<TResponse> {\n return {\n async send(options?: RpcSendOptions): Promise<TResponse> {\n const { methodName, params, responseTransformer } = pendingRequest;\n const payload = createRpcMessage(methodName, params);\n const response = await rpcConfig.transport<RpcResponse<unknown>>({\n payload,\n signal: options?.abortSignal,\n });\n return (responseTransformer ? responseTransformer(response, methodName) : response) as TResponse;\n },\n };\n}\n","import { Callable } from '@solana/rpc-spec-types';\n\nimport { RpcRequest } from './rpc-request';\n\nexport type RpcApiConfig = Readonly<{\n parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;\n responseTransformer?: <T>(response: unknown, methodName?: string) => T;\n}>;\n\nexport type RpcApi<TRpcMethods> = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;\n};\n\ntype RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\nexport interface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\nexport function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods> {\n return new Proxy({} as RpcApi<TRpcMethods>, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get<TMethodName extends keyof RpcApi<TRpcMethods>>(\n ...args: Parameters<NonNullable<ProxyHandler<RpcApi<TRpcMethods>>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcRequest<ReturnType<TRpcMethods[TMethodName]>> {\n const params = config?.parametersTransformer\n ? config?.parametersTransformer(rawParams, methodName)\n : rawParams;\n const responseTransformer = config?.responseTransformer\n ? config?.responseTransformer<ReturnType<TRpcMethods[TMethodName]>>\n : (rawResponse: unknown) => rawResponse as ReturnType<TRpcMethods[TMethodName]>;\n return {\n methodName,\n params,\n responseTransformer,\n };\n };\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createRpcMessage } from '@solana/rpc-spec-types';
|
|
2
|
+
|
|
3
|
+
// src/rpc.ts
|
|
4
|
+
function createRpc(rpcConfig) {
|
|
5
|
+
return makeProxy(rpcConfig);
|
|
6
|
+
}
|
|
7
|
+
function makeProxy(rpcConfig) {
|
|
8
|
+
return new Proxy(rpcConfig.api, {
|
|
9
|
+
defineProperty() {
|
|
10
|
+
return false;
|
|
11
|
+
},
|
|
12
|
+
deleteProperty() {
|
|
13
|
+
return false;
|
|
14
|
+
},
|
|
15
|
+
get(target, p, receiver) {
|
|
16
|
+
return function(...rawParams) {
|
|
17
|
+
const methodName = p.toString();
|
|
18
|
+
const createRpcRequest = Reflect.get(target, methodName, receiver);
|
|
19
|
+
const newRequest = createRpcRequest ? createRpcRequest(...rawParams) : { methodName, params: rawParams };
|
|
20
|
+
return createPendingRpcRequest(rpcConfig, newRequest);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function createPendingRpcRequest(rpcConfig, pendingRequest) {
|
|
26
|
+
return {
|
|
27
|
+
async send(options) {
|
|
28
|
+
const { methodName, params, responseTransformer } = pendingRequest;
|
|
29
|
+
const payload = createRpcMessage(methodName, params);
|
|
30
|
+
const response = await rpcConfig.transport({
|
|
31
|
+
payload,
|
|
32
|
+
signal: options?.abortSignal
|
|
33
|
+
});
|
|
34
|
+
return responseTransformer ? responseTransformer(response, methodName) : response;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/rpc-api.ts
|
|
40
|
+
function createRpcApi(config) {
|
|
41
|
+
return new Proxy({}, {
|
|
42
|
+
defineProperty() {
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
deleteProperty() {
|
|
46
|
+
return false;
|
|
47
|
+
},
|
|
48
|
+
get(...args) {
|
|
49
|
+
const [_, p] = args;
|
|
50
|
+
const methodName = p.toString();
|
|
51
|
+
return function(...rawParams) {
|
|
52
|
+
const params = config?.parametersTransformer ? config?.parametersTransformer(rawParams, methodName) : rawParams;
|
|
53
|
+
const responseTransformer = config?.responseTransformer ? config?.responseTransformer : (rawResponse) => rawResponse;
|
|
54
|
+
return {
|
|
55
|
+
methodName,
|
|
56
|
+
params,
|
|
57
|
+
responseTransformer
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { createRpc, createRpcApi };
|
|
65
|
+
//# sourceMappingURL=index.native.mjs.map
|
|
66
|
+
//# sourceMappingURL=index.native.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts"],"names":[],"mappings":";;;AAkCO,SAAS,UACZ,SACgB,EAAA;AAChB,EAAA,OAAO,UAAU,SAAS,CAAA,CAAA;AAC9B,CAAA;AAEA,SAAS,UACL,SACgB,EAAA;AAChB,EAAO,OAAA,IAAI,KAAM,CAAA,SAAA,CAAU,GAAK,EAAA;AAAA,IAC5B,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,GAAA,CAAI,MAAQ,EAAA,CAAA,EAAG,QAAU,EAAA;AACrB,MAAA,OAAO,YAAa,SAAsB,EAAA;AACtC,QAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,QAAA,MAAM,gBAAmB,GAAA,OAAA,CAAQ,GAAI,CAAA,MAAA,EAAQ,YAAY,QAAQ,CAAA,CAAA;AACjE,QAAM,MAAA,UAAA,G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,CACnD,GAAA,SAAA,CAAA;AACN,QAAA,MAAM,sBAAsB,MAAQ,EAAA,mBAAA,GAC9B,MAAQ,EAAA,mBAAA,GACR,CAAC,WAAyB,KAAA,WAAA,CAAA;AAChC,QAAO,OAAA;AAAA,UACH,UAAA;AAAA,UACA,MAAA;AAAA,UACA,mBAAA;AAAA,SACJ,CAAA;AAAA,OACJ,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL","file":"index.native.mjs","sourcesContent":["import {\n Callable,\n createRpcMessage,\n Flatten,\n OverloadImplementations,\n RpcResponse,\n UnionToIntersection,\n} from '@solana/rpc-spec-types';\n\nimport { RpcApi } from './rpc-api';\nimport { PendingRpcRequest, RpcRequest, RpcSendOptions } from './rpc-request';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{\n api: RpcApi<TRpcMethods>;\n transport: TRpcTransport;\n}>;\n\nexport type Rpc<TRpcMethods> = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;\n};\n\ntype PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper<TMethodImplementation> =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>>\n : never;\n\nexport function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return makeProxy(rpcConfig) as Rpc<TRpcMethods>;\n}\n\nfunction makeProxy<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const createRpcRequest = Reflect.get(target, methodName, receiver);\n const newRequest = createRpcRequest\n ? createRpcRequest(...rawParams)\n : { methodName, params: rawParams };\n return createPendingRpcRequest(rpcConfig, newRequest);\n };\n },\n }) as Rpc<TRpcMethods>;\n}\n\nfunction createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport, TResponse>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n pendingRequest: RpcRequest<TResponse>,\n): PendingRpcRequest<TResponse> {\n return {\n async send(options?: RpcSendOptions): Promise<TResponse> {\n const { methodName, params, responseTransformer } = pendingRequest;\n const payload = createRpcMessage(methodName, params);\n const response = await rpcConfig.transport<RpcResponse<unknown>>({\n payload,\n signal: options?.abortSignal,\n });\n return (responseTransformer ? responseTransformer(response, methodName) : response) as TResponse;\n },\n };\n}\n","import { Callable } from '@solana/rpc-spec-types';\n\nimport { RpcRequest } from './rpc-request';\n\nexport type RpcApiConfig = Readonly<{\n parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;\n responseTransformer?: <T>(response: unknown, methodName?: string) => T;\n}>;\n\nexport type RpcApi<TRpcMethods> = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;\n};\n\ntype RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\nexport interface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\nexport function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods> {\n return new Proxy({} as RpcApi<TRpcMethods>, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get<TMethodName extends keyof RpcApi<TRpcMethods>>(\n ...args: Parameters<NonNullable<ProxyHandler<RpcApi<TRpcMethods>>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcRequest<ReturnType<TRpcMethods[TMethodName]>> {\n const params = config?.parametersTransformer\n ? config?.parametersTransformer(rawParams, methodName)\n : rawParams;\n const responseTransformer = config?.responseTransformer\n ? config?.responseTransformer<ReturnType<TRpcMethods[TMethodName]>>\n : (rawResponse: unknown) => rawResponse as ReturnType<TRpcMethods[TMethodName]>;\n return {\n methodName,\n params,\n responseTransformer,\n };\n };\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rpcSpecTypes = require('@solana/rpc-spec-types');
|
|
4
|
+
|
|
5
|
+
// src/rpc.ts
|
|
6
|
+
function createRpc(rpcConfig) {
|
|
7
|
+
return makeProxy(rpcConfig);
|
|
8
|
+
}
|
|
9
|
+
function makeProxy(rpcConfig) {
|
|
10
|
+
return new Proxy(rpcConfig.api, {
|
|
11
|
+
defineProperty() {
|
|
12
|
+
return false;
|
|
13
|
+
},
|
|
14
|
+
deleteProperty() {
|
|
15
|
+
return false;
|
|
16
|
+
},
|
|
17
|
+
get(target, p, receiver) {
|
|
18
|
+
return function(...rawParams) {
|
|
19
|
+
const methodName = p.toString();
|
|
20
|
+
const createRpcRequest = Reflect.get(target, methodName, receiver);
|
|
21
|
+
const newRequest = createRpcRequest ? createRpcRequest(...rawParams) : { methodName, params: rawParams };
|
|
22
|
+
return createPendingRpcRequest(rpcConfig, newRequest);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function createPendingRpcRequest(rpcConfig, pendingRequest) {
|
|
28
|
+
return {
|
|
29
|
+
async send(options) {
|
|
30
|
+
const { methodName, params, responseTransformer } = pendingRequest;
|
|
31
|
+
const payload = rpcSpecTypes.createRpcMessage(methodName, params);
|
|
32
|
+
const response = await rpcConfig.transport({
|
|
33
|
+
payload,
|
|
34
|
+
signal: options?.abortSignal
|
|
35
|
+
});
|
|
36
|
+
return responseTransformer ? responseTransformer(response, methodName) : response;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/rpc-api.ts
|
|
42
|
+
function createRpcApi(config) {
|
|
43
|
+
return new Proxy({}, {
|
|
44
|
+
defineProperty() {
|
|
45
|
+
return false;
|
|
46
|
+
},
|
|
47
|
+
deleteProperty() {
|
|
48
|
+
return false;
|
|
49
|
+
},
|
|
50
|
+
get(...args) {
|
|
51
|
+
const [_, p] = args;
|
|
52
|
+
const methodName = p.toString();
|
|
53
|
+
return function(...rawParams) {
|
|
54
|
+
const params = config?.parametersTransformer ? config?.parametersTransformer(rawParams, methodName) : rawParams;
|
|
55
|
+
const responseTransformer = config?.responseTransformer ? config?.responseTransformer : (rawResponse) => rawResponse;
|
|
56
|
+
return {
|
|
57
|
+
methodName,
|
|
58
|
+
params,
|
|
59
|
+
responseTransformer
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.createRpc = createRpc;
|
|
67
|
+
exports.createRpcApi = createRpcApi;
|
|
68
|
+
//# sourceMappingURL=index.node.cjs.map
|
|
69
|
+
//# sourceMappingURL=index.node.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts"],"names":["createRpcMessage"],"mappings":";;;;;AAkCO,SAAS,UACZ,SACgB,EAAA;AAChB,EAAA,OAAO,UAAU,SAAS,CAAA,CAAA;AAC9B,CAAA;AAEA,SAAS,UACL,SACgB,EAAA;AAChB,EAAO,OAAA,IAAI,KAAM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OAAA,IAAI,KAAM,CAAA,EAA2B,EAAA;AAAA,IACxC,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,OACO,IACL,EAAA;AACE,MAAM,MAAA,CAAC,CAAG,EAAA,CAAC,CAAI,GAAA,IAAA,CAAA;AACf,MAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,MAAA,OAAO,YACA,SAG6C,EAAA;AAChD,QAAA,MAAM,SAAS,MAAQ,EAAA,qBAAA,GACjB,QAAQ,qBAAsB,CAAA,SAAA,EAAW,UAAU,CACnD,GAAA,SAAA,CAAA;AACN,QAAA,MAAM,sBAAsB,MAAQ,EAAA,mBAAA,GAC9B,MAAQ,EAAA,mBAAA,GACR,CAAC,WAAyB,KAAA,WAAA,CAAA;AAChC,QAAO,OAAA;AAAA,UACH,UAAA;AAAA,UACA,MAAA;AAAA,UACA,mBAAA;AAAA,SACJ,CAAA;AAAA,OACJ,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL","file":"index.node.cjs","sourcesContent":["import {\n Callable,\n createRpcMessage,\n Flatten,\n OverloadImplementations,\n RpcResponse,\n UnionToIntersection,\n} from '@solana/rpc-spec-types';\n\nimport { RpcApi } from './rpc-api';\nimport { PendingRpcRequest, RpcRequest, RpcSendOptions } from './rpc-request';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{\n api: RpcApi<TRpcMethods>;\n transport: TRpcTransport;\n}>;\n\nexport type Rpc<TRpcMethods> = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;\n};\n\ntype PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper<TMethodImplementation> =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>>\n : never;\n\nexport function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return makeProxy(rpcConfig) as Rpc<TRpcMethods>;\n}\n\nfunction makeProxy<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const createRpcRequest = Reflect.get(target, methodName, receiver);\n const newRequest = createRpcRequest\n ? createRpcRequest(...rawParams)\n : { methodName, params: rawParams };\n return createPendingRpcRequest(rpcConfig, newRequest);\n };\n },\n }) as Rpc<TRpcMethods>;\n}\n\nfunction createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport, TResponse>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n pendingRequest: RpcRequest<TResponse>,\n): PendingRpcRequest<TResponse> {\n return {\n async send(options?: RpcSendOptions): Promise<TResponse> {\n const { methodName, params, responseTransformer } = pendingRequest;\n const payload = createRpcMessage(methodName, params);\n const response = await rpcConfig.transport<RpcResponse<unknown>>({\n payload,\n signal: options?.abortSignal,\n });\n return (responseTransformer ? responseTransformer(response, methodName) : response) as TResponse;\n },\n };\n}\n","import { Callable } from '@solana/rpc-spec-types';\n\nimport { RpcRequest } from './rpc-request';\n\nexport type RpcApiConfig = Readonly<{\n parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;\n responseTransformer?: <T>(response: unknown, methodName?: string) => T;\n}>;\n\nexport type RpcApi<TRpcMethods> = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;\n};\n\ntype RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\nexport interface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\nexport function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods> {\n return new Proxy({} as RpcApi<TRpcMethods>, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get<TMethodName extends keyof RpcApi<TRpcMethods>>(\n ...args: Parameters<NonNullable<ProxyHandler<RpcApi<TRpcMethods>>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcRequest<ReturnType<TRpcMethods[TMethodName]>> {\n const params = config?.parametersTransformer\n ? config?.parametersTransformer(rawParams, methodName)\n : rawParams;\n const responseTransformer = config?.responseTransformer\n ? config?.responseTransformer<ReturnType<TRpcMethods[TMethodName]>>\n : (rawResponse: unknown) => rawResponse as ReturnType<TRpcMethods[TMethodName]>;\n return {\n methodName,\n params,\n responseTransformer,\n };\n };\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createRpcMessage } from '@solana/rpc-spec-types';
|
|
2
|
+
|
|
3
|
+
// src/rpc.ts
|
|
4
|
+
function createRpc(rpcConfig) {
|
|
5
|
+
return makeProxy(rpcConfig);
|
|
6
|
+
}
|
|
7
|
+
function makeProxy(rpcConfig) {
|
|
8
|
+
return new Proxy(rpcConfig.api, {
|
|
9
|
+
defineProperty() {
|
|
10
|
+
return false;
|
|
11
|
+
},
|
|
12
|
+
deleteProperty() {
|
|
13
|
+
return false;
|
|
14
|
+
},
|
|
15
|
+
get(target, p, receiver) {
|
|
16
|
+
return function(...rawParams) {
|
|
17
|
+
const methodName = p.toString();
|
|
18
|
+
const createRpcRequest = Reflect.get(target, methodName, receiver);
|
|
19
|
+
const newRequest = createRpcRequest ? createRpcRequest(...rawParams) : { methodName, params: rawParams };
|
|
20
|
+
return createPendingRpcRequest(rpcConfig, newRequest);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function createPendingRpcRequest(rpcConfig, pendingRequest) {
|
|
26
|
+
return {
|
|
27
|
+
async send(options) {
|
|
28
|
+
const { methodName, params, responseTransformer } = pendingRequest;
|
|
29
|
+
const payload = createRpcMessage(methodName, params);
|
|
30
|
+
const response = await rpcConfig.transport({
|
|
31
|
+
payload,
|
|
32
|
+
signal: options?.abortSignal
|
|
33
|
+
});
|
|
34
|
+
return responseTransformer ? responseTransformer(response, methodName) : response;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/rpc-api.ts
|
|
40
|
+
function createRpcApi(config) {
|
|
41
|
+
return new Proxy({}, {
|
|
42
|
+
defineProperty() {
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
deleteProperty() {
|
|
46
|
+
return false;
|
|
47
|
+
},
|
|
48
|
+
get(...args) {
|
|
49
|
+
const [_, p] = args;
|
|
50
|
+
const methodName = p.toString();
|
|
51
|
+
return function(...rawParams) {
|
|
52
|
+
const params = config?.parametersTransformer ? config?.parametersTransformer(rawParams, methodName) : rawParams;
|
|
53
|
+
const responseTransformer = config?.responseTransformer ? config?.responseTransformer : (rawResponse) => rawResponse;
|
|
54
|
+
return {
|
|
55
|
+
methodName,
|
|
56
|
+
params,
|
|
57
|
+
responseTransformer
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { createRpc, createRpcApi };
|
|
65
|
+
//# sourceMappingURL=index.node.mjs.map
|
|
66
|
+
//# sourceMappingURL=index.node.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts"],"names":[],"mappings":";;;AAkCO,SAAS,UACZ,SACgB,EAAA;AAChB,EAAA,OAAO,UAAU,SAAS,CAAA,CAAA;AAC9B,CAAA;AAEA,SAAS,UACL,SACgB,EAAA;AAChB,EAAO,OAAA,IAAI,KAAM,CAAA,SAAA,CAAU,GAAK,EAAA;AAAA,IAC5B,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,GAAA,CAAI,MAAQ,EAAA,CAAA,EAAG,QAAU,EAAA;AACrB,MAAA,OAAO,YAAa,SAAsB,EAAA;AACtC,QAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,QAAA,MAAM,gBAAmB,GAAA,OAAA,CAAQ,GAAI,CAAA,MAAA,EAAQ,YAAY,QAAQ,CAAA,CAAA;AACjE,QAAM,MAAA,UAAA,GAAa,mBACb,gBAAiB,CAAA,GAAG,SAAS,CAC7B,GAAA,EAAE,UAAY,EAAA,MAAA,EAAQ,SAAU,EAAA,CAAA;AACtC,QAAO,OAAA,uBAAA,CAAwB,WAAW,UAAU,CAAA,CAAA;AAAA,OACxD,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL,CAAA;AAEA,SAAS,uBAAA,CACL,WACA,cAC4B,EAAA;AAC5B,EAAO,OAAA;AAAA,IACH,MAAM,KAAK,OAA8C,EAAA;AACrD,MAAA,MAAM,EAAE,UAAA,EAAY,MAAQ,EAAA,mBAAA,EAAwB,GAAA,cAAA,CAAA;AACpD,MAAM,MAAA,OAAA,GAAU,gBAAiB,CAAA,UAAA,EAAY,MAAM,CAAA,CAAA;AACnD,MAAM,MAAA,QAAA,GAAW,MAAM,SAAA,CAAU,SAAgC,CAAA;AAAA,QAC7D,OAAA;AAAA,QACA,QAAQ,OAAS,EAAA,WAAA;AAAA,OACpB,CAAA,CAAA;AACD,MAAA,OAAQ,mBAAsB,GAAA,mBAAA,CAAoB,QAAU,EAAA,UAAU,CAAI,GAAA,QAAA,CAAA;AAAA,KAC9E;AAAA,GACJ,CAAA;AACJ,CAAA;;;ACvDO,SAAS,aAAgD,MAA4C,EAAA;AACxG,EAAO,OAAA,IAAI,KAAM,CAAA,EAA2B,EAAA;AAAA,IACxC,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAiB,GAAA;AACb,MAAO,OAAA,KAAA,CAAA;AAAA,KACX;AAAA,IACA,OACO,IACL,EAAA;AACE,MAAM,MAAA,CAAC,CAAG,EAAA,CAAC,CAAI,GAAA,IAAA,CAAA;AACf,MAAM,MAAA,UAAA,GAAa,EAAE,QAAS,EAAA,CAAA;AAC9B,MAAA,OAAO,YACA,SAG6C,EAAA;AAChD,QAAA,MAAM,SAAS,MAAQ,EAAA,qBAAA,GACjB,QAAQ,qBAAsB,CAAA,SAAA,EAAW,UAAU,CACnD,GAAA,SAAA,CAAA;AACN,QAAA,MAAM,sBAAsB,MAAQ,EAAA,mBAAA,GAC9B,MAAQ,EAAA,mBAAA,GACR,CAAC,WAAyB,KAAA,WAAA,CAAA;AAChC,QAAO,OAAA;AAAA,UACH,UAAA;AAAA,UACA,MAAA;AAAA,UACA,mBAAA;AAAA,SACJ,CAAA;AAAA,OACJ,CAAA;AAAA,KACJ;AAAA,GACH,CAAA,CAAA;AACL","file":"index.node.mjs","sourcesContent":["import {\n Callable,\n createRpcMessage,\n Flatten,\n OverloadImplementations,\n RpcResponse,\n UnionToIntersection,\n} from '@solana/rpc-spec-types';\n\nimport { RpcApi } from './rpc-api';\nimport { PendingRpcRequest, RpcRequest, RpcSendOptions } from './rpc-request';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{\n api: RpcApi<TRpcMethods>;\n transport: TRpcTransport;\n}>;\n\nexport type Rpc<TRpcMethods> = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;\n};\n\ntype PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper<TMethodImplementation> =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>>\n : never;\n\nexport function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return makeProxy(rpcConfig) as Rpc<TRpcMethods>;\n}\n\nfunction makeProxy<TRpcMethods, TRpcTransport extends RpcTransport>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n): Rpc<TRpcMethods> {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const createRpcRequest = Reflect.get(target, methodName, receiver);\n const newRequest = createRpcRequest\n ? createRpcRequest(...rawParams)\n : { methodName, params: rawParams };\n return createPendingRpcRequest(rpcConfig, newRequest);\n };\n },\n }) as Rpc<TRpcMethods>;\n}\n\nfunction createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport, TResponse>(\n rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>,\n pendingRequest: RpcRequest<TResponse>,\n): PendingRpcRequest<TResponse> {\n return {\n async send(options?: RpcSendOptions): Promise<TResponse> {\n const { methodName, params, responseTransformer } = pendingRequest;\n const payload = createRpcMessage(methodName, params);\n const response = await rpcConfig.transport<RpcResponse<unknown>>({\n payload,\n signal: options?.abortSignal,\n });\n return (responseTransformer ? responseTransformer(response, methodName) : response) as TResponse;\n },\n };\n}\n","import { Callable } from '@solana/rpc-spec-types';\n\nimport { RpcRequest } from './rpc-request';\n\nexport type RpcApiConfig = Readonly<{\n parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;\n responseTransformer?: <T>(response: unknown, methodName?: string) => T;\n}>;\n\nexport type RpcApi<TRpcMethods> = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;\n};\n\ntype RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\nexport interface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\nexport function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods> {\n return new Proxy({} as RpcApi<TRpcMethods>, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get<TMethodName extends keyof RpcApi<TRpcMethods>>(\n ...args: Parameters<NonNullable<ProxyHandler<RpcApi<TRpcMethods>>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcRequest<ReturnType<TRpcMethods[TMethodName]>> {\n const params = config?.parametersTransformer\n ? config?.parametersTransformer(rawParams, methodName)\n : rawParams;\n const responseTransformer = config?.responseTransformer\n ? config?.responseTransformer<ReturnType<TRpcMethods[TMethodName]>>\n : (rawResponse: unknown) => rawResponse as ReturnType<TRpcMethods[TMethodName]>;\n return {\n methodName,\n params,\n responseTransformer,\n };\n };\n },\n });\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Callable } from '@solana/rpc-spec-types';
|
|
2
|
+
import { RpcRequest } from './rpc-request';
|
|
3
|
+
export type RpcApiConfig = Readonly<{
|
|
4
|
+
parametersTransformer?: <T extends unknown[]>(params: T, methodName: string) => unknown;
|
|
5
|
+
responseTransformer?: <T>(response: unknown, methodName?: string) => T;
|
|
6
|
+
}>;
|
|
7
|
+
export type RpcApi<TRpcMethods> = {
|
|
8
|
+
[MethodName in keyof TRpcMethods]: RpcReturnTypeMapper<TRpcMethods[MethodName]>;
|
|
9
|
+
};
|
|
10
|
+
type RpcReturnTypeMapper<TRpcMethod> = TRpcMethod extends Callable ? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethod>> : never;
|
|
11
|
+
type RpcApiMethod = (...args: any) => any;
|
|
12
|
+
export interface RpcApiMethods {
|
|
13
|
+
[methodName: string]: RpcApiMethod;
|
|
14
|
+
}
|
|
15
|
+
export declare function createRpcApi<TRpcMethods extends RpcApiMethods>(config?: RpcApiConfig): RpcApi<TRpcMethods>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=rpc-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-api.d.ts","sourceRoot":"","sources":["../../src/rpc-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC;IAChC,qBAAqB,CAAC,EAAE,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC;IACxF,mBAAmB,CAAC,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC;CAC1E,CAAC,CAAC;AAEH,MAAM,MAAM,MAAM,CAAC,WAAW,IAAI;KAC7B,UAAU,IAAI,MAAM,WAAW,GAAG,mBAAmB,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;CAClF,CAAC;AAEF,KAAK,mBAAmB,CAAC,UAAU,IAAI,UAAU,SAAS,QAAQ,GAC5D,CAAC,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,GAC/D,KAAK,CAAC;AAGZ,KAAK,YAAY,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,CAAC;AAC1C,MAAM,WAAW,aAAa;IAC1B,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY,CAAC;CACtC;AAED,wBAAgB,YAAY,CAAC,WAAW,SAAS,aAAa,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,WAAW,CAAC,CAgC1G"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type RpcRequest<TResponse> = {
|
|
2
|
+
methodName: string;
|
|
3
|
+
params: unknown;
|
|
4
|
+
responseTransformer?: (response: unknown, methodName: string) => TResponse;
|
|
5
|
+
};
|
|
6
|
+
export type PendingRpcRequest<TResponse> = {
|
|
7
|
+
send(options?: RpcSendOptions): Promise<TResponse>;
|
|
8
|
+
};
|
|
9
|
+
export type RpcSendOptions = Readonly<{
|
|
10
|
+
abortSignal?: AbortSignal;
|
|
11
|
+
}>;
|
|
12
|
+
//# sourceMappingURL=rpc-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-request.d.ts","sourceRoot":"","sources":["../../src/rpc-request.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,CAAC,SAAS,IAAI;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,mBAAmB,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,SAAS,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,SAAS,IAAI;IACvC,IAAI,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IAClC,WAAW,CAAC,EAAE,WAAW,CAAC;CAC7B,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-transport.d.ts","sourceRoot":"","sources":["../../src/rpc-transport.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG,QAAQ,CAAC;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB,CAAC,CAAC;AAEH,MAAM,WAAW,YAAY;IACzB,CAAC,SAAS,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;CAC/D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Callable, Flatten, OverloadImplementations, UnionToIntersection } from '@solana/rpc-spec-types';
|
|
2
|
+
import { RpcApi } from './rpc-api';
|
|
3
|
+
import { PendingRpcRequest } from './rpc-request';
|
|
4
|
+
import { RpcTransport } from './rpc-transport';
|
|
5
|
+
export type RpcConfig<TRpcMethods, TRpcTransport extends RpcTransport> = Readonly<{
|
|
6
|
+
api: RpcApi<TRpcMethods>;
|
|
7
|
+
transport: TRpcTransport;
|
|
8
|
+
}>;
|
|
9
|
+
export type Rpc<TRpcMethods> = {
|
|
10
|
+
[TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder<OverloadImplementations<TRpcMethods, TMethodName>>;
|
|
11
|
+
};
|
|
12
|
+
type PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<Flatten<{
|
|
13
|
+
[P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper<TMethodImplementations[P]>;
|
|
14
|
+
}>>;
|
|
15
|
+
type PendingRpcRequestReturnTypeMapper<TMethodImplementation> = TMethodImplementation extends Callable ? (...args: Parameters<TMethodImplementation>) => PendingRpcRequest<ReturnType<TMethodImplementation>> : never;
|
|
16
|
+
export declare function createRpc<TRpcMethods, TRpcTransport extends RpcTransport>(rpcConfig: RpcConfig<TRpcMethods, TRpcTransport>): Rpc<TRpcMethods>;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=rpc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,EAER,OAAO,EACP,uBAAuB,EAEvB,mBAAmB,EACtB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAA8B,MAAM,eAAe,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,MAAM,SAAS,CAAC,WAAW,EAAE,aAAa,SAAS,YAAY,IAAI,QAAQ,CAAC;IAC9E,GAAG,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IACzB,SAAS,EAAE,aAAa,CAAC;CAC5B,CAAC,CAAC;AAEH,MAAM,MAAM,GAAG,CAAC,WAAW,IAAI;KAC1B,WAAW,IAAI,MAAM,WAAW,GAAG,wBAAwB,CAAC,uBAAuB,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;CAClH,CAAC;AAEF,KAAK,wBAAwB,CAAC,sBAAsB,IAAI,mBAAmB,CACvE,OAAO,CAAC;KACH,CAAC,IAAI,MAAM,sBAAsB,GAAG,iCAAiC,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC;CACpG,CAAC,CACL,CAAC;AAEF,KAAK,iCAAiC,CAAC,qBAAqB,IAExD,qBAAqB,SAAS,QAAQ,GAChC,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,qBAAqB,CAAC,KAAK,iBAAiB,CAAC,UAAU,CAAC,qBAAqB,CAAC,CAAC,GACpG,KAAK,CAAC;AAEhB,wBAAgB,SAAS,CAAC,WAAW,EAAE,aAAa,SAAS,YAAY,EACrE,SAAS,EAAE,SAAS,CAAC,WAAW,EAAE,aAAa,CAAC,GACjD,GAAG,CAAC,WAAW,CAAC,CAElB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@solana/rpc-spec",
|
|
3
|
+
"version": "2.0.0-canary-20240731173615",
|
|
4
|
+
"description": "A generic implementation of JSON RPCs using proxies",
|
|
5
|
+
"exports": {
|
|
6
|
+
"browser": {
|
|
7
|
+
"import": "./dist/index.browser.mjs",
|
|
8
|
+
"require": "./dist/index.browser.cjs"
|
|
9
|
+
},
|
|
10
|
+
"node": {
|
|
11
|
+
"import": "./dist/index.node.mjs",
|
|
12
|
+
"require": "./dist/index.node.cjs"
|
|
13
|
+
},
|
|
14
|
+
"react-native": "./dist/index.native.mjs",
|
|
15
|
+
"types": "./dist/types/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"browser": {
|
|
18
|
+
"./dist/index.node.cjs": "./dist/index.browser.cjs",
|
|
19
|
+
"./dist/index.node.mjs": "./dist/index.browser.mjs"
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/index.node.cjs",
|
|
22
|
+
"module": "./dist/index.node.mjs",
|
|
23
|
+
"react-native": "./dist/index.native.mjs",
|
|
24
|
+
"types": "./dist/types/index.d.ts",
|
|
25
|
+
"type": "commonjs",
|
|
26
|
+
"files": [
|
|
27
|
+
"./dist/"
|
|
28
|
+
],
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"keywords": [
|
|
31
|
+
"blockchain",
|
|
32
|
+
"solana",
|
|
33
|
+
"web3"
|
|
34
|
+
],
|
|
35
|
+
"author": "Solana Labs Maintainers <maintainers@solanalabs.com>",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/solana-labs/solana-web3.js"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "http://github.com/solana-labs/solana-web3.js/issues"
|
|
43
|
+
},
|
|
44
|
+
"browserslist": [
|
|
45
|
+
"supports bigint and not dead",
|
|
46
|
+
"maintained node versions"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@solana/rpc-spec-types": "2.0.0-canary-20240731173615"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"typescript": ">=5"
|
|
53
|
+
},
|
|
54
|
+
"bundlewatch": {
|
|
55
|
+
"defaultCompression": "gzip",
|
|
56
|
+
"files": [
|
|
57
|
+
{
|
|
58
|
+
"path": "./dist/index*.js"
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
|
|
64
|
+
"compile:typedefs": "tsc -p ./tsconfig.declarations.json",
|
|
65
|
+
"dev": "jest -c ../../node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
|
|
66
|
+
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || pnpm publish --tag ${PUBLISH_TAG:-canary} --access public --no-git-checks",
|
|
67
|
+
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
|
|
68
|
+
"style:fix": "pnpm eslint --fix src/* && pnpm prettier --log-level warn --ignore-unknown --write ./*",
|
|
69
|
+
"test:lint": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-lint.config.ts --rootDir . --silent",
|
|
70
|
+
"test:prettier": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-prettier.config.ts --rootDir . --silent",
|
|
71
|
+
"test:treeshakability:browser": "agadoo dist/index.browser.mjs",
|
|
72
|
+
"test:treeshakability:native": "agadoo dist/index.native.mjs",
|
|
73
|
+
"test:treeshakability:node": "agadoo dist/index.node.mjs",
|
|
74
|
+
"test:typecheck": "tsc --noEmit",
|
|
75
|
+
"test:unit:browser": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent",
|
|
76
|
+
"test:unit:node": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent"
|
|
77
|
+
}
|
|
78
|
+
}
|