Skip to content

fix(fastify): Use runtime keys for Clerk client#8640

Open
wobsoriano wants to merge 2 commits into
mainfrom
rob/fastify-clerk-client-runtime-keys
Open

fix(fastify): Use runtime keys for Clerk client#8640
wobsoriano wants to merge 2 commits into
mainfrom
rob/fastify-clerk-client-runtime-keys

Conversation

@wobsoriano
Copy link
Copy Markdown
Member

@wobsoriano wobsoriano commented May 24, 2026

Description

Cherry picked from #8560, focused on creating a clerkClient from resolved runtime middleware options before calling authenticateRequest.

This also adds a clerk property to the request object similar to how the Hono SDK passes the inner Clerk client from middleware, so devs can access the same runtime-configured client in route handlers via request.clerk. This is useful when keys are provided directly to clerkPlugin() at runtime, since downstream Backend API calls will use the same resolved options as authentication.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 24, 2026

🦋 Changeset detected

Latest commit: d807c2a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/fastify Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented May 24, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
clerk-js-sandbox Skipped Skipped May 24, 2026 11:51pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 24, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR exposes the runtime-created Clerk client on the Fastify request object. The changes add a clerk: ClerkClient property to the FastifyRequest type, modify the plugin to decorate the request with a clerk property, and update the middleware to create a Clerk client from runtime options and assign it to request.clerk. Tests verify the client is created with the correct runtime keys and is available on the request. A changeset documents the fix for session verification when runtime keys are provided.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: exposing the Clerk client initialized with runtime keys on the Fastify request object.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description clearly relates to the changeset by explaining the addition of a clerkClient created from runtime middleware options and a clerk property on the request object for accessing the runtime-configured client.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/fastify/src/types.ts`:
- Around line 4-7: The FastifyRequest augmentation declares clerk as ClerkClient
but the runtime plugin calls instance.decorateRequest('clerk', null); causing a
type mismatch—change the augmented type to allow null (e.g.,
FastifyRequest.clerk: ClerkClient | null) so it matches the decorateRequest
initialization; update the declaration in types.ts referencing
FastifyRequest.clerk and ensure any code using clerk handles the nullable type
or narrows it before use.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 27764212-21cd-44c6-a734-3cc813337102

📥 Commits

Reviewing files that changed from the base of the PR and between fe5a49c and 5e23bc8.

📒 Files selected for processing (6)
  • .changeset/silent-fastify-handshakes.md
  • packages/fastify/src/__tests__/clerkPlugin.test.ts
  • packages/fastify/src/__tests__/withClerkMiddleware.test.ts
  • packages/fastify/src/clerkPlugin.ts
  • packages/fastify/src/types.ts
  • packages/fastify/src/withClerkMiddleware.ts

Comment on lines +4 to +7
declare module 'fastify' {
interface FastifyRequest {
clerk: ClerkClient;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the type/decorator contract mismatch locations
rg -n "interface FastifyRequest|clerk:\\s*ClerkClient|decorateRequest\\('clerk'" packages/fastify/src/types.ts packages/fastify/src/clerkPlugin.ts

Repository: clerk/javascript

Length of output: 259


Fix FastifyRequest.clerk type to match decorateRequest initialization

packages/fastify/src/types.ts declares FastifyRequest.clerk: ClerkClient, but packages/fastify/src/clerkPlugin.ts initializes it via instance.decorateRequest('clerk', null);, creating a compile-time type contract mismatch. Align the runtime value with the augmented type (e.g., make it nullable).

Suggested minimal fix
 declare module 'fastify' {
   interface FastifyRequest {
-    clerk: ClerkClient;
+    clerk: ClerkClient | null;
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
declare module 'fastify' {
interface FastifyRequest {
clerk: ClerkClient;
}
declare module 'fastify' {
interface FastifyRequest {
clerk: ClerkClient | null;
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/fastify/src/types.ts` around lines 4 - 7, The FastifyRequest
augmentation declares clerk as ClerkClient but the runtime plugin calls
instance.decorateRequest('clerk', null); causing a type mismatch—change the
augmented type to allow null (e.g., FastifyRequest.clerk: ClerkClient | null) so
it matches the decorateRequest initialization; update the declaration in
types.ts referencing FastifyRequest.clerk and ensure any code using clerk
handles the nullable type or narrows it before use.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 24, 2026

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@8640

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@8640

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@8640

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@8640

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@8640

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@8640

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@8640

@clerk/express

npm i https://pkg.pr.new/@clerk/express@8640

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@8640

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@8640

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@8640

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@8640

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@8640

@clerk/react

npm i https://pkg.pr.new/@clerk/react@8640

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@8640

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@8640

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@8640

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@8640

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@8640

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@8640

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@8640

commit: d807c2a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant