fix(nitro): type inference for api routes returning promise (#1483)

This commit is contained in:
ziga 2021-10-27 10:19:53 +02:00 committed by GitHub
parent 3bc984d0c0
commit 245aa6b68f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -70,12 +70,13 @@ async function writeTypes (nitroContext: NitroContext) {
if (typeof mw.handle !== 'string') { continue }
const relativePath = relative(nitroContext._nuxt.buildDir, mw.handle).replace(/\.[a-z]+$/, '')
routeTypes[mw.route] = routeTypes[mw.route] || []
routeTypes[mw.route].push(`ReturnType<typeof import('${relativePath}').default>`)
routeTypes[mw.route].push(`Awaited<ReturnType<typeof import('${relativePath}').default>>`)
}
const lines = [
'// Generated by nitro',
'declare module \'@nuxt/nitro\' {',
' type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T',
' interface InternalApi {',
...Object.entries(routeTypes).map(([path, types]) => ` '${path}': ${types.join(' | ')}`),
' }',