Skip to content

Commit

Permalink
feat: emit unique symbol names for Implementation / createRouter
Browse files Browse the repository at this point in the history
…functions

- previous names still exported as aliases for backwards compatibility,
  will probably keep these around forever, as sometimes they are more
  convenient

- only occurs when splitting the generated code by tag / slug / etc,
  when outputting a single file the original names are still used for
  now.

relates: #111
  • Loading branch information
mnahkies committed Oct 27, 2024
1 parent d2b8276 commit ca56d3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
9 changes: 7 additions & 2 deletions e2e/src/generated/routes/headers.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions e2e/src/generated/routes/validation.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/openapi-code-generator/src/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Input {
): OperationGroup[] {
switch (strategy) {
case "none": {
return [{name: "generated", operations: this.allOperations()}]
return [{name: "", operations: this.allOperations()}]
}
case "first-tag":
return this.operationsByFirstTag()
Expand Down Expand Up @@ -154,7 +154,7 @@ export class Input {

private operationsByFirstTag(): OperationGroup[] {
return this.groupOperations((operation) => {
const tag = operation.tags[0] ?? "generated"
const tag = operation.tags[0]

if (!tag) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,20 +398,32 @@ export class ServerRouterBuilder implements ICompilable {
}

toString(): string {
const implementationExportName = "Implementation"
const moduleName = titleCase(this.name)

const implementationExportName = `${moduleName}Implementation`
const createRouterExportName = `create${moduleName}Router`

const routes = this.statements
const code = `
${this.operationTypes.flatMap((it) => it.statements).join("\n\n")}
${this.implementationExport(implementationExportName)}
export function createRouter(implementation: ${implementationExportName}): KoaRouter {
export function ${createRouterExportName}(implementation: ${implementationExportName}): KoaRouter {
const router = new KoaRouter()
${routes.join("\n\n")}
return router
}
${
moduleName &&
`
export {${createRouterExportName} as createRouter}
export ${this.implementationMethod === "type" || this.implementationMethod === "interface" ? "type" : ""} {${implementationExportName} as Implementation}
`
}
`
return code
}
Expand Down

0 comments on commit ca56d3c

Please sign in to comment.