Repository navigation
fix(wp-native-client): keep WP_Error data on errors; shell request/completeLogin - #123
Merged
Merged
Conversation
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.
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.
Fixes #122
What changed
wp-native-client
ApiErrornow carriesdata. Every transport (FetchTransport,AuthFetchTransport,WpApiFetchTransport) keeps the WP_Errordataobject on ability andrequest()errors. That includesretry_after, a rotatedcontinuation_token, andrevalidation.new ApiError(message, code, status, data = {})keeps the old 3-argument form working.messageandcodeare unchanged: WordPress's values, falling back toRequest failed with status N/request_failed.AuthFetchTransportused to throw a bareUnauthorized/unauthorizedfor 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 anApiErrorfrom a second copy of the package, whereinstanceoffails. That happens in practice: the shell test below sees two module instances under tsx.client.request({ method, path, query, body, headers })was already public onmain(added in 0.2.0). It uses the same bearer, proactive refresh and refresh-and-retry on 401 as ability calls. Its errors now includedatatoo. The README has a new Errors section.wp-native-shell
useAuth().request(req): authenticated plain-REST calls through the auth context (shorthand forclient.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 sameinstallSession()thatlogin,continueLoginandregisternow 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_Errordata. The call is extracted torequestContinueLogin()and tested.tsx(same as api-client) so tests can import source.tsxis added to the shell's devDependencies, and the lockfile only gains that line.Tests
packages/api-client:npm testpasses 31/31. The newtests/error-data.test.mjscovers 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 /isApiErroracross copies.packages/shell:npm testpasses 5/5. The newtests/session.test.mjschecks 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 surfacesretry_after/continuation_token.npm run typecheckandnpm run test:transportpass. Both packages build cleanly (tsc -b --clean && tsc -b).gutenberg.yml, whosepackagegate has been red onmainsince the gutenberg v0.2.0 release. The consumer fixture pinnedwp-native-gutenbergto'0.1.0'. It now reads the version frompackages/gutenberg/package.json, so the release-owned version stays untouched and the fixture stops breaking on each release.gate:packageandgate:fixturepass locally.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:
ApiError.data/isApiError; shell:completeLogin/request/getDeviceId), and make sure they actually publish to npm.package.jsonto 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.