Skip to content

fix(wp-native-client): keep WP_Error data on errors; shell request/completeLogin - #123

Merged
chubes4 merged 3 commits into
mainfrom
fix/client-error-data-and-request
Nov 17, 2026
Merged

chubes4 merged 3 commits into
mainfrom
fix/client-error-data-and-request

Conversation

@chubes4

@chubes4 chubes4 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Fixes #122

What changed

wp-native-client

  • ApiError now carries data. Every transport (FetchTransport, AuthFetchTransport, WpApiFetchTransport) keeps the WP_Error data object on ability and request() errors. That includes retry_after, a rotated continuation_token, and revalidation. new ApiError(message, code, status, data = {}) keeps the old 3-argument form working. message and code are unchanged: WordPress's values, falling back to Request failed with status N / request_failed.
  • 401 bodies are no longer thrown away. AuthFetchTransport used to throw a bare Unauthorized / unauthorized for any 401. It now keeps the WP_Error, and only falls back to those values when there is no body. Refresh-and-retry is unchanged. If the retried request still fails (e.g. two_factor_invalid), the caller gets that response's code, message and data.
  • isApiError(value) is a type guard that also matches an ApiError from a second copy of the package, where instanceof fails. That happens in practice: the shell test below sees two module instances under tsx.
  • client.request({ method, path, query, body, headers }) was already public on main (added in 0.2.0). It uses the same bearer, proactive refresh and refresh-and-retry on 401 as ability calls. Its errors now include data too. The README has a new Errors section.

wp-native-shell

  • useAuth().request(req): authenticated plain-REST calls through the auth context (shorthand for client.request()).
  • useAuth().completeLogin(success): installs a login success obtained outside the shell, such as a social sign-in route that returns wp-native-auth tokens. It goes through the same installSession() that login, continueLogin and register now share: transport.setTokens → discover() → signed-in state. It rejects challenges and malformed results without storing anything. This is what extrachill-app#122 needs.
  • useAuth().getDeviceId(): the shell's persistent device id. Refresh is device-bound, so external login routes must send it.
  • continueLogin() errors carry the WP_Error data. The call is extracted to requestContinueLogin() and tested.
  • The shell test script now runs under tsx (same as api-client) so tests can import source. tsx is added to the shell's devDependencies, and the lockfile only gains that line.

Tests

  • packages/api-client: npm test passes 31/31. The new tests/error-data.test.mjs covers ability errors over both fetch transports, request() with nested data, a non-auth 401 surviving refresh-and-retry, non-JSON and non-object-data fallbacks, the session_expired path, api-fetch errors, and the 3-arg constructor / isApiError across copies.
  • packages/shell: npm test passes 5/5. The new tests/session.test.mjs checks that tokens land in the shell storage keys and a restarted stack reloads them, that discovery runs with the new bearer, that challenges and malformed results are rejected without writes, and that continue-login sends the shell device id and surfaces retry_after / continuation_token.
  • Root npm run typecheck and npm run test:transport pass. Both packages build cleanly (tsc -b --clean && tsc -b).
  • Gutenberg CI fix: the lockfile change triggers gutenberg.yml, whose package gate has been red on main since the gutenberg v0.2.0 release. The consumer fixture pinned wp-native-gutenberg to '0.1.0'. It now reads the version from packages/gutenberg/package.json, so the release-owned version stays untouched and the fixture stops breaking on each release. gate:package and gate:fixture pass locally.
  • No CI workflow covers these packages yet (Wire managed CI gates for packages/shell and packages/api-client (npm workspace dependency setup) #84). I left fix: emit Node-loadable ESM imports in wp-native-client #72 alone: the shell tests use tsx, so Node-loadable ESM isn't needed here.

What extrachill-app needs to pick this up

The app gets both packages from npm: "wp-native-client": "^0.1.0" and "wp-native-shell": "^0.2.0", locked to 0.1.0 and 0.2.0. Caret on 0.x does not cross minors, so the app will not take any later release on its own.

The npm registry also only has client ≤ 0.1.0 and shell ≤ 0.2.0. The 0.2.x client and 0.2.1–0.3.0 shell releases are tagged but not on npm. After this merges, someone needs to:

  1. Release a client minor and a shell minor through Homeboy (feature: ApiError.data / isApiError; shell: completeLogin / request / getDeviceId), and make sure they actually publish to npm.
  2. Raise the ranges in extrachill-app's package.json to those minors and refresh its lockfile.

Versions and changelogs are release-owned, so this PR doesn't change them. The app changes (Extra-Chill/extrachill-app#122 fix, and PR #121's client adoption) feature-detect these APIs and keep working on the installed 0.1.0 / 0.2.0.

Written directly by Extra Chill Bot (AI agent) for Chris Huber.

ApiError now carries the WP_Error data object (retry_after, rotated
continuation tokens, revalidation hints) from every transport. 401s from
AuthFetchTransport keep their WP_Error body instead of a bare
'Unauthorized'. Adds isApiError() for duplicate package copies.
Login, continued login, register and the new completeLogin() share one
installSession() path so externally obtained tokens (social sign-in) land
in the shell transport. request() exposes the client's authenticated
plain-REST call. continueLogin() errors carry WP_Error data.
… fixture

The fixture pinned wp-native-gutenberg 0.1.0, so the package gate has
failed on main since the v0.2.0 release. Read the version from the
package under test instead.
@chubes4
chubes4 merged commit 82796b7 into main Nov 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp-native-client: keep WP_Error data on ability errors and expose authenticated REST requests

1 participant