Skip to content

Commit

Permalink
TypeScript
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <sora@morimoto.io>
  • Loading branch information
smorimoto committed Oct 4, 2024
1 parent 90bfa2e commit 00295d8
Show file tree
Hide file tree
Showing 58 changed files with 721 additions and 989 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Options:
--js generate js api module with declaration file (default: false)
--module-name-index <number> determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits) (default: 0)
--module-name-first-tag splits routes based on the first tag (default: false)
--disableStrictSSL disabled strict SSL (default: false)
--disableProxy disabled proxy (default: false)
--axios generate axios http client (default: false)
--unwrap-response-data unwrap the data item from the response (default: false)
--disable-throw-on-error Do not throw an error when response.ok is not true (default: false)
Expand Down Expand Up @@ -403,7 +401,7 @@ type PrimitiveTypeStructValue =
| string
| ((
schema: Record<string, any>,
parser: import("./src/schema-parser/schema-parser").SchemaParser,
parser: import("./src/schema-parser/schema-parser").SchemaParser
) => string);

type PrimitiveTypeStruct = Record<
Expand All @@ -416,7 +414,7 @@ type PrimitiveTypeStruct = Record<
>;

declare const primitiveTypeConstructs: (
struct: PrimitiveTypeStruct,
struct: PrimitiveTypeStruct
) => Partial<PrimitiveTypeStruct>;

generateApi({
Expand Down
25 changes: 6 additions & 19 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,17 @@ const generateCommand = defineCommand({
type: "string",
description:
"determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits)",
default: codeGenBaseConfig.moduleNameIndex,
default: codeGenBaseConfig.moduleNameIndex.toString(),
},
"module-name-first-tag": {
type: "boolean",
description: "splits routes based on the first tag",
default: codeGenBaseConfig.moduleNameFirstTag,
},
disableStrictSSL: {
type: "boolean",
description: "disabled strict SSL",
default: codeGenBaseConfig.disableStrictSSL,
},
disableProxy: {
type: "boolean",
description: "disabled proxy",
default: codeGenBaseConfig.disableProxy,
},
axios: {
type: "boolean",
description: "generate axios http client",
default: codeGenBaseConfig.httpClientType === HTTP_CLIENT.AXIOS,
default: false,
},
"unwrap-response-data": {
type: "boolean",
Expand Down Expand Up @@ -223,12 +213,12 @@ const generateCommand = defineCommand({
default: codeGenBaseConfig.defaultResponseType,
},
"type-prefix": {
type: "boolean",
type: "string",
description: "data contract name prefix",
default: codeGenBaseConfig.typePrefix,
},
"type-suffix": {
type: "boolean",
type: "string",
description: "data contract name suffix",
default: codeGenBaseConfig.typeSuffix,
},
Expand Down Expand Up @@ -277,7 +267,6 @@ const generateCommand = defineCommand({
"custom-config": {
type: "string",
description: "custom config: primitiveTypeConstructs, hooks, ... ",
default: "",
},
},
run: async ({ args }) => {
Expand Down Expand Up @@ -306,8 +295,6 @@ const generateCommand = defineCommand({
debug: args.debug,
defaultResponseAsSuccess: args["default-as-success"],
defaultResponseType: args["default-response"],
disableProxy: args.disableProxy,
disableStrictSSL: args.disableStrictSSL,
disableThrowOnError: args["disable-throw-on-error"],
enumNamesAsValues: args["enum-names-as-values"],
extractEnums: args["extract-enums"],
Expand All @@ -325,11 +312,11 @@ const generateCommand = defineCommand({
args["http-client"] || args.axios
? HTTP_CLIENT.AXIOS
: HTTP_CLIENT.FETCH,
input: path.resolve(process.cwd(), args.path),
input: path.resolve(process.cwd(), args.path as string),
modular: args.modular,
moduleNameFirstTag: args["module-name-first-tag"],
moduleNameIndex: +args["module-name-index"] || 0,
output: path.resolve(process.cwd(), args.output || "."),
output: path.resolve(process.cwd(), (args.output as string) || "."),
patch: args.patch,
silent: args.silent,
singleHttpClient: args["single-http-client"],
Expand Down
53 changes: 24 additions & 29 deletions src/code-formatter.js → src/code-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import * as prettier from "prettier";
import * as typescript from "typescript";
import type { CodeGenConfig } from "./configuration.js";

class CodeFormatter {
/**
* @type {CodeGenConfig}
*/
config;
export class CodeFormatter {
config: CodeGenConfig;

constructor({ config }) {
constructor(config: CodeGenConfig) {
this.config = config;
}

removeUnusedImports = (content) => {
removeUnusedImports = (content: string) => {
const tempFileName = "file.ts";

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

if (fileTextChanges?.textChanges.length) {
Expand All @@ -35,11 +34,7 @@ class CodeFormatter {
return content;
};

/**
* @param content
* @returns {Promise<string>}
*/
prettierFormat = async (content) => {
prettierFormat = async (content: string) => {
const formatted = await prettier.format(
content,
this.config.prettierOptions,
Expand All @@ -48,7 +43,7 @@ class CodeFormatter {
};

formatCode = async (
code,
code: string,
{ removeUnusedImports = true, prettierFormat = true } = {},
) => {
if (removeUnusedImports) {
Expand All @@ -62,22 +57,24 @@ class CodeFormatter {
}

class TsLanguageServiceHost {
constructor(fileName, content) {
fileName: string;
content: string;
compilerOptions: typescript.CompilerOptions;

constructor(fileName: string, content: string) {
this.fileName = fileName;
this.content = content;
const tsconfig = typescript.findConfigFile(
fileName,
typescript.sys.fileExists,
);

Object.assign(this, {
fileName,
content,
compilerOptions: tsconfig
? typescript.convertCompilerOptionsFromJson(
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
.compilerOptions,
).options
: typescript.getDefaultCompilerOptions(),
});
this.compilerOptions = tsconfig
? typescript.convertCompilerOptionsFromJson(
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
.compilerOptions,
"",
).options
: typescript.getDefaultCompilerOptions();
}

getNewLine() {
Expand All @@ -101,16 +98,14 @@ class TsLanguageServiceHost {
getScriptSnapshot() {
return typescript.ScriptSnapshot.fromString(this.content);
}
readFile(fileName, encoding) {
readFile(fileName: string, encoding: string) {
if (fileName === this.fileName) {
return this.content;
}

return typescript.sys.readFile(fileName, encoding);
}
fileExists(path) {
fileExists(path: string) {
return typescript.sys.fileExists(path);
}
}

export { CodeFormatter };
Loading

0 comments on commit 00295d8

Please sign in to comment.