TypeScript code generator via OpenAPI scheme.
MIT License
Bot releases are hidden (Show)
Goooood News!!! Data Types including format
are now supported !!!!
components:
schemas:
Binary:
type: string
format: binary
IntOrString:
type: string
format: int-or-string
AandB:
type: string
format: A-and-B
import { CodeGenerator, Option } from "@himenon/openapi-typescript-code-generator";
const option: Option = {
convertOption: {
formatConversions: [
{
selector: {
format: "binary",
},
output: {
type: ["Blob"],
},
},
{
selector: {
format: "int-or-string",
},
output: {
type: ["number", "string"],
},
},
{
selector: {
format: "A-and-B",
},
output: {
type: ["A", "B"],
multiType: "allOf",
},
},
],
},
};
const codeGenerator = new CodeGenerator(inputFilename, option);
export namespace Schemas {
export type Binary = Blob;
export type IntOrString = number | string;
export type AandB = A & B;
}
https://github.com/kubernetes/kubernetes/blob/master/api/openapi-spec/swagger.json
Deprecated API
New API
string
in addition to the return value of ts.statement[]
.Before
import { generateTypeScriptCode } from "@himenon/openapi-typescript-code-generator";
const code = generateTypeScriptCode({
entryPoint,
log: {
validator: {
displayLogLines: 1,
},
},
});
After
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
import * as Templates from "@himenon/openapi-typescript-code-generator/templates";
import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const codeGenerator = new CodeGenerator(entryPoint);
codeGenerator.validateOpenApiSchema({ logger: { displayLogLines: 1 } });
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
option: {},
};
const code = codeGenerator.generateTypeDefinition([
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
apiClientGeneratorTemplate,
]);
rewriteCodeAfterTypeDeclaration
userBefore
import { generateTypeScriptCode } from "@himenon/openapi-typescript-code-generator";
const code = generateTypeScriptCode({
entryPoint,
option: {
rewriteCodeAfterTypeDeclaration: (context, codeGeneratorParamsList) => {
/** awesome code */
return [];
},
},
});
After
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
import * as Templates from "@himenon/openapi-typescript-code-generator/templates";
import type * as Types from "@himenon/openapi-typescript-code-generator/types";
const codeGenerator = new CodeGenerator(entryPoint);
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
option: {},
};
const code = codeGenerator.generateTypeDefinition([
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
apiClientGeneratorTemplate,
{
generator: () => {
/** awesome code */ // <------------ here!
return [];
},
}
]);
Published by Himenon over 3 years ago