import type { ImportNameArg } from '../dsl/Import.js'; import { Definition } from '../dsl/Definition.js'; import type { OasDocument } from '../oas/document/Document.js'; import type { OasSchema } from '../oas/schema/Schema.js'; import type { OasRef } from '../oas/ref/Ref.js'; import type { ClientSettings } from '../types/Settings.js'; import type { Method } from '../types/Method.js'; import type { OperationInsertable } from '../dsl/operation/types.js'; import type { OasOperation } from '../oas/operation/Operation.js'; import type { ModelInsertable } from '../dsl/model/types.js'; import type { GenerationType, GeneratedValue } from '../types/GeneratedValue.js'; import { ContentSettings } from '../dsl/ContentSettings.js'; import type { RefName } from '../types/RefName.js'; import type * as log from '../../deps/jsr.io/@std/log/0.224.14/mod.js'; import type { Logger } from '../types/Logger.js'; import type { ResultType } from '../types/Results.js'; import type { StackTrail } from './StackTrail.js'; import type { Identifier } from '../dsl/Identifier.js'; import type { SchemaToNonRef, SchemaToValueFn, SchemaType, TypeSystemOutput } from '../types/TypeSystem.js'; import { Inserted } from '../dsl/Inserted.js'; import { File } from '../dsl/File.js'; import { JsonFile } from '../dsl/JsonFile.js'; import type { GeneratorsMapContainer } from '../types/GeneratorType.js'; import type { OperationSource, ModelSource, Preview, Mapping } from '../types/Preview.js'; import type { OasVoid } from '../oas/void/Void.js'; type ConstructorArgs = { oasDocument: OasDocument; settings: ClientSettings | undefined; logger: log.Logger; stackTrail: StackTrail; captureCurrentResult: (result: ResultType) => void; toGeneratorConfigMap: () => GeneratorsMapContainer; }; /** * Arguments for picking a specific export from a generator module. * * Used to select and configure specific exports from generator modules * during the artifact generation process. */ export type PickArgs = { /** The name of the export to pick from the generator module */ name: string; /** The file path where the export should be made available */ exportPath: string; }; /** * Arguments for registering a JSON file in the generation context. * * Used to register JSON configuration files, manifests, or other JSON * data that should be included in the generated output artifacts. */ export type RegisterJsonArgs = { /** The destination file path where the JSON should be written */ destinationPath: string; /** The JSON object to write to the file */ json: Record; }; /** * Arguments for applying package imports to a generated file. * * Used to configure import statements and dependencies when generating * code files that need to reference external packages or modules. */ export type ApplyPackageImportsArgs = { /** The destination file path where imports should be applied */ destinationPath: string; /** The export path for the module being imported */ exportPath: string; }; /** * Base arguments for registering generated content in the generation context. * * Provides the fundamental configuration options for registering imports, * re-exports, and definitions that will be included in generated files. */ export type BaseRegisterArgs = { /** Import statements to include, organized by module path */ imports?: Record; /** Re-export statements to include, organized by module path */ reExports?: Record; /** Definition objects to include in the generated content */ definitions?: (Definition | undefined)[]; }; /** * Arguments for registering generated content with a specific destination. * * Extends BaseRegisterArgs to include a destination path, allowing content * to be registered and associated with a specific output file location. */ export type RegisterArgs = { /** Import statements to include, organized by module path */ imports?: Record; /** Re-export statements to include, organized by module path */ reExports?: Record; /** Definition objects to include in the generated content */ definitions?: (Definition | undefined)[]; /** The destination file path where the content should be registered */ destinationPath: string; }; /** * Arguments for creating and registering a definition from a schema. * * Used to transform OpenAPI schema objects into code definitions and * register them in the generation context for output file creation. * * @template Schema - The schema type extending SchemaType */ export type CreateAndRegisterDefinition = { /** The OpenAPI schema to transform into a definition */ schema: Schema; /** The identifier for the generated definition */ identifier: Identifier; /** The destination file path where the definition should be registered */ destinationPath: string; /** Function to transform the schema into a generated value */ schemaToValueFn: SchemaToValueFn; /** Optional root reference name for the schema */ rootRef?: RefName; /** Whether to exclude this definition from exports */ noExport?: boolean; }; /** * Arguments for defining and registering a value in the generation context. * * Used to create definitions from pre-generated values and register them * in the generation context for inclusion in output files. * * @template V - The generated value type extending GeneratedValue */ export type DefineAndRegisterArgs = { /** The identifier for the definition */ identifier: Identifier; /** The generated value to define */ value: V; /** The destination file path where the definition should be registered */ destinationPath: string; /** Whether to exclude this definition from exports */ noExport?: boolean; }; /** * Arguments for retrieving operation-specific settings. * * Used to get generator-specific configuration for a particular * OpenAPI operation based on its path and HTTP method. */ export type GetOperationSettingsArgs = { /** The ID of the generator requesting settings */ generatorId: string; /** The API path for the operation */ path: string; /** The HTTP method for the operation */ method: Method; }; /** * Arguments for adding render dependencies for an operation. * * Used to specify additional dependencies that should be included * when rendering code for a specific OpenAPI operation. */ export type AddRenderDependencyArgs = { /** The ID of the generator adding dependencies */ generatorId: string; /** The OpenAPI operation requiring dependencies */ operation: OasOperation; /** Array of dependency names or paths to include */ dependencies: string[]; }; /** * Arguments for retrieving model-specific settings. * * Used to get generator-specific configuration for a particular * OpenAPI model based on its reference name. */ export type ToModelSettingsArgs = { /** The ID of the generator requesting model settings */ generatorId: string; /** The reference name of the model */ refName: RefName; }; /** * Options for inserting an operation into the generation context. * * Configures how an OpenAPI operation should be processed and * included in the generated code output. * * @template T - The generation type extending GenerationType */ export type InsertOperationOptions = { /** Whether to exclude this operation from exports */ noExport?: boolean; /** The type of generation to apply */ generation?: T; /** Custom destination path for the operation */ destinationPath?: string; }; /** * Arguments for inserting a normalized model into the generation context. * * Used to process and register OpenAPI schema objects as normalized * model definitions with fallback naming when schema names are unavailable. * * @template Schema - The schema type (OasSchema, OasRef, or OasVoid) */ export type InsertNormalisedModelArgs | OasVoid> = { /** Fallback name to use if the schema doesn't have a name */ fallbackName: string; /** The OpenAPI schema to normalize and insert */ schema: Schema; /** The destination file path for the model */ destinationPath: string; }; /** * Options for inserting a normalized model. * * Configures how a normalized model should be processed and * included in the generated code output. */ export type InsertNormalisedModelOptions = { /** Whether to exclude this model from exports */ noExport?: boolean; }; /** * Return type for inserting a normalized model. * * Provides type-safe return values based on the schema type being processed. * Returns different Definition types depending on whether the schema is a * reference or a concrete schema. * * @template V - The generated value type * @template Schema - The schema type being processed */ export type InsertNormalisedModelReturn | OasVoid> = Schema extends OasRef<'schema'> ? Definition : Definition['type']>>; /** * Options for inserting a model into the generation context. * * Configures how a model should be processed and included in * the generated code output. * * @template T - The generation type extending GenerationType */ export type InsertModelOptions = { /** Whether to exclude this model from exports */ noExport?: boolean; /** The type of generation to apply */ generation?: T; /** Custom destination path for the model */ destinationPath?: string; }; /** * Return type for insert operations in the generation context. * * Represents the result of inserting content into the generation * context, providing type-safe access to the inserted content. * * @template V - The generated value type * @template T - The generation type * @template EnrichmentType - The enrichment data type */ export type InsertReturn = Inserted; /** * Arguments for generating operation content settings. * * @template V - The value type for the operation * @template EnrichmentType - Optional enrichment type for the operation */ export type ToOperationSettingsArgs = { operation: OasOperation; insertable: OperationInsertable; }; /** * Arguments for building model content settings. * * @template V - The value type for the model * @template EnrichmentType - Optional enrichment type for the model */ export type BuildModelSettingsArgs = { refName: RefName; insertable: ModelInsertable; }; type GenerateResult = { files: Map; previews: Record>; mappings: Record>; }; /** * The generation context for the second phase of the SKMTC transformation pipeline. * * `GenerateContext` manages the transformation of parsed OAS (OpenAPI Schema) objects * into code artifacts using pluggable generators. It provides APIs for model and operation * generation, file management, dependency tracking, and artifact registration. * * ## Key Responsibilities * * - **Generator Orchestration**: Executes pluggable model and operation generators * - **Schema Processing**: Provides utilities for working with OAS schemas and references * - **File Management**: Handles file creation, imports, exports, and dependencies * - **Artifact Registration**: Collects generated definitions and files for rendering * - **Type System Integration**: Bridges OAS types with generator-specific type systems * - **Settings Management**: Handles skipping logic and client customizations * * ## Generator Integration * * The context works with two main types of generators: * - **Model Generators**: Transform schema definitions into type definitions * - **Operation Generators**: Transform API operations into client functions * * @example Basic usage in a model generator * ```typescript * import { ModelBase } from '@skmtc/core'; * * class TypeScriptInterface extends ModelBase { * generate(): Definition { * const schema = this.context.getSchema(this.refName); * * return new Definition({ * context: this.context, * identifier: Identifier.createType(this.refName), * description: schema.description, * value: { * generatorKey: this.generatorKey, * content: this.generateInterfaceBody(schema) * } * }); * } * } * ``` * * @example Operation generator usage * ```typescript * class ApiClientGenerator extends OperationBase { * generate(): Definition { * const operation = this.context.getOperation(this.path, this.method); * * const functionName = this.context.createOperationName(this.path, this.method); * * return new Definition({ * context: this.context, * identifier: Identifier.createVariable(functionName), * value: { * generatorKey: this.generatorKey, * content: this.generateClientFunction(operation) * } * }); * } * } * ``` * * @example Schema and type system integration * ```typescript * class MyGenerator extends ModelBase { * generate(): Definition { * const schema = this.context.getSchema(this.refName); * * // Transform schema using type system * const typeOutput = this.context.transformSchema(schema, { * stringType: 'string', * numberType: 'number', * arrayType: (items) => `Array<${items}>` * }); * * // Register dependencies * if (schema.hasReferences()) { * this.context.addImportsToFile('./models/types.ts', { * './common': ['BaseModel'] * }); * } * * return new Definition({ * context: this.context, * identifier: Identifier.createType(this.refName), * value: { * generatorKey: this.generatorKey, * content: typeOutput.content * } * }); * } * } * ``` */ export declare class GenerateContext { #private; /** The parsed OpenAPI document being processed */ oasDocument: OasDocument; /** Client settings for customization (optional) */ settings: ClientSettings | undefined; /** Logger instance for tracking generation progress */ logger: Logger; /** Function to capture processing results at current stack position */ captureCurrentResult: (result: ResultType) => void; /** Function that returns the generator configuration map */ toGeneratorConfigMap: () => GeneratorsMapContainer; /** Stack trail for tracking current processing context */ stackTrail: StackTrail; /** Tracking model nesting depth to prevent infinite recursion */ modelDepth: Record; /** * Creates a new GenerateContext instance for the generation phase. * * @param args - Constructor arguments including document, settings, and handlers */ constructor({ oasDocument, settings, logger, captureCurrentResult, stackTrail, toGeneratorConfigMap }: ConstructorArgs); /** * @internal */ toArtifacts(): GenerateResult; /** * Executes a function within a traced context for debugging and monitoring. * * @param token - Trace identifier or path segments * @param fn - Function to execute within the trace context * @returns The result of the traced function execution */ trace(token: string | string[], fn: () => T): T; /** * Create and register a definition with the given `identifier` at `destinationPath`. * * @experimental */ defineAndRegister({ identifier, value, destinationPath, noExport }: DefineAndRegisterArgs): Definition; /** * Registers JSON content for output to a file. * * @experimental This method is experimental and may change in future versions * @param args - Registration arguments with destination path and JSON content */ registerJson({ destinationPath, json }: RegisterJsonArgs): void; /** * Insert supplied `imports` and `definitions` into file at `destinationPath`. * * If an import from a specified module already exists in the file, the * import names are appended to the existing import. * * Definitions will only be added if there is not already a definition with * the same name in the file. * * @mutates this.files */ register({ imports, definitions, destinationPath, reExports }: RegisterArgs): void; /** * Insert operation into the output file with path `destinationPath`. * * Insert will perform the following steps: * 1. Generate content settings for the supplied operation * 2. Look up definition in file with path `destinationPath` * 3. If definition is not found, it will create a new one and register it * 4. If the definition is defined at a location that is different from * the current file, it will add an import to the current file from * that location * 5. Use the content settings to generate the operation using the * insertable's driver * @mutates this.files */ insertOperation(insertable: OperationInsertable, operation: OasOperation, { generation, destinationPath, noExport }?: InsertOperationOptions): Inserted; /** * Inserts a normalized model definition into the generation context. * * @param insertable - Model insertable configuration with prototype and transform functions * @param schema - OAS schema, reference, or void type to generate model from * @param options - Insertion options including generation type and destination * @returns Inserted model instance with settings and definition */ insertNormalisedModel | OasVoid, EnrichmentType>(insertable: ModelInsertable, { schema, fallbackName, destinationPath }: InsertNormalisedModelArgs, { noExport }?: InsertNormalisedModelOptions): InsertNormalisedModelReturn; /** * Insert model into the output file with path `destinationPath`. * * Insert will perform the following steps: * 1. Generate content settings for the supplied model * 2. Look up definition in file with path `destinationPath` * 3. If definition is not found, it will create a new one and register it * 4. If the definition is defined at a location that is different from * the current file, it will add an import to the current file from * that location * 5. Use the content settings to generate the model using the * insertable's driver * @mutates this.files */ insertModel(insertable: ModelInsertable, refName: RefName, { generation, destinationPath, noExport }?: InsertModelOptions): Inserted; /** * Generate and return content settings for operation insertable and * operation. * * Content settings are produced by passing base settings and operation * through toIdentifier and toExportPath static methods on the * insertable. * @param { operation, insertable } * @returns */ toOperationContentSettings({ operation, insertable }: ToOperationSettingsArgs): ContentSettings; /** * Generate and return content settings for model insertable and refName. * * Content settings are produced by passing base settings and refName * through toIdentifier and toExportPath static methods on the * insertable. * @param { refName, insertable } * @returns Content settings for model */ toModelContentSettings({ refName, insertable }: BuildModelSettingsArgs): ContentSettings; /** * Perform one lookup of schema by `refName`. * @param refName * @returns Matching schema or ref * @throws if schema is not found */ resolveSchemaRefOnce(refName: RefName, generatorId: string): OasSchema | OasRef<'schema'>; /** * Check if definition name `name` in file with path `exportPath` * has already been created and registered. * * @param { name, exportPath } * @returns Matching definition if found or `undefined` otherwise */ findDefinition({ name, exportPath }: PickArgs): Definition | undefined; } type ToOperationSourceArgs = { operation: OasOperation; generatorId: string; }; /** * Creates an OperationSource from an operation and generator ID. * * Transforms operation and generator information into a source descriptor * that can be used for tracking operation origins in the generation pipeline. * * @param args - Arguments containing operation and generator ID * @returns OperationSource descriptor for the operation */ export declare const toOperationSource: ({ operation, generatorId }: ToOperationSourceArgs) => OperationSource; type ToModelSourceArgs = { refName: RefName; generatorId: string; }; /** * Creates a ModelSource from a reference name and generator ID. * * Transforms model reference and generator information into a source descriptor * that can be used for tracking model origins in the generation pipeline. * * @param args - Arguments containing reference name and generator ID * @returns ModelSource descriptor for the model */ export declare const toModelSource: ({ refName, generatorId }: ToModelSourceArgs) => ModelSource; export {}; //# sourceMappingURL=GenerateContext.d.ts.map