diff --git a/.changeset/heavy-melons-fry.md b/.changeset/heavy-melons-fry.md new file mode 100644 index 000000000..d270c088d --- /dev/null +++ b/.changeset/heavy-melons-fry.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: support custom config file path diff --git a/packages/openapi-ts/bin/index.cjs b/packages/openapi-ts/bin/index.cjs index 33df5361a..90de5b482 100755 --- a/packages/openapi-ts/bin/index.cjs +++ b/packages/openapi-ts/bin/index.cjs @@ -23,6 +23,7 @@ const params = program .option('-d, --debug', 'Run in debug mode?') .option('--dry-run [value]', 'Skip writing files to disk?') .option('--exportCore [value]', 'Write core files to disk') + .option('-f, --file [value]', 'Path to the config file') .option( '-i, --input ', 'OpenAPI specification (path, url, or string content)', @@ -56,6 +57,9 @@ const processParams = (obj, booleanKeys) => { obj[key] = parsedValue; } } + if (obj.file) { + obj.configFile = obj.file; + } return obj; }; diff --git a/packages/openapi-ts/src/index.ts b/packages/openapi-ts/src/index.ts index 83dba0b2b..fe785dc36 100644 --- a/packages/openapi-ts/src/index.ts +++ b/packages/openapi-ts/src/index.ts @@ -171,7 +171,14 @@ const getTypes = (userConfig: ClientConfig): Config['types'] => { }; const initConfigs = async (userConfig: UserConfig): Promise => { + let configurationFile: string | undefined = undefined; + if (userConfig.configFile) { + const parts = userConfig.configFile.split('.'); + configurationFile = parts.slice(0, parts.length - 1).join('.'); + } + const { config: configFromFile } = await loadConfig({ + configFile: configurationFile, jitiOptions: { esmResolve: true, }, @@ -191,6 +198,7 @@ const initConfigs = async (userConfig: UserConfig): Promise => { const { base, client = 'fetch', + configFile = '', debug = false, dryRun = false, exportCore = true, @@ -233,6 +241,7 @@ const initConfigs = async (userConfig: UserConfig): Promise => { return setConfig({ base, client, + configFile, debug, dryRun, exportCore: isStandaloneClient(client) ? false : exportCore, diff --git a/packages/openapi-ts/src/openApi/common/parser/__tests__/operation.spec.ts b/packages/openapi-ts/src/openApi/common/parser/__tests__/operation.spec.ts index 616ab0bd5..883e89aa5 100644 --- a/packages/openapi-ts/src/openApi/common/parser/__tests__/operation.spec.ts +++ b/packages/openapi-ts/src/openApi/common/parser/__tests__/operation.spec.ts @@ -6,6 +6,7 @@ import { getOperationName, parseResponseStatusCode } from '../operation'; describe('getOperationName', () => { const options1: Parameters[0] = { client: 'fetch', + configFile: '', debug: false, dryRun: true, exportCore: false, @@ -29,6 +30,7 @@ describe('getOperationName', () => { const options2: Parameters[0] = { client: 'fetch', + configFile: '', debug: false, dryRun: true, exportCore: false, diff --git a/packages/openapi-ts/src/openApi/v2/parser/__tests__/getServices.spec.ts b/packages/openapi-ts/src/openApi/v2/parser/__tests__/getServices.spec.ts index d8eac0431..d375857ad 100644 --- a/packages/openapi-ts/src/openApi/v2/parser/__tests__/getServices.spec.ts +++ b/packages/openapi-ts/src/openApi/v2/parser/__tests__/getServices.spec.ts @@ -7,6 +7,7 @@ describe('getServices', () => { it('should create a unnamed service if tags are empty', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: true, exportCore: true, diff --git a/packages/openapi-ts/src/openApi/v3/parser/__tests__/getServices.spec.ts b/packages/openapi-ts/src/openApi/v3/parser/__tests__/getServices.spec.ts index aab29852d..77b35de2e 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/__tests__/getServices.spec.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/__tests__/getServices.spec.ts @@ -7,6 +7,7 @@ describe('getServices', () => { it('should create a unnamed service if tags are empty', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: true, exportCore: true, diff --git a/packages/openapi-ts/src/types/client.ts b/packages/openapi-ts/src/types/client.ts index 00fbcfa22..29fffdfa3 100644 --- a/packages/openapi-ts/src/types/client.ts +++ b/packages/openapi-ts/src/types/client.ts @@ -6,7 +6,10 @@ export interface Client { server: string; services: Service[]; /** - * Map of generated types, keys are type names + * Map of generated types where type names are keys. This is used to track + * uniquely generated types as we may want to deduplicate if there are + * multiple definitions with the same name but different value, or if we + * want to transform names. */ types: Record; version: string; diff --git a/packages/openapi-ts/src/types/config.ts b/packages/openapi-ts/src/types/config.ts index 90b90e2fb..2ddc50477 100644 --- a/packages/openapi-ts/src/types/config.ts +++ b/packages/openapi-ts/src/types/config.ts @@ -18,6 +18,11 @@ export interface ClientConfig { | 'fetch' | 'node' | 'xhr'; + /** + * Path to the config file. Set this value if you don't use the default + * config file name, or it's not located in the project root. + */ + configFile?: string; /** * Run in debug mode? * @default false diff --git a/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts b/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts index e1e7f2e80..3382dfc29 100644 --- a/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts +++ b/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts @@ -11,6 +11,7 @@ describe('registerHandlebarHelpers', () => { it('should register the helpers', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, @@ -40,6 +41,7 @@ describe('registerHandlebarTemplates', () => { it('should return correct templates', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, diff --git a/packages/openapi-ts/src/utils/write/__tests__/class.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/class.spec.ts index a44b145c0..033f3dcff 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/class.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/class.spec.ts @@ -13,6 +13,7 @@ describe('writeClientClass', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, diff --git a/packages/openapi-ts/src/utils/write/__tests__/client.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/client.spec.ts index b09fb17d6..9cbf22539 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/client.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/client.spec.ts @@ -13,6 +13,7 @@ describe('writeClient', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, diff --git a/packages/openapi-ts/src/utils/write/__tests__/core.spec.ts b/packages/openapi-ts/src/utils/write/__tests__/core.spec.ts index 30a495580..b80e90359 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/core.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/core.spec.ts @@ -26,6 +26,7 @@ describe('writeCore', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, @@ -81,6 +82,7 @@ describe('writeCore', () => { const config = setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, @@ -119,6 +121,7 @@ describe('writeCore', () => { const config = setConfig({ base: 'foo', client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, 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 0293f66cf..bee791c10 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/index.spec.ts @@ -13,6 +13,7 @@ describe('processIndex', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, 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 1bd3205fa..ab803adc2 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/models.spec.ts @@ -13,6 +13,7 @@ describe('processTypes', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, 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 29d8e872e..6712d6587 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts @@ -14,6 +14,7 @@ describe('processSchemas', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, 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 2ae878260..5e41069ca 100644 --- a/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts +++ b/packages/openapi-ts/src/utils/write/__tests__/services.spec.ts @@ -14,6 +14,7 @@ describe('processServices', () => { it('writes to filesystem', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, @@ -107,6 +108,7 @@ describe('methodNameBuilder', () => { it('use default name', async () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true, @@ -145,6 +147,7 @@ describe('methodNameBuilder', () => { setConfig({ client: 'fetch', + configFile: '', debug: false, dryRun: false, exportCore: true,