Skip to content

fix(api): type the middleware context, so 2,056 of the API's 2,467 type errors were never real - #704

Merged
thinkclay merged 3 commits into
mainfrom
claude/477-api-env
Sep 2, 2026
Merged

thinkclay merged 3 commits into
mainfrom
claude/477-api-env

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

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.

Measured

deno check over functions/api with functions/api/deno.json, before and after declaring the shape once and using it in all 81 files that build an app:

Found 2467 errors   ->   Found 411 errors
code before after
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 an any type 51 5

What this does not do

It 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, 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 check over all of functions/ against the api config gives 2800 → 2789, which looks like the change does nothing. That run is confounded — trpc/, news-import/ and stripe-webhook/ have their own deno configs, and checking them against api's produces resolution noise that swamps the signal. Scoped to functions/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-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) — the one reached from .from() — is what accepts { count, head }.

So 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 the type says 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

c.set("organization", ...) is normalized: 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 that key today, so it was latent rather than live — but the value now matches its declared type either way.

Closes #477

thinkclay and others added 2 commits September 2, 2026 01:33
…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]>
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
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]>
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


@thinkclay
thinkclay merged commit 92b004d into main Sep 2, 2026
13 checks passed
@thinkclay
thinkclay deleted the claude/477-api-env branch September 2, 2026 06:17
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]>
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.

fix(api): authMiddleware context is untyped, so c.get() is never and test:deno:types cannot pass

1 participant