import { EventEmitter } from 'node:events'; import { ApiDocument } from '@opra/common'; import type { IFileWriter } from '../interfaces/file-writer.interface.js'; import type { ILogger } from '../interfaces/logger.interface.js'; import { _generateArrayTypeCode, _generateComplexTypeCode, _generateEnumTypeCode, _generateMappedTypeCode, _generateMixinTypeCode, _generateSimpleTypeCode, _generateTypeCode, _generateUnionTypeCode, generateDataType } from './generators/generate-data-type.js'; import { generateDocument } from './generators/generate-document.js'; import { generateHttpApi } from './generators/generate-http-api.js'; import { generateHttpController } from './generators/generate-http-controller.js'; import { TsFile } from './ts-file.js'; /** * TsGenerator * * A class responsible for generating TypeScript code from an OPRA API document. */ export declare namespace TsGenerator { /** * Configuration options for TsGenerator. */ interface Options { /** The URL of the OPRA service. Only required when generating from a * URL/document-id (the default) rather than passing an already-built * `ApiDocument` straight to `generateFiles()`. */ serviceUrl?: string; /** The output directory for the generated files. Only meaningful for * `generate()`'s own disk-writing step — `generateFiles()` alone * never reads it. Defaults to `cwd`. */ outDir?: string; /** The current working directory `outDir` is resolved against. * Defaults to `process.cwd()` — resolved lazily, by `generate()`, * not here, so constructing a `TsGenerator` has no Node-API * dependency of its own. */ cwd?: string; /** Logger instance for outputting information. */ logger?: ILogger; /** File writer instance. Defaults to FileWriter. */ writer?: IFileWriter; /** Optional header to add to each generated file. */ fileHeader?: string; /** Whether to add .js extension to imports. */ importExt?: boolean; /** Whether to export references with namespaces. */ referenceNamespaces?: boolean; /** Language the fetched documentation texts should be in (see * `?lang=` on the service's own `$schema` endpoint). Only meaningful * when generating from a `serviceUrl`. */ lang?: string; } /** One generated source file, purely in memory — what `generateFiles()` * returns, before `generate()`'s own disk-writing step turns each of * these into a real file under `outDir`. */ interface GeneratedFile { /** Path relative to `outDir` (e.g. `./models/types/customer.ts`). */ filename: string; /** The file's own generated TypeScript source. */ content: string; } } /** * TsGenerator * * Main class for managing the TypeScript code generation process. */ export declare class TsGenerator extends EventEmitter { protected generateDocument: typeof generateDocument; protected generateDataType: typeof generateDataType; protected _generateTypeCode: typeof _generateTypeCode; protected _generateArrayTypeCode: typeof _generateArrayTypeCode; protected _generateComplexTypeCode: typeof _generateComplexTypeCode; protected _generateEnumTypeCode: typeof _generateEnumTypeCode; protected _generateMappedTypeCode: typeof _generateMappedTypeCode; protected _generateMixinTypeCode: typeof _generateMixinTypeCode; protected _generateSimpleTypeCode: typeof _generateSimpleTypeCode; protected _generateUnionTypeCode: typeof _generateUnionTypeCode; protected generateHttpApi: typeof generateHttpApi; protected generateHttpController: typeof generateHttpController; protected _documentRoot: string; protected _typesRoot: string; protected _typesNamespace: string; protected _apiPath: string; protected _fileHeaderDocInfo: string; protected _files: Record; protected _started: boolean; protected _document?: ApiDocument; protected _documentsMap: Map; protected _filesMap: WeakMap; protected _generatedFiles?: TsGenerator.GeneratedFile[]; readonly serviceUrl?: string; readonly lang?: string; /** Raw, as given to the constructor — resolved against `cwd` (with * its own `process.cwd()` fallback) lazily, only by `generate()`'s * own disk-writing step. */ readonly outDir?: string; readonly cwd?: string; readonly writer?: IFileWriter; readonly options: { importExt: boolean; referenceNamespaces?: boolean; }; fileHeader: string; /** * Initializes a new TsGenerator instance. * * @param init - Configuration options. */ constructor(init: TsGenerator.Options); /** * Runs the actual code-generation pipeline and returns the produced * files purely in memory — no disk access at all. This is the * Node-API-free half of `generate()` (see there for the other half: * writing these to disk under `outDir`), kept separate so a caller * that already has a live `ApiDocument` (e.g. one reconstructed * client-side from a fetched schema, with no `outDir`/filesystem to * speak of) can generate without either. * * Passing `document` skips the `serviceUrl` HTTP fetch inside * `generateDocument()` entirely. Memoized — a second call (with or * without `document`) returns the same result rather than * regenerating. */ generateFiles(document?: ApiDocument): Promise; /** * Starts the code generation process: builds every file (see * `generateFiles()`, which this passes `document` through to) then * writes each one to disk under `outDir`, after first removing * whatever was generated there previously. `write-to-disk.js` (the * only Node-API-dependent part of this whole class) is imported here, * dynamically, rather than at this file's own top level — see there * for why. * * @throws {@link Error} If generation fails. */ generate(document?: ApiDocument): Promise; /** * Retrieves a file from the internal cache by its path. * * @param filePath - The path of the file to retrieve. * @returns The TsFile instance or undefined if not found. */ protected getFile(filePath: string): TsFile; /** * Adds a new file to the generator or returns an existing one. * * @param filePath - The path of the file to add. * @param returnExists - Whether to return the file if it already exists instead of throwing. * @returns The newly created or existing TsFile instance. * @throws {@link Error} If the file already exists and returnExists is false. */ protected addFile(filePath: string, returnExists?: boolean): TsFile; protected extend(): TsGenerator; }