feat: list custom APIs from registerApi in Object.keys(wx)#234
Merged
Conversation
The service-layer `wx` Proxy had only get/set traps, so APIs registered via MiniApp.registerApi were callable but invisible to Object.keys(wx). Frameworks that build their API surface by enumeration — e.g. Taro's processApis — never generated wrappers for them. Add has/ownKeys/getOwnPropertyDescriptor traps (guarded for non-extensible targets) backed by a name set fed from each platform's container/native layer: web via the service Worker config, native via a globalThis.__diminaRegisteredApis global. The names are snapshotted from whatever is registered before the worker starts — register custom APIs before presentView.
dea3147 to
6d32d3f
Compare
Member
|
建议改成:service 初始化时把这些 API 注册成 api 对象上的真实 own enumerable property。 建议实现类似: 另外一点: native 注入 __diminaRegisteredApis 时现在是手拼 JS 数组字符串,比如 "${name}"。建议改成统一 JSON 序列化,和 Web worker 保持同一策略。 |
Contributor
Author
|
@dos1in 随手把 namespace 的 JSON 序列化也改了 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题:
通过
MiniApp.registerApi注册的自定义 API,在小程序中可以通过wx.<name>()调用,但不会出现在Object.keys(wx)里。Taro 小程序运行时会由
@tarojs/shared的processApis根据Object.keys(wx)一次性生成Taro.xxx包装。由于自定义API 无法被枚举到,Taro 也不会为它们生成对应的Taro.<name>。这导致无法使用Taro.xxx调用注册的自定 API。修复方案:
给 service 层
wx补上ownKeys/getOwnPropertyDescriptor/has拦截器,让已注册的自定义 API 名字能被Object.keys(wx)枚举到