Skip to content

Commit a80c18e

Browse files
committed
Fix more types
Signed-off-by: Sora Morimoto <[email protected]>
1 parent f94d381 commit a80c18e

File tree

12 files changed

+58
-51
lines changed

12 files changed

+58
-51
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"@types/node": "22.7.4",
6969
"@types/swagger2openapi": "7.0.4",
7070
"axios": "1.7.7",
71+
"openapi-types": "12.1.3",
7172
"tsup": "8.3.0",
7273
"vitest": "2.1.2"
7374
},

src/code-gen-process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class CodeGenProcess {
162162
fileName: this.config.fileName,
163163
translateToJavaScript: this.config.toJS,
164164
customTranslator: this.config.customTranslator
165-
? new this.config.customTranslator(this)
165+
? new this.config.customTranslator()
166166
: null,
167167
utils: this.getRenderTemplateData().utils,
168168
};
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#!/usr/bin/env node
2-
1+
import type { GenerateTemplatesParams } from "../../../types/index.js";
32
import { TemplatesGenProcess } from "./templates-gen-process.js";
43

5-
export async function generateTemplates(config) {
4+
export async function generateTemplates(config: GenerateTemplatesParams) {
65
const codeGenProcess = new TemplatesGenProcess(config);
76
return await codeGenProcess.start();
87
}

src/commands/generate-templates/templates-gen-process.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import path from "node:path";
22
import url from "node:url";
33
import { consola } from "consola";
4-
import type { GenerateTemplatesOutput } from "../../../types/index.js";
4+
import type {
5+
GenerateTemplatesOutput,
6+
GenerateTemplatesParams,
7+
} from "../../../types/index.js";
58
import { FileSystem } from "../../util/file-system.js";
69
import { TemplatesGenConfig } from "./configuration.js";
710

@@ -22,7 +25,7 @@ export class TemplatesGenProcess {
2225

2326
importTemplatePrefixes = ["@base", "@modular", "@default"];
2427

25-
constructor(config) {
28+
constructor(config: GenerateTemplatesParams) {
2629
this.config = new TemplatesGenConfig(config);
2730
this.fileSystem = new FileSystem();
2831
}

src/component-type-name-resolver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getRandomInt } from "./util/random.js";
66
export class ComponentTypeNameResolver extends NameResolver {
77
counter = 1;
88
fallbackNameCounter = 1;
9-
countersByVariant = new Map();
9+
countersByVariant = new Map<string, number>();
1010

1111
constructor(config: CodeGenConfig, reservedNames: string[]) {
1212
super(config, reservedNames, (variants) => {
@@ -15,7 +15,8 @@ export class ComponentTypeNameResolver extends NameResolver {
1515
if (!this.countersByVariant.has(randomVariant)) {
1616
this.countersByVariant.set(randomVariant, 0);
1717
}
18-
const variantCounter = this.countersByVariant.get(randomVariant) + 1;
18+
const variantCounter =
19+
(this.countersByVariant.get(randomVariant) as number) + 1;
1920
this.countersByVariant.set(randomVariant, variantCounter);
2021
const dirtyResolvedName = `${randomVariant}${variantCounter}`;
2122
consola.debug(

src/configuration.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as cosmiconfig from "cosmiconfig";
22
import lodash from "lodash";
3+
import type { OpenAPI } from "openapi-types";
34
import * as typescript from "typescript";
45
import type {
56
ExtractingOptions,
@@ -9,6 +10,7 @@ import { ComponentTypeNameResolver } from "./component-type-name-resolver.js";
910
import * as CONSTANTS from "./constants.js";
1011
import type { MonoSchemaParser } from "./schema-parser/mono-schema-parser.js";
1112
import type { SchemaParser } from "./schema-parser/schema-parser.js";
13+
import type { Translator } from "./translators/translator.js";
1214
import { objectAssign } from "./util/object-assign.js";
1315

1416
const TsKeyword = {
@@ -162,7 +164,7 @@ export class CodeGenConfig {
162164
output = "";
163165
url = "";
164166
cleanOutput = false;
165-
spec = null;
167+
spec: OpenAPI.Document | null = null;
166168
fileName = "Api.ts";
167169
authorizationToken: string | undefined;
168170
requestOptions = null;
@@ -215,7 +217,7 @@ export class CodeGenConfig {
215217
emitDecoratorMetadata: true,
216218
skipLibCheck: true,
217219
};
218-
customTranslator;
220+
customTranslator?: new () => Translator;
219221

220222
Ts = {
221223
Keyword: structuredClone(TsKeyword),
@@ -334,10 +336,10 @@ export class CodeGenConfig {
334336
primitiveTypes: Record<
335337
string,
336338
| string
337-
| ((schema: any, parser: SchemaParser) => string)
339+
| ((schema: OpenAPI.Document, parser: SchemaParser) => string)
338340
| ({ $default: string } & Record<
339341
string,
340-
string | ((schema: any, parser: SchemaParser) => string)
342+
string | ((schema: OpenAPI.Document, parser: SchemaParser) => string)
341343
>)
342344
> = {
343345
integer: () => this.Ts.Keyword.Number,
@@ -419,11 +421,7 @@ export class CodeGenConfig {
419421
this.Ts.Keyword.Boolean,
420422
];
421423
this.jsEmptyTypes = [this.Ts.Keyword.Null, this.Ts.Keyword.Undefined];
422-
this.componentTypeNameResolver = new ComponentTypeNameResolver(
423-
this,
424-
null,
425-
[],
426-
);
424+
this.componentTypeNameResolver = new ComponentTypeNameResolver(this, []);
427425
}
428426

429427
update = (update: Partial<GenerateApiConfiguration["config"]>) => {

src/schema-walker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import lodash from "lodash";
2+
import type { OpenAPI } from "openapi-types";
23
import type { CodeGenConfig } from "./configuration.js";
34
import type { SwaggerSchemaResolver } from "./swagger-schema-resolver.js";
45

@@ -7,8 +8,8 @@ import type { SwaggerSchemaResolver } from "./swagger-schema-resolver.js";
78
export class SchemaWalker {
89
config: CodeGenConfig;
910
swaggerSchemaResolver: SwaggerSchemaResolver;
10-
schemas = new Map<string, Record<string, any>>();
11-
caches = new Map<string, Record<string, any>>();
11+
schemas = new Map<string, OpenAPI.Document>();
12+
caches = new Map<string, OpenAPI.Document>();
1213

1314
constructor(
1415
config: CodeGenConfig,
@@ -18,7 +19,7 @@ export class SchemaWalker {
1819
this.swaggerSchemaResolver = swaggerSchemaResolver;
1920
}
2021

21-
addSchema = (name: string, schema: Record<string, unknown>) => {
22+
addSchema = (name: string, schema: OpenAPI.Document) => {
2223
this.schemas.set(name, structuredClone(schema));
2324
};
2425

src/swagger-schema-resolver.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { consola } from "consola";
22
import * as yaml from "js-yaml";
33
import lodash from "lodash";
4+
import type { OpenAPI, OpenAPIV2 } from "openapi-types";
45
import * as swagger2openapi from "swagger2openapi";
56
import type { CodeGenConfig } from "./configuration.js";
67
import type { FileSystem } from "./util/file-system.js";
@@ -20,7 +21,7 @@ export class SwaggerSchemaResolver {
2021
async create() {
2122
const { spec, patch, input, url, authorizationToken } = this.config;
2223

23-
if (this.config.spec) {
24+
if (spec) {
2425
return await this.convertSwaggerObject(spec, { patch });
2526
}
2627

@@ -35,11 +36,11 @@ export class SwaggerSchemaResolver {
3536
}
3637

3738
convertSwaggerObject(
38-
swaggerSchema: Record<string, unknown>,
39+
swaggerSchema: OpenAPI.Document,
3940
converterOptions: { patch?: boolean },
4041
): Promise<{
41-
usageSchema: Record<string, any>;
42-
originalSchema: Record<string, unknown>;
42+
usageSchema: OpenAPI.Document;
43+
originalSchema: OpenAPI.Document;
4344
}> {
4445
return new Promise((resolve) => {
4546
const result = structuredClone(swaggerSchema);
@@ -51,11 +52,11 @@ export class SwaggerSchemaResolver {
5152
result.info,
5253
);
5354

54-
if (!result.openapi) {
55+
if (!Object.hasOwn(result, "openapi")) {
5556
result.paths = lodash.merge({}, result.paths);
5657

5758
swagger2openapi.convertObj(
58-
result,
59+
result as OpenAPIV2.Document,
5960
{
6061
...converterOptions,
6162
warnOnly: true,
@@ -95,15 +96,15 @@ export class SwaggerSchemaResolver {
9596
async fetchSwaggerSchemaFile(
9697
pathToSwagger: string,
9798
urlToSwagger: string,
98-
authToken: string,
99+
authToken?: string,
99100
) {
100101
if (this.fileSystem.pathIsExist(pathToSwagger)) {
101102
return this.getSwaggerSchemaByPath(pathToSwagger);
102103
}
103104
consola.info(`try to get swagger by URL "${urlToSwagger}"`);
104105
return await this.request.download({
105106
url: urlToSwagger,
106-
authToken,
107+
authToken: authToken,
107108
});
108109
}
109110

src/templates-worker.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,18 @@ export class TemplatesWorker {
171171
.keys(this.config.templatePaths)
172172
.find((key) => path_.startsWith(`@${key}`));
173173

174-
const rawPath = path.resolve(
175-
path_.replace(
176-
`@${foundTemplatePathKey}`,
177-
this.config.templatePaths[foundTemplatePathKey],
178-
),
179-
);
180-
const fixedPath = this.findTemplateWithExt(rawPath);
174+
if (foundTemplatePathKey) {
175+
const rawPath = path.resolve(
176+
path_.replace(
177+
`@${foundTemplatePathKey}`,
178+
lodash.get(this.config.templatePaths, foundTemplatePathKey),
179+
),
180+
);
181+
const fixedPath = this.findTemplateWithExt(rawPath);
181182

182-
if (fixedPath) {
183-
return this.fileSystem.getFileContent(fixedPath);
183+
if (fixedPath) {
184+
return this.fileSystem.getFileContent(fixedPath);
185+
}
184186
}
185187

186188
const customPath =

src/type-name-formatter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,28 @@ export class TypeNameFormatter {
6060
name: string,
6161
options: { type?: FormattingSchemaType },
6262
): string => {
63-
const { type } = options || {};
64-
6563
if (!this.isValidName(name)) {
6664
if (!/^[a-zA-Z_$]/g.test(name)) {
6765
const fixPrefix =
68-
type === "enum-key"
66+
options.type === "enum-key"
6967
? this.config.fixInvalidEnumKeyPrefix
7068
: this.config.fixInvalidTypeNamePrefix;
71-
name = `${fixPrefix} ${name}`;
69+
return `${fixPrefix} ${name}`;
7270
}
7371

7472
// specific replaces for TSOA 3.x
75-
if (name.includes("."))
76-
name = name
73+
if (name.includes(".")) {
74+
return name
7775
.replace(/Exclude_keyof[A-Za-z]+/g, () => "ExcludeKeys")
7876
.replace(/%22~AND~%22/g, "And")
7977
.replace(/%22~OR~%22/g, "Or")
8078
.replace(/(\.?%22)|\./g, "_")
8179
.replace(/__+$/, "");
80+
}
8281

83-
if (name.includes("-")) name = lodash.startCase(name).replace(/ /g, "");
82+
if (name.includes("-")) {
83+
return lodash.startCase(name).replace(/ /g, "");
84+
}
8485
}
8586

8687
return name;

0 commit comments

Comments
 (0)