AutoRest Core provides comprehensive document processing capabilities for OpenAPI and Swagger specifications, including document identification, format detection, and content transformation.
Functions for identifying and categorizing different types of input documents.
/**
* Document types supported by AutoRest
* Used for routing documents to appropriate processors
*/
enum DocumentType {
/** OpenAPI 2.0 (Swagger) specification */
OpenAPI2 = "OpenAPI2",
/** OpenAPI 3.0+ specification */
OpenAPI3 = "OpenAPI3",
/** AutoRest literate configuration document */
LiterateConfiguration = "LiterateConfiguration",
/** Unknown or unsupported document type */
Unknown = "Unknown"
}
/**
* Identify document type from content analysis
* Examines document structure to determine specification type
* @param content - Document content as string
* @returns Promise resolving to detected DocumentType
*/
async function IdentifyDocument(content: string): Promise<DocumentType>;Usage Examples:
import { IdentifyDocument, DocumentType } from "@autorest/core";
// Identify OpenAPI specification
const swaggerContent = `{
"swagger": "2.0",
"info": { "title": "Pet Store", "version": "1.0" }
}`;
const docType = await IdentifyDocument(swaggerContent);
console.log(docType); // DocumentType.OpenAPI2
// Identify OpenAPI 3.0
const openApiContent = `{
"openapi": "3.0.0",
"info": { "title": "Pet Store", "version": "1.0" }
}`;
const docType3 = await IdentifyDocument(openApiContent);
console.log(docType3); // DocumentType.OpenAPI3
// Identify configuration document
const configContent = `
# AutoRest Configuration
> see https://aka.ms/autorest
\`\`\`yaml
input-file: swagger.json
output-folder: ./generated
\`\`\`
`;
const configType = await IdentifyDocument(configContent);
console.log(configType); // DocumentType.LiterateConfigurationUtilities for determining document formats and supported file extensions.
/**
* Supported document formats for input processing
*/
enum DocumentFormat {
/** Markdown format (including literate configuration) */
Markdown = "markdown",
/** YAML format for OpenAPI specifications */
Yaml = "yaml",
/** JSON format for OpenAPI specifications */
Json = "json",
/** Unknown or unsupported format */
Unknown = "unknown"
}
/**
* Maps file extensions to document formats
* Used for initial format detection before content analysis
*/
const DocumentExtension: {
yaml: DocumentFormat.Yaml;
yml: DocumentFormat.Yaml;
json: DocumentFormat.Json;
md: DocumentFormat.Markdown;
markdown: DocumentFormat.Markdown;
};
/**
* File patterns for different document types
* Useful for glob matching and file discovery
*/
const DocumentPatterns: {
yaml: ["*.yaml", "*.yml"];
json: ["*.json"];
markdown: ["*.markdown", "*.md"];
all: string[]; // Combined array of all patterns
};Functions for validating OpenAPI documents and supported file types.
/**
* Check if document content represents an OpenAPI specification
* Examines document structure for OpenAPI/Swagger indicators
* @param content - Document content to analyze
* @returns Promise resolving to true if OpenAPI document
*/
async function IsOpenApiDocument(content: string): Promise<boolean>;
/**
* Check if file extension is supported for OpenAPI documents
* @param extension - File extension (without dot)
* @returns Promise resolving to true if supported OpenAPI extension
*/
async function IsOpenApiExtension(extension: string): Promise<boolean>;
/**
* Check if file extension is for configuration documents
* @param extension - File extension (without dot)
* @returns Promise resolving to true if supported config extension
*/
async function IsConfigurationExtension(extension: string): Promise<boolean>;Validation Examples:
import {
IsOpenApiDocument,
IsOpenApiExtension,
IsConfigurationExtension
} from "@autorest/core";
// Validate OpenAPI content
const apiSpec = `{
"swagger": "2.0",
"info": { "title": "API", "version": "1.0" },
"paths": {}
}`;
const isOpenApi = await IsOpenApiDocument(apiSpec);
console.log(isOpenApi); // true
// Check file extensions
const yamlSupported = await IsOpenApiExtension("yaml");
console.log(yamlSupported); // true
const mdConfig = await IsConfigurationExtension("md");
console.log(mdConfig); // true
const unsupported = await IsOpenApiExtension("txt");
console.log(unsupported); // falseContent transformation utilities for processing different document formats.
/**
* Convert literate configuration document to JSON format
* Extracts YAML/JSON code blocks from markdown and processes them
* @param content - Literate document content (markdown with code blocks)
* @returns Promise resolving to JSON string representation
*/
async function LiterateToJson(content: string): Promise<string>;Transformation Example:
import { LiterateToJson } from "@autorest/core";
const literateConfig = `
# My AutoRest Configuration
This configuration generates a C# client.
\`\`\`yaml
input-file: https://petstore.swagger.io/v2/swagger.json
output-folder: ./generated
csharp:
namespace: PetStore.Client
client-name: PetStoreClient
\`\`\`
Additional notes about the configuration...
`;
const jsonConfig = await LiterateToJson(literateConfig);
console.log(JSON.parse(jsonConfig));
// {
// "input-file": "https://petstore.swagger.io/v2/swagger.json",
// "output-folder": "./generated",
// "csharp": {
// "namespace": "PetStore.Client",
// "client-name": "PetStoreClient"
// }
// }Typical workflow for processing input documents:
import {
IdentifyDocument,
IsOpenApiDocument,
DocumentType,
LiterateToJson
} from "@autorest/core";
async function processDocument(content: string, filename: string) {
// Step 1: Identify document type
const docType = await IdentifyDocument(content);
switch (docType) {
case DocumentType.OpenAPI2:
console.log("Processing Swagger 2.0 specification");
// Handle OpenAPI 2.0 specific processing
break;
case DocumentType.OpenAPI3:
console.log("Processing OpenAPI 3.0+ specification");
// Handle OpenAPI 3.0+ specific processing
break;
case DocumentType.LiterateConfiguration:
console.log("Processing literate configuration");
// Convert to JSON and process as configuration
const jsonConfig = await LiterateToJson(content);
console.log("Extracted configuration:", jsonConfig);
break;
case DocumentType.Unknown:
console.warn(`Unknown document type: ${filename}`);
// Attempt additional validation
const mightBeOpenApi = await IsOpenApiDocument(content);
if (mightBeOpenApi) {
console.log("Content appears to be OpenAPI, processing anyway");
}
break;
}
}
// Process multiple documents
const documents = [
{ content: swaggerContent, filename: "swagger.json" },
{ content: configContent, filename: "readme.md" }
];
for (const doc of documents) {
await processDocument(doc.content, doc.filename);
}