You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, users might want to generate it with other tools like zod.
In order to support this, we need to update handlers-meta rollup plugin to detect defineRouteMeta and actually evaluate (run) the code necessary in place since by-design this information should be statically extracted into the bundle.
Reacted by Ttou, Vladislav Diachenko, Maximedotair, mola1129, Chad McElligott, Leonard Hertel, Niklas Huthmann, Misael Abanto, Franz Antony Bendezu Isidro , sergio9929 and 36 more
Reacted by Sacha Stafyniak, mola1129, Gabriel Serejo, Ted Klingenberg, Pascal Mathis, Quan Do, csj8520, John Strickler, Damian Koper and Max
Reacted by Matt. , Quan Do, csj8520, John Strickler and Damian Koper
Jiti cannot resolve imports from #nitro-internal-virtual/@sandros94
but #nitro-internal-virtual/* is just an alias, which iirc jiti does support in its configs (tho I never used jiti directly atm). I'm taking a look at its src
EDIT: oh, it is something you already pass via alias: nitro.options.alias 🤔
Hello, Witout the possibility to derive schema from object in the code, this defineRouteMeta function is not as usefull and lead to code duplication. is there any update on this feature ?
Would now be a good time to consider maybe another way to accomplish this?
We could accept ZOD (and other major validators) in definePageMeta instead of JSON Schema directly... maybe via a custom transformer function that runs at build time?
Reacted by arb85, csj8520, lukhaiminh, Stanislav Ilnytskyi, ilnytskyistrix, Gabriel Serejo, John Strickler and Lars Myrup
I have tried to find a solution for this problem here, but I still haven't found it yet. My attempt was to traverse the AST object, and every time it finds an 'Identifier' that has not yet been bound, it fires a hook from inside a callback in astToObject, which lets us know the name of that identifier. My objective with this is to be able to handle the import or variable by name in the object. This is how I made this callback:
--- a/src/build/plugins/route-meta.ts+++ b/src/build/plugins/route-meta.ts@@ -61,7 +61,9 @@ export function routeMeta(nitro: Nitro) {
node.expression.callee.name === "defineRouteMeta" &&
node.expression.arguments.length === 1
) {
- meta = astToObject(node.expression.arguments[0] as any);+ meta = astToObject(node.expression.arguments[0] as any, {+ hook: (name) => // Here comes the transverse+ });
break;
}
}
@@ -79,24 +81,29 @@ export function routeMeta(nitro: Nitro) {
} satisfies Plugin;
}
-function astToObject(node: Expression | Literal): any {+interface AstToObjectSettings { hook: (name: string) => any }++function astToObject(node: Expression | Literal, settings?: Partial<AstToObjectSettings>): any {
switch (node.type) {
case "ObjectExpression": {
const obj: Record<string, any> = {};
for (const prop of node.properties) {
if (prop.type === "Property") {
const key = (prop.key as any).name ?? (prop.key as any).value;
- obj[key] = astToObject(prop.value as any);+ obj[key] = astToObject(prop.value as any, settings);
}
}
return obj;
}
case "ArrayExpression": {
- return node.elements.map((el) => astToObject(el as any)).filter(Boolean);+ return node.elements.map((el) => astToObject(el as any, settings)).filter(Boolean);
}
case "Literal": {
return node.value;
}
+ case "Identifier": {+ return astToObject(settings?.hook?.(node.name), settings)+ }
// No default
}
}
H3 v2 has a meta property available on the handler now. Presumably Nitro v3 could use that instead, letting you supply standard-schema compatible schemas in lieu of JSON schema types, generating the OpenAPI doc on demand for the first request, then caching it if required. Could also be leveraged for type generation?
@pi0 Could you please elaborate why meta information, specifically the openAPI info should be statically extracted?
You mentioned this should be done by design, but IMO this only complicates the implementation needed to support this feature, furthermore I'm not sure if we actually can evaluate all transformation code needed by this usecase.
Couldn't we implement this using runtime behaviour?
In nestjs, class decorators are used to embed swagger property info which is done at compile time (TS Decorators) but at runtime, these info are collected and then fed into the endpoint handler (/swagger for example)
Happy to take this. Before I start: is #3096's build-time eval still the plan for
v3, or would you prefer the runtime read from #4560? I'd lean build-time so route
rules keep working, but that means a nested build and I remember you weren't keen
on that.
Activity
clawbolt commentedon Mar 1, 2025
There is my openapi example project: https://github.com/Dg0230/nitro-openapi-example.
LouisHaftmann commentedon May 6, 2025
I've been playing around with jiti and got the following to work:
Updated internals:
src/build/plugins/handlers-meta.tsUsage:
server/api/hello-world.tsIssues:
The only missing piece is how to handle Nitro's auto imports because they are not available in jiti's context.
LouisHaftmann commentedon May 6, 2025
Got auto imports to work! Now it's only missing
#nitro-internal-virtual/exports:sandros94 commentedon May 12, 2025
Is there a workaround available atm?
@LouisHaftmann what issue are you facing with
#nitro-internal-virtual/?I was looking for generating the api documentation starting from the validation of each route via tools like
@standard-community/standard-jsonor@standard-community/standard-openapiLouisHaftmann commentedon May 12, 2025
Jiti cannot resolve imports from
#nitro-internal-virtual/@sandros94sandros94 commentedon May 12, 2025
but
#nitro-internal-virtual/*is just an alias, which iirc jiti does support in its configs (tho I never used jiti directly atm). I'm taking a look at its srcEDIT: oh, it is something you already pass via
alias: nitro.options.alias🤔LouisHaftmann commentedon May 12, 2025
#nitro-internal-virtual/is also used for rollup virtual files which cannot be easily accessed/resolved by jitiExistentialAlex commentedon Jun 25, 2025
Has there been any progress on this?
BYohann commentedon Jul 5, 2025
Hello, Witout the possibility to derive schema from object in the code, this defineRouteMeta function is not as usefull and lead to code duplication. is there any update on this feature ?
dan-hale commentedon Aug 6, 2025
Would now be a good time to consider maybe another way to accomplish this?
We could accept ZOD (and other major validators) in definePageMeta instead of JSON Schema directly... maybe via a custom transformer function that runs at build time?
lukhaiminh commentedon Aug 20, 2025
I find this really necessary. I’m using Zod and can’t dynamically pass a variable to the schema in defineRouteMeta. Could you please update it?
TutorFx commentedon Oct 18, 2025
I have tried to find a solution for this problem here, but I still haven't found it yet. My attempt was to traverse the AST object, and every time it finds an 'Identifier' that has not yet been bound, it fires a hook from inside a callback in astToObject, which lets us know the name of that identifier. My objective with this is to be able to handle the import or variable by name in the object. This is how I made this callback:
ceigey commentedon Nov 2, 2025
H3 v2 has a
metaproperty available on the handler now. Presumably Nitro v3 could use that instead, letting you supply standard-schema compatible schemas in lieu of JSON schema types, generating the OpenAPI doc on demand for the first request, then caching it if required. Could also be leveraged for type generation?Saeid-Za commentedon Nov 24, 2025
@pi0 Could you please elaborate why meta information, specifically the openAPI info should be statically extracted?
You mentioned this should be done by design, but IMO this only complicates the implementation needed to support this feature, furthermore I'm not sure if we actually can evaluate all transformation code needed by this usecase.
Couldn't we implement this using runtime behaviour?
In nestjs, class decorators are used to embed swagger property info which is done at compile time (TS Decorators) but at runtime, these info are collected and then fed into the endpoint handler (/swagger for example)
InfernalAzazel commentedon May 18, 2026
Has there been any progress on this?
TutorFx commentedon Nov 18, 2026
Happy to take this. Before I start: is #3096's build-time eval still the plan for
v3, or would you prefer the runtime read from #4560? I'd lean build-time so route
rules keep working, but that means a nested build and I remember you weren't keen
on that.