Repository navigation
fix(api): type the middleware context, so 2,056 of the API's 2,467 type errors were never real - #704
Merged
Merged
Conversation
…pe errors were never real
`authMiddleware` stored `supabase`, `user`, `authType` and friends on a plain
untyped `Context`, and no route file parameterized its app — every one
constructed a bare `new OpenAPIHono()` or `new Hono()`. So `c.get("supabase")`
inferred as `never` (or `{}` in a whole-program check) and every property
access on it was an error.
`deno check` over `functions/api`, before and after declaring the shape once
and using it in all 81 files that build an app:
Found 2467 errors -> Found 411 errors
TS2769 No overload matches this call 812 -> 0
TS2339 Property does not exist on type 1091 -> 18
TS18046 X is of type unknown 137 -> 5
TS7006 Parameter implicitly has any type 51 -> 5
This does NOT make `pnpm test:deno:types` pass. 411 errors remain, dominated by
364 TS2345 where a handler's return type does not match the OpenAPI `responses`
map it declares — a real contract gap, and adjacent to the schema work in #660.
What it does is make the gate capable of carrying signal at all: a genuine type
error is no longer one of two thousand.
Two of the surviving errors are real defects the typing exposed, both fixed
here:
**Notification counts were always zero.** `PATCH /v1/notifications/read-all`
and `DELETE /v1/notifications` did
.update({...}).eq(...).select("*", { count: "exact", head: true })
`select()` after a mutation is PostgrestTransformBuilder's returning-clause
overload, which takes columns only — `PostgrestQueryBuilder.select(columns,
options)` is the one that accepts `{ count, head }`. The options object was
silently discarded, `count` came back null, and both endpoints reported
`updated_count: 0` / `deleted_count: 0` on every call. `count` now goes on
`update()` / `delete()`, where it belongs.
**A fire-and-forget error handler that could not fire.** The api_keys
`last_used_at` touch ended in `.then(...).catch(...)`. PostgREST builders
resolve with `{ error }` rather than rejecting, so a failed update never
reached the catch — and the builder's `then()` returns `PromiseLike<void>`,
which has no `.catch` at all, so it was not even well-typed. It now reads the
error off the resolved value.
Also normalizes `c.set("organization", ...)`: the embedded `organizations:
organization_id (...)` relation is typed as an array because supabase-js cannot
infer cardinality from the select string, while PostgREST returns an object.
Nothing reads this key today, so it was latent rather than live.
Closes #477
Co-Authored-By: Claude Opus 5 <[email protected]>
…open #703 added routes/moderation.ts with a bare `new OpenAPIHono()`, so it arrived with the same untyped context this branch removes everywhere else: 27 type errors, 3 after parameterizing it. Co-Authored-By: Claude Opus 5 <[email protected]>
thinkclay
force-pushed
the
claude/477-api-env
branch
from
September 2, 2026 05:43
0786da8 to
d77654f
Compare
Contributor
📊 Performance Benchmark Results |
…since before this branch
`Lint REST API Code` runs `deno fmt --check` in functions/api and is red on
main: 16 of 141 files unformatted there. This branch's single-line import
rewrites — `import { type ApiEnv, authMiddleware } from ...` — exceed the line
width and pushed it to 30.
Formatting all 141 rather than only my 14 makes the check pass rather than fail
less, which is the point of having it.
Verified: `deno fmt --check` clean, `deno lint` clean across 137 files, and
`deno check` unchanged at 414 errors — the same 411 measured before the rebase
plus moderation.ts's 3, which arrived with #703.
Co-Authored-By: Claude Opus 5 <[email protected]>
Contributor
📊 Performance Benchmark Results |
thinkclay
added a commit
that referenced
this pull request
Sep 11, 2026
…as never sent (#743) #617 repaired the select behind GET /v1/profiles/organizations/{slug} — it used to ask for industry, size, location and founded_year and filter on is_public, none of which exist on core.organizations — but the response schema above it was left as written. So the endpoint has been returning 200 while its published OpenAPI contract, and the SDK type generated from it, were wrong in six ways. Checked against a live response rather than reasoned about: field declared actually returned description string object (rich-text document) industry string object { id, name, slug } size string ABSENT location string ABSENT founded_year number ABSENT address - returned, undeclared Nothing broke visibly: @hono/zod-openapi documents responses rather than validating them, and no screen imports useOrganizationProfile. The SDK tests passed throughout because their msw handler hand-wrote the declared shape, so they asserted against a response the API cannot produce (fixed in Scaffald/sdk#26, pointer bumped here). size, location and founded_year are dropped rather than added. No table in any schema has those columns — a search of information_schema.columns matches nothing outside pg_catalog and storage — so keeping them would mean inventing product surface to satisfy a schema that was only ever aspirational. `address` is the real column behind what it called `location`. Verified after the change: every declared field matches the live response by type, and no undeclared field is returned. deno check on profiles.ts is unchanged at 3 errors — all pre-existing members of the TS2345 handler-response family measured in #704, two of them on routes this does not touch. One trap is recorded in the code rather than left for the next reader: supabase-js types the embedded to-one relation as an array because it cannot infer cardinality from the select string, so the static type says `industry: {...}[]` while the wire format is an object. The schema follows the wire format. Closes #482 Co-authored-by: Claude Opus 5 <[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.
authMiddlewarestoredsupabase,user,authTypeand friends on a plain untypedContext, and no route file parameterized its app — every one constructed a barenew OpenAPIHono()ornew Hono(). Soc.get("supabase")inferred asnever(or{}in a whole-program check), and every property access on it was an error.Measured
deno checkoverfunctions/apiwithfunctions/api/deno.json, before and after declaring the shape once and using it in all 81 files that build an app:What this does not do
It does not make
pnpm test:deno:typespass. 411 errors remain, dominated by 364 TS2345 where a handler's return type does not match the OpenAPIresponsesmap it declares — a real contract gap, adjacent to the schema work in #660.What it does is make the gate capable of carrying signal. A genuine type error is no longer one of two thousand.
(A methodological note, since the first number I measured was wrong: running
deno checkover all offunctions/against the api config gives 2800 → 2789, which looks like the change does nothing. That run is confounded —trpc/,news-import/andstripe-webhook/have their own deno configs, and checking them against api's produces resolution noise that swamps the signal. Scoped tofunctions/api, which is the only thing this PR touches, the reduction is 83%.)Two real defects the typing exposed
Both fixed here, because both were only invisible while everything was
any.Notification counts were always zero
PATCH /v1/notifications/read-allandDELETE /v1/notificationsdid:select()after a mutation isPostgrestTransformBuilder's returning-clause overload, which takes columns only.PostgrestQueryBuilder.select(columns, options)— the one reached from.from()— is what accepts{ count, head }.So the options object was silently discarded,
countcame backnull, and both endpoints reportedupdated_count: 0/deleted_count: 0on every call.countnow goes onupdate()/delete(), where the type says it belongs.A fire-and-forget error handler that could not fire
The
api_keyslast_used_attouch ended in.then(...).catch(...). PostgREST builders resolve with{ error }rather than rejecting, so a failed update never reached the catch — and the builder'sthen()returnsPromiseLike<void>, which has no.catchat all, so it was not even well-typed. It now reads the error off the resolved value.Also
c.set("organization", ...)is normalized: the embeddedorganizations:organization_id (...)relation is typed as an array, because supabase-js cannot infer cardinality from the select string, while PostgREST returns an object. Nothing reads that key today, so it was latent rather than live — but the value now matches its declared type either way.Closes #477