Skip to content

Commit d0fd8f1

Browse files
committed
fix: throw formatting errors, generate missing slug/tag
1 parent 35819e1 commit d0fd8f1

File tree

10 files changed

+75
-48
lines changed

10 files changed

+75
-48
lines changed

packages/documentation/src/lib/playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const defaultConfig = {
5050
enableRuntimeResponseValidation: true,
5151
extractInlineSchemas: true,
5252
allowUnusedImports: false,
53-
groupingStrategy: "none",
53+
groupingStrategy: "first-tag",
5454
tsAllowAny: false,
5555
tsCompilerOptions: {exactOptionalPropertyTypes: false},
5656
enableTypedBasePaths: true,

packages/openapi-code-generator/src/core/input.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ export class Input {
169169
const tag = operation.tags[0]
170170

171171
if (!tag) {
172-
throw new Error(
173-
`cannot group operations by first tag as operationId: '${operation.operationId}' has no tags`,
174-
)
172+
return "generated"
175173
}
176174

177175
return tag.toLowerCase()
@@ -185,9 +183,7 @@ export class Input {
185183
).replace(/[{}]*/g, "")
186184

187185
if (!slug) {
188-
throw new Error(
189-
`cannot group operations by first slug as operationId: '${operation.operationId}' has no slugs`,
190-
)
186+
return "generated"
191187
}
192188

193189
return slug.toLowerCase()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export interface IFormatter {
2-
format(filename: string, raw: string): Promise<string>
2+
format(filename: string, raw: string): Promise<{result: string; err?: Error}>
33
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function runTest(
2929
const formatter = await TypescriptFormatterBiome.createNodeFormatter()
3030

3131
return {
32-
output: await formatter.format("unit-test.ts", builder.toString()),
32+
output: (await formatter.format("unit-test.ts", builder.toString())).result,
3333
hasServers: builder.hasServers,
3434
hasOperationServers: builder.hasOperationServers,
3535
builder,

packages/openapi-code-generator/src/typescript/common/schema-builders/schema-builder.test-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function schemaBuilderTestHarness(
6666
const x = ${schema}
6767
`,
6868
)
69-
).trim()
69+
).result.trim()
7070

7171
const schemas = (
7272
await formatter.format(
@@ -76,7 +76,7 @@ export function schemaBuilderTestHarness(
7676
includeHeader: false,
7777
}),
7878
)
79-
).trim()
79+
).result.trim()
8080

8181
return {
8282
code,

packages/openapi-code-generator/src/typescript/common/type-builder.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,10 @@ describe.each(testVersions)(
461461
const x: ${type}
462462
`,
463463
)
464-
).trim(),
464+
).result.trim(),
465465
types: (
466466
await formatter.format("unit-test.types.ts", builder.toString())
467-
).trim(),
467+
).result.trim(),
468468
}
469469
}
470470
},

packages/openapi-code-generator/src/typescript/common/typescript-emitter.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,46 @@ export class TypescriptEmitter {
2525
}
2626
})
2727

28-
logger.time("format output")
28+
logger.time("format and write output")
2929

30-
await Promise.all(
30+
const result = await Promise.allSettled(
3131
outputs.map(async (output) => {
32-
output.data = await this.formatter.format(output.filename, output.data)
32+
const {result, err} = await this.formatter.format(
33+
output.filename,
34+
output.data,
35+
)
36+
output.data = result
37+
await this.writeOutput(output.filename, output.data)
38+
39+
if (err) {
40+
throw err
41+
}
3342
}),
3443
)
3544

36-
logger.time("write output")
45+
const error = result.find((result) => result.status === "rejected")
3746

38-
await Promise.all(
39-
outputs.map(async (output) => {
40-
await this.writeOutput(output.filename, output.data)
41-
}),
42-
)
47+
if (error) {
48+
throw error.reason
49+
}
4350
}
4451

4552
private async writeOutput(filename: string, data: string) {
46-
const outputDirectory = path.dirname(
47-
path.join(this.config.destinationDirectory, filename),
48-
)
49-
const outputFilepath = path.join(this.config.destinationDirectory, filename)
53+
try {
54+
const outputDirectory = path.dirname(
55+
path.join(this.config.destinationDirectory, filename),
56+
)
57+
const outputFilepath = path.join(
58+
this.config.destinationDirectory,
59+
filename,
60+
)
5061

51-
await this.fsAdaptor.mkDir(outputDirectory, true)
52-
await this.fsAdaptor.writeFile(outputFilepath, data)
62+
await this.fsAdaptor.mkDir(outputDirectory, true)
63+
await this.fsAdaptor.writeFile(outputFilepath, data)
5364

54-
logger.info(`Wrote ${outputFilepath}`)
65+
logger.info(`Wrote ${outputFilepath}`)
66+
} catch (err) {
67+
throw new Error(`failed to write ${filename}`, {cause: err})
68+
}
5569
}
5670
}

packages/openapi-code-generator/src/typescript/common/typescript-formatter.biome.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ export class TypescriptFormatterBiome implements IFormatter {
2828
})
2929
}
3030

31-
async format(filename: string, raw: string): Promise<string> {
31+
async format(
32+
filename: string,
33+
raw: string,
34+
): Promise<{result: string; err?: Error}> {
35+
const trimmed = raw
36+
.split("\n")
37+
.map((it) => it.trim())
38+
.join("\n")
39+
3240
try {
33-
const trimmed = raw
34-
.split("\n")
35-
.map((it) => it.trim())
36-
.join("\n")
3741
const formatted = this.biome.formatContent(trimmed, {
3842
filePath: filename,
3943
})
4044

41-
return formatted.content
45+
return {result: formatted.content}
4246
} catch (err) {
4347
logger.error("failed to format", {err})
44-
return raw
48+
return {
49+
result: trimmed,
50+
err: new Error(`failed to format ${filename}`, {cause: err}),
51+
}
4552
}
4653
}
4754

packages/openapi-code-generator/src/typescript/common/typescript-formatter.prettier.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ const plugins = [
99
export class TypescriptFormatterPrettier implements IFormatter {
1010
private constructor() {}
1111

12-
async format(filename: string, raw: string): Promise<string> {
13-
try {
14-
const trimmed = raw
15-
.split("\n")
16-
.map((it) => it.trim())
17-
.join("\n")
12+
async format(
13+
filename: string,
14+
raw: string,
15+
): Promise<{result: string; err?: Error}> {
16+
const trimmed = raw
17+
.split("\n")
18+
.map((it) => it.trim())
19+
.join("\n")
1820

19-
return prettier.format(trimmed, {
21+
try {
22+
const formatted = await prettier.format(trimmed, {
2023
semi: false,
2124
arrowParens: "always",
2225
parser: "typescript",
2326
plugins,
2427
})
28+
29+
return {result: formatted}
2530
} catch (err) {
2631
logger.error("failed to format", {err})
27-
return raw
32+
return {
33+
result: trimmed,
34+
err: new Error(`failed to format ${filename}`, {cause: err}),
35+
}
2836
}
2937
}
3038

packages/openapi-code-generator/src/typescript/typescript-koa/typescript-koa.generator.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ describe("typescript/typescript-koa", () => {
102102
tags: [],
103103
})
104104

105-
return formatter.format(
106-
"unit-test.ts",
107-
serverRouterBuilder.implementationExport("UnitTestImplementation"),
108-
)
105+
return (
106+
await formatter.format(
107+
"unit-test.ts",
108+
serverRouterBuilder.implementationExport("UnitTestImplementation"),
109+
)
110+
).result
109111
}
110112
})
111113
})

0 commit comments

Comments
 (0)