@@ -8,13 +8,20 @@ import {
88 SyntaxKind ,
99 VariableDeclarationKind ,
1010} from "ts-morph"
11+ import { CompilerOptions } from "../../core/file-loader"
1112import { Input } from "../../core/input"
1213import {
1314 IRModelObject ,
1415 IROperation ,
1516 IRParameter ,
1617} from "../../core/openapi-types-normalized"
17- import { HttpMethod , isDefined , isHttpMethod , titleCase } from "../../core/utils"
18+ import {
19+ HttpMethod ,
20+ isDefined ,
21+ isHttpMethod ,
22+ isTruthy ,
23+ titleCase ,
24+ } from "../../core/utils"
1825import { OpenapiGeneratorConfig } from "../../templates.types"
1926import { CompilationUnit , ICompilable } from "../common/compilation-units"
2027import { ImportBuilder } from "../common/import-builder"
@@ -315,6 +322,7 @@ ${routes.join("\n\n")}
315322export class NextJSAppRouterBuilder implements ICompilable {
316323 constructor (
317324 public readonly filename : string ,
325+ private readonly imports : ImportBuilder ,
318326 private readonly companionFilename : string ,
319327 private readonly sourceFile : SourceFile ,
320328 ) { }
@@ -388,7 +396,7 @@ export class NextJSAppRouterBuilder implements ICompilable {
388396 toCompilationUnit ( ) : CompilationUnit {
389397 // Reconcile imports - attempt to find an existing one and replace it with correct one
390398 const imports = this . sourceFile . getImportDeclarations ( )
391- const from = ImportBuilder . normalizeFrom (
399+ const from = this . imports . normalizeFrom (
392400 "./" + this . companionFilename ,
393401 "./" + this . filename ,
394402 )
@@ -412,25 +420,39 @@ export class NextJSAppRouterBuilder implements ICompilable {
412420
413421 return new CompilationUnit (
414422 this . filename ,
415- new ImportBuilder ( ) ,
423+ this . imports ,
416424 this . toString ( ) ,
417425 false ,
418426 )
419427 }
420428}
421429
430+ function findImportAlias ( dest : string , compilerOptions : CompilerOptions ) {
431+ const relative = "./" + path . relative ( process . cwd ( ) , dest ) + "/*"
432+
433+ const alias = Object . entries ( compilerOptions . paths || { } ) . find ( ( [ , paths ] ) =>
434+ paths . includes ( relative ) ,
435+ )
436+
437+ return alias ? alias [ 0 ] . replace ( "*" , "" ) : undefined
438+ }
439+
422440export async function generateTypescriptNextJS (
423441 config : OpenapiGeneratorConfig ,
424442) : Promise < void > {
425443 const input = config . input
426444
445+ const importAlias = findImportAlias ( config . dest , config . compilerOptions )
446+
427447 const subDirectory = process . env [ "OPENAPI_INTEGRATION_TESTS" ]
428448 ? path . basename ( config . input . loader . entryPointKey )
429449 : ""
430450
431- const appDirectory = [ "./app" , subDirectory ] . filter ( isDefined ) . join ( path . sep )
432- const generatedDirectory = [ "./generated" , subDirectory ]
433- . filter ( isDefined )
451+ const appDirectory = [ "." , "app" , subDirectory ]
452+ . filter ( isTruthy )
453+ . join ( path . sep )
454+ const generatedDirectory = [ "." , "generated" , subDirectory ]
455+ . filter ( isTruthy )
434456 . join ( path . sep )
435457
436458 const rootTypeBuilder = await TypeBuilder . fromInput (
@@ -455,7 +477,7 @@ export async function generateTypescriptNextJS(
455477 routeToNextJSFilepath ( group . name ) ,
456478 )
457479
458- const imports = new ImportBuilder ( { filename} )
480+ const imports = new ImportBuilder ( { filename} , importAlias )
459481
460482 const routerBuilder = new ServerRouterBuilder (
461483 filename ,
@@ -488,6 +510,7 @@ export async function generateTypescriptNextJS(
488510
489511 const nextJSAppRouterBuilder = new NextJSAppRouterBuilder (
490512 nextJsAppRouterPath ,
513+ imports ,
491514 filename ,
492515 sourceFile ,
493516 )
@@ -508,7 +531,10 @@ export async function generateTypescriptNextJS(
508531 const clientOutputPath = [ generatedDirectory , "clients" , "client.ts" ] . join (
509532 path . sep ,
510533 )
511- const clientImportBuilder = new ImportBuilder ( { filename : clientOutputPath } )
534+ const clientImportBuilder = new ImportBuilder (
535+ { filename : clientOutputPath } ,
536+ importAlias ,
537+ )
512538
513539 const fetchClientBuilder = new TypescriptFetchClientBuilder (
514540 clientOutputPath ,
0 commit comments