Skip to content

Commit ad3ec06

Browse files
authored
docs: update readme (#37)
* docs: update readme * fix: Adjusting the parameters to be extracted * chore: update example code
1 parent 5ce24d3 commit ad3ec06

27 files changed

+847
-336
lines changed

README.md

+221-33
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
[日本語](./docs/ja/README-ja.md)
44

5-
This package generates TypeScript typedefs and API Client from the OpenAPI v3 series API specification.
6-
It uses TypeScript AST to generate the code, and converts it exactly to TypeScript code.
7-
It not only converts `allOf` and `oneOf` into `intersection` type and `union` type, but also converts the directory structure of the reference destination into `namespace` and generates the API Client.
8-
The hierarchical structure of the directory is converted to the hierarchical structure of the type definition.
5+
This library provides TypeScript type definitions and extracted parameters from OpenAPI v3.0.x compliant specifications.
6+
TypeScript AST is used to generate the code, which is accurately converted to TypeScript code.
7+
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.
98

109
## Usage
1110

@@ -17,63 +16,252 @@ The hierarchical structure of the directory is converted to the hierarchical str
1716
yarn add -D @himenon/openapi-typescript-code-generator
1817
```
1918

20-
### DEMO
19+
## DEMO
2120

2221
- [DEMO](./example/README.md)
2322
- [DEMO: github/rest-api-client code generate](https://github.com/Himenon/github-rest-api-client/tree/master/source)
2423
- https://github.com/github/rest-api-description
2524

26-
### Basic usage
25+
## Usage
26+
27+
### Generate typedef-only code
2728

2829
```ts
2930
import * as fs from "fs";
3031

31-
import * as CodeGenerator from "@himenon/openapi-typescript-code-generator";
32+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
3233

3334
const main = () => {
34-
const params: CodeGenerator.Params = {
35-
entryPoint: "your/openapi/spec.yml", // support .yml, .yaml, .json
36-
};
37-
const code = CodeGenerator.generateTypeScriptCode(params);
35+
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");
36+
const code = codeGenerator.generateTypeDefinition();
3837
fs.writeFileSync("client.ts", code, { encoding: "utf-8" });
3938
};
4039

4140
main();
4241
```
4342

44-
### Create the original API Client template.
45-
46-
We have an entry point in `option.rewriteCodeAfterTypeDeclaration` to generate non-typed code.
47-
The first argument can be TypeScript's `TransformationContext`, and the second argument contains the information of the type definition generated before this.
48-
By using [ts-ast-viewer](https://ts-ast-viewer.com), code extension by AST can facilitate code extension.
43+
### Generate code containing the API Client
4944

5045
```ts
5146
import * as fs from "fs";
5247

53-
import ts from "typescript";
48+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
49+
import * as Templates from "@himenon/openapi-typescript-code-generator/templates";
50+
import type * as Types from "@himenon/openapi-typescript-code-generator/types";
51+
52+
const main = () => {
53+
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");
54+
55+
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
56+
generator: Templates.ApiClient.generator,
57+
option: {},
58+
};
59+
60+
const code = codeGenerator.generateTypeDefinition([
61+
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
62+
apiClientGeneratorTemplate,
63+
]);
5464

55-
import * as CodeGenerator from "../lib";
65+
fs.writeFileSync("client.ts", code, { encoding: "utf-8" });
66+
};
67+
68+
main();
69+
```
70+
71+
### Split the type definition file and the API Client implementation
72+
73+
```ts
74+
import * as fs from "fs";
75+
76+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
77+
import * as Templates from "@himenon/openapi-typescript-code-generator/templates";
78+
import type * as Types from "@himenon/openapi-typescript-code-generator/types";
5679

5780
const main = () => {
58-
const params: CodeGenerator.Params = {
59-
entryPoint: "your/openapi/spec.yml", // support .yml, .yaml, .json
60-
option: {
61-
rewriteCodeAfterTypeDeclaration: (context: Pick<ts.TransformationContext, "factory">, codeGeneratorParamsList: CodeGenerator.Converter.v3.CodeGeneratorParams[]): ts.Statement[] => {
62-
const factory = context.factory; // https://ts-ast-viewer.com/ is very very very useful !
63-
return []; // generate no api client
81+
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");
82+
83+
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
84+
generator: Templates.ApiClient.generator,
85+
option: {},
86+
};
87+
88+
const typeDefCode = codeGenerator.generateTypeDefinition();
89+
const apiClientCode = codeGenerator.generateCode([
90+
{
91+
generator: () => {
92+
return [`import { Schemas } from "./types";`];
6493
},
6594
},
66-
};
67-
const code = CodeGenerator.generateTypeScriptCode(params);
68-
fs.writeFileSync("client.ts", code, { encoding: "utf-8" });
95+
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
96+
apiClientGeneratorTemplate,
97+
]);
98+
99+
fs.writeFileSync("types.ts", typeDefCode, { encoding: "utf-8" });
100+
fs.writeFileSync("apiClient.ts", apiClientCode, { encoding: "utf-8" });
69101
};
70102

71103
main();
72104
```
73105

74-
### Restrictions
106+
## Create a Code Template
107+
108+
The examples in this section can be used in the following ways
109+
110+
```ts
111+
import * as fs from "fs";
112+
113+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
114+
import type * as Types from "@himenon/openapi-typescript-code-generator/types";
115+
116+
/** Write the definition of the Code Template here. */
117+
const customGenerator: Types.CodeGenerator.CustomGenerator<{}> = {
118+
/** .... */
119+
}
120+
121+
const codeGenerator = new CodeGenerator("your/openapi/spec.yml");
122+
123+
const code = codeGenerator.generateCode([
124+
customGenerator,
125+
]);
126+
127+
fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
128+
```
129+
130+
### Define a text-based code template
131+
132+
A self-defined code generator can return an array of `string`.
133+
134+
```ts
135+
import * as Types from "@himenon/openapi-typescript-code-generator/types";
136+
137+
interface Option {
138+
showLog?: boolean;
139+
}
140+
141+
const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): string[] => {
142+
if (option && option.showLog) {
143+
console.log("show log message");
144+
}
145+
return ["Hello world"];
146+
};
147+
148+
const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
149+
generator: generator,
150+
option: {},
151+
}
152+
```
153+
154+
### Define using the information extracted from OpenAPI Schema
155+
156+
The self-defined code generator can accept parameters extracted from OpenAPI Schema.
157+
See Type definitions for available parameters.
158+
159+
```ts
160+
import * as Types from "@himenon/openapi-typescript-code-generator/types";
161+
162+
interface Option {
163+
}
164+
165+
const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): string[] => {
166+
return payload.map((params) => {
167+
return `function ${params.operationId}() { console.log("${params.comment}") }`;
168+
})
169+
};
170+
171+
const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
172+
generator: generator,
173+
option: {},
174+
}
175+
```
176+
177+
### Define a code template with TypeScript AST
178+
179+
You can extend your code using the API of TypeScript AST.
180+
You can directly use the API of TypeScript AST or use the wrapper API of TypeScript AST provided by this library.
181+
182+
```ts
183+
import * as Types from "@himenon/openapi-typescript-code-generator/types";
184+
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/api";
185+
186+
interface Option {
187+
}
188+
189+
const factory = TsGenerator.Factory.create();
190+
191+
const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): Types.CodeGenerator.IntermediateCode[] => {
192+
return payload.map((params) => {
193+
return factory.InterfaceDeclaration.create({
194+
export: true,
195+
name: params.functionName,
196+
members: [],
197+
})
198+
})
199+
};
200+
201+
const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
202+
generator: generator,
203+
option: {},
204+
}
205+
```
206+
207+
## API
208+
209+
### CodeGenerator
210+
211+
```ts
212+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
213+
```
214+
215+
#### validateOpenApiSchema
216+
217+
Performs validation of the input OpenAPI Schema.
218+
219+
#### generateTypeDefinition
220+
221+
Generates code that converts OpenAPI Schema to TypeScript type definitions.
222+
223+
#### generateCode
224+
225+
You can specify several of your own code generators, and the generators can use parameters extracted from OpenAPI Schema.
226+
It internally performs the conversion of an array of `string` or `ts.Statement` as a string.
227+
228+
For example, creating a generator in units of file divisions increases the reusability of the generator.
229+
230+
#### getCodeGeneratorParamsArray
231+
232+
It provides parameters extracted from OpenAPI Schema.
233+
234+
#### getAdditionalTypeDefinitionCustomCodeGenerator
235+
236+
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.
237+
238+
※ 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.
239+
240+
### TsGenerator
241+
242+
```ts
243+
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/api";
244+
```
245+
246+
This is a wrapper API for the TypeScript AST used internally.
247+
It is subject to change without notice.
248+
249+
### OpenApiTools
250+
251+
```ts
252+
import { OpenApiTools } from "@himenon/openapi-typescript-code-generator/api";
253+
```
254+
255+
#### Parser
256+
257+
- `OpenApiTools.Parser`
258+
259+
This is the API for parsing OpenAPI Schema.
260+
It is subject to change without notice.
261+
262+
## Restrictions
75263

76-
#### Directory Restrictions for Remote Reference
264+
### Directory Restrictions for Remote Reference
77265

78266
There is a limitation on the directory structure supported.
79267
To simplify implementation when converting directory structures to TypeScript namespaces, Remote References using `$ref` should only be defined in the following directory structures.
@@ -91,7 +279,7 @@ components/
91279
paths/
92280
```
93281

94-
#### HTTP communication restrictions for Remote Reference
282+
### HTTP communication restrictions for Remote Reference
95283

96284
`$ref: http://....` Currently not supported. We hope to support it in the future.
97285

@@ -103,7 +291,7 @@ Adding test cases is a very powerful support for stabilizing the behavior, so pl
103291
Also, the basic design concepts of this repository can be found below. If you want to make changes that do not follow these concepts, please fork and extend them.
104292
If your changes are in line with the design concept, please submit a pull request or issue!
105293

106-
### Design Concept
294+
## Design Concept
107295

108296
- Be typedef first.
109297
- Typedefs should not contain any entities (file size should be 0 when typedefs are converted to `.js`)
@@ -113,7 +301,7 @@ If your changes are in line with the design concept, please submit a pull reques
113301
- Conform to the OpenAPI specification.
114302
- It should be a single file to maintain portability.
115303

116-
### Development
304+
## Development
117305

118306
```bash
119307
git clone https://github.com/Himenon/openapi-typescript-code-generator.git
@@ -123,7 +311,7 @@ yarn
123311
yarn build && yarn test
124312
```
125313

126-
### Useful development tools
314+
## Useful development tools
127315

128316
TypeScript AST
129317

0 commit comments

Comments
 (0)