Skip to content

04Reference

Programmatic API

generate(), validate(), loadConfig(), defineConfig() and their types, read from the published declarations.

The CLI is a thin layer over these functions. Use them to generate from a build script, a test or another tool:

scripts/generate.ts
import { formatDiagnostic, generate } from 'swaggerz';
// Loads swaggerz.config.* from the working directory, like the CLI.
const result = await generate({ cwd: '.' });
for (const diagnostic of result.diagnostics) console.log(formatDiagnostic(diagnostic));
if (!result.ok) throw new Error('Generation failed; nothing was written.');
console.log(`${result.write?.created.length ?? 0} files created`);

Everything below is exported from swaggerz and follows semantic versioning, except the types of the intermediate representation and the project model, which areexperimental until 1.0 and may change in minor releases.

Functions

defineConfig

Declares a configuration with full type checking and editor autocomplete.

function defineConfig(config: UserConfig): UserConfig;
function defineConfig(config: (env: ConfigEnv) => UserConfig | Promise<UserConfig>): (env: ConfigEnv) => UserConfig | Promise<UserConfig>;
Example
import { defineConfig } from 'swaggerz';
export default defineConfig({
spec: { version: '3.1', input: './openapi.yaml' },
output: { directory: './src/generated/api' },
});

definePlugin

Declares a plugin with full type checking.

function definePlugin(plugin: SwaggerzPlugin): SwaggerzPlugin;

formatDiagnostic

Formats a diagnostic for terminals: severity, code, message, location, context and hint. Control characters in the diagnostic's text are escaped (see printable).

function formatDiagnostic(diagnostic: Diagnostic, options?: FormatOptions): string;

generate

Loads configuration, compiles the specification and writes the generated client. Throws SwaggerzError when the configuration cannot be loaded; all other problems are returned as diagnostics with ok: false.

function generate(options?: GenerateOptions): Promise<GenerationResult>;

loadConfig

Loads, merges, validates and resolves configuration. Throws SwaggerzError on failure.

function loadConfig(options: LoadConfigOptions): Promise<LoadedConfig>;

validate

Validates the configuration and specification and generates in memory without writing.

function validate(options?: ApiOptions): Promise<CompileResult>;

Classes

SwaggerzError

Thrown when compilation cannot continue. Carries the diagnostics collected so far.

class SwaggerzError extends Error {
readonly diagnostics: readonly Diagnostic[];
constructor(message: string, diagnostics?: readonly Diagnostic[]);
}

Configuration and result types

Type aliases

Experimental: the IR and the project model

Plugins receive these in their hook contexts. They are listed here so plugin authors can find them; their shapes are in dist/ir/types.d.ts anddist/analysis/types.d.ts of the package.

  • isStandardMethod
  • wireMethod
  • HTTP_METHODS
  • IR_VERSION
  • ApiDocument
  • ApiInfo
  • ArrayConstraints
  • ArraySchema
  • BinarySchema
  • BooleanSchema
  • DiscriminatorIR
  • EncodingIR
  • EnumSchema
  • ExampleIR
  • ExternalDocsIR
  • HeaderIR
  • IntersectionSchema
  • MediaTypeIR
  • ModelSymbol
  • NeverSchema
  • NullSchema
  • NumberConstraints
  • NumberSchema
  • OAuthFlowIR
  • OAuthFlowsIR
  • ObjectConstraints
  • ObjectSchema
  • OperationIR
  • OperationSymbol
  • ParameterIR
  • ProjectModel
  • PropertyIR
  • RefSchema
  • RequestBodyIR
  • ResponseIR
  • SchemaAssertions
  • SchemaDefinition
  • SchemaMeta
  • ServerIR
  • SourceRef
  • StringConstraints
  • StringSchema
  • TagIR
  • UnionSchema
  • UnknownSchema
  • WebhookIR
  • Extensions
  • HttpMethod
  • JsonPrimitive
  • JsonValue
  • ParameterLocation
  • ParameterStyle
  • SchemaIR
  • SchemaKind
  • SecurityRequirementIR
  • SecuritySchemeIR
  • SpecVersion
  • StandardHttpMethod