Skip to content

fix(api-keys): list, create and revoke agree with the route, and a real round trip pins it - #35

Merged
thinkclay merged 1 commit into
mainfrom
fix/api-keys-envelope
Oct 6, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/api-keys-envelope

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Fixes the SDK half of Scaffald/SaaS#1015: /office/api-keys crashes into the error boundary for every office user (TypeError: x.map is not a function at APIKeysList).

Cause

The api function wraps every api-keys response in { data }. #744 and #30 unwrapped update() and getUsage(); list(), create() and revoke() still returned the body verbatim. So list() handed callers {data: [...]} typed as ApiKey[].

revoke() was also wrong in a second way: it POSTed to /v1/api-keys/:id/revoke, which does not exist. Revocation is DELETE /v1/api-keys/:id (a soft delete, is_active = false).

Why the tests did not catch it

  • The MSW mocks returned a bare array and invented the /revoke route, so the unit tests encoded the wrong contract and passed.
  • The integration smoke accepted either shape (Array.isArray(result) ? result : result.data).

Change

  • list(), create(), revoke() unwrap { data }; revoke() uses DELETE /:id.
  • The mocks return what the route returns.
  • The smoke is strict. Its default user belongs to no organization, and the route answers 403 for that, so the smoke asserts the 403 for that case.
  • New api-keys.integration.test.ts: create → list → revoke → list against the real local api as a seeded team_admin.

Evidence

  • The round trip is red on the old resource (create() returned the wrapper: expected undefined to deeply equal Any<String>) and green on this one.
  • SCAFFALD_INTEGRATION=1 pnpm test:integration (api-keys + endpoint smoke): 9/9.
  • vitest run: 778/778. tsc --noEmit is clean and tsup builds.

Found along the way (not fixed here)

The round trip defaults to [email protected], not [email protected]. Every handler in the api route resolves the caller's org and role from one arbitrary team membership (.limit(1).single()). clay is team_admin in four teams and member in three, so revoke refuses him. That will be filed on Scaffald/SaaS and fixed in the route.

The SaaS side (pointer bump plus a capture of the screen working) follows once this merges.

🤖 Generated with Claude Code

…al round trip pins it

The api wraps every api-keys response in { data }. #744 and #30 unwrapped
update() and getUsage(); list(), create() and revoke() were left returning
the body verbatim, so list() handed callers an object typed as an array and
/office/api-keys crashed on `.map` for every office user (Scaffald/SaaS#1015).

- list(), create(), revoke() unwrap { data }.
- revoke() called POST /v1/api-keys/:id/revoke, a route that does not exist.
  Revocation is DELETE /v1/api-keys/:id (a soft delete).
- The MSW mocks returned bare bodies and invented the /revoke route, so the
  unit tests encoded the wrong contract and passed. They now return what the
  route returns.
- The integration smoke accepted either shape; it is strict now. Its default
  user has no organization, so it asserts the route's 403 for that case.
- api-keys.integration.test.ts: create -> list -> revoke -> list against the
  real local api as a seeded team_admin. Red on the old resource (create()
  returned the wrapper: `expected undefined to deeply equal Any<String>`),
  green on this one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thinkclay
thinkclay merged commit a31f576 into main Oct 6, 2026
@thinkclay
thinkclay deleted the fix/api-keys-envelope branch October 6, 2026 11:59
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Oct 6, 2026
…with the route (#1050)

Brings in Scaffald/sdk#35. The api wraps every api-keys response in { data };
list(), create() and revoke() returned the body verbatim (and revoke() called
a route that does not exist), so /office/api-keys crashed into the error
boundary on `x.map is not a function` for every office user (#1015).

Co-authored-by: Claude Opus 5.5 <[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