Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
51bf050
refactor(CodeGenerator): remove unused typescript import and simplify…
Himenon May 1, 2026
7bd98ab
refactor(TsGenerator): simplify code generation to use string arrays …
Himenon May 1, 2026
3656f01
refactor(factory): delete all individual TypeScript AST node factory …
Himenon May 1, 2026
d78e7f5
refactor: replace typescript AST types with string return types acros…
Himenon May 1, 2026
04f3144
refactor: replace TypeScript AST types with string return types acros…
Himenon May 1, 2026
845c902
test: update snapshots to include generator version v1.1.0
Himenon May 1, 2026
4e66438
test(argo-rollout-test): update snapshot with formatting and version …
Himenon May 1, 2026
504d3d6
chore(package.json): remove typescript peer dependency
Himenon May 1, 2026
cdbd6d5
test(PathParameter): add explicit vitest imports
Himenon May 1, 2026
f298063
chore(.dependency-cruiser.cjs): expand test file exclusion patterns t…
Himenon May 1, 2026
2f56deb
fix: escape double quotes in generated string literals and enum values
Himenon May 1, 2026
1f5212b
fix: snapshot差分をzeroにする修正 1
Himenon May 1, 2026
f309629
feat: change tsc scope
Himenon May 2, 2026
43f03a8
feat: update snapshot
Himenon May 2, 2026
95fe90a
feat: update readme
Himenon May 2, 2026
8cd378a
refactor: index.ts to factory.ts
Himenon May 2, 2026
1e2705b
refactor: remove ast api I/F
Himenon May 2, 2026
ccdeaac
test: add test code
Himenon May 2, 2026
38e9e8b
Merge branch 'main' of https://github.com/Himenon/openapi-typescript-…
Himenon May 4, 2026
8621632
fix: confilict
Himenon May 4, 2026
c83989c
feat: remove factory directory
Himenon May 5, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[日本語](./docs/ja/README-ja.md)

This library provides TypeScript type definitions and extracted parameters from OpenAPI v3.0.x compliant specifications.
TypeScript AST is used to generate the code, which is accurately converted to TypeScript code.
Template literals are used to generate the code, which is accurately converted to TypeScript code.
Since the parameters extracted from OpenAPI can be used freely, it can be used for automatic generation of API Client and Server Side code, load balancer configuration files, etc.

## Playground
Expand Down Expand Up @@ -436,10 +436,10 @@ export namespace Schemas {
}
```

### Define a code template with TypeScript AST
### Define a code template with Template literals

You can extend your code using the API of TypeScript AST.
You can directly use the API of TypeScript AST or use the wrapper API of TypeScript AST provided by this library.
You can extend your code using the API for generating code.
You can directly use the Template literals or use the wrapper API provided by this library.

```ts
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
Expand All @@ -456,7 +456,7 @@ const generator: Types.CodeGenerator.GenerateFunction<Option> = (
return payload.map(params => {
return factory.InterfaceDeclaration.create({
export: true,
name: params.functionName,
name: params.convertedParams.functionName,
members: [],
});
});
Expand Down Expand Up @@ -487,7 +487,7 @@ Generates code that converts OpenAPI Schema to TypeScript type definitions.
#### generateCode

You can specify several of your own code generators, and the generators can use parameters extracted from OpenAPI Schema.
It internally performs the conversion of an array of `string` or `ts.Statement` as a string.
It internally performs the conversion of an array of `string` as a string.

For example, creating a generator in units of file divisions increases the reusability of the generator.

Expand All @@ -507,7 +507,7 @@ This is a type definition file for `Templates.FunctionalApiClient`. The reason i
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/dist/api";
```

This is a wrapper API for the TypeScript AST used internally.
This is an API for generating code using Template literals.
It is subject to change without notice.

### OpenApiTools
Expand Down Expand Up @@ -561,7 +561,7 @@ If your changes are in line with the design concept, please submit a pull reques
- Typedefs should not contain any entities (file size should be 0 when typedefs are converted to `.js`)
- The directory structure should be mapped to the typedef structure.
- No dependency on any API client library.
- Can be extended by TypeScript AST.
- Can be extended by Template literals.
- Conform to the OpenAPI specification.
- It should be a single file to maintain portability.

Expand All @@ -580,9 +580,9 @@ pnpm run test

## Useful development tools

TypeScript AST
Prettier (or other formatters)

- https://ts-ast-viewer.com
- https://prettier.io/

## LICENCE

Expand Down
20 changes: 10 additions & 10 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @himenon/openapi-typescript-code-generator

このライブラリは OpenAPI v3.0.x 系に準拠した仕様書から TypeScript の型定義と抽出したパラメーターを提供します。
コードの生成には TypeScript AST を利用し、正確に TypeScript のコードへ変換します。
コードの生成にはテンプレートリテラルを利用し、正確に TypeScript のコードへ変換します。
OpenAPI から抽出したパラメーターは自由に使うことができるため、API Client や Server Side 用のコード、ロードバランサーの設定ファイルなどの自動生成に役立てることができます。

## Playground
Expand Down Expand Up @@ -432,10 +432,10 @@ export namespace Schemas {
}
```

### TypeScript AST によるコードテンプレートを定義する
### テンプレートリテラルによるコードテンプレートを定義する

TypeScript AST の API を利用したコードの拡張が可能です
直接 TypeScript の AST の API を利用したり、本ライブラリが提供する TypeScript AST のラッパー API を利用できます。
テンプレートリテラルを利用したコードの拡張が可能です
直接テンプレートリテラルを利用したり、本ライブラリが提供するコード生成用 API を利用できます。

```ts
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
Expand All @@ -452,7 +452,7 @@ const generator: Types.CodeGenerator.GenerateFunction<Option> = (
return payload.map(params => {
return factory.InterfaceDeclaration.create({
export: true,
name: params.functionName,
name: params.convertedParams.functionName,
members: [],
});
});
Expand Down Expand Up @@ -483,7 +483,7 @@ OpenAPI Schema を TypeScript の型定義に変換したコードを生成し
#### generateCode

独自のコードジェネレーターを複数指定することができ、ジェネレーターは OpenAPI Schema から抽出したパラメーターを利用できます。
内部で`string`または`ts.Statement`の配列を文字列として変換を行います。
内部で`string`の配列を文字列として変換を行います。

たとえばファイルの分割の単位でジェネレーターを作成するとジェネレーターの再利用性が高まります。

Expand All @@ -503,7 +503,7 @@ OpenAPI Schema から抽出したパラメーターを取得できます。
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/dist/api";
```

内部で利用している TypeScript AST のラッパー API です。
内部で利用している、テンプレートリテラルを用いてコードを生成するための API です。
告知なく変更する可能性があります。

### OpenApiTools
Expand Down Expand Up @@ -557,7 +557,7 @@ API 仕様書から TypeScript のコードへ変換するとき、参照関係
- 型定義に実体が含まれないこと(型定義部分を`.js`に変換したとき、ファイルサイズが 0 になること)
- ディレクトリ構造が型定義の構造に写像されること
- どの API クライアントライブラリにも依存しないこと
- TypeScript AST による拡張ができること
- テンプレートリテラルによる拡張ができること
- OpenAPI の仕様に準拠すること
- 1 ファイル化することにより、ポータビリティを保つこと

Expand All @@ -576,9 +576,9 @@ pnpm run test

### 便利な開発ツール

TypeScript AST
Prettier (or other formatters)

- https://ts-ast-viewer.com
- https://prettier.io/

## LICENCE

Expand Down
2 changes: 1 addition & 1 deletion examples/readme-sample/ast-code-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const generator: Types.CodeGenerator.GenerateFunction<Option> = (
return payload.map(params => {
return factory.InterfaceDeclaration.create({
export: true,
name: params.functionName,
name: params.convertedParams.functionName,
members: [],
});
});
Expand Down
1 change: 1 addition & 0 deletions examples/readme-sample/generator-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/dist/typ
import * as fs from "fs";

/** ここにCode Templateの定義を記述してください */
// @ts-ignore -- placeholder example, generator is intentionally omitted
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove

const customGenerator: Types.CodeGenerator.CustomGenerator<{}> = {
/** .... */
};
Expand Down
2 changes: 1 addition & 1 deletion examples/readme-sample/use-extract-schema-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Option = {};

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[]): string[] => {
return payload.map(params => {
return `function ${params.operationId}() { console.log("${params.comment}") }`;
return `function ${params.operationId}() { console.log("${params.operationParams.comment}") }`;
});
};

Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "@himenon/openapi-typescript-code-generator",
"version": "1.1.3",
"description": "OpenAPI Code Generator using TypeScript AST.",
"description": "OpenAPI Code Generator using Template literals.",
"keywords": [
"openapi",
"openapi3",
"openapi-codegen",
"openapi-generator",
"typescript",
"typescript-ast"
"typescript"
],
"homepage": "https://github.com/Himenon/openapi-typescript-code-generator#readme",
"bugs": {
Expand Down Expand Up @@ -119,9 +118,6 @@
"typescript": "6.0.3",
"vitest": "^4.1.5"
},
"peerDependencies": {
"typescript": ">=5"
},
"packageManager": "pnpm@10.33.2",
"engines": {
"node": ">=22.0.0"
Expand Down
10 changes: 4 additions & 6 deletions src/code-templates/_shared/ApiClientArgument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type ts from "typescript";

import type { TsGenerator } from "../../api";
import type { CodeGenerator } from "../../types";

Expand Down Expand Up @@ -52,7 +50,7 @@ export const createResponseContentTypeReference = (factory: TsGenerator.Factory.
};

const createHeaders = (factory: TsGenerator.Factory.Type, { convertedParams }: CodeGenerator.Params) => {
const members = [];
const members: string[] = [];

if (convertedParams.has2OrMoreRequestContentTypes) {
members.push(
Expand Down Expand Up @@ -90,10 +88,10 @@ const createHeaders = (factory: TsGenerator.Factory.Type, { convertedParams }: C
* requestBody: {requestBodyName}[T];
* }
*/
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params): ts.InterfaceDeclaration | undefined => {
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params): string | undefined => {
const { convertedParams } = params;
const typeParameters: ts.TypeParameterDeclaration[] = [];
const members: ts.TypeElement[] = [];
const typeParameters: string[] = [];
const members: string[] = [];
if (convertedParams.hasRequestBody && convertedParams.has2OrMoreRequestContentTypes) {
typeParameters.push(
factory.TypeParameterDeclaration.create({
Expand Down
13 changes: 3 additions & 10 deletions src/code-templates/_shared/ApiClientInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ts from "typescript";

import type { TsGenerator } from "../../api";
import type { CodeGenerator } from "../../types";
import type { MethodType } from "./MethodBody/types";
Expand All @@ -12,7 +10,7 @@ const createErrorResponsesTypeAlias = (typeName: string, factory: TsGenerator.Fa
return factory.TypeAliasDeclaration.create({
export: true,
name: typeName,
type: ts.factory.createToken(ts.SyntaxKind.VoidKeyword),
type: "void",
});
}
return factory.TypeAliasDeclaration.create({
Expand All @@ -33,7 +31,7 @@ const createSuccessResponseTypeAlias = (typeName: string, factory: TsGenerator.F
return factory.TypeAliasDeclaration.create({
export: true,
name: typeName,
type: ts.factory.createToken(ts.SyntaxKind.VoidKeyword),
type: "void",
});
}
return factory.TypeAliasDeclaration.create({
Expand Down Expand Up @@ -156,12 +154,7 @@ const createEncodingInterface = (factory: TsGenerator.Factory.Type) => {
});
};

export const create = (
factory: TsGenerator.Factory.Type,
list: CodeGenerator.Params[],
methodType: MethodType,
option: Option,
): ts.Statement[] => {
export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], methodType: MethodType, option: Option): string[] => {
const objectLikeOrAnyType = factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
Expand Down
6 changes: 2 additions & 4 deletions src/code-templates/_shared/MethodBody/CallRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type ts from "typescript";

import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import * as Utils from "../utils";
Expand All @@ -18,7 +16,7 @@ export interface Params {
* "application/x-www-form-urlencoded": {},
* }
*/
const createEncodingParams = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params): ts.Expression | undefined => {
const createEncodingParams = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params): string | undefined => {
const content = params.operationParams.requestBody?.content;
if (!content) {
return;
Expand All @@ -36,7 +34,7 @@ const createEncodingParams = (factory: TsGenerator.Factory.Type, params: CodeGen
/**
* this.apiClient.request("GET", url, requestBody, headers, queryParameters);
*/
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, methodType: MethodType): ts.CallExpression => {
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, methodType: MethodType): string => {
const { convertedParams } = params;
const apiClientVariableIdentifier: Record<MethodType, string> = {
class: "this.apiClient.request",
Expand Down
4 changes: 1 addition & 3 deletions src/code-templates/_shared/MethodBody/HeaderParameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type ts from "typescript";

import type { TsGenerator } from "../../../api";
import * as Utils from "../utils";

Expand All @@ -8,7 +6,7 @@ export interface Params {
object: Utils.LiteralExpressionObject;
}

export const create = (factory: TsGenerator.Factory.Type, params: Params): ts.VariableStatement => {
export const create = (factory: TsGenerator.Factory.Type, params: Params): string => {
return factory.VariableStatement.create({
declarationList: factory.VariableDeclarationList.create({
flag: "const",
Expand Down
13 changes: 4 additions & 9 deletions src/code-templates/_shared/MethodBody/PathParameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type ts from "typescript";

import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import { escapeText2 as escapeText } from "../../../utils";
Expand All @@ -16,8 +14,8 @@ export const isPathParameter = (params: any): params is CodeGenerator.PickedPara
const generateUrlVariableStatement = (
factory: TsGenerator.Factory.Type,
urlTemplate: Utils.Params$TemplateExpression,
variableExpression: ts.Expression,
): ts.VariableStatement => {
variableExpression: string,
): string => {
return factory.VariableStatement.create({
declarationList: factory.VariableDeclarationList.create({
declarations: [
Expand All @@ -38,10 +36,7 @@ const generateUrlVariableStatement = (
/**
* const uri = `[head]${params.parameter.[parameterName]}`;
*/
const generateUriVariableStatement = (
factory: TsGenerator.Factory.Type,
urlTemplate: Utils.Params$TemplateExpression,
): ts.VariableStatement => {
const generateUriVariableStatement = (factory: TsGenerator.Factory.Type, urlTemplate: Utils.Params$TemplateExpression): string => {
return factory.VariableStatement.create({
declarationList: factory.VariableDeclarationList.create({
declarations: [
Expand Down Expand Up @@ -134,7 +129,7 @@ export const create = (
requestUri: string,
pathParameters: CodeGenerator.PickedParameter[],
methodType: MethodType,
): ts.VariableStatement => {
): string => {
const urlTemplate: Utils.Params$TemplateExpression =
pathParameters.length > 0 ? generateUrlTemplateExpression(factory, requestUri, pathParameters) : [{ type: "string", value: requestUri }];
if (methodType === "currying-function") {
Expand Down
10 changes: 4 additions & 6 deletions src/code-templates/_shared/MethodBody/QueryParameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ts from "typescript";

import type { TsGenerator } from "../../../api";
import * as Utils from "../../../utils";
import * as UtilsExtra from "../utils";
Expand All @@ -18,9 +16,9 @@ export interface Params {
};
}

export const create = (factory: TsGenerator.Factory.Type, params: Params): ts.VariableStatement => {
const properties: ts.PropertyAssignment[] = Object.entries(params.object).reduce<ts.PropertyAssignment[]>((previous, [key, item]) => {
const childProperties: ts.PropertyAssignment[] = [
export const create = (factory: TsGenerator.Factory.Type, params: Params): string => {
const properties: string[] = Object.entries(params.object).reduce<string[]>((previous, [key, item]) => {
const childProperties: string[] = [
factory.PropertyAssignment.create({
name: "value",
initializer:
Expand All @@ -40,7 +38,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: Params): ts.Va
childProperties.push(
factory.PropertyAssignment.create({
name: "explode",
initializer: item.explode ? ts.factory.createTrue() : ts.factory.createFalse(),
initializer: item.explode ? "true" : "false",
}),
);
const childObjectInitializer = factory.ObjectLiteralExpression.create({
Expand Down
Loading
Loading