8000 feat(kit): add forward-compatible nitro types by danielroe · Pull Request #34036 · nuxt/nuxt · GitHub
[go: up one dir, main page]

Skip to content

feat(kit): add forward-compatible nitro types#34036

Merged
danielroe merged 3 commits intomainfrom
feat/kit-nitro
Jan 6, 2026
Merged

feat(kit): add forward-compatible nitro types#34036
danielroe merged 3 commits intomainfrom
feat/kit-nitro

Conversation

@danielroe
Copy link
Member

🔗 Linked issue

📚 Description

this is a forward-compatible update to kit which automatically prefer types for whichever of nitro/nitropack the user has installed

in addition, we now normalise to uppercase handler methods (as they are normalised safely to lower case here and uppercase will be the new default for nitro v3)

@danielroe danielroe requested a review from pi0 January 6, 2026 10:37
@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@pkg-pr-new
Copy link
pkg-pr-new bot commented Jan 6, 2026

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@34036

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@34036

nuxt

npm i https://pkg.pr.new/nuxt@34036

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@34036

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@34036

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@34036

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@34036

commit: d9ef8aa

@coderabbitai
Copy link
Contributor
coderabbitai bot commented Jan 6, 2026

Walkthrough

Adds packages/kit/src/nitro-types.ts, a TypeScript compatibility layer that detects Nitro v2 vs v3 via a conditional type (isNitroV2) which checks for an options key and guards with an additional ___INVALID key check. It exposes unified conditional aliases: Nitro, NitroDevEventHandler, NitroEventHandler, NitroRouteConfig, and NitroRuntimeConfig. Existing files (nitro.ts, pages.ts, runtime-config.ts) now import these types from the new local module instead of external Nitro packages.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(kit): add forward-compatible nitro types' clearly and directly summarizes the main change: introducing forward-compatible Nitro type definitions in the kit package.
Description check ✅ Passed The description is directly related to the changeset, explaining the forward-compatible Nitro type preference system and handler method normalisation to uppercase, which aligns with the actual code changes across multiple files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/kit-nitro

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
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

🤖 Fix all issues with AI Agents
In @packages/kit/src/nitro.ts:
- Around line 28-30: The push uses an unsafe cast "as any" in addServerHandler
when calling normalizeHandlerMethod; remove the cast and instead give
normalizeHandlerMethod an explicit return type that reflects the added method
property (e.g. NitroEventHandler & { method?: Uppercase<HANDLER_METHOD_RE> }) or
define a dedicated EnrichedHandler type and update the type of
useNuxt().options.serverHandlers to accept that enriched handler; then push the
normalized handler without casting and update any call sites or imports
referencing normalizeHandlerMethod, NitroEventHandler, serverHandlers, or
HANDLER_METHOD_RE accordingly.
🧹 Nitpick comments (1)
packages/kit/src/nitro.ts (1)

8-9: Consider renaming the type to avoid naming collision.

Both the regex constant and the type alias share the name HANDLER_METHOD_RE. Whilst TypeScript allows this (values and types occupy separate namespaces), it can be confusing for maintainability and readability.

Consider renaming the type to something like HTTPMethod or HandlerMethod to make the distinction clearer.

🔎 Proposed refactor
 const HANDLER_METHOD_RE = /\.(get|head|patch|post|put|delete|connect|options|trace)(\.\w+)*$/
-type HANDLER_METHOD_RE = 'get' | 'head' | 'patch' | 'post' | 'put' | 'delete' | 'connect' | 'options' | 'trace'
+type HTTPMethod = 'get' | 'head' | 'patch' | 'post' | 'put' | 'delete' | 'connect' | 'options' | 'trace'

Then update line 18:

-    method: method?.toUpperCase() as Uppercase<HANDLER_METHOD_RE> | undefined,
+    method: method?.toUpperCase() as Uppercase<HTTPMethod> | undefined,
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c64ff6 and 8970cdd.

⛔ Files ignored due to path filters (2)
  • packages/kit/package.json is excluded by !**/package.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !pnpm-lock.yaml
📒 Files selected for processing (4)
  • packages/kit/src/nitro-types.ts
  • packages/kit/src/nitro.ts
  • packages/kit/src/pages.ts
  • packages/kit/src/runtime-config.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/kit/src/pages.ts
  • packages/kit/src/runtime-config.ts
  • packages/kit/src/nitro-types.ts
  • packages/kit/src/nitro.ts
**/*.{ts,tsx,js,jsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently

Files:

  • packages/kit/src/pages.ts
  • packages/kit/src/runtime-config.ts
  • packages/kit/src/nitro-types.ts
  • packages/kit/src/nitro.ts
🧠 Learnings (1)
📚 Learning: 2024-12-12T12:36:34.871Z
Learnt from: huang-julien
Repo: nuxt/nuxt PR: 29366
File: packages/nuxt/src/app/components/nuxt-root.vue:16-19
Timestamp: 2024-12-12T12:36:34.871Z
Learning: In `packages/nuxt/src/app/components/nuxt-root.vue`, when optimizing bundle size by conditionally importing components based on route metadata, prefer using inline conditional imports like:

```js
const IsolatedPage = route?.meta?.isolate ? defineAsyncComponent(() => import('#build/isolated-page.mjs')) : null
```

instead of wrapping the import in a computed property or importing the component unconditionally.

Applied to files:

  • packages/kit/src/pages.ts
🧬 Code graph analysis (1)
packages/kit/src/nitro.ts (1)
packages/kit/src/context.ts (1)
  • useNuxt (30-37)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: code
  • GitHub Check: build
  • GitHub Check: codeql (javascript-typescript)
  • GitHub Check: codeql (actions)
🔇 Additional comments (7)
packages/kit/src/nitro-types.ts (2)

6-10: Conditional type exports look correct.

The conditional type pattern correctly provides a unified interface for Nitro types across v2 and v3. Assuming the version detection mechanism is validated, this approach should provide seamless forward compatibility.


1-4: This forward-compatible type detection pattern is intentional and well-designed for bridging Nitro v2 (nitropack) to v3 (nitro) during the framework transition (planned for Nuxt 5). The conditional type 'options' extends keyof NitroV2.Nitro ? true : false is a compile-time TypeScript check, not runtime code, and correctly resolves during type-checking based on which package's types are available. The 'options' property is a legitimate distinguishing feature between the two versions' APIs. Import-time errors for missing dependencies are the expected and appropriate behaviour. This is not a fragile edge case; it's an intentional forward-compatibility bridge with proper error handling.

Likely an incorrect or invalid review comment.

packages/kit/src/runtime-config.ts (2)

9-9: Import change aligns with centralized type surface.

The import of NitroRuntimeConfig from the local nitro-types module correctly consolidates type dependencies for forward compatibility.


33-33: Type cast for Nitro compatibility is appropriate.

The cast to NitroRuntimeConfig ensures type compatibility with the Nitro instance's updateConfig method. Since runtimeConfig is already typed as Record<string, unknown> (from the function parameter), this cast provides the necessary type refinement without runtime changes.

packages/kit/src/pages.ts (1)

6-6: Import refactoring correctly centralizes type dependencies.

The change from nitropack/types to the local ./nitro-types.ts module aligns with the PR's goal of providing forward-compatible type definitions across Nitro versions.

packages/kit/src/nitro.ts (2)

5-5: Type imports correctly use centralized module.

The import change aligns with the new forward-compatible type surface defined in nitro-types.ts.


14-22: Uppercase normalization aligns with Nitro v3 behaviour.

The change to uppercase HTTP method names correctly prepares for Nitro v3, where uppercase will be the default. The type cast to Uppercase<HANDLER_METHOD_RE> provides proper type safety for the transformation.

@codspeed-hq
Copy link
codspeed-hq bot commented Jan 6, 2026

CodSpeed Performance Report

Merging #34036 will not alter performance

Comparing feat/kit-nitro (d9ef8aa) with main (edaea7b)

Summary

✅ 10 untouched

@socket-security
Copy link

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedpostcss@​8.5.61001008284100
Addedprettier@​3.7.4901009795100

View full report

Copy link
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: 0

🧹 Nitpick comments (1)
packages/kit/src/nitro-types.ts (1)

4-8: Consider adding documentation for the type detection logic.

The ___INVALID key check guards against unresolved type imports, which is clever but relies on TypeScript's internal behaviour. Consider adding a comment explaining this rationale to help future maintainers understand the purpose of this guard.

Additionally, the detection logic assumes only one package is installed. If both nitro and nitropack are present, it will default to v2. If neither is installed, it defaults to v3, which might produce confusing error messages.

📝 Suggested documentation
+// Detect Nitro v2 by checking for the 'options' key in the Nitro type.
+// The ___INVALID check guards against unresolved type imports, ensuring
+// we don't mistakenly treat a failed import as Nitro v2.
 type isNitroV2 = 'options' extends keyof NitroV2.Nitro
   ? '___INVALID' extends keyof NitroV2.Nitro
     ? false
     : true
   : false
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8970cdd and d9ef8aa.

⛔ Files ignored due to path filters (2)
  • packages/kit/package.json is excluded by !**/package.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !pnpm-lock.yaml
📒 Files selected for processing (1)
  • packages/kit/src/nitro-types.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/kit/src/nitro-types.ts
**/*.{ts,tsx,js,jsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently

Files:

  • packages/kit/src/nitro-types.ts
🔇 Additional comments (2)
packages/kit/src/nitro-types.ts (2)

10-14: Clean compatibility layer implementation.

The conditional type exports provide a clean, unified API that automatically adapts to whichever Nitro version is installed. The pattern is consistent and straightforward, making it easy to maintain and extend if additional types need to be exposed in the future.


1-2: Type imports and version detection are correctly implemented.

The imports are properly structured for type-only access, and the versions (nitropack 2.12.9 and nitro 3.0.1-alpha.1) exist with the expected type structures. The ___INVALID key detection is a documented TypeScript pattern used to distinguish resolved from unresolved types, making it a valid mechanism for detecting which version is installed. The options key exists in nitropack v2's Nitro type as expected. No changes required.

@danielroe danielroe merged commit 1c4d220 into main Jan 6, 2026
99 of 101 checks passed
@danielroe danielroe deleted the feat/kit-nitro branch January 6, 2026 12:44
@github-actions github-actions bot mentioned this pull request Jan 6, 2026
@github-actions github-actions bot mentioned this pull request Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0