AutoRest Core is the central orchestration engine for AutoRest, a tool that generates client libraries for accessing RESTful web services from OpenAPI specifications. It provides the complete pipeline infrastructure for processing OpenAPI/Swagger documents, managing extensions, and coordinating code generation across multiple target languages.
npm install @autorest/coreimport {
AutoRest,
AutorestContext,
Message,
Channel,
Artifact,
IdentifyDocument,
IsOpenApiDocument,
IsOpenApiExtension,
IsConfigurationExtension,
LiterateToJson,
Shutdown,
DocumentType,
DocumentFormat,
IFileSystem
} from "@autorest/core";For CommonJS:
const {
AutoRest,
AutorestContext,
Message,
Channel,
Artifact,
IdentifyDocument,
IsOpenApiDocument,
IsOpenApiExtension,
IsConfigurationExtension,
LiterateToJson,
Shutdown,
DocumentType,
DocumentFormat,
IFileSystem
} = require("@autorest/core");import { AutoRest } from "@autorest/core";
import { RealFileSystem } from "@azure-tools/datastore";
// Create AutoRest instance
const autorest = new AutoRest(
(message) => console.log(message), // Logger sink
new RealFileSystem()
);
// Add configuration
autorest.AddConfiguration({
"input-file": "path/to/swagger.json",
"output-folder": "./generated",
"csharp": {}
});
// Process the OpenAPI specification
const result = autorest.Process();
// Wait for completion
const success = await result.finish;
console.log("Generation complete:", success);AutoRest Core is built around several key architectural components:
AutoRest Core provides two command-line interfaces:
Central AutoRest class that orchestrates the entire code generation pipeline, handling configuration management and plugin execution.
class AutoRest extends EventEmitter {
constructor(
loggerSink: LoggerSink,
fileSystem?: IFileSystem,
configFileOrFolderUri?: string
);
AddConfiguration(configuration: any): void;
ResetConfiguration(): Promise<void>;
Process(): { finish: Promise<boolean | Error>; cancel(): void };
RegenerateView(includeDefault?: boolean): Promise<AutorestContext>;
Invalidate(): void;
readonly view: Promise<AutorestContext>;
}
type LoggerSink = (message: Message) => void;Comprehensive messaging system for structured communication between pipeline components, with typed message channels and source location tracking.
enum Channel {
Information = "information",
Warning = "warning",
Error = "error",
Debug = "debug",
Verbose = "verbose",
Fatal = "fatal",
Hint = "hint",
File = "file",
Configuration = "configuration",
Protect = "protect",
Control = "Control"
}
interface Message {
Channel: Channel;
Key?: Iterable<string>;
Details?: any;
Text: string;
Source?: Array<SourceLocation>;
Plugin?: string;
FormattedMessage?: string;
}OpenAPI and Swagger document identification, parsing, and type detection with support for multiple document formats.
enum DocumentType {
OpenAPI2 = "OpenAPI2",
OpenAPI3 = "OpenAPI3",
LiterateConfiguration = "LiterateConfiguration",
Unknown = "Unknown"
}
async function IdentifyDocument(content: string): Promise<DocumentType>;
async function IsOpenApiDocument(content: string): Promise<boolean>;
async function IsOpenApiExtension(extension: string): Promise<boolean>;
async function IsConfigurationExtension(extension: string): Promise<boolean>;
async function LiterateToJson(content: string): Promise<string>;Extensible plugin architecture with built-in plugins for common operations like validation, transformation, and code generation.
type PipelinePlugin = (
config: AutorestContext,
input: DataSource,
sink: DataSink
) => Promise<DataSource>;
interface PipelinePluginDefinition {
readonly name: string;
readonly plugin: PipelinePlugin;
readonly extension: AutoRestExtension | undefined;
readonly builtIn: boolean;
}
async function runPipeline(configView: AutorestContext, fileSystem: IFileSystem): Promise<void>;
async function loadPlugins(context: AutorestContext): Promise<{ [pluginName: string]: PipelinePluginDefinition }>;Context-aware configuration system with hierarchical merging, extension support, and nested configuration scoping.
class AutorestContext {
constructor(
config: AutorestConfiguration,
fileSystem: CachingFileSystem,
messageEmitter: MessageEmitter,
logger: AutorestLogger,
stats: StatsCollector,
plugin?: PipelinePluginDefinition
);
GetEntry(key: string): any;
IsOutputArtifactRequested(artifact: string): boolean;
getNestedConfiguration(scope: string, plugin?: PipelinePluginDefinition): Iterable<AutorestContext>;
extendWith(...overrides: AutorestNormalizedConfiguration[]): AutorestContext;
updateConfigurationFile(filename: string, content: string): void;
resolveDirectives(predicate?: (each: ResolvedDirective) => boolean): void;
readonly config: AutorestConfiguration;
readonly fileSystem: CachingFileSystem;
readonly logger: AutorestLogger;
readonly pluginName: string;
}interface Artifact {
uri: string;
type: string;
content: string;
}
interface SourceLocation {
document: string;
Position: EnhancedPosition;
}
interface ArtifactMessage extends Message {
Details: Artifact & { sourceMap?: Array<Mapping> | RawSourceMap };
}
enum DocumentFormat {
Markdown = "markdown",
Yaml = "yaml",
Json = "json",
Unknown = "unknown"
}/**
* Shut down active AutoRest extension processes
* Call this when your application is terminating to clean up resources
*/
async function Shutdown(): Promise<void>;interface IFileSystem {
// Re-exported from @azure-tools/datastore
// Full interface available in that package
}