0
# AutoRest Processing
1
2
Core code generation orchestration using the AutoRest class. This is the primary interface for programmatically running the AutoRest pipeline, managing configuration, and handling the complete workflow from OpenAPI specification loading to code generation.
3
4
## Capabilities
5
6
### AutoRest Class
7
8
The main orchestrator class that manages the entire AutoRest code generation pipeline with comprehensive event handling and configuration management.
9
10
```typescript { .api }
11
/**
12
* An instance of the AutoRest generator
13
* Main class for orchestrating code generation from OpenAPI specifications
14
*/
15
class AutoRest extends EventEmitter {
16
/**
17
* Creates a new AutoRest instance
18
* @param fileSystem - File system implementation for reading/writing files
19
* @param configFileOrFolderUri - URI of configuration file or folder
20
*/
21
constructor(fileSystem?: IFileSystem, configFileOrFolderUri?: string);
22
23
/** Current configuration view */
24
get view(): Promise<ConfigurationView>;
25
26
/**
27
* Add configuration to the current instance
28
* @param configuration - Configuration object to merge
29
*/
30
AddConfiguration(configuration: any): void;
31
32
/**
33
* Reset all configurations
34
*/
35
ResetConfiguration(): Promise<void>;
36
37
/**
38
* Start processing the configured inputs
39
* @returns Object with finish promise and cancel function
40
*/
41
Process(): { finish: Promise<boolean | Error>, cancel: () => void };
42
43
/**
44
* Regenerate the configuration view
45
* @param includeDefault - Whether to include default configuration
46
*/
47
RegenerateView(includeDefault?: boolean): Promise<ConfigurationView>;
48
49
/**
50
* Invalidate the current configuration view
51
*/
52
Invalidate(): void;
53
}
54
```
55
56
### AutoRest Events
57
58
The AutoRest class provides several events for monitoring the generation process.
59
60
```typescript { .api }
61
interface AutoRest {
62
/** Event: Signals when Process() finishes */
63
Finished: IEvent<AutoRest, boolean | Error>;
64
65
/** Event: Signals when a file is generated */
66
GeneratedFile: IEvent<AutoRest, Artifact>;
67
68
/** Event: Signals when a folder should be cleared */
69
ClearFolder: IEvent<AutoRest, string>;
70
71
/** Event: Signals when a message is generated */
72
Message: IEvent<AutoRest, Message>;
73
}
74
75
interface IEvent<TSource, TArgs> {
76
Subscribe(fn: (source: TSource, args: TArgs) => void): void;
77
Dispatch(args: TArgs): void;
78
}
79
```
80
81
**Usage Examples:**
82
83
```typescript
84
import { AutoRest, Channel } from "@microsoft.azure/autorest-core";
85
86
// Create AutoRest instance
87
const autorest = new AutoRest();
88
89
// Set up event handlers
90
autorest.Message.Subscribe((_, message) => {
91
switch (message.Channel) {
92
case Channel.Error:
93
case Channel.Fatal:
94
console.error(`ERROR: ${message.Text}`);
95
break;
96
case Channel.Warning:
97
console.warn(`WARNING: ${message.Text}`);
98
break;
99
case Channel.Information:
100
console.log(`INFO: ${message.Text}`);
101
break;
102
case Channel.Debug:
103
if (process.env.DEBUG) {
104
console.debug(`DEBUG: ${message.Text}`);
105
}
106
break;
107
}
108
});
109
110
autorest.GeneratedFile.Subscribe((_, artifact) => {
111
console.log(`Generated file: ${artifact.uri} (${artifact.type})`);
112
});
113
114
autorest.ClearFolder.Subscribe((_, folderPath) => {
115
console.log(`Clearing folder: ${folderPath}`);
116
});
117
118
autorest.Finished.Subscribe((_, result) => {
119
if (result === true) {
120
console.log("AutoRest processing completed successfully");
121
} else {
122
console.error("AutoRest processing failed:", result);
123
}
124
});
125
126
// Configure and run
127
autorest.AddConfiguration({
128
"input-file": "https://petstore.swagger.io/v2/swagger.json",
129
"output-folder": "./generated",
130
"namespace": "PetStore.Client",
131
"client-name": "PetStoreClient"
132
});
133
134
const { finish, cancel } = autorest.Process();
135
136
// Optionally set up cancellation
137
setTimeout(() => {
138
console.log("Cancelling AutoRest processing...");
139
cancel();
140
}, 30000); // Cancel after 30 seconds
141
142
// Wait for completion
143
try {
144
const result = await finish;
145
console.log("Final result:", result);
146
} catch (error) {
147
console.error("Processing error:", error);
148
}
149
```
150
151
### Configuration Integration
152
153
```typescript
154
// Adding multiple configuration layers
155
autorest.AddConfiguration({
156
"input-file": ["api1.json", "api2.json"],
157
"output-folder": "./generated"
158
});
159
160
autorest.AddConfiguration({
161
"client-name": "MyApiClient",
162
"namespace": "MyCompany.ApiClient"
163
});
164
165
// Access merged configuration
166
const config = await autorest.view;
167
console.log("Input files:", config.InputFileUris);
168
console.log("Raw config:", config.Raw);
169
```
170
171
### Configuration View Access
172
173
```typescript
174
// Access current configuration
175
const configView = await autorest.view;
176
177
// Check specific configuration values
178
const outputFolder = configView.GetEntry("output-folder");
179
const inputFiles = configView.InputFileUris;
180
const debugMode = configView.GetEntry("debug");
181
182
// Access raw configuration object
183
const rawConfig = configView.Raw;
184
```
185
186
### Error Handling
187
188
```typescript
189
const autorest = new AutoRest();
190
191
autorest.Message.Subscribe((_, message) => {
192
if (message.Channel === Channel.Fatal) {
193
console.error("Fatal error:", message.Text);
194
// Handle fatal errors
195
}
196
});
197
198
const { finish } = autorest.Process();
199
200
try {
201
const result = await finish;
202
203
if (result instanceof Error) {
204
console.error("AutoRest failed with error:", result.message);
205
} else if (result === false) {
206
console.error("AutoRest was cancelled or failed");
207
} else {
208
console.log("AutoRest completed successfully");
209
}
210
} catch (error) {
211
console.error("Unexpected error:", error);
212
}
213
```