diff --git a/.changeset/polite-rice-leave.md b/.changeset/polite-rice-leave.md new file mode 100644 index 0000000000..4c7be44d86 --- /dev/null +++ b/.changeset/polite-rice-leave.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: rename models.gen.ts to types.gen.ts diff --git a/.changeset/spicy-numbers-smell.md b/.changeset/spicy-numbers-smell.md new file mode 100644 index 0000000000..e4148be231 --- /dev/null +++ b/.changeset/spicy-numbers-smell.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: export enums from index.ts diff --git a/docs/openapi-ts/migrating.md b/docs/openapi-ts/migrating.md index b118f85c7b..413a5d1c62 100644 --- a/docs/openapi-ts/migrating.md +++ b/docs/openapi-ts/migrating.md @@ -19,17 +19,19 @@ Currently, `index.ts` file exports all generated artifacts. We will be slowly mo export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models'; // [!code --] -export * from './schemas'; // [!code --] -export * from './services'; // [!code --] +export * from './enums.gen'; // [!code --] +export * from './schemas.gen'; // [!code --] +export * from './services.gen'; // [!code --] +export * from './types.gen'; // [!code --] ``` Any non-core related imports should be imported as ```js -import type { Model } from 'client/models'; -import { $Schema } from 'client/schemas'; -import { DefaultService } from 'client/services'; +import { Enum } from 'client/enums.gen' +import { $Schema } from 'client/schemas.gen'; +import { DefaultService } from 'client/services.gen'; +import type { Model } from 'client/types.gen'; ``` You don't have to update imports from `core` directory. These will be addressed in later releases. @@ -50,6 +52,21 @@ This config option is deprecated and will be removed. This config option is deprecated and will be removed. +## v0.40.0 + +### Exported `enums.gen.ts` file + +Enums are now re-exported from the main `index.ts` file. This enables a cleaner migration to v0.39.0. + +### Renamed `models.gen.ts` file + +`models.gen.ts` is now called `types.gen.ts`. If you use imports from `models.gen.ts`, you should be able to easily find and replace all instances. + +```js +import type { Model } from 'client/models.gen' // [!code --] +import type { Model } from 'client/types.gen' // [!code ++] +``` + ## v0.39.0 ### Single `enums.gen.ts` file diff --git a/packages/openapi-ts/src/compiler/index.ts b/packages/openapi-ts/src/compiler/index.ts index fd3b4d347e..c50d3f0c1d 100644 --- a/packages/openapi-ts/src/compiler/index.ts +++ b/packages/openapi-ts/src/compiler/index.ts @@ -14,27 +14,24 @@ export type { Property } from './typedef'; export type { Comments } from './utils'; export type { ClassElement, Node, TypeNode } from 'typescript'; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export const generatedFileName = (fileName: string, insertGen = true) => { +const splitNameAndExtension = (fileName: string) => { const match = fileName.match(/\.[0-9a-z]+$/i); const extension = match ? match[0].slice(1) : ''; - const filePath = fileName.slice(0, fileName.length - (extension ? extension.length + 1 : 0)); - return [filePath, insertGen && 'gen', extension].filter(Boolean).join('.'); -}; - -export const filePath = (folderPath: string, fileName: string, insertGen = true) => { - const name = generatedFileName(fileName, insertGen); - return path.resolve(folderPath, name); + const name = fileName.slice(0, fileName.length - (extension ? extension.length + 1 : 0)); + return { extension, name }; }; export class TypeScriptFile { private _headers: Array = []; private _imports: Array = []; private _items: Array = []; + private _name: string; private _path: PathLike; - public constructor({ path, header = true }: { path: PathLike; header?: boolean }) { - this._path = path; + public constructor({ dir, name, header = true }: { dir: string; name: string; header?: boolean }) { + this._name = this._setName(name); + this._path = path.resolve(dir, this.getName()); + if (header) { const text = 'This file is auto-generated by @hey-api/openapi-ts'; const comment = addLeadingComment(undefined, [text], true, false); @@ -46,10 +43,36 @@ export class TypeScriptFile { this._items = [...this._items, ...nodes]; } - public addNamedImport(...params: Parameters): void { + public addNamedImport(...params: Parameters): void { this._imports = [...this._imports, compiler.import.named(...params)]; } + public getName(withExtension = true) { + if (withExtension) { + return this._name; + } + + const { name } = splitNameAndExtension(this._name); + return name; + } + + public isEmpty() { + return !this._items.length; + } + + public remove(options?: Parameters[1]) { + rmSync(this._path, options); + } + + private _setName(fileName: string) { + if (fileName.includes('index')) { + return fileName; + } + + const { extension, name } = splitNameAndExtension(fileName); + return [name, 'gen', extension].filter(Boolean).join('.'); + } + public toString(seperator: string = '\n') { let output: string[] = []; if (this._headers.length) { @@ -63,17 +86,12 @@ export class TypeScriptFile { } public write(seperator = '\n') { - // TODO: throw if path is not set. do not throw if items are empty - if (!this._items.length || !this._path) { + if (this.isEmpty()) { this.remove({ force: true }); return; } writeFileSync(this._path, this.toString(seperator)); } - - public remove(options?: Parameters[1]) { - rmSync(this._path, options); - } } export const compiler = { diff --git a/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts index dbf4ce83a8..8132b9c405 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts @@ -3,12 +3,13 @@ import path from 'node:path'; import { describe, expect, it, vi } from 'vitest'; +import { TypeScriptFile } from '../../../compiler'; import { setConfig } from '../../config'; -import { writeClientIndex } from '../index'; +import { processIndex } from '../index'; vi.mock('node:fs'); -describe('writeClientIndex', () => { +describe('processIndex', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', @@ -30,15 +31,32 @@ describe('writeClientIndex', () => { useOptions: true, }); - const client: Parameters[0] = { - enumNames: [], - models: [], - server: 'http://localhost:8080', - services: [], - version: '1.0', + const files: Parameters[0]['files'] = { + enums: new TypeScriptFile({ + dir: '/', + name: 'enums.ts', + }), + index: new TypeScriptFile({ + dir: '/', + name: 'index.ts', + }), + schemas: new TypeScriptFile({ + dir: '/', + name: 'schemas.ts', + }), + services: new TypeScriptFile({ + dir: '/', + name: 'services.ts', + }), + types: new TypeScriptFile({ + dir: '/', + name: 'models.ts', + }), }; - await writeClientIndex(client, '/'); + await processIndex({ files }); + + files.index.write(); expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/index.ts'), expect.anything()); }); diff --git a/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts index a950ec99db..f6748ba5a0 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts @@ -3,13 +3,13 @@ import path from 'node:path'; import { describe, expect, it, vi } from 'vitest'; +import { TypeScriptFile } from '../../../compiler'; import { setConfig } from '../../config'; -import { writeTypesAndEnums } from '../models'; -import { openApi } from './models'; +import { processTypesAndEnums } from '../models'; vi.mock('node:fs'); -describe('writeTypesAndEnums', () => { +describe('processTypesAndEnums', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', @@ -32,7 +32,7 @@ describe('writeTypesAndEnums', () => { useOptions: true, }); - const client: Parameters[2] = { + const client: Parameters[0]['client'] = { enumNames: [], models: [ { @@ -59,8 +59,24 @@ describe('writeTypesAndEnums', () => { version: 'v1', }; - await writeTypesAndEnums(openApi, '/', client); + const fileEnums = new TypeScriptFile({ + dir: '/', + name: 'enums.ts', + }); + const fileModels = new TypeScriptFile({ + dir: '/', + name: 'models.ts', + }); + + await processTypesAndEnums({ + client, + fileEnums, + fileModels, + }); + + fileEnums.write(); + fileModels.write(); - expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/models.gen.ts'), expect.anything()); + expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/models.gen.ts'), expect.anything()); }); }); diff --git a/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts index 4e14508cbc..b18d5ef386 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts @@ -3,13 +3,14 @@ import path from 'node:path'; import { describe, expect, it, vi } from 'vitest'; +import { TypeScriptFile } from '../../../compiler'; import { setConfig } from '../../config'; -import { writeSchemas } from '../schemas'; +import { processSchemas } from '../schemas'; import { openApi } from './models'; vi.mock('node:fs'); -describe('writeSchemas', () => { +describe('processSchemas', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', @@ -42,8 +43,15 @@ describe('writeSchemas', () => { }; } - await writeSchemas(openApi, '/'); + const file = new TypeScriptFile({ + dir: '/', + name: 'schemas.ts', + }); + + await processSchemas({ file, openApi }); + + file.write(); - expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.gen.ts'), expect.anything()); + expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/schemas.gen.ts'), expect.anything()); }); }); diff --git a/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts index 58889db105..53fad374d4 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts @@ -1,14 +1,15 @@ import { writeFileSync } from 'node:fs'; +import path from 'node:path'; import { describe, expect, it, vi } from 'vitest'; +import { TypeScriptFile } from '../../../compiler'; import { setConfig } from '../../config'; -import { writeServices } from '../services'; -import { openApi } from './models'; +import { processServices } from '../services'; vi.mock('node:fs'); -describe('writeServices', () => { +describe('processServices', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', @@ -30,7 +31,7 @@ describe('writeServices', () => { useOptions: false, }); - const client: Parameters[2] = { + const client: Parameters[0]['client'] = { enumNames: [], models: [], server: 'http://localhost:8080', @@ -45,8 +46,18 @@ describe('writeServices', () => { version: 'v1', }; - await writeServices(openApi, '/', client); + const file = new TypeScriptFile({ + dir: '/', + name: 'services.ts', + }); + const files = { + services: file, + }; + + await processServices({ client, files }); + + file.write(); - expect(writeFileSync).toHaveBeenCalled(); + expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/services.gen.ts'), expect.anything()); }); }); diff --git a/packages/openapi-ts/src/utils/write/client.ts b/packages/openapi-ts/src/utils/write/client.ts index 82766438f6..7a25c83df7 100644 --- a/packages/openapi-ts/src/utils/write/client.ts +++ b/packages/openapi-ts/src/utils/write/client.ts @@ -1,16 +1,17 @@ import { existsSync, mkdirSync } from 'node:fs'; import path from 'node:path'; +import { TypeScriptFile } from '../../compiler'; import type { OpenApi } from '../../openApi'; import type { Client } from '../../types/client'; import { getConfig } from '../config'; import type { Templates } from '../handlebars'; import { writeClientClass } from './class'; import { writeCore } from './core'; -import { writeClientIndex } from './index'; -import { writeTypesAndEnums } from './models'; -import { writeSchemas } from './schemas'; -import { writeServices } from './services'; +import { processIndex } from './index'; +import { processTypesAndEnums } from './models'; +import { processSchemas } from './schemas'; +import { processServices } from './services'; /** * Write our OpenAPI client, using the given templates at the given output @@ -31,37 +32,56 @@ export const writeClient = async (openApi: OpenApi, client: Client, templates: T client.models = client.models.filter(model => regexp.test(model.name)); } - if (!existsSync(path.resolve(config.output))) { - mkdirSync(path.resolve(config.output), { recursive: true }); - } + const outputPath = path.resolve(config.output); - const sections = [ - { - dir: 'core', - fn: writeCore, - }, - { - dir: '', - fn: writeSchemas, - }, - { - dir: '', - fn: writeTypesAndEnums, - }, - { - dir: '', - fn: writeServices, - }, - { - dir: '', - fn: writeClientClass, - }, - ] as const; + if (!existsSync(outputPath)) { + mkdirSync(outputPath, { recursive: true }); + } - for (const section of sections) { - const sectionPath = path.resolve(config.output, section.dir); - await section.fn(openApi, sectionPath, client, templates); + const files: Record = { + index: new TypeScriptFile({ + dir: config.output, + name: 'index.ts', + }), + }; + if (config.enums) { + files.enums = new TypeScriptFile({ + dir: config.output, + name: 'enums.ts', + }); + } + if (config.schemas) { + files.schemas = new TypeScriptFile({ + dir: config.output, + name: 'schemas.ts', + }); + } + if (config.exportServices) { + files.services = new TypeScriptFile({ + dir: config.output, + name: 'services.ts', + }); } + if (config.exportModels) { + files.types = new TypeScriptFile({ + dir: config.output, + name: 'types.ts', + }); + } + + await processSchemas({ file: files.schemas, openApi }); + await processTypesAndEnums({ client, fileEnums: files.enums, fileModels: files.types }); + await processServices({ client, files }); + + // deprecated files + await writeClientClass(openApi, outputPath, client, templates); + await writeCore(openApi, path.resolve(config.output, 'core'), client, templates); + + await processIndex({ files }); - await writeClientIndex(client, config.output); + files.enums?.write('\n\n'); + files.schemas?.write('\n\n'); + files.services?.write('\n\n'); + files.types?.write('\n\n'); + files.index.write(); }; diff --git a/packages/openapi-ts/src/utils/write/index.ts b/packages/openapi-ts/src/utils/write/index.ts index 68ab8a58a9..a773e11635 100644 --- a/packages/openapi-ts/src/utils/write/index.ts +++ b/packages/openapi-ts/src/utils/write/index.ts @@ -1,50 +1,39 @@ -import { compiler, filePath, generatedFileName, TypeScriptFile } from '../../compiler'; -import type { Client } from '../../types/client'; +import { compiler, TypeScriptFile } from '../../compiler'; import { getConfig } from '../config'; -/** - * Generate the OpenAPI client index file and write it to disk. - * The index file just contains all the exports you need to use the client as a standalone. - * @param client Client containing models, schemas, and services - * @param outputPath Directory to write the generated files to - */ -export const writeClientIndex = async (client: Client, outputPath: string): Promise => { +export const processIndex = async ({ files }: { files: Record }): Promise => { const config = getConfig(); - const fileIndex = new TypeScriptFile({ path: filePath(outputPath, 'index.ts', false) }); - if (config.name) { - fileIndex.add(compiler.export.named([config.name], `./${config.name}`)); + files.index.add(compiler.export.named([config.name], `./${config.name}`)); } if (config.exportCore) { - fileIndex.add(compiler.export.named('ApiError', './core/ApiError')); + files.index.add(compiler.export.named('ApiError', './core/ApiError')); if (config.serviceResponse === 'response') { - fileIndex.add(compiler.export.named({ isTypeOnly: true, name: 'ApiResult' }, './core/ApiResult')); + files.index.add(compiler.export.named({ isTypeOnly: true, name: 'ApiResult' }, './core/ApiResult')); } if (config.name) { - fileIndex.add(compiler.export.named('BaseHttpRequest', './core/BaseHttpRequest')); + files.index.add(compiler.export.named('BaseHttpRequest', './core/BaseHttpRequest')); } if (config.client !== 'angular') { - fileIndex.add(compiler.export.named(['CancelablePromise', 'CancelError'], './core/CancelablePromise')); + files.index.add(compiler.export.named(['CancelablePromise', 'CancelError'], './core/CancelablePromise')); } - fileIndex.add( + files.index.add( compiler.export.named(['OpenAPI', { isTypeOnly: true, name: 'OpenAPIConfig' }], './core/OpenAPI') ); } - if (client.models.length) { - if (config.exportModels) { - fileIndex.add(compiler.export.all(generatedFileName('./models'))); - } - if (config.schemas) { - fileIndex.add(compiler.export.all(generatedFileName('./schemas'))); - } + if (files.enums && !files.enums.isEmpty()) { + files.index.add(compiler.export.all(`./${files.enums.getName(false)}`)); } - - if (client.services.length && config.exportServices) { - fileIndex.add(compiler.export.all(generatedFileName('./services'))); + if (files.schemas && !files.schemas.isEmpty()) { + files.index.add(compiler.export.all(`./${files.schemas.getName(false)}`)); + } + if (files.services && !files.services.isEmpty()) { + files.index.add(compiler.export.all(`./${files.services.getName(false)}`)); + } + if (files.types && !files.types.isEmpty()) { + files.index.add(compiler.export.all(`./${files.types.getName(false)}`)); } - - fileIndex.write(); }; diff --git a/packages/openapi-ts/src/utils/write/models.ts b/packages/openapi-ts/src/utils/write/models.ts index 55f0f67c49..5dd94e89a7 100644 --- a/packages/openapi-ts/src/utils/write/models.ts +++ b/packages/openapi-ts/src/utils/write/models.ts @@ -1,6 +1,6 @@ -import { type Comments, compiler, filePath, type Node, TypeScriptFile } from '../../compiler'; +import { type Comments, compiler, type Node, TypeScriptFile } from '../../compiler'; import { addLeadingComment } from '../../compiler/utils'; -import type { Model, OpenApi, OperationParameter, Service } from '../../openApi'; +import type { Model, OperationParameter, Service } from '../../openApi'; import { ensureValidTypeScriptJavaScriptIdentifier } from '../../openApi/common/parser/sanitize'; import type { Client } from '../../types/client'; import { getConfig } from '../config'; @@ -214,36 +214,28 @@ const processServiceTypes = (services: Service[], onNode: OnNode) => { onNode(node); }; -/** - * Generate Models using the Handlebar template and write to disk. - * @param openApi {@link OpenApi} Dereferenced OpenAPI specification - * @param outputPath Directory to write the generated files to - * @param client Client containing models, schemas, and services - */ -export const writeTypesAndEnums = async (openApi: OpenApi, outputPath: string, client: Client): Promise => { - const config = getConfig(); - - const fileEnums = new TypeScriptFile({ path: filePath(outputPath, 'enums.ts') }); - const fileModels = new TypeScriptFile({ path: filePath(outputPath, 'models.ts') }); - +export const processTypesAndEnums = async ({ + client, + fileEnums, + fileModels, +}: { + client: Client; + fileEnums?: TypeScriptFile; + fileModels?: TypeScriptFile; +}): Promise => { for (const model of client.models) { processModel(client, model, (node, type) => { if (type === 'enum') { - fileEnums.add(node); + fileEnums?.add(node); } else { - fileModels.add(node); + fileModels?.add(node); } }); } if (client.services.length) { processServiceTypes(client.services, node => { - fileModels.add(node); + fileModels?.add(node); }); } - - if (config.exportModels) { - fileEnums.write('\n\n'); - fileModels.write('\n\n'); - } }; diff --git a/packages/openapi-ts/src/utils/write/schemas.ts b/packages/openapi-ts/src/utils/write/schemas.ts index a0a541c835..9bc58d737c 100644 --- a/packages/openapi-ts/src/utils/write/schemas.ts +++ b/packages/openapi-ts/src/utils/write/schemas.ts @@ -1,23 +1,17 @@ -import { compiler, filePath, TypeScriptFile } from '../../compiler'; +import { compiler, TypeScriptFile } from '../../compiler'; import type { OpenApi } from '../../openApi'; import { ensureValidTypeScriptJavaScriptIdentifier } from '../../openApi/common/parser/sanitize'; -import { getConfig } from '../config'; -/** - * Generate Schemas using the Handlebar template and write to disk. - * @param openApi {@link OpenApi} Dereferenced OpenAPI specification - * @param outputPath Directory to write the generated files to - */ -export const writeSchemas = async (openApi: OpenApi, outputPath: string): Promise => { - const config = getConfig(); - - const fileSchemas = new TypeScriptFile({ path: filePath(outputPath, 'schemas.ts') }); +export const processSchemas = async ({ file, openApi }: { file?: TypeScriptFile; openApi: OpenApi }): Promise => { + if (!file) { + return; + } const addSchema = (name: string, obj: any) => { const validName = `$${ensureValidTypeScriptJavaScriptIdentifier(name)}`; const expression = compiler.types.object({ obj }); const statement = compiler.export.asConst(validName, expression); - fileSchemas.add(statement); + file.add(statement); }; // OpenAPI 2.0 @@ -47,8 +41,4 @@ export const writeSchemas = async (openApi: OpenApi, outputPath: string): Promis } } } - - if (config.schemas) { - fileSchemas.write('\n\n'); - } }; diff --git a/packages/openapi-ts/src/utils/write/services.ts b/packages/openapi-ts/src/utils/write/services.ts index 16f8d2c364..476dcab521 100644 --- a/packages/openapi-ts/src/utils/write/services.ts +++ b/packages/openapi-ts/src/utils/write/services.ts @@ -1,5 +1,5 @@ -import { ClassElement, compiler, filePath, FunctionParameter, generatedFileName, TypeScriptFile } from '../../compiler'; -import type { OpenApi, Operation, OperationParameter, Service } from '../../openApi'; +import { ClassElement, compiler, FunctionParameter, TypeScriptFile } from '../../compiler'; +import type { Operation, OperationParameter, Service } from '../../openApi'; import type { Client } from '../../types/client'; import { getConfig } from '../config'; import { escapeComment, escapeDescription, escapeName } from '../escape'; @@ -209,53 +209,55 @@ export const processService = (service: Service) => { }); }; -/** - * Generate Services using the Handlebar template and write to disk. - * @param openApi {@link OpenApi} Dereferenced OpenAPI specification - * @param outputPath Directory to write the generated files to - * @param client Client containing models, schemas, and services - */ -export const writeServices = async (openApi: OpenApi, outputPath: string, client: Client): Promise => { - const config = getConfig(); +export const processServices = async ({ + client, + files, +}: { + client: Client; + files: Record; +}): Promise => { + const file = files.services; - const fileServices = new TypeScriptFile({ path: filePath(outputPath, 'services.ts') }); + if (!file) { + return; + } + + const config = getConfig(); let imports: string[] = []; for (const service of client.services) { - fileServices.add(processService(service)); + file.add(processService(service)); const exported = serviceExportedNamespace(); imports = [...imports, exported]; } // Import required packages and core files. if (config.client === 'angular') { - fileServices.addNamedImport('Injectable', '@angular/core'); + file.addNamedImport('Injectable', '@angular/core'); if (config.name === undefined) { - fileServices.addNamedImport('HttpClient', '@angular/common/http'); + file.addNamedImport('HttpClient', '@angular/common/http'); } - fileServices.addNamedImport({ isTypeOnly: true, name: 'Observable' }, 'rxjs'); + file.addNamedImport({ isTypeOnly: true, name: 'Observable' }, 'rxjs'); } else { - fileServices.addNamedImport({ isTypeOnly: true, name: 'CancelablePromise' }, './core/CancelablePromise'); + file.addNamedImport({ isTypeOnly: true, name: 'CancelablePromise' }, './core/CancelablePromise'); } if (config.serviceResponse === 'response') { - fileServices.addNamedImport({ isTypeOnly: true, name: 'ApiResult' }, './core/ApiResult'); + file.addNamedImport({ isTypeOnly: true, name: 'ApiResult' }, './core/ApiResult'); } if (config.name) { - fileServices.addNamedImport( + file.addNamedImport( { isTypeOnly: config.client !== 'angular', name: 'BaseHttpRequest' }, './core/BaseHttpRequest' ); } else { - fileServices.addNamedImport('OpenAPI', './core/OpenAPI'); - fileServices.addNamedImport({ alias: '__request', name: 'request' }, './core/request'); + file.addNamedImport('OpenAPI', './core/OpenAPI'); + file.addNamedImport({ alias: '__request', name: 'request' }, './core/request'); } // Import all models required by the services. - const models = imports.filter(unique).map(imp => ({ isTypeOnly: true, name: imp })); - fileServices.addNamedImport(models, generatedFileName('./models')); - - if (config.exportServices) { - fileServices.write('\n\n'); + if (files.types && !files.types.isEmpty()) { + const models = imports.filter(unique).map(name => ({ isTypeOnly: true, name })); + file.addNamedImport(models, `./${files.types.getName(false)}`); } }; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/index.ts.snap index efce7d5740..2d4902304d 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v2/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v2/index.ts.snap @@ -3,6 +3,7 @@ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; +export * from './enums.gen'; export * from './schemas.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.gen.ts.snap index b826eabd48..0731e4637d 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.gen.ts.snap @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultService { /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v2/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v2/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/index.ts.snap index efce7d5740..2d4902304d 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/index.ts.snap @@ -3,6 +3,7 @@ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; +export * from './enums.gen'; export * from './schemas.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap index b8cd990401..1b354cf5fb 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultService { /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/index.ts.snap index cf735bbd1a..f29539a498 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/index.ts.snap @@ -2,6 +2,6 @@ export { ApiError } from './core/ApiError'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; export * from './schemas.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap index 819ccd051c..e6d0720b9e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap @@ -5,7 +5,7 @@ import { HttpClient } from '@angular/common/http'; import type { Observable } from 'rxjs'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; @Injectable({ providedIn: 'root', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/index.ts.snap index b2c63640f5..cc07b6aab2 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/index.ts.snap @@ -5,5 +5,6 @@ export { ApiError } from './core/ApiError'; export { BaseHttpRequest } from './core/BaseHttpRequest'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; +export * from './enums.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap index b164b1550a..1d7a41bef1 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap @@ -2,7 +2,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import type { BaseHttpRequest } from './core/BaseHttpRequest'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultService { constructor(public readonly httpRequest: BaseHttpRequest) {} diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/index.ts.snap index a40165ab16..8e0c78ad5f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/index.ts.snap @@ -1,4 +1,4 @@ // This file is auto-generated by @hey-api/openapi-ts -export * from './models.gen'; export * from './schemas.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_date/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/index.ts.snap index efce7d5740..2d4902304d 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/index.ts.snap @@ -3,6 +3,7 @@ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; +export * from './enums.gen'; export * from './schemas.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap index b8cd990401..1b354cf5fb 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultService { /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/index.ts.snap index 74e20e5738..832d672087 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/index.ts.snap @@ -3,5 +3,5 @@ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/services.gen.ts.snap index 090047cab6..8ae06bf866 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/services.gen.ts.snap @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultsService { /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/index.ts.snap index 1a30d96c9c..0339b6e31e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/index.ts.snap @@ -1,3 +1,3 @@ // This file is auto-generated by @hey-api/openapi-ts -export * from './models.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/index.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/index.ts.snap index 74e20e5738..832d672087 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/index.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/index.ts.snap @@ -3,5 +3,5 @@ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI'; -export * from './models.gen'; export * from './services.gen'; +export * from './types.gen'; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.gen.ts.snap index 18adb5fbfe..ca5ea9692b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.gen.ts.snap @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { $OpenApiTs } from './models.gen'; +import type { $OpenApiTs } from './types.gen'; export class DefaultsService { /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/models.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/types.gen.ts.snap similarity index 100% rename from packages/openapi-ts/test/__snapshots__/test/generated/v3_options/models.gen.ts.snap rename to packages/openapi-ts/test/__snapshots__/test/generated/v3_options/types.gen.ts.snap