liamcottle / rustplus.js

Unofficial NodeJS library for controlling Smart Switches in the PC game Rust
224 stars 46 forks source link

Add type declarations #51

Closed s8wa2 closed 2 months ago

s8wa2 commented 1 year ago

Submitting this PR as it includes type definitions for this previously non-typed repository (other than basic JSDOC). I'm hoping it will improve future developer experiences by providing detailed information of variables, properties, and methods. Additionally, I'm no expert in making type declarations or TS; it has worked fine in my projects, but please give it a quick review.

olijeffers0n commented 1 year ago

Looks good for those that use TS bindings - Protobuf can always be a pain to work without types!

s8wa2 commented 1 year ago

I have updated the build:types script such that it now recognizes uint64 as type long instead of type number. This can be modified with the --ts_proto_opt=forceLong argument. It can convert to string, bigInt, number, or long. Note that it will only perform the conversion if you use the AppMessage.decode method from proto.js inside of rustplus.js instead of using the root.lookupType method. Also note that Steam64s would be above Number.MAX_SAFE_INTEGER.

TL;DR: You can now import enums such as AppMarkerType.CH47 in JS, fixed decoded uint64 type from number->long.

s8wa2 commented 1 year ago

Also, whatever type --ts_proto_opt=forceLong is set to, it will require that for the arguments. Although strings and numbers could still work, setting the type to long would require that uint64 arguments in a request are of type long also. For example: (assuming steam64 is of type string)

rustplus.sendRequest({promoteToLeader: {steamId: steam64}}) // Type string is not assignable to type Long.
rustplus.sendRequest({promoteToLeader: {steamId: parseInt(steam64)}}) // Type number is not assignable to type Long.
rustplus.sendRequest({promoteToLeader: {steamId: Long.fromString(steam64)}}) // Works, no type error

If you want to avoid conversions, I would recommend setting --ts_proto_opt=forceLong=string and making sure you use the proto.js exported AppMessage.decode method.

s8wa2 commented 2 months ago

Going to close for now. Liam mentioned waiting for other v3 changes before merging. Published my own version on npm until liam wants to merge, so I will need to re-make PR either way.

I'm available on Discord and GitHub, just send me a message if you want it merged.