From ea51130ecb70620077211d0303fef262f527b31e Mon Sep 17 00:00:00 2001 From: Lubos Date: Sun, 20 Oct 2024 17:25:59 +0800 Subject: [PATCH] refactor: move out legacy parser functions --- .../src/generate/__tests__/class.spec.ts | 8 +- .../src/generate/__tests__/core.spec.ts | 18 +- .../src/generate/__tests__/index.spec.ts | 2 +- .../src/generate/__tests__/output.spec.ts | 7 +- .../src/generate/__tests__/schemas.spec.ts | 8 +- .../src/generate/__tests__/services.spec.ts | 20 +-- .../src/generate/__tests__/types.spec.ts | 9 +- packages/openapi-ts/src/generate/class.ts | 2 +- packages/openapi-ts/src/generate/client.ts | 37 ++-- packages/openapi-ts/src/generate/core.ts | 2 +- packages/openapi-ts/src/generate/indexFile.ts | 6 +- packages/openapi-ts/src/generate/output.ts | 168 +++++++++++------- packages/openapi-ts/src/generate/plugins.ts | 26 +-- packages/openapi-ts/src/generate/schemas.ts | 2 +- packages/openapi-ts/src/generate/services.ts | 151 ++++++++++------ .../openapi-ts/src/generate/transformers.ts | 3 +- packages/openapi-ts/src/generate/types.ts | 44 +++-- packages/openapi-ts/src/index.ts | 18 +- packages/openapi-ts/src/ir/context.ts | 1 + .../src/openApi/__tests__/index.spec.ts | 18 +- packages/openapi-ts/src/openApi/index.ts | 2 +- .../src/plugins/@hey-api/schemas/config.ts | 2 +- .../src/plugins/@hey-api/schemas/types.ts | 4 +- .../src/plugins/@hey-api/services/config.ts | 2 +- .../src/plugins/@hey-api/services/types.ts | 4 +- .../src/plugins/@hey-api/types/config.ts | 2 +- .../src/plugins/@hey-api/types/types.ts | 4 +- .../plugins/@tanstack/query-core/plugin.ts | 23 +-- .../plugins/@tanstack/react-query/config.ts | 4 +- .../plugins/@tanstack/react-query/types.ts | 4 +- .../plugins/@tanstack/solid-query/config.ts | 4 +- .../plugins/@tanstack/solid-query/types.ts | 4 +- .../plugins/@tanstack/svelte-query/config.ts | 4 +- .../plugins/@tanstack/svelte-query/types.ts | 4 +- .../src/plugins/@tanstack/vue-query/config.ts | 4 +- .../src/plugins/@tanstack/vue-query/types.ts | 4 +- packages/openapi-ts/src/plugins/types.ts | 13 +- packages/openapi-ts/src/plugins/zod/config.ts | 6 +- packages/openapi-ts/src/plugins/zod/plugin.ts | 4 +- packages/openapi-ts/src/plugins/zod/types.ts | 4 +- .../src/utils/__tests__/postprocess.spec.ts | 6 +- packages/openapi-ts/test/sample.cjs | 2 +- 42 files changed, 354 insertions(+), 306 deletions(-) diff --git a/packages/openapi-ts/src/generate/__tests__/class.spec.ts b/packages/openapi-ts/src/generate/__tests__/class.spec.ts index 6dc6c81b3..24c90a32c 100644 --- a/packages/openapi-ts/src/generate/__tests__/class.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/class.spec.ts @@ -3,12 +3,12 @@ import { writeFileSync } from 'node:fs'; import { describe, expect, it, vi } from 'vitest'; import { setConfig } from '../../utils/config'; -import { generateClientClass } from '../class'; +import { generateLegacyClientClass } from '../class'; import { mockTemplates, openApi } from './mocks'; vi.mock('node:fs'); -describe('generateClientClass', () => { +describe('generateLegacyClientClass', () => { it('writes to filesystem', async () => { setConfig({ client: { @@ -33,7 +33,7 @@ describe('generateClientClass', () => { useOptions: true, }); - const client: Parameters[2] = { + const client: Parameters[2] = { models: [], server: 'http://localhost:8080', services: [], @@ -41,7 +41,7 @@ describe('generateClientClass', () => { version: 'v1', }; - await generateClientClass(openApi, './dist', client, mockTemplates); + await generateLegacyClientClass(openApi, './dist', client, mockTemplates); expect(writeFileSync).toHaveBeenCalled(); }); diff --git a/packages/openapi-ts/src/generate/__tests__/core.spec.ts b/packages/openapi-ts/src/generate/__tests__/core.spec.ts index 3ebf059e3..d40484bad 100644 --- a/packages/openapi-ts/src/generate/__tests__/core.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/core.spec.ts @@ -4,19 +4,19 @@ import path from 'node:path'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { setConfig } from '../../utils/config'; -import { generateCore } from '../core'; +import { generateLegacyCore } from '../core'; import { mockTemplates } from './mocks'; vi.mock('node:fs'); -describe('generateCore', () => { - let templates: Parameters[2]; +describe('generateLegacyCore', () => { + let templates: Parameters[2]; beforeEach(() => { templates = mockTemplates; }); it('writes to filesystem', async () => { - const client: Parameters[1] = { + const client: Parameters[1] = { models: [], server: 'http://localhost:8080', services: [], @@ -47,7 +47,7 @@ describe('generateCore', () => { useOptions: true, }); - await generateCore('/', client, templates); + await generateLegacyCore('/', client, templates); expect(writeFileSync).toHaveBeenCalledWith( path.resolve('/', '/OpenAPI.ts'), @@ -76,7 +76,7 @@ describe('generateCore', () => { }); it('uses client server value for base', async () => { - const client: Parameters[1] = { + const client: Parameters[1] = { models: [], server: 'http://localhost:8080', services: [], @@ -107,7 +107,7 @@ describe('generateCore', () => { useOptions: true, }); - await generateCore('/', client, templates); + await generateLegacyCore('/', client, templates); expect(templates.core.settings).toHaveBeenCalledWith({ $config: config, @@ -118,7 +118,7 @@ describe('generateCore', () => { }); it('uses custom value for base', async () => { - const client: Parameters[1] = { + const client: Parameters[1] = { models: [], server: 'http://localhost:8080', services: [], @@ -150,7 +150,7 @@ describe('generateCore', () => { useOptions: true, }); - await generateCore('/', client, templates); + await generateLegacyCore('/', client, templates); expect(templates.core.settings).toHaveBeenCalledWith({ $config: config, diff --git a/packages/openapi-ts/src/generate/__tests__/index.spec.ts b/packages/openapi-ts/src/generate/__tests__/index.spec.ts index 33bd2c9cf..7750bf887 100644 --- a/packages/openapi-ts/src/generate/__tests__/index.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/index.spec.ts @@ -48,7 +48,7 @@ describe('generateIndexFile', () => { }), }; - await generateIndexFile({ files }); + generateIndexFile({ files }); files.index.write(); diff --git a/packages/openapi-ts/src/generate/__tests__/output.spec.ts b/packages/openapi-ts/src/generate/__tests__/output.spec.ts index ce3a7c0b4..3e0b69745 100644 --- a/packages/openapi-ts/src/generate/__tests__/output.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/output.spec.ts @@ -4,12 +4,12 @@ import { describe, expect, it, vi } from 'vitest'; import type { Client } from '../../types/client'; import { setConfig } from '../../utils/config'; -import { generateOutput } from '../output'; +import { generateLegacyOutput } from '../output'; import { mockTemplates, openApi } from './mocks'; vi.mock('node:fs'); -describe('generateOutput', () => { +describe('generateLegacyOutput', () => { it('writes to filesystem', async () => { setConfig({ client: { @@ -42,9 +42,8 @@ describe('generateOutput', () => { version: 'v1', }; - await generateOutput({ + await generateLegacyOutput({ client, - context: undefined, openApi, templates: mockTemplates, }); diff --git a/packages/openapi-ts/src/generate/__tests__/schemas.spec.ts b/packages/openapi-ts/src/generate/__tests__/schemas.spec.ts index 31fcc0111..e17be682f 100644 --- a/packages/openapi-ts/src/generate/__tests__/schemas.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/schemas.spec.ts @@ -6,12 +6,12 @@ import { describe, expect, it, vi } from 'vitest'; import type { OpenApiV3Schema } from '../../openApi'; import type { Files } from '../../types/utils'; import { setConfig } from '../../utils/config'; -import { generateSchemas } from '../schemas'; +import { generateLegacySchemas } from '../schemas'; import { openApi } from './mocks'; vi.mock('node:fs'); -describe('generateSchemas', () => { +describe('generateLegacySchemas', () => { it('writes to filesystem', async () => { setConfig({ client: { @@ -50,7 +50,7 @@ describe('generateSchemas', () => { const files: Files = {}; - await generateSchemas({ files, openApi }); + await generateLegacySchemas({ files, openApi }); files.schemas.write(); @@ -103,7 +103,7 @@ describe('generateSchemas', () => { const files: Files = {}; - await generateSchemas({ files, openApi }); + await generateLegacySchemas({ files, openApi }); files.schemas.write(); diff --git a/packages/openapi-ts/src/generate/__tests__/services.spec.ts b/packages/openapi-ts/src/generate/__tests__/services.spec.ts index dfd3ed290..871261502 100644 --- a/packages/openapi-ts/src/generate/__tests__/services.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/services.spec.ts @@ -7,11 +7,11 @@ import type { Operation } from '../../types/client'; import type { Files } from '../../types/utils'; import { setConfig } from '../../utils/config'; import { TypeScriptFile } from '../files'; -import { generateServices } from '../services'; +import { generateLegacyServices } from '../services'; vi.mock('node:fs'); -describe('generateServices', () => { +describe('generateLegacyServices', () => { it('writes to filesystem', async () => { setConfig({ client: { @@ -36,7 +36,7 @@ describe('generateServices', () => { useOptions: false, }); - const client: Parameters[0]['client'] = { + const client: Parameters[0]['client'] = { models: [], server: 'http://localhost:8080', services: [ @@ -80,9 +80,8 @@ describe('generateServices', () => { name: 'types.ts', }); - await generateServices({ + await generateLegacyServices({ client, - context: undefined, files, }); @@ -120,7 +119,7 @@ describe('methodNameBuilder', () => { summary: null, }; - const client: Parameters[0]['client'] = { + const client: Parameters[0]['client'] = { models: [], server: 'http://localhost:8080', services: [ @@ -166,9 +165,8 @@ describe('methodNameBuilder', () => { name: 'types.ts', }); - await generateServices({ + await generateLegacyServices({ client, - context: undefined, files, }); @@ -214,9 +212,8 @@ describe('methodNameBuilder', () => { name: 'types.ts', }); - await generateServices({ + await generateLegacyServices({ client, - context: undefined, files, }); @@ -264,9 +261,8 @@ describe('methodNameBuilder', () => { name: 'types.ts', }); - await generateServices({ + await generateLegacyServices({ client, - context: undefined, files, }); diff --git a/packages/openapi-ts/src/generate/__tests__/types.spec.ts b/packages/openapi-ts/src/generate/__tests__/types.spec.ts index 46e97188e..99bd6f12b 100644 --- a/packages/openapi-ts/src/generate/__tests__/types.spec.ts +++ b/packages/openapi-ts/src/generate/__tests__/types.spec.ts @@ -5,11 +5,11 @@ import { describe, expect, it, vi } from 'vitest'; import { setConfig } from '../../utils/config'; import { TypeScriptFile } from '../files'; -import { generateTypes } from '../types'; +import { generateLegacyTypes } from '../types'; vi.mock('node:fs'); -describe('generateTypes', () => { +describe('generateLegacyTypes', () => { it('writes to filesystem', async () => { setConfig({ client: { @@ -34,7 +34,7 @@ describe('generateTypes', () => { useOptions: true, }); - const client: Parameters[0]['client'] = { + const client: Parameters[0]['client'] = { models: [ { $refs: [], @@ -73,9 +73,8 @@ describe('generateTypes', () => { }), }; - await generateTypes({ + await generateLegacyTypes({ client, - context: undefined, files, }); diff --git a/packages/openapi-ts/src/generate/class.ts b/packages/openapi-ts/src/generate/class.ts index 0e04a7ea3..5da0c6956 100644 --- a/packages/openapi-ts/src/generate/class.ts +++ b/packages/openapi-ts/src/generate/class.ts @@ -18,7 +18,7 @@ import { ensureDirSync } from './utils'; * @param client Client containing models, schemas, and services * @param templates The loaded handlebar templates */ -export const generateClientClass = async ( +export const generateLegacyClientClass = async ( openApi: OpenApi, outputPath: string, client: Client, diff --git a/packages/openapi-ts/src/generate/client.ts b/packages/openapi-ts/src/generate/client.ts index fcc47f7d9..45cff435d 100644 --- a/packages/openapi-ts/src/generate/client.ts +++ b/packages/openapi-ts/src/generate/client.ts @@ -1,16 +1,21 @@ import { copyFileSync } from 'node:fs'; import path from 'node:path'; -import { getConfig, isLegacyClient } from '../utils/config'; +import type { Config } from '../types/config'; import { ensureDirSync, relativeModulePath } from './utils'; +/** + * Returns path to the client module. When using client packages, this will be + * simply the name of the package. When bundling a client, this will be a + * relative path to the bundled client folder. + */ export const clientModulePath = ({ + config, sourceOutput, }: { + config: Config; sourceOutput: string; -}) => { - const config = getConfig(); - +}): string => { if (config.client.bundle) { return relativeModulePath({ moduleOutput: 'client', @@ -24,26 +29,20 @@ export const clientModulePath = ({ export const clientOptionsTypeName = () => 'Options'; /** - * (optional) Creates a `client.ts` file containing the same exports as the - * client package. Creates a `client` directory containing the modules from - * the client package. These files are generated only when `client.bundle` is - * set to true. + * Creates a `client` directory containing the same modules as the client package. */ -export const generateClient = async ( - outputPath: string, - moduleName: string, -) => { - const config = getConfig(); - - if (isLegacyClient(config) || !config.client.bundle) { - return; - } - +export const generateClientBundle = ({ + name, + outputPath, +}: { + name: string; + outputPath: string; +}): void => { // create directory for client modules const dirPath = path.resolve(outputPath, 'client'); ensureDirSync(dirPath); - const clientModulePath = path.normalize(require.resolve(moduleName)); + const clientModulePath = path.normalize(require.resolve(name)); const clientModulePathComponents = clientModulePath.split(path.sep); const clientSrcPath = [ ...clientModulePathComponents.slice( diff --git a/packages/openapi-ts/src/generate/core.ts b/packages/openapi-ts/src/generate/core.ts index b547910af..c2b9641d5 100644 --- a/packages/openapi-ts/src/generate/core.ts +++ b/packages/openapi-ts/src/generate/core.ts @@ -18,7 +18,7 @@ import type { Templates } from '../utils/handlebars'; * @param client Client containing models, schemas, and services * @param templates The loaded handlebar templates */ -export const generateCore = async ( +export const generateLegacyCore = async ( outputPath: string, client: Client, templates: Templates, diff --git a/packages/openapi-ts/src/generate/indexFile.ts b/packages/openapi-ts/src/generate/indexFile.ts index fbc2045c8..9db159e28 100644 --- a/packages/openapi-ts/src/generate/indexFile.ts +++ b/packages/openapi-ts/src/generate/indexFile.ts @@ -3,11 +3,7 @@ import type { Files } from '../types/utils'; import { getConfig } from '../utils/config'; import { TypeScriptFile } from './files'; -export const generateIndexFile = async ({ - files, -}: { - files: Files; -}): Promise => { +export const generateIndexFile = ({ files }: { files: Files }): void => { const config = getConfig(); files.index = new TypeScriptFile({ diff --git a/packages/openapi-ts/src/generate/output.ts b/packages/openapi-ts/src/generate/output.ts index cfec98192..19c60a013 100644 --- a/packages/openapi-ts/src/generate/output.ts +++ b/packages/openapi-ts/src/generate/output.ts @@ -4,17 +4,17 @@ import type { IRContext } from '../ir/context'; import type { OpenApi } from '../openApi'; import type { Client } from '../types/client'; import type { Files } from '../types/utils'; -import { getConfig } from '../utils/config'; +import { getConfig, isLegacyClient } from '../utils/config'; import type { Templates } from '../utils/handlebars'; -import { generateClientClass } from './class'; -import { generateClient } from './client'; -import { generateCore } from './core'; +import { generateLegacyClientClass } from './class'; +import { generateClientBundle } from './client'; +import { generateLegacyCore } from './core'; import { generateIndexFile } from './indexFile'; -import { generatePlugins } from './plugins'; -import { generateSchemas } from './schemas'; -import { generateServices } from './services'; -import { generateResponseTransformers } from './transformers'; -import { generateTypes } from './types'; +import { generateLegacyPlugins } from './plugins'; +import { generateLegacySchemas } from './schemas'; +import { generateLegacyServices, generateServices } from './services'; +import { generateLegacyTransformers } from './transformers'; +import { generateLegacyTypes, generateTypes } from './types'; /** * Write our OpenAPI client, using the given templates at the given output @@ -22,20 +22,18 @@ import { generateTypes } from './types'; * @param client Client containing models, schemas, and services * @param templates Templates wrapper with all loaded Handlebars templates */ -export const generateOutput = async ({ +export const generateLegacyOutput = async ({ client, - context, openApi, templates, }: { - client: Client | undefined; - context: IRContext | undefined; + client: Client; openApi: OpenApi; templates: Templates; }): Promise => { const config = getConfig(); - // TODO: parser - handle IR + // TODO: parser - move to config.input if (client) { if (config.services.include && config.services.asClass) { const regexp = new RegExp(config.services.include); @@ -54,65 +52,51 @@ export const generateOutput = async ({ const files: Files = {}; - await generateClient(outputPath, config.client.name); + if (!isLegacyClient(config) && config.client.bundle) { + await generateClientBundle({ name: config.client.name, outputPath }); + } // types.gen.ts - await generateTypes({ - client, - context, - files, - }); + await generateLegacyTypes({ client, files }); // schemas.gen.ts - await generateSchemas({ files, openApi }); + await generateLegacySchemas({ files, openApi }); // transformers - // TODO: parser - handle IR - if (client) { - if ( - config.services.export && - client.services.length && - config.types.dates === 'types+transform' - ) { - await generateResponseTransformers({ - client, - onNode: (node) => { - files.types?.add(node); - }, - onRemoveNode: () => { - files.types?.removeNode(); - }, - }); - } + if ( + config.services.export && + client.services.length && + config.types.dates === 'types+transform' + ) { + await generateLegacyTransformers({ + client, + onNode: (node) => { + files.types?.add(node); + }, + onRemoveNode: () => { + files.types?.removeNode(); + }, + }); } // services.gen.ts - await generateServices({ - client, - context, - files, - }); + await generateLegacyServices({ client, files }); // deprecated files - if (client) { - await generateClientClass(openApi, outputPath, client, templates); - await generateCore( - path.resolve(config.output.path, 'core'), - client, - templates, - ); - } + await generateLegacyClientClass(openApi, outputPath, client, templates); + await generateLegacyCore( + path.resolve(config.output.path, 'core'), + client, + templates, + ); + // TODO: parser - remove after moving types, services, transformers, and schemas into plugin // index.ts. Any files generated after this won't be included in exports // from the index file. - await generateIndexFile({ files }); + generateIndexFile({ files }); // plugins - await generatePlugins({ - client, - context, - files, - }); + await generateLegacyPlugins({ client, files }); Object.entries(files).forEach(([name, file]) => { if (config.dryRun) { @@ -125,18 +109,66 @@ export const generateOutput = async ({ file.write('\n\n'); } }); +}; + +export const generateOutput = async ({ context }: { context: IRContext }) => { + const outputPath = path.resolve(context.config.output.path); - if (context) { - Object.entries(context.files).forEach(([name, file]) => { - if (config.dryRun) { - return; - } - - if (name === 'index') { - file.write(); - } else { - file.write('\n\n'); - } + if (context.config.client.bundle) { + generateClientBundle({ + name: context.config.client.name, + outputPath, }); } + + // types.gen.ts + generateTypes({ context }); + + // schemas.gen.ts + // await generateLegacySchemas({ files, openApi }); + + // transformers + if ( + context.config.services.export && + // client.services.length && + context.config.types.dates === 'types+transform' + ) { + // await generateLegacyTransformers({ + // client, + // onNode: (node) => { + // files.types?.add(node); + // }, + // onRemoveNode: () => { + // files.types?.removeNode(); + // }, + // }); + } + + // services.gen.ts + generateServices({ context }); + + // TODO: parser - remove after moving types, services, transformers, and schemas into plugin + // index.ts. Any files generated after this won't be included in exports + // from the index file. + generateIndexFile({ files: context.files }); + + // plugins + for (const plugin of context.config.plugins) { + plugin.handler({ + context, + plugin: plugin as never, + }); + } + + Object.entries(context.files).forEach(([name, file]) => { + if (context.config.dryRun) { + return; + } + + if (name === 'index') { + file.write(); + } else { + file.write('\n\n'); + } + }); }; diff --git a/packages/openapi-ts/src/generate/plugins.ts b/packages/openapi-ts/src/generate/plugins.ts index e9c5d1b74..5cfa61324 100644 --- a/packages/openapi-ts/src/generate/plugins.ts +++ b/packages/openapi-ts/src/generate/plugins.ts @@ -1,18 +1,15 @@ import path from 'node:path'; -import type { IRContext } from '../ir/context'; import type { Client } from '../types/client'; import type { Files } from '../types/utils'; import { getConfig, isLegacyClient } from '../utils/config'; import { TypeScriptFile } from './files'; -export const generatePlugins = async ({ +export const generateLegacyPlugins = async ({ client, files, - context, }: { - client: Client | undefined; - context: IRContext | undefined; + client: Client; files: Files; }) => { const config = getConfig(); @@ -32,19 +29,10 @@ export const generatePlugins = async ({ dir: outputDir, name: `${outputParts[outputParts.length - 1]}.ts`, }); - - if (context) { - plugin.handler_experimental({ - context, - files, - plugin: plugin as never, - }); - } else if (client) { - plugin.handler({ - client, - files, - plugin: plugin as never, - }); - } + plugin.handlerLegacy({ + client, + files, + plugin: plugin as never, + }); } }; diff --git a/packages/openapi-ts/src/generate/schemas.ts b/packages/openapi-ts/src/generate/schemas.ts index 7990119b6..9cb68648b 100644 --- a/packages/openapi-ts/src/generate/schemas.ts +++ b/packages/openapi-ts/src/generate/schemas.ts @@ -68,7 +68,7 @@ const toSchemaName = ( return `${validName}Schema`; }; -export const generateSchemas = async ({ +export const generateLegacySchemas = async ({ files, openApi, }: { diff --git a/packages/openapi-ts/src/generate/services.ts b/packages/openapi-ts/src/generate/services.ts index 7bd9a7e8f..c3c135e2e 100644 --- a/packages/openapi-ts/src/generate/services.ts +++ b/packages/openapi-ts/src/generate/services.ts @@ -36,6 +36,8 @@ import { irRef } from './types'; type OnNode = (node: Node) => void; type OnImport = (name: string) => void; +const servicesId = 'services'; + export const generateImport = ({ meta, onImport, @@ -794,31 +796,23 @@ const processService = ({ onNode(statement); }; -const checkPrerequisites = ({ - context, - files, -}: { - context: IRContext | undefined; - files: Files; -}) => { - if (!context) { - const config = getConfig(); - - if (!config.client.name) { - throw new Error( - '🚫 client needs to be set to generate services - which HTTP client do you want to use?', - ); - } +const checkLegacyPrerequisites = ({ files }: { files: Files }) => { + const config = getConfig(); - if (!files.types) { - throw new Error( - '🚫 types need to be exported to generate services - enable type generation', - ); - } + if (!config.client.name) { + throw new Error( + '🚫 client needs to be set to generate services - which HTTP client do you want to use?', + ); + } - return; + if (!files.types) { + throw new Error( + '🚫 types need to be exported to generate services - enable type generation', + ); } +}; +const checkPrerequisites = ({ context }: { context: IRContext }) => { if (!context.config.client.name) { throw new Error( '🚫 client needs to be set to generate services - which HTTP client do you want to use?', @@ -832,13 +826,11 @@ const checkPrerequisites = ({ } }; -export const generateServices = async ({ +export const generateLegacyServices = async ({ client, - context, files, }: { - client: Client | undefined; - context: IRContext | undefined; + client: Client; files: Files; }): Promise => { const config = getConfig(); @@ -847,7 +839,7 @@ export const generateServices = async ({ return; } - checkPrerequisites({ context, files }); + checkLegacyPrerequisites({ files }); const isLegacy = isLegacyClient(config); @@ -861,16 +853,16 @@ export const generateServices = async ({ // Import required packages and core files. if (!isLegacy) { files.services.import({ - module: clientModulePath({ sourceOutput: servicesOutput }), + module: clientModulePath({ config, sourceOutput: servicesOutput }), name: 'createClient', }); files.services.import({ - module: clientModulePath({ sourceOutput: servicesOutput }), + module: clientModulePath({ config, sourceOutput: servicesOutput }), name: 'createConfig', }); files.services.import({ asType: true, - module: clientModulePath({ sourceOutput: servicesOutput }), + module: clientModulePath({ config, sourceOutput: servicesOutput }), name: clientOptionsTypeName(), }); } else { @@ -944,37 +936,84 @@ export const generateServices = async ({ files.services.add(statement); } - if (client) { - for (const service of client.services) { - processService({ - client, - onClientImport: (imported) => { - files.services.import({ - module: clientModulePath({ sourceOutput: servicesOutput }), - name: imported, - }); - }, - onImport: (imported) => { - files.services.import({ - // this detection could be done safer, but it shouldn't cause any issues - asType: !imported.endsWith('Transformer'), - module: `./${files.types.getName(false)}`, - name: imported, - }); - }, - onNode: (node) => { - files.services.add(node); - }, - service, - }); - } - return; + for (const service of client.services) { + processService({ + client, + onClientImport: (imported) => { + files.services.import({ + module: clientModulePath({ config, sourceOutput: servicesOutput }), + name: imported, + }); + }, + onImport: (imported) => { + files.services.import({ + // this detection could be done safer, but it shouldn't cause any issues + asType: !imported.endsWith('Transformer'), + module: `./${files.types.getName(false)}`, + name: imported, + }); + }, + onNode: (node) => { + files.services.add(node); + }, + service, + }); } +}; - if (!context) { +export const generateServices = ({ context }: { context: IRContext }) => { + // TODO: parser - once services are a plugin, this logic can be simplified + if (!context.config.services.export) { return; } + checkPrerequisites({ context }); + + const file = context.createFile({ + id: servicesId, + path: 'services', + }); + const servicesOutput = file.getName(false); + + // import required packages and core files + file.import({ + module: clientModulePath({ + config: context.config, + sourceOutput: servicesOutput, + }), + name: 'createClient', + }); + file.import({ + module: clientModulePath({ + config: context.config, + sourceOutput: servicesOutput, + }), + name: 'createConfig', + }); + file.import({ + asType: true, + module: clientModulePath({ + config: context.config, + sourceOutput: servicesOutput, + }), + name: clientOptionsTypeName(), + }); + + // define client first + const statement = compiler.constVariable({ + exportConst: true, + expression: compiler.callExpression({ + functionName: 'createClient', + parameters: [ + compiler.callExpression({ + functionName: 'createConfig', + }), + ], + }), + name: 'client', + }); + file.add(statement); + // TODO: parser - generate services for (const path in context.ir.paths) { const pathItem = context.ir.paths[path as keyof IRPathsObject]; @@ -989,7 +1028,7 @@ export const generateServices = async ({ namespace: 'type', }); if (identifier.name) { - files.services.import({ + file.import({ // this detection could be done safer, but it shouldn't cause any issues asType: !identifier.name.endsWith('Transformer'), module: `./${context.file({ id: 'types' })!.getName(false)}`, diff --git a/packages/openapi-ts/src/generate/transformers.ts b/packages/openapi-ts/src/generate/transformers.ts index c5cd1a090..1d68d7b29 100644 --- a/packages/openapi-ts/src/generate/transformers.ts +++ b/packages/openapi-ts/src/generate/transformers.ts @@ -243,7 +243,8 @@ const generateResponseTransformer = ({ }; }; -export const generateResponseTransformers = async ({ +// handles only response transformers for now +export const generateLegacyTransformers = async ({ client, onNode, onRemoveNode, diff --git a/packages/openapi-ts/src/generate/types.ts b/packages/openapi-ts/src/generate/types.ts index 9913306d0..cc56f04f2 100644 --- a/packages/openapi-ts/src/generate/types.ts +++ b/packages/openapi-ts/src/generate/types.ts @@ -1613,41 +1613,34 @@ const schemaToType = ({ return type; }; -export const generateTypes = async ({ +export const generateLegacyTypes = async ({ client, - context, files, }: { - client: Client | undefined; - context: IRContext | undefined; + client: Client; files: Files; }): Promise => { - if (client) { - const config = getConfig(); - - if (config.types.export) { - files.types = new TypeScriptFile({ - dir: config.output.path, - name: 'types.ts', - }); - } + const config = getConfig(); - const onNode: TypesProps['onNode'] = (node) => { - files.types?.add(node); - }; + if (config.types.export) { + files.types = new TypeScriptFile({ + dir: config.output.path, + name: 'types.ts', + }); + } - for (const model of client.models) { - processModel({ client, model, onNode }); - } + const onNode: TypesProps['onNode'] = (node) => { + files.types?.add(node); + }; - processServiceTypes({ client, onNode }); - return; + for (const model of client.models) { + processModel({ client, model, onNode }); } - if (!context) { - return; - } + processServiceTypes({ client, onNode }); +}; +export const generateTypes = ({ context }: { context: IRContext }): void => { // TODO: parser - once types are a plugin, this logic can be simplified if (!context.config.types.export) { return; @@ -1680,6 +1673,9 @@ export const generateTypes = async ({ } } + // TODO: parser - once types are a plugin, this logic can be simplified + // provide config option on types to generate path types and services + // will set it to true if needed if (context.config.services.export || context.config.types.tree) { for (const path in context.ir.paths) { const pathItem = context.ir.paths[path as keyof IRPathsObject]; diff --git a/packages/openapi-ts/src/index.ts b/packages/openapi-ts/src/index.ts index f9eefb4de..4a530d961 100644 --- a/packages/openapi-ts/src/index.ts +++ b/packages/openapi-ts/src/index.ts @@ -3,9 +3,9 @@ import path from 'node:path'; import { loadConfig } from 'c12'; import { sync } from 'cross-spawn'; -import { generateOutput } from './generate/output'; +import { generateLegacyOutput, generateOutput } from './generate/output'; import type { IRContext } from './ir/context'; -import { parse, parseExperimental } from './openApi'; +import { parseExperimental, parseLegacy } from './openApi'; import type { ParserConfig } from './openApi/config'; import { operationFilterFn, @@ -363,8 +363,9 @@ export async function createClient( }); } + // fallback to legacy parser if (!context) { - const parsed = parse({ + const parsed = parseLegacy({ openApi, parserConfig, }); @@ -375,12 +376,11 @@ export async function createClient( logClientMessage(); Performance.start('generator'); - await generateOutput({ - client, - context, - openApi, - templates, - }); + if (context) { + await generateOutput({ context }); + } else if (client) { + await generateLegacyOutput({ client, openApi, templates }); + } Performance.end('generator'); Performance.start('postprocess'); diff --git a/packages/openapi-ts/src/ir/context.ts b/packages/openapi-ts/src/ir/context.ts index 84d1e8c3c..a25987526 100644 --- a/packages/openapi-ts/src/ir/context.ts +++ b/packages/openapi-ts/src/ir/context.ts @@ -48,6 +48,7 @@ export class IRContext = any> { * to the newly created file. */ public createFile(file: ContextFile): TypeScriptFile { + // TODO: parser - handle attempt to create duplicate const outputParts = file.path.split('/'); const outputDir = path.resolve( this.config.output.path, diff --git a/packages/openapi-ts/src/openApi/__tests__/index.spec.ts b/packages/openapi-ts/src/openApi/__tests__/index.spec.ts index 7faee4c5f..0c8716033 100644 --- a/packages/openapi-ts/src/openApi/__tests__/index.spec.ts +++ b/packages/openapi-ts/src/openApi/__tests__/index.spec.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { type OpenApi, parse } from '..'; +import { type OpenApi, parseLegacy } from '..'; import type { ParserConfig } from '../config'; import * as parseV2 from '../v2'; import * as parseV3 from '../v3'; @@ -32,7 +32,7 @@ describe('parse', () => { paths: {}, swagger: '2', }; - parse({ openApi: spec, parserConfig }); + parseLegacy({ openApi: spec, parserConfig }); expect(spy).toHaveBeenCalledWith(spec); const spec2: OpenApi = { @@ -43,7 +43,7 @@ describe('parse', () => { paths: {}, swagger: '2.0', }; - parse({ openApi: spec2, parserConfig }); + parseLegacy({ openApi: spec2, parserConfig }); expect(spy).toHaveBeenCalledWith(spec2); }); @@ -58,7 +58,7 @@ describe('parse', () => { openapi: '3', paths: {}, }; - parse({ openApi: spec, parserConfig }); + parseLegacy({ openApi: spec, parserConfig }); expect(spy).toHaveBeenCalledWith(spec); const spec2: OpenApi = { @@ -69,7 +69,7 @@ describe('parse', () => { openapi: '3.0', paths: {}, }; - parse({ openApi: spec2, parserConfig }); + parseLegacy({ openApi: spec2, parserConfig }); expect(spy).toHaveBeenCalledWith(spec2); const spec3: OpenApi = { @@ -80,13 +80,15 @@ describe('parse', () => { openapi: '3.1.0', paths: {}, }; - parse({ openApi: spec3, parserConfig }); + parseLegacy({ openApi: spec3, parserConfig }); expect(spy).toHaveBeenCalledWith(spec3); }); it('throws on unknown version', () => { - // @ts-expect-error - expect(() => parse({ openApi: { foo: 'bar' }, parserConfig })).toThrow( + expect(() => + // @ts-expect-error + parseLegacy({ openApi: { foo: 'bar' }, parserConfig }), + ).toThrow( `Unsupported OpenAPI specification: ${JSON.stringify({ foo: 'bar' }, null, 2)}`, ); }); diff --git a/packages/openapi-ts/src/openApi/index.ts b/packages/openapi-ts/src/openApi/index.ts index 8796a5a06..1130c473d 100644 --- a/packages/openapi-ts/src/openApi/index.ts +++ b/packages/openapi-ts/src/openApi/index.ts @@ -35,7 +35,7 @@ export type { OpenApiSchema as OpenApiV3Schema } from './v3/interfaces/OpenApiSc * all the models, services and schema's we should output. * @param openApi The OpenAPI spec that we have loaded from disk. */ -export function parse({ +export function parseLegacy({ openApi, parserConfig, }: { diff --git a/packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts b/packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts index 1079bbdff..00a16314e 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts @@ -2,7 +2,7 @@ import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler: () => {}, - handler_experimental: () => {}, + handlerLegacy: () => {}, name: '@hey-api/schemas', output: 'schemas', }; diff --git a/packages/openapi-ts/src/plugins/@hey-api/schemas/types.ts b/packages/openapi-ts/src/plugins/@hey-api/schemas/types.ts index ecfc8cad1..e9da894af 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/schemas/types.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/schemas/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -14,7 +14,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@hey-api/services/config.ts b/packages/openapi-ts/src/plugins/@hey-api/services/config.ts index 5f33b457a..628080f36 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/services/config.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/services/config.ts @@ -2,7 +2,7 @@ import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler: () => {}, - handler_experimental: () => {}, + handlerLegacy: () => {}, name: '@hey-api/services', output: 'services', }; diff --git a/packages/openapi-ts/src/plugins/@hey-api/services/types.ts b/packages/openapi-ts/src/plugins/@hey-api/services/types.ts index 17f320698..24a6c033f 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/services/types.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/services/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -14,7 +14,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@hey-api/types/config.ts b/packages/openapi-ts/src/plugins/@hey-api/types/config.ts index 4bc33926e..aee1ca4b2 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/types/config.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/types/config.ts @@ -2,7 +2,7 @@ import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler: () => {}, - handler_experimental: () => {}, + handlerLegacy: () => {}, name: '@hey-api/types', output: 'types', }; diff --git a/packages/openapi-ts/src/plugins/@hey-api/types/types.ts b/packages/openapi-ts/src/plugins/@hey-api/types/types.ts index 90b180000..ae6719c63 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/types/types.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/types/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -14,7 +14,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts b/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts index 597021259..97eea525f 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts @@ -32,7 +32,7 @@ import type { Files } from '../../../types/utils'; import { getConfig } from '../../../utils/config'; import { getServiceName } from '../../../utils/postprocess'; import { transformServiceName } from '../../../utils/transform'; -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; import type { PluginConfig as ReactQueryPluginConfig } from '../react-query'; import type { PluginConfig as SolidQueryPluginConfig } from '../solid-query'; import type { PluginConfig as SvelteQueryPluginConfig } from '../svelte-query'; @@ -686,7 +686,7 @@ const createQueryKeyLiteral = ({ return queryKeyLiteral; }; -export const handler: PluginHandler< +export const handlerLegacy: PluginLegacyHandler< | ReactQueryPluginConfig | SolidQueryPluginConfig | SvelteQueryPluginConfig @@ -699,7 +699,7 @@ export const handler: PluginHandler< file.import({ asType: true, - module: clientModulePath({ sourceOutput: plugin.output }), + module: clientModulePath({ config, sourceOutput: plugin.output }), name: clientOptionsTypeName(), }); @@ -1310,24 +1310,27 @@ export const handler: PluginHandler< } }; -export const handler_experimental: PluginHandlerExperimental< +export const handler: PluginHandler< | ReactQueryPluginConfig | SolidQueryPluginConfig | SvelteQueryPluginConfig | VueQueryPluginConfig -> = ({ context, files, plugin }) => { - checkPrerequisites({ files }); +> = ({ context, plugin }) => { + checkPrerequisites({ files: context.files }); - const file = files[plugin.name]; + const file = context.createFile({ + id: plugin.name, + path: plugin.output, + }); // file.import({ // asType: true, - // module: clientModulePath({ sourceOutput: plugin.output }), + // module: clientModulePath({ config: context.config, sourceOutput: plugin.output }), // name: clientOptionsTypeName(), // }); // const typesModulePath = relativeModulePath({ - // moduleOutput: files.types.getName(false), + // moduleOutput: context.files.types.getName(false), // sourceOutput: plugin.output, // }); @@ -1528,7 +1531,7 @@ export const handler_experimental: PluginHandlerExperimental< } // const servicesModulePath = relativeModulePath({ - // moduleOutput: files.services.getName(false), + // moduleOutput: context.files.services.getName(false), // sourceOutput: plugin.output, // }); diff --git a/packages/openapi-ts/src/plugins/@tanstack/react-query/config.ts b/packages/openapi-ts/src/plugins/@tanstack/react-query/config.ts index 47f10e99c..a677693fe 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/react-query/config.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/react-query/config.ts @@ -1,9 +1,9 @@ -import { handler, handler_experimental } from '../query-core/plugin'; +import { handler, handlerLegacy } from '../query-core/plugin'; import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler, - handler_experimental, + handlerLegacy, infiniteQueryOptions: true, mutationOptions: true, name: '@tanstack/react-query', diff --git a/packages/openapi-ts/src/plugins/@tanstack/react-query/types.ts b/packages/openapi-ts/src/plugins/@tanstack/react-query/types.ts index 9cfc8f2d5..58574b0dc 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/react-query/types.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/react-query/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -30,7 +30,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@tanstack/solid-query/config.ts b/packages/openapi-ts/src/plugins/@tanstack/solid-query/config.ts index 8de06edc0..44277aead 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/solid-query/config.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/solid-query/config.ts @@ -1,9 +1,9 @@ -import { handler, handler_experimental } from '../query-core/plugin'; +import { handler, handlerLegacy } from '../query-core/plugin'; import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler, - handler_experimental, + handlerLegacy, infiniteQueryOptions: true, mutationOptions: true, name: '@tanstack/solid-query', diff --git a/packages/openapi-ts/src/plugins/@tanstack/solid-query/types.ts b/packages/openapi-ts/src/plugins/@tanstack/solid-query/types.ts index dcf53b9f1..df9d9e328 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/solid-query/types.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/solid-query/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -30,7 +30,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@tanstack/svelte-query/config.ts b/packages/openapi-ts/src/plugins/@tanstack/svelte-query/config.ts index 46b04a4e6..d36ad23aa 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/svelte-query/config.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/svelte-query/config.ts @@ -1,9 +1,9 @@ -import { handler, handler_experimental } from '../query-core/plugin'; +import { handler, handlerLegacy } from '../query-core/plugin'; import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler, - handler_experimental, + handlerLegacy, infiniteQueryOptions: true, mutationOptions: true, name: '@tanstack/svelte-query', diff --git a/packages/openapi-ts/src/plugins/@tanstack/svelte-query/types.ts b/packages/openapi-ts/src/plugins/@tanstack/svelte-query/types.ts index 4fbe82223..b674f1220 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/svelte-query/types.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/svelte-query/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -30,7 +30,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/@tanstack/vue-query/config.ts b/packages/openapi-ts/src/plugins/@tanstack/vue-query/config.ts index b66b90cf9..1aa56b87f 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/vue-query/config.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/vue-query/config.ts @@ -1,9 +1,9 @@ -import { handler, handler_experimental } from '../query-core/plugin'; +import { handler, handlerLegacy } from '../query-core/plugin'; import type { PluginConfig } from './types'; export const defaultConfig: Required = { handler, - handler_experimental, + handlerLegacy, infiniteQueryOptions: true, mutationOptions: true, name: '@tanstack/vue-query', diff --git a/packages/openapi-ts/src/plugins/@tanstack/vue-query/types.ts b/packages/openapi-ts/src/plugins/@tanstack/vue-query/types.ts index 94f414d6a..63a5f58cd 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/vue-query/types.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/vue-query/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../../types'; +import type { PluginHandler, PluginLegacyHandler } from '../../types'; interface Config { /** @@ -30,7 +30,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/plugins/types.ts b/packages/openapi-ts/src/plugins/types.ts index 2dbb78212..005f2f364 100644 --- a/packages/openapi-ts/src/plugins/types.ts +++ b/packages/openapi-ts/src/plugins/types.ts @@ -2,16 +2,15 @@ import type { IRContext } from '../ir/context'; import type { Client } from '../types/client'; import type { Files } from '../types/utils'; -export type PluginHandler = (args: { +export type PluginLegacyHandler = (args: { client: Client; files: Files; - plugin: Omit, 'handler' | 'handler_experimental'>; + plugin: Omit, 'handler' | 'handlerLegacy'>; }) => void; -export type PluginHandlerExperimental = (args: { +export type PluginHandler = (args: { context: IRContext; - files: Files; - plugin: Omit, 'handler' | 'handler_experimental'>; + plugin: Omit, 'handler' | 'handlerLegacy'>; }) => void; type KeyTypes = string | number | symbol; @@ -28,9 +27,7 @@ export type DefaultPluginConfigsMap< > = { [K in U]: { handler: PluginHandler>>; - handler_experimental?: PluginHandlerExperimental< - Required> - >; + handlerLegacy: PluginLegacyHandler>>; name: string; output?: string; }; diff --git a/packages/openapi-ts/src/plugins/zod/config.ts b/packages/openapi-ts/src/plugins/zod/config.ts index 1dc743971..ebe64377e 100644 --- a/packages/openapi-ts/src/plugins/zod/config.ts +++ b/packages/openapi-ts/src/plugins/zod/config.ts @@ -1,9 +1,9 @@ -import { handler } from './plugin'; +import { handlerLegacy } from './plugin'; import type { PluginConfig } from './types'; export const defaultConfig: Required = { - handler, - handler_experimental: () => {}, + handler: () => {}, + handlerLegacy, name: 'zod', output: 'zod', }; diff --git a/packages/openapi-ts/src/plugins/zod/plugin.ts b/packages/openapi-ts/src/plugins/zod/plugin.ts index d8e33ab3b..eed8fec99 100644 --- a/packages/openapi-ts/src/plugins/zod/plugin.ts +++ b/packages/openapi-ts/src/plugins/zod/plugin.ts @@ -1,7 +1,7 @@ import { compiler } from '../../compiler'; import type { TypeScriptFile } from '../../generate/files'; import type { Client, Model } from '../../types/client'; -import type { PluginHandler } from '../types'; +import type { PluginLegacyHandler } from '../types'; import type { PluginConfig } from './types'; interface TypesProps { @@ -189,7 +189,7 @@ const processModel = (props: TypesProps) => { } }; -export const handler: PluginHandler = ({ +export const handlerLegacy: PluginLegacyHandler = ({ client, files, plugin, diff --git a/packages/openapi-ts/src/plugins/zod/types.ts b/packages/openapi-ts/src/plugins/zod/types.ts index 6faeafe70..7ed7f79a2 100644 --- a/packages/openapi-ts/src/plugins/zod/types.ts +++ b/packages/openapi-ts/src/plugins/zod/types.ts @@ -1,4 +1,4 @@ -import type { PluginHandler, PluginHandlerExperimental } from '../types'; +import type { PluginHandler, PluginLegacyHandler } from '../types'; interface Config { /** @@ -14,7 +14,7 @@ interface Config { export interface PluginConfig extends Config { handler: PluginHandler; - handler_experimental?: PluginHandlerExperimental; + handlerLegacy: PluginLegacyHandler; } export interface UserConfig extends Omit {} diff --git a/packages/openapi-ts/src/utils/__tests__/postprocess.spec.ts b/packages/openapi-ts/src/utils/__tests__/postprocess.spec.ts index 2d70d2339..dca7cd465 100644 --- a/packages/openapi-ts/src/utils/__tests__/postprocess.spec.ts +++ b/packages/openapi-ts/src/utils/__tests__/postprocess.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { parse } from '../../openApi'; +import { parseLegacy } from '../../openApi'; import type { ParserConfig } from '../../openApi/config'; import { getServiceName, postProcessClient } from '../postprocess'; @@ -35,7 +35,7 @@ describe('getServiceName', () => { describe('getServices', () => { it('should create a unnamed service if tags are empty', () => { - const parserClient = parse({ + const parserClient = parseLegacy({ openApi: { info: { title: 'x', @@ -69,7 +69,7 @@ describe('getServices', () => { describe('getServices', () => { it('should create a unnamed service if tags are empty', () => { - const parserClient = parse({ + const parserClient = parseLegacy({ openApi: { info: { title: 'x', diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index 5f2e18160..b9e9aa26b 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -9,7 +9,7 @@ const main = async () => { name: '@hey-api/client-fetch', }, // debug: true, - // experimental_parser: true, + experimental_parser: true, // input: './test/spec/v3-transforms.json', // input: './test/spec/v3.json', input: './test/spec/3.1.0/full.json',