Support for renaming JS methods when re-exporting#1010
Conversation
@Jimbo4350, the easiest we could do is have a nullable |
|
This PR is stale because it has been open 45 days with no activity. |
|
This PR is stale because it has been open 45 days with no activity. |
30ff6c5 to
3e32d61
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces support for renaming methods when re-exporting them into the JS API surface, enabling globally-unique exported function names while exposing shorter/more ergonomic names in the JS object hierarchy (notably simplifying wallet testnet method names).
Changes:
- Extend
MethodInfowith amethodSimpleNameand include it in the API info JSON payload. - Update the JS wrapper and TypeScript declaration generation to use
simpleNamefor JS-facing property names while still calling WASM exports by the globally-uniquename. - Update JS tests and examples to use the simplified wallet testnet method names.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| cardano-wasm/src-lib/Cardano/Wasm/Api/InfoToTypeScript.hs | Generate TS interface method names from methodSimpleName (JS-facing name). |
| cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs | Add methodSimpleName to method metadata and emit it in JSON; apply renames for testnet wallet methods. |
| cardano-wasm/npm-wrapper/api.test.js | Update test to call renamed testnet wallet restore method. |
| cardano-wasm/lib-wrapper/main.js | Use method.simpleName as the JS property name while still calling exports via method.name. |
| cardano-wasm/lib-wrapper/cardano-api.d.ts | Update declaration file to reflect renamed testnet wallet methods. |
| cardano-wasm/examples/simple-wallet/example.js | Update example usage to call the renamed testnet wallet methods (and correct wallet method location). |
| cardano-wasm/examples/basic/example.js | Update example usage to call the renamed testnet wallet restore method. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| -- | Information about a single method of a virtual object. | ||
| data MethodInfo = MethodInfo | ||
| { methodName :: String | ||
| -- ^ Name of the method in the virtual object of the JS API (which should match the exported function). | ||
| -- ^ Name of the global method name in the API (which should match the exported function and it must be unique globally). | ||
| , methodSimpleName :: String | ||
| -- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName). |
There was a problem hiding this comment.
After adding the new required methodSimpleName field to MethodInfo, at least one MethodInfo { ... } construction in apiInfo wasn't updated: appendCertificateToTx (around Info.hs:319) is missing methodSimpleName, which will cause a compile error (missing field in record construction). Please add it there (likely equal to methodName).
| -- ^ Name of the method in the virtual object of the JS API (which should match the exported function). | ||
| -- ^ Name of the global method name in the API (which should match the exported function and it must be unique globally). | ||
| , methodSimpleName :: String | ||
| -- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName). |
There was a problem hiding this comment.
The doc comment for methodSimpleName says it “does not need to be unique unlike methodName”. While it may not need to be globally unique, it still must be unique within its containing JS object/group; otherwise later assignments in lib-wrapper/main.js will overwrite earlier methods on the target object. Please clarify the comment to avoid implying that collisions are safe.
| -- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName). | |
| -- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than 'methodName'; it does not need to be globally unique, but must be unique within its containing JS object/group to avoid overwriting other methods). |
Changelog
Context
See #1003 (comment). Names of functions exported by the API need to be unique, but we can rename them as we re-export them for the JS API function.
How to trust this PR
The tests passing gives strong guarantee the renaming is done correctly. Other than that, it could be that there is some file that wasn't updated that we missed. And also it would be a matter of checking whether the comments and code design is sensible.
I would look at the commit separately too, because the first one is a refactoring, and the second one is a simple change.
Checklist