-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate / Archive this repo - it's does more harm than good. Prefer fetch
+ JSDoc.
#77
Comments
For reference, GPT says that the type hinting could be achieved like this: // Step 1: Define the type mappings for your RPC methods
type RpcMethodMap = {
foobar: [string, ( string | number), number];
bar: [number, number];
// Add more methods as needed
};
// Step 2: Create a type that maps each method name to its corresponding argument tuple
type RpcMethodArgs<T extends keyof RpcMethodMap> = RpcMethodMap[T];
// Step 3: Define a generic function that enforces the correct argument types based on the method name
class MyRpcApi {
request<T extends keyof RpcMethodMap>(method: T, args: RpcMethodArgs<T>): void {
// Implement the function logic here
console.log(`Method: ${method}, Args: ${args}`);
}
}
// Example usage
const myRpcApi = new MyRpcApi();
myRpcApi.request('foobar', ['a', 'b', 3]); // Correct
myRpcApi.request('bar', [1, 2]); // Correct
// myRpcApi.request('foobar', [1, 2, 3]); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
// myRpcApi.request('bar', ['a', 'b']); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. That could be translated to JSDoc or /**
* @typedef {Object} RpcMethodMap
* @property {[string, ( string | number), number]} foobar
* @property {[number, number]} bar
*/
/**
* @template {keyof RpcMethodMap} T
* @typedef {RpcMethodMap[T]} RpcMethodArgs
*/
class MyRpcApi {
/**
* @template {keyof RpcMethodMap} T
* @param {T} method
* @param {RpcMethodArgs<T>} args
*/
request(method, args) {
// Implement the function logic here
console.log(`Method: ${method}, Args: ${args}`);
}
}
// Example usage
const myRpcApi = new MyRpcApi();
myRpcApi.request('foobar', ['a', 'b', 3]); // Correct
myRpcApi.request('bar', [1, 2]); // Correct
// myRpcApi.request('foobar', [1, 2, 3]); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
// myRpcApi.request('bar', ['a', 'b']); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. /**
* Defines the argument types for each RPC method.
*/
type RpcMethodMap = {
foobar: [string, ( string | number), number];
bar: [number, number];
// Add more methods as needed
};
/**
* Utility type to extract the argument tuple type for a given method name.
*/
type RpcMethodArgs<T extends keyof RpcMethodMap> = RpcMethodMap[T];
declare class MyRpcApi {
/**
* Makes a request to the specified RPC method with the given arguments.
* @param method The name of the RPC method to call.
* @param args The arguments to pass to the RPC method.
*/
request<T extends keyof RpcMethodMap>(method: T, args: RpcMethodArgs<T>): void;
}
export { MyRpcApi }; |
coolaj86
changed the title
Deprecate / Archive this repo - it's does more harm than good. Just use
Deprecate / Archive this repo - it's does more harm than good. Just use Aug 7, 2024
fetch
.fetch
+ JSDoc.
coolaj86
changed the title
Deprecate / Archive this repo - it's does more harm than good. Just use
Deprecate / Archive this repo - it's does more harm than good. Prefer Aug 7, 2024
fetch
+ JSDoc.fetch
+ JSDoc.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After years of submitting bugfixes and refactoring this library a little at a time, I finally found out what it actually does - which is so simple by comparison to how this C++ to JS port (I assume) turned out (I assume by the bitcoin team) that it's difficult to believe.
I'm not even sure if the people who use this regularly know what this does due to all of the complex and abstract metaprogramming obscuring the functionality (otherwise I imagine they wouldn't use it).
It's just a really, really complicated way to do an http call that's actually this simple:
DashRPC, in truth:
DashRPC, as a JS function:
Here's the library reimplemented in just a few lines:
Source: DashTx.js
DashRPC, as a JS lib:
Or if you want more of a library feel with a constructor, some options, and few more niceties:
Adding some flourish
init()
And if you wanted to make it convenient, you could add an
init()
method that loops untilE_IN_WARMUP
disappears:Type Hinting
The argument could be made that this provides some type hinting, but it doesn't even work with tsc or vim or VSCode.
It's done in such a bespoke way, that can't be auto-generated to keep up with the actual Dash RPCs, so it's worse to have it than to not having it at all.
If there were some machine-friendly JSON file for type hints, it could very simply be applied to each argument at the time each request is made:
Alternatively the type hinting could be generated as a build step... but it would result it thousands of extra lines of code (I know because I experimented with it already: https://github.com/dashhive/DashRPC.js/blob/v20.0.0/scripts/generate.js)
The text was updated successfully, but these errors were encountered: