Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Migrate to TypeScript #69

Merged
merged 11 commits into from
Nov 18, 2020
Merged

Migrate to TypeScript #69

merged 11 commits into from
Nov 18, 2020

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented Nov 17, 2020

A lot of other packages depend on json-rpc-engine, and it's high time we migrate it to TypeScript. The implementation is unchanged, but this is breaking because some stuff has moved around:

  • All exports are now named, and re-exported via src/index.ts/dist/index.js
  • asMiddleware is converted to an instance method of JsonRpcEngine
  • Add scaffold middleware tests

@rekmarks rekmarks requested a review from a team as a code owner November 17, 2020 02:38
Comment on lines +8 to +10
"engines": {
"node": ">=10.0.0"
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript complains without it.

Copy link

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to limit the amount of feedback on changes that were unrelated to this work, but I may have left one or two. I tagged feedback according to https://www.notion.so/Code-Review-ebbc0a971d4c4ed6845db1a280ea1910

stack?: unknown;
}

export interface JsonRpcRequest<T> {
Copy link

@brad-decker brad-decker Nov 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?: Some interfaces are declared with generics but only appear in the code being instantiated with <unknown>. This isn't my only question related to this, but I suppose I am wondering what the intention was of exposing this?

Copy link
Member Author

@rekmarks rekmarks Nov 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend for consumers to import these types and e.g. use them in their middlewares and to cast results from engine.handle.

src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Show resolved Hide resolved
src/createScaffoldMiddleware.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Show resolved Hide resolved
@rekmarks
Copy link
Member Author

rekmarks commented Nov 17, 2020

I'm pushing up one more commit to address feedback,

In addition to addressing feedback, I made some other changes:

  • Update eth-rpc-errors so we can type JsonRpcError.stack as a string: 548c8b7
  • Change the callback error types to unknown: ede7181
    • We'd like them to be Error | JsonRpcError | null, but at JsonRpcEngine.handle callsite, we actually have no idea what the error in the callback will be.

src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
src/JsonRpcEngine.ts Outdated Show resolved Hide resolved
_originalError?: unknown;
}

type InternalMiddleware = (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use a Generic, but not worth doing if we will remove these eventually

Copy link
Member Author

@rekmarks rekmarks Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and we don't perform type-sensitive operations on response.result or response.error internally anyway.

});
}

private async _processRequest(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could carry the generic all the way down the chain into these private methods, so long as you don't attempt to narrow this type they'll be treated as unknown but won't cause any heartache for end-users when they use the generics to define the params/response types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might do that in the follow-up when I remove the internal types!

Copy link

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, @rekmarks 🎉

@rekmarks rekmarks merged commit 9b04c79 into master Nov 18, 2020
@rekmarks rekmarks deleted the typescript branch November 18, 2020 20:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants