Documentation
¶
Overview ¶
Package typescript provides TypeScript-specific code generation utilities.
Index ¶
- Variables
- func Camelize(s string, lowercaseFirst bool) string
- func Dashize(s string) string
- func SanitizeName(name string) string
- func Underscore(s string) string
- type BaseGenerator
- func (g *BaseGenerator) EscapeReservedWord(name string) string
- func (g *BaseGenerator) FromModel(name string, _ any) *codegen.CodegenModel
- func (g *BaseGenerator) FromOperation(path, httpMethod string, _ any) *codegen.CodegenOperation
- func (g *BaseGenerator) FromParameter(_ any) *codegen.CodegenParameter
- func (g *BaseGenerator) FromProperty(name string, _ any, required bool) *codegen.CodegenProperty
- func (g *BaseGenerator) FromResponse(code string, _ any) *codegen.CodegenResponse
- func (g *BaseGenerator) FromSecurityScheme(name string, _ any) *codegen.CodegenSecurity
- func (g *BaseGenerator) GetAdditionalProperties() map[string]any
- func (g *BaseGenerator) GetApiTemplateFiles() map[string]string
- func (g *BaseGenerator) GetConfig() *config.GeneratorConfig
- func (g *BaseGenerator) GetImportMapping() map[string]string
- func (g *BaseGenerator) GetLanguageSpecificPrimitives() map[string]bool
- func (g *BaseGenerator) GetModelTemplateFiles() map[string]string
- func (g *BaseGenerator) GetReservedWords() map[string]bool
- func (g *BaseGenerator) GetSchemaType(schemaType, format string) string
- func (g *BaseGenerator) GetSupportingFiles() []generator.SupportingFile
- func (g *BaseGenerator) GetTypeDeclaration(schemaType, format string) string
- func (g *BaseGenerator) GetTypeMapping() map[string]string
- func (g *BaseGenerator) IsPrimitive(typeName string) bool
- func (g *BaseGenerator) IsReservedWord(word string) bool
- func (g *BaseGenerator) PostProcessModels(models []*codegen.CodegenModel) []*codegen.CodegenModel
- func (g *BaseGenerator) PostProcessOperations(operations []*codegen.CodegenOperation) []*codegen.CodegenOperation
- func (g *BaseGenerator) SanitizeOperationId(operationId string) string
- func (g *BaseGenerator) SetConfig(cfg *config.GeneratorConfig)
- func (g *BaseGenerator) ToApiFilename(name string) string
- func (g *BaseGenerator) ToApiName(name string) string
- func (g *BaseGenerator) ToModelFilename(name string) string
- func (g *BaseGenerator) ToModelName(name string) string
- func (g *BaseGenerator) ToParamName(name string) string
- func (g *BaseGenerator) ToVarName(name string) string
- type FetchGenerator
- func (g *FetchGenerator) EscapeReservedWord(name string) string
- func (g *FetchGenerator) GetHelp() string
- func (g *FetchGenerator) GetName() string
- func (g *FetchGenerator) GetTag() generator.GeneratorType
- func (g *FetchGenerator) GetTypeDeclaration(schemaType, format string) string
- func (g *FetchGenerator) PostProcessModels(models []*codegen.CodegenModel) []*codegen.CodegenModel
- func (g *FetchGenerator) PostProcessOperations(operations []*codegen.CodegenOperation) []*codegen.CodegenOperation
- func (g *FetchGenerator) ProcessOpts() error
- func (g *FetchGenerator) ToApiFilename(name string) string
- func (g *FetchGenerator) ToModelFilename(name string) string
Constants ¶
This section is empty.
Variables ¶
var Primitives = map[string]bool{ "string": true, "String": true, "boolean": true, "Boolean": true, "Double": true, "Integer": true, "Long": true, "Float": true, "Object": true, "object": true, "Array": true, "ReadonlyArray": true, "Date": true, "number": true, "any": true, "File": true, "Error": true, "Map": true, "Set": true, "Awaited": true, "Partial": true, "Required": true, "Readonly": true, "Record": true, "Pick": true, "Omit": true, "Exclude": true, "Extract": true, "NonNullable": true, "Parameters": true, "ConstructorParameters": true, "ReturnType": true, "InstanceType": true, "NoInfer": true, "ThisParameterType": true, "OmitThisParameter": true, "ThisType": true, "Uppercase": true, "Lowercase": true, "Capitalize": true, "Uncapitalize": true, }
Primitives is the set of TypeScript primitive types. It mirrors upstream openapi-generator's AbstractTypeScriptClientCodegen#languageSpecificPrimitives, including the built-in TypeScript utility types (Record, Partial, ...), so that a schema named e.g. "Record" is renamed (ModelRecord) instead of colliding with the language's own Record<K, V> type.
var ReservedWords = map[string]bool{ "varLocalPath": true, "queryParameters": true, "headerParams": true, "formParams": true, "useFormData": true, "varLocalDeferred": true, "requestOptions": true, "abstract": true, "await": true, "boolean": true, "break": true, "byte": true, "case": true, "catch": true, "char": true, "class": true, "const": true, "continue": true, "debugger": true, "default": true, "delete": true, "do": true, "double": true, "else": true, "enum": true, "export": true, "extends": true, "false": true, "final": true, "finally": true, "float": true, "for": true, "function": true, "goto": true, "if": true, "implements": true, "import": true, "in": true, "instanceof": true, "int": true, "interface": true, "let": true, "long": true, "native": true, "new": true, "null": true, "package": true, "private": true, "protected": true, "public": true, "return": true, "short": true, "static": true, "super": true, "switch": true, "synchronized": true, "this": true, "throw": true, "transient": true, "true": true, "try": true, "typeof": true, "var": true, "void": true, "volatile": true, "while": true, "with": true, "yield": true, "blob": true, "file": true, "date": true, "error": true, "map": true, "set": true, "array": true, "object": true, }
ReservedWords is the set of TypeScript reserved words.
var TypeMapping = map[string]string{
"Set": "Set",
"set": "Set",
"Array": "Array",
"array": "Array",
"boolean": "boolean",
"decimal": "string",
"string": "string",
"int": "number",
"float": "number",
"number": "number",
"long": "number",
"short": "number",
"char": "string",
"double": "number",
"object": "any",
"integer": "number",
"Map": "any",
"map": "any",
"date": "string",
"DateTime": "string",
"date-time": "string",
"binary": "any",
"File": "any",
"file": "any",
"ByteArray": "string",
"UUID": "string",
"URI": "string",
"Error": "Error",
"AnyType": "any",
}
TypeMapping maps OpenAPI types to TypeScript types.
Functions ¶
func SanitizeName ¶
SanitizeName sanitizes a name for use as a TypeScript type/API name or operation ID. It mirrors upstream openapi-generator's default DefaultCodegen#sanitizeName (removeCharRegEx "\\W", i.e. no "$" exception): a known set of separators (brackets, parens, ".", ":", "-", "|", " ", "/", "\") become "_", while any other punctuation — including "$" — is dropped rather than turned into a separator, so "input.name" becomes "input_name" but "a+b" becomes "ab" (not "a_b").
Property and parameter names must NOT use this function — see sanitizeIdentifierName, which upstream reaches via a different removeCharRegEx ("[^\\w$]") that keeps "$" instead of stripping it.
Types ¶
type BaseGenerator ¶
type BaseGenerator struct {
// Configuration
Config *config.GeneratorConfig
TSConfig *config.TypeScriptFetchConfig
// Type mappings
TypeMapping map[string]string
ImportMapping map[string]string
ReservedWords map[string]bool
LanguageSpecificPrimitives map[string]bool
InstantiationTypes map[string]string
// Name mappings
NameMapping map[string]string
ParameterNameMapping map[string]string
ModelNameMapping map[string]string
EnumNameMapping map[string]string
// Package configuration
ModelPackage string
ApiPackage string
SourceDir string
// Naming configuration
ModelNamePrefix string
ModelNameSuffix string
ApiNameSuffix string
// Template files
ApiTemplateFiles map[string]string
ModelTemplateFiles map[string]string
SupportingFiles []generator.SupportingFile
// Additional properties for templates
AdditionalProperties map[string]any
// Behavior flags
ModelPropertyNaming config.ModelPropertyNamingType
NullSafeAdditionalProps bool
AllowUnicodeIdentifiers bool
}
BaseGenerator is the base generator for TypeScript languages. It mirrors AbstractTypeScriptClientCodegen in Java.
func NewBaseGenerator ¶
func NewBaseGenerator() *BaseGenerator
NewBaseGenerator creates a new TypeScript base generator.
func (*BaseGenerator) EscapeReservedWord ¶
func (g *BaseGenerator) EscapeReservedWord(name string) string
EscapeReservedWord escapes a reserved word.
func (*BaseGenerator) FromModel ¶
func (g *BaseGenerator) FromModel(name string, _ any) *codegen.CodegenModel
FromModel converts an OpenAPI schema to a CodegenModel.
func (*BaseGenerator) FromOperation ¶
func (g *BaseGenerator) FromOperation(path, httpMethod string, _ any) *codegen.CodegenOperation
FromOperation converts an OpenAPI operation to a CodegenOperation.
func (*BaseGenerator) FromParameter ¶
func (g *BaseGenerator) FromParameter(_ any) *codegen.CodegenParameter
FromParameter converts an OpenAPI parameter to a CodegenParameter.
func (*BaseGenerator) FromProperty ¶
func (g *BaseGenerator) FromProperty(name string, _ any, required bool) *codegen.CodegenProperty
FromProperty converts an OpenAPI property to a CodegenProperty.
func (*BaseGenerator) FromResponse ¶
func (g *BaseGenerator) FromResponse(code string, _ any) *codegen.CodegenResponse
FromResponse converts an OpenAPI response to a CodegenResponse.
func (*BaseGenerator) FromSecurityScheme ¶
func (g *BaseGenerator) FromSecurityScheme(name string, _ any) *codegen.CodegenSecurity
FromSecurityScheme converts security scheme to CodegenSecurity.
func (*BaseGenerator) GetAdditionalProperties ¶
func (g *BaseGenerator) GetAdditionalProperties() map[string]any
GetAdditionalProperties returns additional properties for templates.
func (*BaseGenerator) GetApiTemplateFiles ¶
func (g *BaseGenerator) GetApiTemplateFiles() map[string]string
GetApiTemplateFiles returns API template files.
func (*BaseGenerator) GetConfig ¶
func (g *BaseGenerator) GetConfig() *config.GeneratorConfig
GetConfig returns the generator config.
func (*BaseGenerator) GetImportMapping ¶
func (g *BaseGenerator) GetImportMapping() map[string]string
GetImportMapping returns the import mapping.
func (*BaseGenerator) GetLanguageSpecificPrimitives ¶
func (g *BaseGenerator) GetLanguageSpecificPrimitives() map[string]bool
GetLanguageSpecificPrimitives returns primitive types.
func (*BaseGenerator) GetModelTemplateFiles ¶
func (g *BaseGenerator) GetModelTemplateFiles() map[string]string
GetModelTemplateFiles returns model template files.
func (*BaseGenerator) GetReservedWords ¶
func (g *BaseGenerator) GetReservedWords() map[string]bool
GetReservedWords returns the reserved words.
func (*BaseGenerator) GetSchemaType ¶
func (g *BaseGenerator) GetSchemaType(schemaType, format string) string
GetSchemaType returns the TypeScript type for an OpenAPI schema type.
func (*BaseGenerator) GetSupportingFiles ¶
func (g *BaseGenerator) GetSupportingFiles() []generator.SupportingFile
GetSupportingFiles returns supporting files.
func (*BaseGenerator) GetTypeDeclaration ¶
func (g *BaseGenerator) GetTypeDeclaration(schemaType, format string) string
GetTypeDeclaration returns the type declaration for a schema.
func (*BaseGenerator) GetTypeMapping ¶
func (g *BaseGenerator) GetTypeMapping() map[string]string
GetTypeMapping returns the type mapping.
func (*BaseGenerator) IsPrimitive ¶
func (g *BaseGenerator) IsPrimitive(typeName string) bool
IsPrimitive checks if a type is a language primitive.
func (*BaseGenerator) IsReservedWord ¶
func (g *BaseGenerator) IsReservedWord(word string) bool
IsReservedWord checks if a word is reserved.
func (*BaseGenerator) PostProcessModels ¶
func (g *BaseGenerator) PostProcessModels(models []*codegen.CodegenModel) []*codegen.CodegenModel
PostProcessModels post-processes models.
func (*BaseGenerator) PostProcessOperations ¶
func (g *BaseGenerator) PostProcessOperations(operations []*codegen.CodegenOperation) []*codegen.CodegenOperation
PostProcessOperations post-processes operations.
func (*BaseGenerator) SanitizeOperationId ¶
func (g *BaseGenerator) SanitizeOperationId(operationId string) string
SanitizeOperationId sanitizes an operation ID.
func (*BaseGenerator) SetConfig ¶
func (g *BaseGenerator) SetConfig(cfg *config.GeneratorConfig)
SetConfig sets the generator config.
func (*BaseGenerator) ToApiFilename ¶
func (g *BaseGenerator) ToApiFilename(name string) string
ToApiFilename returns the API file name.
func (*BaseGenerator) ToApiName ¶
func (g *BaseGenerator) ToApiName(name string) string
ToApiName converts a tag to an API class name.
func (*BaseGenerator) ToModelFilename ¶
func (g *BaseGenerator) ToModelFilename(name string) string
ToModelFilename returns the model file name.
func (*BaseGenerator) ToModelName ¶
func (g *BaseGenerator) ToModelName(name string) string
ToModelName converts a schema name to a TypeScript model name.
func (*BaseGenerator) ToParamName ¶
func (g *BaseGenerator) ToParamName(name string) string
ToParamName converts a parameter name.
func (*BaseGenerator) ToVarName ¶
func (g *BaseGenerator) ToVarName(name string) string
ToVarName converts a property name to a variable name.
type FetchGenerator ¶
type FetchGenerator struct {
*BaseGenerator
// Fetch-specific configuration
WithPackageJson bool
ImportFileExtension string
UseSingleRequestParameter bool
PrefixParameterInterfaces bool
WithoutRuntimeChecks bool
StringEnums bool
FileNaming string
WithInterfaces bool
ValidationAttributes bool
// contains filtered or unexported fields
}
FetchGenerator implements the typescript-fetch generator. It mirrors TypeScriptFetchClientCodegen in Java.
func NewFetchGenerator ¶
func NewFetchGenerator() *FetchGenerator
NewFetchGenerator creates a new TypeScript Fetch generator.
func (*FetchGenerator) EscapeReservedWord ¶
func (g *FetchGenerator) EscapeReservedWord(name string) string
EscapeReservedWord escapes reserved words.
func (*FetchGenerator) GetHelp ¶
func (g *FetchGenerator) GetHelp() string
GetHelp returns the help text.
func (*FetchGenerator) GetName ¶
func (g *FetchGenerator) GetName() string
GetName returns the generator name.
func (*FetchGenerator) GetTag ¶
func (g *FetchGenerator) GetTag() generator.GeneratorType
GetTag returns the generator type.
func (*FetchGenerator) GetTypeDeclaration ¶
func (g *FetchGenerator) GetTypeDeclaration(schemaType, format string) string
GetTypeDeclaration returns the type declaration for file/binary types.
func (*FetchGenerator) PostProcessModels ¶
func (g *FetchGenerator) PostProcessModels(models []*codegen.CodegenModel) []*codegen.CodegenModel
PostProcessModels post-processes models for TypeScript-Fetch.
func (*FetchGenerator) PostProcessOperations ¶
func (g *FetchGenerator) PostProcessOperations(operations []*codegen.CodegenOperation) []*codegen.CodegenOperation
PostProcessOperations post-processes operations for TypeScript-Fetch.
func (*FetchGenerator) ProcessOpts ¶
func (g *FetchGenerator) ProcessOpts() error
ProcessOpts processes CLI options and initializes the generator.
func (*FetchGenerator) ToApiFilename ¶
func (g *FetchGenerator) ToApiFilename(name string) string
ToApiFilename returns the API file name with file naming convention applied.
func (*FetchGenerator) ToModelFilename ¶
func (g *FetchGenerator) ToModelFilename(name string) string
ToModelFilename returns the model file name with file naming convention applied.