Skip to content

Add x402 header parser, typed subscribe wrapper, and fix dropped error details (3.2.0) - #3

Merged
urbantech merged 1 commit into
mainfrom
feature/x402-helpers-9368
Oct 28, 2026
Merged

urbantech merged 1 commit into
mainfrom
feature/x402-helpers-9368

Conversation

@urbantech

Copy link
Copy Markdown
Contributor

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 via npm 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 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 — confirmed this exact same pre-existing failure reproduces identically on core'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, and X402Client'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

npx jest tests/x402.test.ts
Test Suites: 1 passed, 1 total
Tests:       17 passed, 17 total
npx jest tests/zerodb.test.ts
Test Suites: 1 passed, 1 total
Tests:       39 passed, 39 total

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 in tests/client.test.ts, and dependency-missing react/vue peer errors from tsc --noEmit in src/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.ts produced; confirmed X402Client/parseX402PaymentHeader/X402PaymentRequiredError are present in the built output.

Refs AINative-Studio/core#9368

…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
urbantech merged commit 2436834 into main Oct 28, 2026
0 of 9 checks passed
@urbantech
urbantech deleted the feature/x402-helpers-9368 branch October 28, 2026 23:33
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]>
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.

1 participant