Namespace TypedRest.CodeGeneration.TypeScript
Generates TypeScript source code for TypedRest for TypeScript clients from OpenAPI/Swagger documents.
Note
NuGet package: TypedRest.CodeGeneration.TypeScript
Usage
var reader = new OpenApiStreamReader(new OpenApiReaderSettings().AddTypedRest());
var doc = reader.Read(File.OpenRead("myapi.yml"), out _);
foreach (var file in doc.GenerateTypedRestTypeScript(new TypeScriptGenerationOptions("MyService")
{
DtoNamespace = "dtos",
GenerateDtos = true
}))
file.WriteToDirectory("myclient/");
GenerateTypedRestTypeScript uses the endpoints described by the document's x-typedrest extension, or infers them from the paths if there is no such extension.
Differences from the C# generator
TypedRest for TypeScript has no reactive endpoints, so the polling, streaming, sse and streaming-collection kinds are degraded to their closest non-reactive equivalent and reported through the IGenerationLog.
TypeScript is structurally typed and TypedRest for TypeScript has no endpoint interfaces, so GenerateInterfaces has no effect.
DTO properties keep the exact name used on the wire, because TypedRest for TypeScript deserializes with JSON.parse() and a cast and therefore has no way to map a property to a differently named field.
Extension points
GenerateTypedRestTypeScript takes an optional PatternRegistry controlling what is inferred, and an optional BuilderRegistry controlling what is emitted:
var files = doc.GenerateTypedRestTypeScript(options, log, patterns, builders);
Implement IBuilder<TEndpoint> to change the code emitted for an endpoint kind. A BuilderRegistry holds at most one builder per kind, so replacing a built-in one means composing the registry yourself rather than adding to BuilderRegistry.Default.
To change how types and properties are named, derive from NamingStrategy and pass it to GenerateTypedRestTypeScriptEndpoints.
API
Namespaces
Classes
- Messages
The GenerationMessages the TypeScript generator can report.
- Modules
The modules of the TypedRest runtime library that generated code imports from.
- NamingStrategy
The default INamingStrategy for TypeScript clients.
- OpenApiDocumentExtensions
Generates TypeScript TypedRest clients for OpenAPI/Swagger documents.
- TypeNameRegistry
Keeps track of the type names handed out during a generation run, so that no two generated types end up in the same file.
- TypeScriptClientGenerator
Generates the source code of a TypeScript TypedRest client.
- TypeScriptGenerationOptions
Options controlling the generation of a TypeScript TypedRest client.
Interfaces
- INamingStrategy
Decides what the types and members of a generated TypeScript client are called.