@@ -8,13 +8,20 @@ import {
8
8
SyntaxKind ,
9
9
VariableDeclarationKind ,
10
10
} from "ts-morph"
11
+ import { CompilerOptions } from "../../core/file-loader"
11
12
import { Input } from "../../core/input"
12
13
import {
13
14
IRModelObject ,
14
15
IROperation ,
15
16
IRParameter ,
16
17
} 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"
18
25
import { OpenapiGeneratorConfig } from "../../templates.types"
19
26
import { CompilationUnit , ICompilable } from "../common/compilation-units"
20
27
import { ImportBuilder } from "../common/import-builder"
@@ -315,6 +322,7 @@ ${routes.join("\n\n")}
315
322
export class NextJSAppRouterBuilder implements ICompilable {
316
323
constructor (
317
324
public readonly filename : string ,
325
+ private readonly imports : ImportBuilder ,
318
326
private readonly companionFilename : string ,
319
327
private readonly sourceFile : SourceFile ,
320
328
) { }
@@ -388,7 +396,7 @@ export class NextJSAppRouterBuilder implements ICompilable {
388
396
toCompilationUnit ( ) : CompilationUnit {
389
397
// Reconcile imports - attempt to find an existing one and replace it with correct one
390
398
const imports = this . sourceFile . getImportDeclarations ( )
391
- const from = ImportBuilder . normalizeFrom (
399
+ const from = this . imports . normalizeFrom (
392
400
"./" + this . companionFilename ,
393
401
"./" + this . filename ,
394
402
)
@@ -412,25 +420,39 @@ export class NextJSAppRouterBuilder implements ICompilable {
412
420
413
421
return new CompilationUnit (
414
422
this . filename ,
415
- new ImportBuilder ( ) ,
423
+ this . imports ,
416
424
this . toString ( ) ,
417
425
false ,
418
426
)
419
427
}
420
428
}
421
429
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
+
422
440
export async function generateTypescriptNextJS (
423
441
config : OpenapiGeneratorConfig ,
424
442
) : Promise < void > {
425
443
const input = config . input
426
444
445
+ const importAlias = findImportAlias ( config . dest , config . compilerOptions )
446
+
427
447
const subDirectory = process . env [ "OPENAPI_INTEGRATION_TESTS" ]
428
448
? path . basename ( config . input . loader . entryPointKey )
429
449
: ""
430
450
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 )
434
456
. join ( path . sep )
435
457
436
458
const rootTypeBuilder = await TypeBuilder . fromInput (
@@ -455,7 +477,7 @@ export async function generateTypescriptNextJS(
455
477
routeToNextJSFilepath ( group . name ) ,
456
478
)
457
479
458
- const imports = new ImportBuilder ( { filename} )
480
+ const imports = new ImportBuilder ( { filename} , importAlias )
459
481
460
482
const routerBuilder = new ServerRouterBuilder (
461
483
filename ,
@@ -488,6 +510,7 @@ export async function generateTypescriptNextJS(
488
510
489
511
const nextJSAppRouterBuilder = new NextJSAppRouterBuilder (
490
512
nextJsAppRouterPath ,
513
+ imports ,
491
514
filename ,
492
515
sourceFile ,
493
516
)
@@ -508,7 +531,10 @@ export async function generateTypescriptNextJS(
508
531
const clientOutputPath = [ generatedDirectory , "clients" , "client.ts" ] . join (
509
532
path . sep ,
510
533
)
511
- const clientImportBuilder = new ImportBuilder ( { filename : clientOutputPath } )
534
+ const clientImportBuilder = new ImportBuilder (
535
+ { filename : clientOutputPath } ,
536
+ importAlias ,
537
+ )
512
538
513
539
const fetchClientBuilder = new TypescriptFetchClientBuilder (
514
540
clientOutputPath ,
0 commit comments