Skip to content

Commit fbbbc7e

Browse files
authored
refactor: decompose koa server generator (#316)
split the `typescript-koa` template into smaller, re-usable pieces, with the view to make use of this for future server templates, eg: `typescript-express` and `typescript-nextjs` this should also make it easier to write unit tests for the generation
1 parent 5a9e573 commit fbbbc7e

21 files changed

+835
-636
lines changed

packages/openapi-code-generator/src/templates.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type {OpenapiGenerator} from "./templates.types"
2-
import {generateTypescriptAngular} from "./typescript/typescript-angular/typescript-angular.generator"
3-
import {generateTypescriptAxios} from "./typescript/typescript-axios/typescript-axios.generator"
4-
import {generateTypescriptFetch} from "./typescript/typescript-fetch/typescript-fetch.generator"
5-
import {generateTypescriptKoa} from "./typescript/typescript-koa/typescript-koa.generator"
2+
import {generateTypescriptAngular} from "./typescript/client/typescript-angular/typescript-angular.generator"
3+
import {generateTypescriptAxios} from "./typescript/client/typescript-axios/typescript-axios.generator"
4+
import {generateTypescriptFetch} from "./typescript/client/typescript-fetch/typescript-fetch.generator"
5+
import {generateTypescriptKoa} from "./typescript/server/typescript-koa/typescript-koa.generator"
66

77
export const templates = {
88
"typescript-fetch": {

packages/openapi-code-generator/src/typescript/common/client-builder.ts renamed to packages/openapi-code-generator/src/typescript/client/abstract-client-builder.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type {Input} from "../../core/input"
22
import type {IROperation} from "../../core/openapi-types-normalized"
3+
import {CompilationUnit, type ICompilable} from "../common/compilation-units"
4+
import type {ImportBuilder} from "../common/import-builder"
5+
import type {SchemaBuilder} from "../common/schema-builders/schema-builder"
6+
import type {TypeBuilder} from "../common/type-builder"
7+
import {union} from "../common/type-utils"
38
import {ClientOperationBuilder} from "./client-operation-builder"
49
import {ClientServersBuilder} from "./client-servers-builder"
5-
import {CompilationUnit, type ICompilable} from "./compilation-units"
6-
import type {ImportBuilder} from "./import-builder"
7-
import type {SchemaBuilder} from "./schema-builders/schema-builder"
8-
import type {TypeBuilder} from "./type-builder"
9-
import {quotedStringLiteral, union} from "./type-utils"
1010

11-
export abstract class TypescriptClientBuilder implements ICompilable {
11+
export abstract class AbstractClientBuilder implements ICompilable {
1212
private readonly operations: string[] = []
1313

1414
protected readonly clientServersBuilder: ClientServersBuilder
@@ -18,7 +18,7 @@ export abstract class TypescriptClientBuilder implements ICompilable {
1818
public readonly exportName: string,
1919
protected readonly input: Input,
2020
protected readonly imports: ImportBuilder,
21-
protected readonly models: TypeBuilder,
21+
protected readonly types: TypeBuilder,
2222
protected readonly schemaBuilder: SchemaBuilder,
2323
protected readonly config: {
2424
enableRuntimeResponseValidation: boolean
@@ -43,7 +43,7 @@ export abstract class TypescriptClientBuilder implements ICompilable {
4343
add(operation: IROperation): void {
4444
const builder = new ClientOperationBuilder(
4545
operation,
46-
this.models,
46+
this.types,
4747
this.schemaBuilder,
4848
)
4949
this.clientServersBuilder.addOperation(operation)

packages/openapi-code-generator/src/typescript/common/client-operation-builder.ts renamed to packages/openapi-code-generator/src/typescript/client/client-operation-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import type {
55
MaybeIRModel,
66
} from "../../core/openapi-types-normalized"
77
import {camelCase, isDefined} from "../../core/utils"
8-
import type {SchemaBuilder} from "./schema-builders/schema-builder"
9-
import type {TypeBuilder} from "./type-builder"
8+
import type {SchemaBuilder} from "../common/schema-builders/schema-builder"
9+
import type {TypeBuilder} from "../common/type-builder"
1010
import {
1111
type MethodParameterDefinition,
1212
combineParams,
1313
requestBodyAsParameter,
1414
statusStringToType,
15-
} from "./typescript-common"
15+
} from "../common/typescript-common"
1616

1717
export type ClientOperationResponseSchemas = {
1818
specific: {

packages/openapi-code-generator/src/typescript/common/client-servers-builder.spec.ts renamed to packages/openapi-code-generator/src/typescript/client/client-servers-builder.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {describe, expect, it} from "@jest/globals"
22
import type {IROperation, IRServer} from "../../core/openapi-types-normalized"
3+
import {ImportBuilder} from "../common/import-builder"
4+
import {TypescriptFormatterBiome} from "../common/typescript-formatter.biome"
35
import {ClientServersBuilder} from "./client-servers-builder"
4-
import {ImportBuilder} from "./import-builder"
5-
import {TypescriptFormatterBiome} from "./typescript-formatter.biome"
66

77
type TestResult = {
88
output: string

packages/openapi-code-generator/src/typescript/common/client-servers-builder.ts renamed to packages/openapi-code-generator/src/typescript/client/client-servers-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type {
33
IRServer,
44
IRServerVariable,
55
} from "../../core/openapi-types-normalized"
6-
import {CompilationUnit, type ICompilable} from "./compilation-units"
7-
import type {ImportBuilder} from "./import-builder"
8-
import {quotedStringLiteral, union} from "./type-utils"
6+
import {CompilationUnit, type ICompilable} from "../common/compilation-units"
7+
import type {ImportBuilder} from "../common/import-builder"
8+
import {quotedStringLiteral, union} from "../common/type-utils"
99

1010
export class ClientServersBuilder implements ICompilable {
1111
readonly operations: Pick<IROperation, "operationId" | "servers">[] = []

packages/openapi-code-generator/src/typescript/typescript-angular/angular-module-builder.ts renamed to packages/openapi-code-generator/src/typescript/client/typescript-angular/angular-module-builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {CompilationUnit, type ICompilable} from "../common/compilation-units"
2-
import {ImportBuilder} from "../common/import-builder"
1+
import {CompilationUnit, type ICompilable} from "../../common/compilation-units"
2+
import {ImportBuilder} from "../../common/import-builder"
33

44
export class AngularModuleBuilder implements ICompilable {
55
private readonly tsImports: ImportBuilder

packages/openapi-code-generator/src/typescript/typescript-angular/angular-service-builder.ts renamed to packages/openapi-code-generator/src/typescript/client/typescript-angular/angular-service-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import {TypescriptClientBuilder} from "../common/client-builder"
2-
import type {ClientOperationBuilder} from "../common/client-operation-builder"
3-
import type {ImportBuilder} from "../common/import-builder"
4-
import {union} from "../common/type-utils"
5-
import {buildMethod, routeToTemplateString} from "../common/typescript-common"
6-
7-
export class AngularServiceBuilder extends TypescriptClientBuilder {
1+
import type {ImportBuilder} from "../../common/import-builder"
2+
import {union} from "../../common/type-utils"
3+
import {
4+
buildMethod,
5+
routeToTemplateString,
6+
} from "../../common/typescript-common"
7+
import {AbstractClientBuilder} from "../abstract-client-builder"
8+
import type {ClientOperationBuilder} from "../client-operation-builder"
9+
10+
export class AngularServiceBuilder extends AbstractClientBuilder {
811
protected buildImports(imports: ImportBuilder): void {
912
imports.from("@angular/core").add("Injectable")
1013

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {titleCase} from "../../core/utils"
2-
import type {OpenapiTypescriptGeneratorConfig} from "../../templates.types"
3-
import {ImportBuilder} from "../common/import-builder"
4-
import {schemaBuilderFactory} from "../common/schema-builders/schema-builder"
5-
import {TypeBuilder} from "../common/type-builder"
1+
import {titleCase} from "../../../core/utils"
2+
import type {OpenapiTypescriptGeneratorConfig} from "../../../templates.types"
3+
import {ImportBuilder} from "../../common/import-builder"
4+
import {schemaBuilderFactory} from "../../common/schema-builders/schema-builder"
5+
import {TypeBuilder} from "../../common/type-builder"
66
import {AngularModuleBuilder} from "./angular-module-builder"
77
import {AngularServiceBuilder} from "./angular-service-builder"
88

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import {TypescriptClientBuilder} from "../common/client-builder"
2-
import type {ClientOperationBuilder} from "../common/client-operation-builder"
3-
import type {ImportBuilder} from "../common/import-builder"
4-
import {union} from "../common/type-utils"
5-
import {asyncMethod, routeToTemplateString} from "../common/typescript-common"
6-
7-
export class TypescriptAxiosClientBuilder extends TypescriptClientBuilder {
1+
import type {ImportBuilder} from "../../common/import-builder"
2+
import {union} from "../../common/type-utils"
3+
import {
4+
asyncMethod,
5+
routeToTemplateString,
6+
} from "../../common/typescript-common"
7+
import {AbstractClientBuilder} from "../abstract-client-builder"
8+
import type {ClientOperationBuilder} from "../client-operation-builder"
9+
10+
export class TypescriptAxiosClientBuilder extends AbstractClientBuilder {
811
protected buildImports(imports: ImportBuilder): void {
912
imports
1013
.from("@nahkies/typescript-axios-runtime/main")

packages/openapi-code-generator/src/typescript/typescript-axios/typescript-axios.generator.ts renamed to packages/openapi-code-generator/src/typescript/client/typescript-axios/typescript-axios.generator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {titleCase} from "../../core/utils"
2-
import type {OpenapiTypescriptGeneratorConfig} from "../../templates.types"
3-
import {ImportBuilder} from "../common/import-builder"
4-
import {schemaBuilderFactory} from "../common/schema-builders/schema-builder"
5-
import {TypeBuilder} from "../common/type-builder"
1+
import {titleCase} from "../../../core/utils"
2+
import type {OpenapiTypescriptGeneratorConfig} from "../../../templates.types"
3+
import {ImportBuilder} from "../../common/import-builder"
4+
import {schemaBuilderFactory} from "../../common/schema-builders/schema-builder"
5+
import {TypeBuilder} from "../../common/type-builder"
66
import {TypescriptAxiosClientBuilder} from "./typescript-axios-client-builder"
77

88
export async function generateTypescriptAxios(

0 commit comments

Comments
 (0)