-
-
Notifications
You must be signed in to change notification settings - Fork 2
Added docs on how to use with Nitro (v3) #208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,45 @@ app.get('/', async event => { | |
| createServer(toNodeListener(app)).listen(3000) | ||
| ``` | ||
|
|
||
| ## Usage with Nitro | ||
|
|
||
| For usage with Nitro you need to create a plugin, e.g. `plugins/i18n.ts`: | ||
|
|
||
| ```ts | ||
| import { definePlugin } from 'nitro'; | ||
| import { defineIntlifyMiddleware } from '@intlify/h3'; | ||
| import { i18nConfig } from '~/i18n/i18n.config'; | ||
|
|
||
| export default definePlugin((nitroApp) => { | ||
| const { onRequest, onResponse } = defineIntlifyMiddleware({ | ||
| locale: (event) => event.context.locale, // Grab locale from event context, always set by middleware | ||
| // You config, | ||
| }); | ||
|
|
||
| nitroApp.hooks.hook('request', onRequest); | ||
| nitroApp.hooks.hook('response', onResponse); | ||
| }); | ||
| ``` | ||
|
|
||
| In this example we detect the locale in a middleware so it is available in every event context, `middleware/detect-locale.ts`: | ||
|
|
||
| ```ts | ||
| import { defineHandler, getValidatedQuery } from 'nitro/h3'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: No, Nitro v3 does not define or export defineHandler and getValidatedQuery from a "nitro/h3" submodule. Nitro v3 exports defineHandler directly from the main "nitro" package for better type inference with H3Event, as shown in official v3 documentation. H3 utilities like getValidatedQuery (from h3 package) are not re-exported by Nitro but can be imported directly from "h3". The package.json exports "./h3" as a minimal proxy (20B file), but this does not include defineHandler or getValidatedQuery. Examples consistently import { defineHandler } from "nitro"; and reference H3 separately for advanced utilities. Citations:
Correct the import statement for Nitro v3 compatibility. The imports are incorrect for Nitro v3. Update line 143 to: import { defineHandler } from 'nitro';
import { getValidatedQuery } from 'h3';🤖 Prompt for AI Agents |
||
| import * as z from 'zod'; | ||
|
|
||
| declare module 'nitro/h3' { | ||
| interface H3EventContext { | ||
| locale: Locale; | ||
| } | ||
| } | ||
|
Comment on lines
+146
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing type definition for The 🔧 Proposed fix to define the Locale type import { defineHandler, getValidatedQuery } from 'nitro/h3';
import * as z from 'zod';
+type Locale = 'en' | 'fr' | 'de' | 'es' | 'nl';
+
declare module 'nitro/h3' {
interface H3EventContext {
locale: Locale;
}
}🤖 Prompt for AI Agents |
||
|
|
||
| export default defineHandler(async (event) => { | ||
| const { locale } = await getValidatedQuery(event, z.object({ locale: z.enum(['en', 'fr', 'de', 'es', 'nl']).optional().default('en') })); | ||
| event.context.locale = locale; | ||
| }); | ||
| ``` | ||
|
|
||
|
|
||
| ## 🛠️ Custom locale detection | ||
|
|
||
| You can detect locale with your custom logic from current `H3Event`. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in comment.
The comment contains a typo: "You config" should be "Your config".
✏️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents