Skip to content

Commit

Permalink
Merge pull request #242 from hey-api/feat/support-more-config-file-types
Browse files Browse the repository at this point in the history
feat(config): use c12 for loading config files
  • Loading branch information
jordanshatford authored Apr 4, 2024
2 parents 509bdee + c939d57 commit 4ee9ba4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 70 deletions.
17 changes: 15 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ description: Configure openapi-ts.

# Configuration

`openapi-ts` supports loading configuration from a file inside your project root directory. You can either create a `openapi-ts.config.cjs` file
`openapi-ts` supports loading configuration from a file inside your project root directory. Your configuration can be in many formats (as supported by [c12](https://github.com/unjs/c12)). Here are some example configs:

`openapi-ts.config.ts`

```ts
import { defineConfig } from '@hey-api/openapi-ts'

export default defineConfig({
input: 'path/to/openapi.json',
output: 'src/client',
})
```

`openapi-ts.config.cjs`

```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
Expand All @@ -15,7 +28,7 @@ module.exports = {
}
```

or `openapi-ts.config.mjs`
`openapi-ts.config.mjs`

```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "11.5.4",
"c12": "1.10.0",
"camelcase": "8.0.0",
"commander": "12.0.0",
"handlebars": "4.7.8"
Expand Down
28 changes: 10 additions & 18 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';

import { loadConfig } from 'c12';
import { sync } from 'cross-spawn';

import { parse } from './openApi';
Expand All @@ -15,9 +15,6 @@ import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;

// TODO: add support for `openapi-ts.config.ts`
const configFiles = ['openapi-ts.config.js', 'openapi-ts.config.cjs', 'openapi-ts.config.mjs'];

// Mapping of all dependencies used in each client. These should be installed in the generated client package
const clientDependencies: Record<Config['client'], string[]> = {
angular: ['@angular/common', '@angular/core', 'rxjs'],
Expand Down Expand Up @@ -78,20 +75,15 @@ const logMissingDependenciesWarning = (client: Config['client'], dependencies: D
}
};

const getConfigFromFile = async (): Promise<UserConfig | undefined> => {
const configPath = configFiles
.map(file => pathToFileURL(path.resolve(process.cwd(), file)))
.find(filePath => existsSync(filePath));
if (!configPath) {
return;
}
// @ts-ignore
const exported = await import(configPath);
return exported.default as UserConfig;
};

const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) => {
const userConfigFromFile = await getConfigFromFile();
const { config: userConfigFromFile } = await loadConfig<UserConfig>({
jitiOptions: {
esmResolve: true,
},
name: 'openapi-ts',
overrides: userConfig,
});

if (userConfigFromFile) {
userConfig = { ...userConfigFromFile, ...userConfig };
}
Expand Down
Loading

0 comments on commit 4ee9ba4

Please sign in to comment.