Repository navigation
Add x402 header parser, typed subscribe wrapper, and fix dropped error details (3.2.0) - #3
Merged
Merged
Conversation
…r details - src/x402/parseHeader.ts: parses the real WWW-Authenticate: Payment scheme="...", network="...", asset="...", amount="...", pay_to="..." header emitted by the backend's x402-subscribe 402 challenge. - src/x402/index.ts: X402Client, a thin typed wrapper for GET /cloud/x402/subscriptions/plans, POST .../subscribe, and GET .../subscription. On the 402 challenge, subscribe() throws a typed X402PaymentRequiredError with .requirements already parsed, preferring the header and falling back to the JSON body. - src/types/x402.ts: request/response/requirements types. - src/errors.ts: new X402PaymentRequiredError extends AINativeError. - src/client.ts: exposes AINativeClient.x402. Also fixes handleError() to carry the response details (body + headers) through on every status branch -- previously only the 429 branch retained anything beyond the message string, so no typed wrapper could ever recover a structured error body. - src/zerodb/index.ts: unrelated but real fix -- corrects stale /public/projects/* endpoint paths to /projects/*, matching the actual live backend routes. - tests/zerodb.test.ts: updates 35 stale assertions that still expected the old /public/projects/* paths -- these were failing (correctly, pointing at a real pre-existing bug) until this commit's own src/zerodb/index.ts fix landed; this test file was never updated to match. - tests/x402.test.ts: 17 unit tests covering the header parser's happy path, whitespace/forward-compat tolerance, several malformed-header cases, and X402Client's plans/subscribe/getSubscription behavior including the 402-to-typed-error conversion from both header and body. Bumps 3.1.0 -> 3.2.0 (additive -- new exports, one bugfix in handleError's details passthrough, no breaking changes). Ported from AINative-Studio/core (developer-tools/sdks/typescript), where this was originally built and tested -- that repo had drifted into a separate, unpublished location; this repo is the real npm publish source for @ainative/sdk. Refs AINative-Studio/core#9368
urbantech
added a commit
that referenced
this pull request
Dec 3, 2026
Two type-level defects. The request paths are correct, so these failed
silently at the field level rather than with an HTTP error.
1. SearchVectorResponse declared totalCount / searchTimeMs, but the API
sends total_count / search_time_ms. There is no case-conversion layer
anywhere in src/ — searched for camel, snake, humps, decamelize and
transformResponse — so the body is passed through untouched and both
fields read undefined at runtime while the compiler guaranteed
number. Any pagination or timing logic built on them breaks with no
error. `vectors` itself was correct, so results came through and only
the metadata was lost, which is what let this survive review.
2. memory.search was typed Promise<ApiResponse<Memory[]>>, but the
endpoint returns an object rather than a bare array.
The real shapes, from the published OpenAPI schema:
.../database/vectors/search -> VectorSearchResult
{vectors, total_count, search_time_ms}
.../database/memory/search -> MemorySearchResult
{memories, total_count, search_time_ms}
The collection keys DIFFER — vectors vs memories — so no shared
interface or single normalising constant is correct for both. Adds
SearchMemoryResponse rather than reusing the vector one.
Corrects the types rather than adding a runtime transform: introducing
snake-to-camel conversion in the client would silently change the shape
of every response across the whole SDK, including endpoints nobody has
audited. Aligning the declared types with the wire format is the
surgical change, and it makes the compiler tell the truth about what
actually arrives.
Confirmed present in the published artifact: @ainative/[email protected] ships
dist/types/zerodb.d.ts:51-52 and dist/index.d.ts:203-204 with the
camelCase fields and no transform, so installed users are affected.
Tests: 211 -> 216 passing, 7 -> 8 suites, all green.
Verified non-vacuous, each defect independently:
- restoring camelCase fields fails 2 of the 5 new tests
- restoring the Memory[] return type fails 1
- restoring both leaves 5 passing
tsc --noEmit reports 7 pre-existing errors in src/react/ and src/vue/
from missing optional peer deps (react, vue). Identical count and
locations on pristine main; zero in the two files changed here.
Refs #3
Co-authored-by: AINative Admin <[email protected]>
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.
Summary
Ports the x402 payment-protocol helpers from
AINative-Studio/core(developer-tools/sdks/typescript, built and tested as part of core#9342/#9343) into this repo — the real npm publish source for@ainative/sdk(confirmed vianpm view @ainative/sdk repository.url; core's copy had drifted into a separate, unpublished location, confirmed file-by-file identical to this repo plus these changes before opening this PR).src/x402/parseHeader.ts: parses the realWWW-Authenticate: Payment scheme="...", network="...", asset="...", amount="...", pay_to="..."header emitted by the backend's x402-subscribe 402 challenge.src/x402/index.ts:X402Client, a thin typed wrapper forGET /cloud/x402/subscriptions/plans,POST .../subscribe, andGET .../subscription. On the 402 challenge,subscribe()throws a typedX402PaymentRequiredErrorwith.requirementsalready parsed, preferring the header and falling back to the JSON body.src/types/x402.ts: request/response/requirements types.src/errors.ts: newX402PaymentRequiredError extends AINativeError.src/client.ts: exposesAINativeClient.x402. Also fixeshandleError()to carry the responsedetails(body + headers) through on every status branch — previously only the 429 branch retained anything beyond the message string, so no typed wrapper could ever recover a structured error body.src/zerodb/index.ts: unrelated but real fix — corrects stale/public/projects/*endpoint paths to/projects/*, matching the actual live backend routes.tests/zerodb.test.ts: updates 35 stale assertions that still expected the old/public/projects/*paths — confirmed this exact same pre-existing failure reproduces identically oncore's own copy too (fixed there separately, core#9372), so it predates this port.tests/x402.test.ts: 17 new unit tests covering the header parser's happy path, whitespace/forward-compat tolerance, several malformed-header cases, andX402Client's plans/subscribe/getSubscription behavior including the 402-to-typed-error conversion from both header and body.Version bumped 3.1.0 → 3.2.0 (additive only — new exports, one bugfix in
handleError's details passthrough, no breaking changes).Test plan — real output
Full suite:
npx jest— 2 pre-existing, unrelated issues confirmed identical on a clean checkout of this branch's base (main) before any of these changes: a known-flaky 10s-timeout rate-limit test intests/client.test.ts, and dependency-missingreact/vuepeer errors fromtsc --noEmitinsrc/react/*/src/vue/*(optional peer deps, not installed in this package). Neither relates to anything this PR touches.npm run build: succeeds,dist/index.js/dist/index.esm.js/dist/index.d.tsproduced; confirmedX402Client/parseX402PaymentHeader/X402PaymentRequiredErrorare present in the built output.Refs AINative-Studio/core#9368