-
Notifications
You must be signed in to change notification settings - Fork 0
feat: tRPC v11 API layer + X-API-Version response header #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
454318c
Initial plan
Copilot 7d9e8b7
feat: add tRPC v1 + API versioning (X-API-Version header)
Copilot 4882542
refactor: extract buildSyntheticRequest to worker/utils/synthetic-req…
Copilot a4784bc
Update worker/trpc/handler.ts
jaypatrick 41de542
Update worker/trpc/trpc.test.ts
jaypatrick 9da3c72
Update docs/architecture/trpc.md
jaypatrick 6917084
Update worker/trpc/client.ts
jaypatrick dd2852d
chore: migrate tRPC deps to deno add (deno.lock managed)
Copilot ab330a4
fix: update pnpm-lock.yaml for @trpc/client and @trpc/server deps
Copilot 134fcc1
Merge remote-tracking branch 'origin/main' into copilot/add-trpc-v1-a…
Copilot 3b4de55
fix(trpc): apply review comment fixes — ZTA gate, rate-limit, header …
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # tRPC API Layer | ||
|
|
||
| ## Overview | ||
|
|
||
| The adblock-compiler Worker exposes a typed tRPC v11 API alongside the existing REST endpoints. | ||
| All tRPC procedures are available at `/api/trpc/*`. | ||
|
|
||
| ## Versioning | ||
|
|
||
| Procedures are namespaced by version: `v1.*`. The `v1` namespace is stable. | ||
| Breaking changes (removed procedures, changed input shapes) will be introduced under `v2` | ||
| without removing `v1`. | ||
|
|
||
| ## Procedure catalogue | ||
|
|
||
| ### v1.health.get (query, public) | ||
|
|
||
| Returns the same payload as `GET /api/health`. | ||
|
|
||
| ### v1.compile.json (mutation, authenticated) | ||
|
|
||
| Accepts a `CompileRequestSchema` body. Returns the compiled ruleset JSON. | ||
|
|
||
| ### v1.version.get (query, public) | ||
|
|
||
| Returns `{ version, apiVersion }`. | ||
|
|
||
| ## Angular client | ||
|
|
||
| ```typescript | ||
| import { createTrpcClient } from './trpc-client'; | ||
|
|
||
| const client = createTrpcClient(environment.apiBaseUrl, () => authService.getToken()); | ||
| const result = await client.v1.compile.json.mutate({ | ||
| configuration: { | ||
| sources: [ | ||
| { | ||
| url: 'https://example.com/easylist.txt', | ||
| }, | ||
| ], | ||
| }, | ||
| preFetchedContent: {}, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Adding a new procedure | ||
|
|
||
| 1. Create (or extend) a router file in `worker/trpc/routers/v1/`. | ||
| 2. Add it to `worker/trpc/routers/v1/index.ts`. | ||
| 3. No changes to `hono-app.ts` required — the tRPC handler is already mounted. | ||
|
|
||
| ## Mount point | ||
|
|
||
| The tRPC handler is mounted directly on the top-level `app` (not the `routes` sub-app) | ||
| so that the `compress` and `logger` middleware scoped to business routes do not wrap | ||
| tRPC responses. See [`hono-routing.md`](./hono-routing.md#trpc-endpoint) for details. | ||
|
|
||
| ```mermaid | ||
| flowchart TD | ||
| R[Incoming Request] --> M1[Global middleware\ntiming · metadata · auth · CORS] | ||
| M1 --> TRPC{path starts with\n/api/trpc?} | ||
| TRPC -->|yes| TH[handleTrpcRequest\nfetchRequestHandler] | ||
| TH --> PROC{procedure auth?} | ||
| PROC -->|publicProcedure| HANDLER[Execute handler] | ||
| PROC -->|protectedProcedure| AUTH_CHECK{userId non-null?} | ||
| AUTH_CHECK -->|no| UNAUTH[TRPCError UNAUTHORIZED\n+ security telemetry] | ||
| AUTH_CHECK -->|yes| HANDLER | ||
| PROC -->|adminProcedure| ADMIN_CHECK{role === admin?} | ||
| ADMIN_CHECK -->|no| FORB[TRPCError FORBIDDEN\n+ security telemetry] | ||
| ADMIN_CHECK -->|yes| HANDLER | ||
| HANDLER --> RESP[Response] | ||
| TRPC -->|no| REST[REST routes sub-app] | ||
| ``` | ||
|
|
||
| ## ZTA notes | ||
|
|
||
| - `protectedProcedure` enforces non-anonymous auth (returns `UNAUTHORIZED` if | ||
| `authContext.userId` is null). | ||
| - `adminProcedure` additionally enforces `role === 'admin'` (returns `FORBIDDEN`). | ||
| - Auth failures emit `AnalyticsService.trackSecurityEvent()` via the `onError` hook | ||
| in `worker/trpc/handler.ts`. | ||
| - `/api/trpc/*` has its own `app.use('/api/trpc/*', rateLimitMiddleware())` middleware | ||
| applied directly on `app` before `handleTrpcRequest`, enforcing tiered rate limits | ||
| (including `rate_limit` security telemetry) for all tRPC calls. | ||
| - `/api/trpc/*` has its own ZTA access-gate middleware that calls `checkUserApiAccess()` | ||
| (blocks banned/suspended users) and `trackApiUsage()` (billing/analytics) — | ||
| matching the same checks applied to REST routes by `routes.use('*', ...)`. | ||
| - CORS is inherited from the global `cors()` middleware already in place on `app`. | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.