Skip to content

Commit

Permalink
feat: functional api client (#99)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `Templates.ApiClient.generator` -> `Templates.ClassApiClient.generator`
  • Loading branch information
Himenon authored Mar 20, 2023
1 parent 60992f9 commit 10d4857
Show file tree
Hide file tree
Showing 111 changed files with 64,193 additions and 446 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand All @@ -86,8 +86,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand Down Expand Up @@ -304,7 +304,7 @@ It provides parameters extracted from OpenAPI Schema.

#### getAdditionalTypeDefinitionCustomCodeGenerator

This is a type definition file for `Templates.ApiClient`. The reason it is not included in `generateTypeDefinition` is that you may not use the type definition generated by this function depending on your usage.
This is a type definition file for `Templates.FunctionalApiClient`. The reason it is not included in `generateTypeDefinition` is that you may not use the type definition generated by this function depending on your usage.

※ The reason it is not included in `generateTypeDefinition` is that you may not use the type definitions generated by this function depending on your application.

Expand Down
10 changes: 5 additions & 5 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand All @@ -80,8 +80,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand Down Expand Up @@ -298,7 +298,7 @@ OpenAPI Schema から抽出したパラメーターを取得できます。

#### getAdditionalTypeDefinitionCustomCodeGenerator

`Templates.ApiClient`向けの型定義ファイルです。`generateTypeDefinition`に含めていない理由は、用途によってこの関数が生成する型定義を利用しない可能性があるためです。
`Templates.FunctionalApiClient`向けの型定義ファイルです。`generateTypeDefinition`に含めていない理由は、用途によってこの関数が生成する型定義を利用しない可能性があるためです。

※ 将来的に`Templates`の API に移動する予定です。

Expand Down
4 changes: 2 additions & 2 deletions example/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import * as Types from "../lib/types"; // = @himenon/openapi-typescript-code-gen
const main = () => {
const codeGenerator = new CodeGenerator("./spec/openapi.yml");
codeGenerator.validateOpenApiSchema({ logger: { displayLogLines: 1 } });
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand Down
4 changes: 2 additions & 2 deletions example/readme-sample/split-typedef-and-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand Down
4 changes: 2 additions & 2 deletions example/readme-sample/typedef-and-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const main = () => {
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.FunctionalApiClient.Option> = {
generator: Templates.FunctionalApiClient.generator,
option: {},
};

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@
"scripts": {
"build": "pnpm ts ./scripts/build.ts",
"clean": "pnpm ts ./scripts/clean.ts",
"format": "run-s format:code format:code:eslint",
"format:code": "prettier \"**/*.{js,jsx,ts,tsx,json,yml,yaml,md,html}\" --write",
"format:code:eslint": "eslint \"**/*.{ts,tsx}\" --fix",
"lerna:version:up": "lerna version --yes",
"release:github:registry": "pnpm publish --no-git-checks --registry https://npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}",
"release:npm:registry": "pnpm publish --no-git-checks",
"test": "run-p test:depcruise test:jest test:code:gen test:snapshot",
"test:code:gen": "pnpm ts ./scripts/testCodeGen.ts",
"test": "run-p test:depcruise test:jest test:code:gen:* test:snapshot",
"test:code:gen": "run-p test:code:gen:*",
"test:code:gen:class": "pnpm ts ./scripts/testCodeGenWithClass.ts",
"test:code:gen:function": "pnpm ts ./scripts/testCodeGenWithFunctional.ts",
"test:depcruise": "depcruise --validate .dependency-cruiser.js src",
"test:eslint": "eslint \"src/**/*.{ts,tsx}\"",
"test:jest": "jest -c ./jest.config.js --collect-coverage",
Expand Down
53 changes: 30 additions & 23 deletions scripts/testCodeGen.ts → scripts/testCodeGenWithClass.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
import * as Writer from "./writer";
import * as Writer from "./writer/Class";

const main = () => {
Writer.generateTypedefCodeOnly("test/api.test.domain/index.yml", "test/code/typedef-only/api.test.domain.ts", true);
Writer.generateTypedefCodeOnly("test/infer.domain/index.yml", "test/code/typedef-only/infer.domain.ts", false);
Writer.generateTypedefCodeOnly("test/json.properties/index.yml", "test/code/typedef-only/json.properties.ts", false);
Writer.generateTypedefCodeOnly("test/api.test.domain/index.yml", "test/code/class/typedef-only/api.test.domain.ts", true);
Writer.generateTypedefCodeOnly("test/infer.domain/index.yml", "test/code/class/typedef-only/infer.domain.ts", false);
Writer.generateTypedefCodeOnly("test/json.properties/index.yml", "test/code/class/typedef-only/json.properties.ts", false);

Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/api.test.domain.ts", true, { sync: false });
Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/sync-api.test.domain.ts", true, { sync: true });
Writer.generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/template-only/infer.domain.ts", false, { sync: true });
Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/class/template-only/api.test.domain.ts", true, { sync: false });
Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/class/template-only/sync-api.test.domain.ts", true, {
sync: true,
});
Writer.generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/class/template-only/infer.domain.ts", false, { sync: true });

Writer.generateTypedefWithTemplateCode("test/api.v2.domain/index.yml", "test/code/typedef-with-template/api.v2.domain.ts", false, {
Writer.generateTypedefWithTemplateCode("test/api.v2.domain/index.yml", "test/code/class/typedef-with-template/api.v2.domain.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/api.test.domain.ts", true, {
Writer.generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/class/typedef-with-template/api.test.domain.ts", true, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/sync-api.test.domain.ts", true, {
sync: true,
});
Writer.generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/typedef-with-template/infer.domain.ts", false, {
Writer.generateTypedefWithTemplateCode(
"test/api.test.domain/index.yml",
"test/code/class/typedef-with-template/sync-api.test.domain.ts",
true,
{
sync: true,
},
);
Writer.generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/class/typedef-with-template/infer.domain.ts", false, {
sync: false,
});

Writer.generateTypedefWithTemplateCode("test/ref.access/index.yml", "test/code/typedef-with-template/ref-access.ts", false, {
Writer.generateTypedefWithTemplateCode("test/ref.access/index.yml", "test/code/class/typedef-with-template/ref-access.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/remote.ref.access/v1.yml", "test/code/typedef-with-template/remote-ref-access.ts", false, {
Writer.generateTypedefWithTemplateCode("test/remote.ref.access/v1.yml", "test/code/class/typedef-with-template/remote-ref-access.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/kubernetes/openapi-v1.18.5.json", "test/code/kubernetes/client-v1.18.5.ts", false, {
Writer.generateTypedefWithTemplateCode("test/kubernetes/openapi-v1.18.5.json", "test/code/class/kubernetes/client-v1.18.5.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/argo-rollout/index.json", "test/code/argo-rollout/client.ts", false, {
Writer.generateTypedefWithTemplateCode("test/argo-rollout/index.json", "test/code/class/argo-rollout/client.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/unknown.schema.domain/index.yml", "test/code/unknown.schema.domain/client.ts", false, {
Writer.generateTypedefWithTemplateCode("test/unknown.schema.domain/index.yml", "test/code/class/unknown.schema.domain/client.ts", false, {
sync: false,
});

Writer.generateSplitCode("test/api.test.domain/index.yml", "test/code/split");
Writer.generateSplitCode("test/multi-type.test.domain/index.yml", "test/code/mulit-type-test.domain");
Writer.generateSplitCode("test/api.test.domain/index.yml", "test/code/class/split");
Writer.generateSplitCode("test/multi-type.test.domain/index.yml", "test/code/class/mulit-type-test.domain");

Writer.generateParameter("test/api.test.domain/index.yml", "test/code/parameter/api.test.domain.json");
Writer.generateParameter("test/infer.domain/index.yml", "test/code/parameter/infer.domain.json");
Writer.generateParameter("test/api.test.domain/index.yml", "test/code/class/parameter/api.test.domain.json");
Writer.generateParameter("test/infer.domain/index.yml", "test/code/class/parameter/infer.domain.json");

Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/format.domain/code.ts");
Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/class/format.domain/code.ts");
};

main();
74 changes: 74 additions & 0 deletions scripts/testCodeGenWithFunctional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as Writer from "./writer/Functional";

const main = () => {
Writer.generateTypedefCodeOnly("test/api.test.domain/index.yml", "test/code/functional/typedef-only/api.test.domain.ts", true);
Writer.generateTypedefCodeOnly("test/infer.domain/index.yml", "test/code/functional/typedef-only/infer.domain.ts", false);
Writer.generateTypedefCodeOnly("test/json.properties/index.yml", "test/code/functional/typedef-only/json.properties.ts", false);

Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/functional/template-only/api.test.domain.ts", true, {
sync: false,
});
Writer.generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/functional/template-only/sync-api.test.domain.ts", true, {
sync: true,
});
Writer.generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/functional/template-only/infer.domain.ts", false, { sync: true });

Writer.generateTypedefWithTemplateCode("test/api.v2.domain/index.yml", "test/code/functional/typedef-with-template/api.v2.domain.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode(
"test/api.test.domain/index.yml",
"test/code/functional/typedef-with-template/api.test.domain.ts",
true,
{
sync: false,
},
);
Writer.generateTypedefWithTemplateCode(
"test/api.test.domain/index.yml",
"test/code/functional/typedef-with-template/sync-api.test.domain.ts",
true,
{
sync: true,
},
);
Writer.generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/functional/typedef-with-template/infer.domain.ts", false, {
sync: false,
});

Writer.generateTypedefWithTemplateCode("test/ref.access/index.yml", "test/code/functional/typedef-with-template/ref-access.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode(
"test/remote.ref.access/v1.yml",
"test/code/functional/typedef-with-template/remote-ref-access.ts",
false,
{
sync: false,
},
);
Writer.generateTypedefWithTemplateCode("test/kubernetes/openapi-v1.18.5.json", "test/code/functional/kubernetes/client-v1.18.5.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/argo-rollout/index.json", "test/code/functional/argo-rollout/client.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode(
"test/unknown.schema.domain/index.yml",
"test/code/functional/unknown.schema.domain/client.ts",
false,
{
sync: false,
},
);

Writer.generateSplitCode("test/api.test.domain/index.yml", "test/code/functional/split");
Writer.generateSplitCode("test/multi-type.test.domain/index.yml", "test/code/functional/mulit-type-test.domain");

Writer.generateParameter("test/api.test.domain/index.yml", "test/code/functional/parameter/api.test.domain.json");
Writer.generateParameter("test/infer.domain/index.yml", "test/code/functional/parameter/infer.domain.json");

Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/functional/format.domain/code.ts");
};

main();
2 changes: 1 addition & 1 deletion scripts/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const main = async () => {
console.log((await shell("pnpm build", cwd)).stdout);
console.log((await shell("pnpm run test:code:gen", cwd)).stdout);

chokidar.watch("./src", {}).on("change", async path => {
chokidar.watch("./src", { ignored: ["src/meta.ts"] }).on("change", async path => {
console.log(`Watch Change file ... ${path}`);
try {
console.log((await shell("pnpm build", cwd)).stdout);
Expand Down
18 changes: 9 additions & 9 deletions scripts/writer/index.ts → scripts/writer/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const generateTemplateCodeOnly = (
inputFilename: string,
outputFilename: string,
isValidate: boolean,
option: Templates.ApiClient.Option,
option: Templates.ClassApiClient.Option,
): void => {
const codeGenerator = new CodeGenerator(inputFilename);
if (isValidate) {
Expand All @@ -35,8 +35,8 @@ export const generateTemplateCodeOnly = (
});
}

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ClassApiClient.Option> = {
generator: Templates.ClassApiClient.generator,
option: option,
};

Expand All @@ -49,7 +49,7 @@ export const generateTypedefWithTemplateCode = (
inputFilename: string,
outputFilename: string,
isValidate: boolean,
option: Templates.ApiClient.Option,
option: Templates.ClassApiClient.Option,
): void => {
const codeGenerator = new CodeGenerator(inputFilename);
if (isValidate) {
Expand All @@ -61,7 +61,7 @@ export const generateTypedefWithTemplateCode = (
const code = codeGenerator.generateTypeDefinition([
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
{
generator: Templates.ApiClient.generator,
generator: Templates.ClassApiClient.generator,
option: option,
},
]);
Expand All @@ -72,8 +72,8 @@ export const generateTypedefWithTemplateCode = (
export const generateSplitCode = (inputFilename: string, outputDir: string) => {
const codeGenerator = new CodeGenerator(inputFilename);

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ClassApiClient.Option> = {
generator: Templates.ClassApiClient.generator,
option: { sync: false, additionalMethodComment: true },
};

Expand Down Expand Up @@ -147,8 +147,8 @@ export const generateFormatTypeCode = (inputFilename: string, outputFilename: st
};
const codeGenerator = new CodeGenerator(inputFilename, option);

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ClassApiClient.Option> = {
generator: Templates.ClassApiClient.generator,
option: {},
};

Expand Down
Loading

0 comments on commit 10d4857

Please sign in to comment.