Skip to content

Commit 0d0f2a9

Browse files
authored
Merge pull request #910 from acacode/ts-migrate
TypeScript
2 parents 90bfa2e + 00295d8 commit 0d0f2a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+721
-989
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ Options:
3939
--js generate js api module with declaration file (default: false)
4040
--module-name-index <number> determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits) (default: 0)
4141
--module-name-first-tag splits routes based on the first tag (default: false)
42-
--disableStrictSSL disabled strict SSL (default: false)
43-
--disableProxy disabled proxy (default: false)
4442
--axios generate axios http client (default: false)
4543
--unwrap-response-data unwrap the data item from the response (default: false)
4644
--disable-throw-on-error Do not throw an error when response.ok is not true (default: false)
@@ -403,7 +401,7 @@ type PrimitiveTypeStructValue =
403401
| string
404402
| ((
405403
schema: Record<string, any>,
406-
parser: import("./src/schema-parser/schema-parser").SchemaParser,
404+
parser: import("./src/schema-parser/schema-parser").SchemaParser
407405
) => string);
408406

409407
type PrimitiveTypeStruct = Record<
@@ -416,7 +414,7 @@ type PrimitiveTypeStruct = Record<
416414
>;
417415

418416
declare const primitiveTypeConstructs: (
419-
struct: PrimitiveTypeStruct,
417+
struct: PrimitiveTypeStruct
420418
) => Partial<PrimitiveTypeStruct>;
421419

422420
generateApi({

index.ts

+6-19
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,17 @@ const generateCommand = defineCommand({
175175
type: "string",
176176
description:
177177
"determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits)",
178-
default: codeGenBaseConfig.moduleNameIndex,
178+
default: codeGenBaseConfig.moduleNameIndex.toString(),
179179
},
180180
"module-name-first-tag": {
181181
type: "boolean",
182182
description: "splits routes based on the first tag",
183183
default: codeGenBaseConfig.moduleNameFirstTag,
184184
},
185-
disableStrictSSL: {
186-
type: "boolean",
187-
description: "disabled strict SSL",
188-
default: codeGenBaseConfig.disableStrictSSL,
189-
},
190-
disableProxy: {
191-
type: "boolean",
192-
description: "disabled proxy",
193-
default: codeGenBaseConfig.disableProxy,
194-
},
195185
axios: {
196186
type: "boolean",
197187
description: "generate axios http client",
198-
default: codeGenBaseConfig.httpClientType === HTTP_CLIENT.AXIOS,
188+
default: false,
199189
},
200190
"unwrap-response-data": {
201191
type: "boolean",
@@ -223,12 +213,12 @@ const generateCommand = defineCommand({
223213
default: codeGenBaseConfig.defaultResponseType,
224214
},
225215
"type-prefix": {
226-
type: "boolean",
216+
type: "string",
227217
description: "data contract name prefix",
228218
default: codeGenBaseConfig.typePrefix,
229219
},
230220
"type-suffix": {
231-
type: "boolean",
221+
type: "string",
232222
description: "data contract name suffix",
233223
default: codeGenBaseConfig.typeSuffix,
234224
},
@@ -277,7 +267,6 @@ const generateCommand = defineCommand({
277267
"custom-config": {
278268
type: "string",
279269
description: "custom config: primitiveTypeConstructs, hooks, ... ",
280-
default: "",
281270
},
282271
},
283272
run: async ({ args }) => {
@@ -306,8 +295,6 @@ const generateCommand = defineCommand({
306295
debug: args.debug,
307296
defaultResponseAsSuccess: args["default-as-success"],
308297
defaultResponseType: args["default-response"],
309-
disableProxy: args.disableProxy,
310-
disableStrictSSL: args.disableStrictSSL,
311298
disableThrowOnError: args["disable-throw-on-error"],
312299
enumNamesAsValues: args["enum-names-as-values"],
313300
extractEnums: args["extract-enums"],
@@ -325,11 +312,11 @@ const generateCommand = defineCommand({
325312
args["http-client"] || args.axios
326313
? HTTP_CLIENT.AXIOS
327314
: HTTP_CLIENT.FETCH,
328-
input: path.resolve(process.cwd(), args.path),
315+
input: path.resolve(process.cwd(), args.path as string),
329316
modular: args.modular,
330317
moduleNameFirstTag: args["module-name-first-tag"],
331318
moduleNameIndex: +args["module-name-index"] || 0,
332-
output: path.resolve(process.cwd(), args.output || "."),
319+
output: path.resolve(process.cwd(), (args.output as string) || "."),
333320
patch: args.patch,
334321
silent: args.silent,
335322
singleHttpClient: args["single-http-client"],

src/code-formatter.js renamed to src/code-formatter.ts

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import * as prettier from "prettier";
22
import * as typescript from "typescript";
3+
import type { CodeGenConfig } from "./configuration.js";
34

4-
class CodeFormatter {
5-
/**
6-
* @type {CodeGenConfig}
7-
*/
8-
config;
5+
export class CodeFormatter {
6+
config: CodeGenConfig;
97

10-
constructor({ config }) {
8+
constructor(config: CodeGenConfig) {
119
this.config = config;
1210
}
1311

14-
removeUnusedImports = (content) => {
12+
removeUnusedImports = (content: string) => {
1513
const tempFileName = "file.ts";
1614

1715
const host = new TsLanguageServiceHost(tempFileName, content);
@@ -20,6 +18,7 @@ class CodeFormatter {
2018
const fileTextChanges = languageService.organizeImports(
2119
{ type: "file", fileName: tempFileName },
2220
{ newLineCharacter: typescript.sys.newLine },
21+
undefined,
2322
)[0];
2423

2524
if (fileTextChanges?.textChanges.length) {
@@ -35,11 +34,7 @@ class CodeFormatter {
3534
return content;
3635
};
3736

38-
/**
39-
* @param content
40-
* @returns {Promise<string>}
41-
*/
42-
prettierFormat = async (content) => {
37+
prettierFormat = async (content: string) => {
4338
const formatted = await prettier.format(
4439
content,
4540
this.config.prettierOptions,
@@ -48,7 +43,7 @@ class CodeFormatter {
4843
};
4944

5045
formatCode = async (
51-
code,
46+
code: string,
5247
{ removeUnusedImports = true, prettierFormat = true } = {},
5348
) => {
5449
if (removeUnusedImports) {
@@ -62,22 +57,24 @@ class CodeFormatter {
6257
}
6358

6459
class TsLanguageServiceHost {
65-
constructor(fileName, content) {
60+
fileName: string;
61+
content: string;
62+
compilerOptions: typescript.CompilerOptions;
63+
64+
constructor(fileName: string, content: string) {
65+
this.fileName = fileName;
66+
this.content = content;
6667
const tsconfig = typescript.findConfigFile(
6768
fileName,
6869
typescript.sys.fileExists,
6970
);
70-
71-
Object.assign(this, {
72-
fileName,
73-
content,
74-
compilerOptions: tsconfig
75-
? typescript.convertCompilerOptionsFromJson(
76-
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
77-
.compilerOptions,
78-
).options
79-
: typescript.getDefaultCompilerOptions(),
80-
});
71+
this.compilerOptions = tsconfig
72+
? typescript.convertCompilerOptionsFromJson(
73+
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
74+
.compilerOptions,
75+
"",
76+
).options
77+
: typescript.getDefaultCompilerOptions();
8178
}
8279

8380
getNewLine() {
@@ -101,16 +98,14 @@ class TsLanguageServiceHost {
10198
getScriptSnapshot() {
10299
return typescript.ScriptSnapshot.fromString(this.content);
103100
}
104-
readFile(fileName, encoding) {
101+
readFile(fileName: string, encoding: string) {
105102
if (fileName === this.fileName) {
106103
return this.content;
107104
}
108105

109106
return typescript.sys.readFile(fileName, encoding);
110107
}
111-
fileExists(path) {
108+
fileExists(path: string) {
112109
return typescript.sys.fileExists(path);
113110
}
114111
}
115-
116-
export { CodeFormatter };

0 commit comments

Comments
 (0)