Conversation
`ankiGetNextTime1-4` and `ankiGetDeckName` returned the raw response text instead of a parsed object, unlike the other API methods. The backend already emits valid JSON for these endpoints via `ApiResult.String`, so the special case in `handleRequest` served no purpose. BREAKING CHANGE: these five methods now resolve to an object rather than a JSON string. Cards calling `JSON.parse()` on the result must drop it. API version bumped to 0.0.4.
There was a problem hiding this comment.
Pull request overview
This PR updates the AnkiDroid JavaScript API so ankiGetNextTime1-4 and ankiGetDeckName resolve to parsed JavaScript objects (consistent with other JS API methods) rather than returning raw JSON strings, and bumps the declared JS API version accordingly.
Changes:
- Bump
CURRENT_JS_API_VERSIONfrom0.0.3to0.0.4. - Remove the JS-side special-casing that returned raw
response.text()fornextTime*anddeckName, so responses are parsed viaJSON.parse()like other endpoints.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPIConstants.kt | Bumps the current JS API version to 0.0.4. |
| AnkiDroid/src/main/assets/scripts/js-api.js | Removes endpoint-specific raw string returns and updates the version header comment. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const responseData = await response.text(); | ||
| if (endpoint.includes("nextTime") || endpoint.includes("deckName")) { | ||
| return responseData; | ||
| } | ||
| return JSON.parse(responseData); |
|
The js addons files need to be removed, because new js addons for whole ecosystem is under consideration. |
Follow-up to the 0.0.4 bump in AnkiDroidJsAPIConstants: the JS API contract helper and addon test fixtures still declared 0.0.3.
|
|
Now snackbar call once. |
requireApiVersion() runs on every js api request, so a card declaring an outdated version showed a snackbar for each call it made. The message asks the user to contact the template developer, which most users cannot act on, let alone repeatedly. Track the contracts already reported and show each message once per reviewer session, including the developer contact snackbar shown for unsupported calls. Also keep the pre-0.0.4 raw string response for nextTime*/deckName when the card declares 0.0.3, which is still accepted per MINIMUM_JS_API_VERSION, rather than changing the return type under those cards.
Purpose / Description
Describe the problem or feature and motivation
ankiGetNextTime1-4andankiGetDeckNamereturned the raw response text instead of a parsed object, unlike the other API methods. The backend already emits valid JSON for these endpoints viaApiResult.String.BREAKING CHANGE: these five methods now resolve to an object rather than a JSON string. Cards calling
JSON.parse()on the result must drop it. API version bumped to 0.0.4.Fixes
ankiGetDeckNameapi returns JSON-encoded string instead of a parsed javascript object #21452Approach
How does this change address the problem?
Removed the
endpoint.includes("nextTime") || endpoint.includes("deckName")special case inhandleRequest(js-api.js) so every endpoint goes throughJSON.parse. Nothing else needed changing:AnkiDroidJsAPI.convertToByteArray(apiContract, string)already wraps these values inApiResult.String, whosetoString()builds aJSONObject— the same{"success": …, "value": …}envelope used by every other endpoint. The special case dates back to the sync-to-async conversion (#14564) and had no counterpart on the backend.CURRENT_JS_API_VERSIONbumped to0.0.4(MINIMUM_JS_API_VERSIONleft at0.0.3), and the version comment injs-api.jsupdated to match. Cards still supplying0.0.3keep working and get the "update your API version" snackbar.How Has This Been Tested?
Manually, in the reviewer WebView, using the reproduction from the issue:
Before:
string {"success" : true, "value": "…"}. After:object {success: true, value: "…"}, matchingankiIsDisplayingAnswerand the rest.Learning (optional, can help others)
Describe the research stage
Links to blog posts, patterns, libraries or addons used to solve this problem
Checklist
Please, go through these checks before submitting the PR.