0
# AutoRest Generator
1
2
The AutoRest class is the main generator instance that orchestrates the entire code generation process. It provides event-driven architecture, configuration management, and coordinates with the processing pipeline to transform OpenAPI specifications into client libraries.
3
4
## Capabilities
5
6
### AutoRest Class
7
8
Main generator instance that manages the complete code generation workflow.
9
10
```typescript { .api }
11
/**
12
* An instance of the AutoRest generator that coordinates the entire code generation process
13
*/
14
class AutoRest {
15
/**
16
* Creates a new AutoRest instance
17
* @param fileSystem - The implementation of the filesystem to load and save files (defaults to RealFileSystem)
18
* @param configFileOrFolderUri - The URI of the configuration file or folder containing the configuration file
19
*/
20
constructor(fileSystem?: IFileSystem, configFileOrFolderUri?: string);
21
22
/**
23
* Gets the current configuration view (creates one if none exists)
24
*/
25
readonly view: Promise<ConfigurationView>;
26
27
/**
28
* Adds configuration to the current configuration stack
29
* @param configuration - Configuration object to add
30
*/
31
AddConfiguration(configuration: any): void;
32
33
/**
34
* Resets all configurations to empty state
35
*/
36
ResetConfiguration(): Promise<void>;
37
38
/**
39
* Starts the processing of OpenAPI specifications
40
* @returns Object with finish promise and cancel function
41
*/
42
Process(): { finish: Promise<boolean | Error>, cancel: () => void };
43
44
/**
45
* Regenerates the configuration view
46
* @param includeDefault - Whether to include default configuration
47
*/
48
RegenerateView(includeDefault?: boolean): Promise<ConfigurationView>;
49
50
/**
51
* Invalidates the current configuration view
52
*/
53
Invalidate(): void;
54
}
55
```
56
57
**Usage Examples:**
58
59
```typescript
60
import { AutoRest, RealFileSystem } from "@microsoft.azure/autorest-core";
61
62
// Create with default file system
63
const autorest = new AutoRest();
64
65
// Create with custom file system and config
66
const autorest2 = new AutoRest(
67
new RealFileSystem(),
68
"./my-config/autorest.json"
69
);
70
71
// Add configuration programmatically
72
autorest.AddConfiguration({
73
"input-file": ["swagger.json", "swagger2.json"],
74
"output-folder": "./generated",
75
"client-name": "MyServiceClient",
76
"namespace": "MyCompany.Services"
77
});
78
79
// Reset configuration
80
await autorest.ResetConfiguration();
81
82
// Get current configuration view
83
const view = await autorest.view;
84
```
85
86
### Event System
87
88
AutoRest provides event-driven communication for monitoring and handling the generation process.
89
90
```typescript { .api }
91
/**
92
* Event: Signals when a Process() finishes
93
*/
94
Finished: IEvent<AutoRest, boolean | Error>;
95
96
/**
97
* Event: Signals when a File is generated
98
*/
99
GeneratedFile: IEvent<AutoRest, Artifact>;
100
101
/**
102
* Event: Signals when a Folder is supposed to be cleared
103
*/
104
ClearFolder: IEvent<AutoRest, string>;
105
106
/**
107
* Event: Signals when a message is generated
108
*/
109
Message: IEvent<AutoRest, Message>;
110
```
111
112
**Usage Examples:**
113
114
```typescript
115
import { AutoRest, Channel } from "@microsoft.azure/autorest-core";
116
117
const autorest = new AutoRest();
118
119
// Listen for generated files
120
autorest.GeneratedFile.Subscribe((sender, artifact) => {
121
console.log(`Generated file: ${artifact.uri}`);
122
console.log(`File type: ${artifact.type}`);
123
console.log(`Content length: ${artifact.content.length}`);
124
});
125
126
// Listen for messages
127
autorest.Message.Subscribe((sender, message) => {
128
switch (message.Channel) {
129
case Channel.Error:
130
console.error(`Error: ${message.Text}`);
131
break;
132
case Channel.Warning:
133
console.warn(`Warning: ${message.Text}`);
134
break;
135
case Channel.Information:
136
console.log(`Info: ${message.Text}`);
137
break;
138
}
139
});
140
141
// Listen for folder clearing
142
autorest.ClearFolder.Subscribe((sender, folderPath) => {
143
console.log(`Clearing folder: ${folderPath}`);
144
});
145
146
// Listen for completion
147
autorest.Finished.Subscribe((sender, result) => {
148
if (result instanceof Error) {
149
console.error("Generation failed:", result);
150
} else {
151
console.log("Generation completed successfully");
152
}
153
});
154
```
155
156
### Process Control
157
158
Control the generation process with cancellation support.
159
160
```typescript { .api }
161
/**
162
* Result of starting a generation process
163
*/
164
interface ProcessResult {
165
/** Promise that resolves when processing completes */
166
finish: Promise<boolean | Error>;
167
/** Function to cancel the ongoing process */
168
cancel: () => void;
169
}
170
```
171
172
**Usage Examples:**
173
174
```typescript
175
const autorest = new AutoRest();
176
177
// Configure input
178
autorest.AddConfiguration({
179
"input-file": "large-api.json",
180
"output-folder": "./output"
181
});
182
183
// Start processing
184
const { finish, cancel } = autorest.Process();
185
186
// Set up timeout cancellation
187
const timeout = setTimeout(() => {
188
console.log("Cancelling due to timeout");
189
cancel();
190
}, 30000); // 30 second timeout
191
192
try {
193
const result = await finish;
194
clearTimeout(timeout);
195
196
if (result instanceof Error) {
197
console.error("Generation failed:", result.message);
198
} else {
199
console.log("Generation successful");
200
}
201
} catch (error) {
202
console.error("Process error:", error);
203
}
204
```
205
206
## Types
207
208
```typescript { .api }
209
interface IEvent<TSender, TArgs> {
210
Subscribe(fn: (sender: TSender, args: TArgs) => void): void;
211
Unsubscribe(fn: (sender: TSender, args: TArgs) => void): void;
212
Dispatch(args: TArgs): void;
213
}
214
```