From 9fef0bb6fdcfd410140a8abdd77973664570657a Mon Sep 17 00:00:00 2001 From: Blechlawine Date: Sat, 28 Jan 2023 20:55:39 +0100 Subject: [PATCH] feat: add generation of route input types --- src/build.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/build.ts b/src/build.ts index 096514f3c6..ecd33acb24 100644 --- a/src/build.ts +++ b/src/build.ts @@ -67,6 +67,10 @@ export async function writeTypes(nitro: Nitro) { string, Partial> > = {}; + const routeInputTypes: Record< + string, + Partial> + > = {}; const middleware = [...nitro.scannedHandlers, ...nitro.options.handlers]; @@ -82,14 +86,23 @@ export async function writeTypes(nitro: Nitro) { if (!routeTypes[mw.route]) { routeTypes[mw.route] = {}; } + if (!routeInputTypes[mw.route]) { + routeInputTypes[mw.route] = {}; + } const method = mw.method || "default"; if (!routeTypes[mw.route][method]) { routeTypes[mw.route][method] = []; } + if (!routeInputTypes[mw.route][method]) { + routeInputTypes[mw.route][method] = []; + } routeTypes[mw.route][method].push( `Awaited>` ); + routeInputTypes[mw.route][method].push( + `import('${relativePath}').Input` + ); } let autoImportedTypes: string[] = []; @@ -132,6 +145,17 @@ export async function writeTypes(nitro: Nitro) { ].join("\n") ), " }", + " interface InternalApiInputs {", + ...Object.entries(routeInputTypes).map(([path, methods]) => + [ + ` '${path}': {`, + ...Object.entries(methods).map( + ([method, types]) => ` '${method}': ${types.join(" | ")}` + ), + " }", + ].join("\n") + ), + " }", "}", ...autoImportedTypes, // Makes this a module for augmentation purposes