Skip to content

Commit 90886c1

Browse files
committed
fix: allow config to be a function
1 parent fad7b40 commit 90886c1

File tree

6 files changed

+124
-116
lines changed

6 files changed

+124
-116
lines changed

.changeset/metal-lizards-check.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hey-api/rollup-plugin': patch
3+
'@hey-api/openapi-ts': patch
4+
---
5+
6+
fix: allow config to be a function

packages/openapi-ts/src/getLogs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { Config, UserConfig } from './types/config';
22

3-
export const getLogs = (userConfig: UserConfig): Config['logs'] => {
3+
export const getLogs = (userConfig: UserConfig | undefined): Config['logs'] => {
44
let logs: Config['logs'] = {
55
level: 'info',
66
path: process.cwd(),
77
};
8-
if (typeof userConfig.logs === 'string') {
8+
if (typeof userConfig?.logs === 'string') {
99
logs.path = userConfig.logs;
1010
} else {
1111
logs = {
1212
...logs,
13-
...userConfig.logs,
13+
...userConfig?.logs,
1414
};
1515
}
1616
return logs;

packages/openapi-ts/src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ import type { Config, UserConfig } from './types/config';
1111
import { registerHandlebarTemplates } from './utils/handlebars';
1212
import { Performance, PerformanceReport } from './utils/performance';
1313

14+
type Configs = UserConfig | (() => UserConfig) | (() => Promise<UserConfig>);
15+
1416
/**
1517
* Generate a client from the provided configuration.
1618
*
1719
* @param userConfig User provided {@link UserConfig} configuration.
1820
*/
1921
export const createClient = async (
20-
userConfig: UserConfig,
22+
userConfig?: Configs,
2123
): Promise<ReadonlyArray<Client | IR.Context>> => {
24+
const resolvedConfig =
25+
typeof userConfig === 'function' ? await userConfig() : userConfig;
26+
2227
let configs: Config[] = [];
2328

2429
try {
2530
Performance.start('createClient');
2631

2732
Performance.start('config');
28-
configs = await initConfigs(userConfig);
33+
configs = await initConfigs(resolvedConfig);
2934
Performance.end('config');
3035

3136
Performance.start('handlebars');
@@ -61,10 +66,10 @@ export const createClient = async (
6166
return result;
6267
} catch (error) {
6368
const config = configs[0] as Config | undefined;
64-
const dryRun = config ? config.dryRun : userConfig?.dryRun;
69+
const dryRun = config ? config.dryRun : resolvedConfig?.dryRun;
6570
// TODO: add setting for log output
6671
if (!dryRun) {
67-
const logs = config?.logs ?? getLogs(userConfig);
72+
const logs = config?.logs ?? getLogs(resolvedConfig);
6873
if (logs.level !== 'silent') {
6974
const logName = `openapi-ts-error-${Date.now()}.log`;
7075
const logsDir = path.resolve(process.cwd(), logs.path ?? '');
@@ -82,7 +87,8 @@ export const createClient = async (
8287
/**
8388
* Type helper for openapi-ts.config.ts, returns {@link UserConfig} object
8489
*/
85-
export const defineConfig = (config: UserConfig): UserConfig => config;
90+
export const defineConfig = async (config: Configs): Promise<UserConfig> =>
91+
typeof config === 'function' ? await config() : config;
8692

8793
export { defaultPlugins } from './initConfigs';
8894
export type { IR } from './ir/types';

packages/openapi-ts/src/initConfigs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ const getWatch = (
240240
};
241241

242242
export const initConfigs = async (
243-
userConfig: UserConfig,
243+
userConfig: UserConfig | undefined,
244244
): Promise<Config[]> => {
245245
let configurationFile: string | undefined = undefined;
246-
if (userConfig.configFile) {
246+
if (userConfig?.configFile) {
247247
const parts = userConfig.configFile.split('.');
248248
configurationFile = parts.slice(0, parts.length - 1).join('.');
249249
}

packages/openapi-ts/test/openapi-ts.config.ts

Lines changed: 99 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,106 @@ import path from 'node:path';
22

33
import { defineConfig } from '../src';
44

5-
export default defineConfig({
6-
// experimentalParser: false,
7-
input: {
8-
branch: 'main',
9-
// exclude: '^#/components/schemas/ModelWithCircularReference$',
10-
// include:
11-
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
12-
organization: 'hey-api',
13-
// path: {
14-
// components: {},
15-
// info: {
16-
// version: '1.0.0',
17-
// },
18-
// openapi: '3.1.0',
19-
// paths: {},
20-
// },
21-
// path: path.resolve(__dirname, 'spec', '3.1.x', 'full.json'),
22-
// path: 'http://localhost:4000/',
23-
path: 'https://get.heyapi.dev/',
24-
// path: 'https://get.heyapi.dev/hey-api/backend?branch=main&version=1.0.0',
25-
// path: 'http://localhost:8000/openapi.json',
26-
// path: './test/spec/v3-transforms.json',
27-
// path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
28-
// path: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml',
29-
// project: 'backend',
30-
project: 'upload-openapi-spec',
31-
// version: '1.0.0',
32-
},
33-
logs: {
34-
// level: 'debug',
35-
path: './logs',
36-
},
37-
// name: 'foo',
38-
output: {
39-
// case: 'snake_case',
40-
// format: 'prettier',
41-
// indexFile: false,
42-
// lint: 'eslint',
43-
path: path.resolve(__dirname, 'generated', 'sample'),
44-
},
45-
plugins: [
46-
// @ts-ignore
47-
{
48-
baseUrl: false,
49-
// bundle: true,
50-
exportFromIndex: true,
51-
name: '@hey-api/client-fetch',
52-
strictBaseUrl: true,
53-
},
54-
// @ts-ignore
55-
{
56-
// name: '@hey-api/schemas',
57-
// type: 'json',
58-
},
59-
// @ts-ignore
60-
{
61-
// asClass: true,
62-
// auth: false,
63-
// client: false,
64-
// include...
65-
name: '@hey-api/sdk',
66-
// operationId: false,
67-
// serviceNameBuilder: '^Parameters',
68-
// throwOnError: true,
69-
// transformer: '@hey-api/transformers',
70-
// transformer: true,
71-
// validator: 'zod',
72-
},
73-
// @ts-ignore
74-
{
75-
bigInt: true,
76-
dates: true,
77-
// name: '@hey-api/transformers',
5+
// @ts-ignore
6+
// eslint-disable-next-line arrow-body-style
7+
export default defineConfig(() => {
8+
// ...
9+
return {
10+
// experimentalParser: false,
11+
input: {
12+
branch: 'main',
13+
// exclude: '^#/components/schemas/ModelWithCircularReference$',
14+
// include:
15+
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
16+
organization: 'hey-api',
17+
// path: {
18+
// components: {},
19+
// info: {
20+
// version: '1.0.0',
21+
// },
22+
// openapi: '3.1.0',
23+
// paths: {},
24+
// },
25+
// path: path.resolve(__dirname, 'spec', '3.1.x', 'full.json'),
26+
// path: 'http://localhost:4000/',
27+
path: 'https://get.heyapi.dev/',
28+
// path: 'https://get.heyapi.dev/hey-api/backend?branch=main&version=1.0.0',
29+
// path: 'http://localhost:8000/openapi.json',
30+
// path: './test/spec/v3-transforms.json',
31+
// path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
32+
// path: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml',
33+
// project: 'backend',
34+
project: 'upload-openapi-spec',
35+
// version: '1.0.0',
7836
},
79-
// @ts-ignore
80-
{
81-
// enums: 'typescript',
82-
// enums: 'typescript+namespace',
83-
// enums: 'javascript',
84-
// enumsCase: 'camelCase',
85-
// exportInlineEnums: true,
86-
// identifierCase: 'preserve',
87-
name: '@hey-api/typescript',
88-
// tree: true,
37+
logs: {
38+
// level: 'debug',
39+
path: './logs',
8940
},
90-
// @ts-ignore
91-
{
92-
// name: 'fastify',
41+
// name: 'foo',
42+
output: {
43+
// case: 'snake_case',
44+
// format: 'prettier',
45+
// indexFile: false,
46+
// lint: 'eslint',
47+
path: path.resolve(__dirname, 'generated', 'sample'),
9348
},
94-
// @ts-ignore
95-
{
96-
name: '@tanstack/react-query',
97-
},
98-
// @ts-ignore
99-
{
100-
// exportFromIndex: true,
101-
// name: 'zod',
102-
},
103-
],
104-
// useOptions: false,
105-
// watch: {
106-
// enabled: true,
107-
// interval: 1_000,
108-
// timeout: 60_000,
109-
// },
49+
plugins: [
50+
{
51+
baseUrl: false,
52+
// bundle: true,
53+
exportFromIndex: true,
54+
name: '@hey-api/client-fetch',
55+
strictBaseUrl: true,
56+
},
57+
{
58+
// name: '@hey-api/schemas',
59+
// type: 'json',
60+
},
61+
{
62+
// asClass: true,
63+
// auth: false,
64+
// client: false,
65+
// include...
66+
name: '@hey-api/sdk',
67+
// operationId: false,
68+
// serviceNameBuilder: '^Parameters',
69+
// throwOnError: true,
70+
// transformer: '@hey-api/transformers',
71+
// transformer: true,
72+
// validator: 'zod',
73+
},
74+
{
75+
bigInt: true,
76+
dates: true,
77+
// name: '@hey-api/transformers',
78+
},
79+
{
80+
// enums: 'typescript',
81+
// enums: 'typescript+namespace',
82+
// enums: 'javascript',
83+
// enumsCase: 'camelCase',
84+
// exportInlineEnums: true,
85+
// identifierCase: 'preserve',
86+
name: '@hey-api/typescript',
87+
// tree: true,
88+
},
89+
{
90+
// name: 'fastify',
91+
},
92+
{
93+
name: '@tanstack/react-query',
94+
},
95+
{
96+
// exportFromIndex: true,
97+
// name: 'zod',
98+
},
99+
],
100+
// useOptions: false,
101+
// watch: {
102+
// enabled: true,
103+
// interval: 1_000,
104+
// timeout: 60_000,
105+
// },
106+
};
110107
});

packages/rollup-plugin/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { createClient, type UserConfig } from '@hey-api/openapi-ts';
1+
import { createClient } from '@hey-api/openapi-ts';
22

33
export function heyApiPlugin(options?: {
44
/**
55
* `@hey-api/openapi-ts` configuration options.
66
*/
7-
config?: UserConfig;
7+
config?: Parameters<typeof createClient>[0];
88
}) {
99
return {
1010
buildStart: async () => {
11-
// @ts-expect-error
12-
await createClient(options?.config ?? {});
11+
await createClient(options?.config);
1312
},
1413
name: 'hey-api-plugin',
1514
};

0 commit comments

Comments
 (0)