fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1826 from hey-api/fix/config-func

fix: allow config to be a function

authored by

Lubos and committed by
GitHub
672796fc fad7b408

+124 -116
+6
.changeset/metal-lizards-check.md
··· 1 + --- 2 + '@hey-api/rollup-plugin': patch 3 + '@hey-api/openapi-ts': patch 4 + --- 5 + 6 + fix: allow config to be a function
+3 -3
packages/openapi-ts/src/getLogs.ts
··· 1 1 import type { Config, UserConfig } from './types/config'; 2 2 3 - export const getLogs = (userConfig: UserConfig): Config['logs'] => { 3 + export const getLogs = (userConfig: UserConfig | undefined): Config['logs'] => { 4 4 let logs: Config['logs'] = { 5 5 level: 'info', 6 6 path: process.cwd(), 7 7 }; 8 - if (typeof userConfig.logs === 'string') { 8 + if (typeof userConfig?.logs === 'string') { 9 9 logs.path = userConfig.logs; 10 10 } else { 11 11 logs = { 12 12 ...logs, 13 - ...userConfig.logs, 13 + ...userConfig?.logs, 14 14 }; 15 15 } 16 16 return logs;
+11 -5
packages/openapi-ts/src/index.ts
··· 11 11 import { registerHandlebarTemplates } from './utils/handlebars'; 12 12 import { Performance, PerformanceReport } from './utils/performance'; 13 13 14 + type Configs = UserConfig | (() => UserConfig) | (() => Promise<UserConfig>); 15 + 14 16 /** 15 17 * Generate a client from the provided configuration. 16 18 * 17 19 * @param userConfig User provided {@link UserConfig} configuration. 18 20 */ 19 21 export const createClient = async ( 20 - userConfig: UserConfig, 22 + userConfig?: Configs, 21 23 ): Promise<ReadonlyArray<Client | IR.Context>> => { 24 + const resolvedConfig = 25 + typeof userConfig === 'function' ? await userConfig() : userConfig; 26 + 22 27 let configs: Config[] = []; 23 28 24 29 try { 25 30 Performance.start('createClient'); 26 31 27 32 Performance.start('config'); 28 - configs = await initConfigs(userConfig); 33 + configs = await initConfigs(resolvedConfig); 29 34 Performance.end('config'); 30 35 31 36 Performance.start('handlebars'); ··· 61 66 return result; 62 67 } catch (error) { 63 68 const config = configs[0] as Config | undefined; 64 - const dryRun = config ? config.dryRun : userConfig?.dryRun; 69 + const dryRun = config ? config.dryRun : resolvedConfig?.dryRun; 65 70 // TODO: add setting for log output 66 71 if (!dryRun) { 67 - const logs = config?.logs ?? getLogs(userConfig); 72 + const logs = config?.logs ?? getLogs(resolvedConfig); 68 73 if (logs.level !== 'silent') { 69 74 const logName = `openapi-ts-error-${Date.now()}.log`; 70 75 const logsDir = path.resolve(process.cwd(), logs.path ?? ''); ··· 82 87 /** 83 88 * Type helper for openapi-ts.config.ts, returns {@link UserConfig} object 84 89 */ 85 - export const defineConfig = (config: UserConfig): UserConfig => config; 90 + export const defineConfig = async (config: Configs): Promise<UserConfig> => 91 + typeof config === 'function' ? await config() : config; 86 92 87 93 export { defaultPlugins } from './initConfigs'; 88 94 export type { IR } from './ir/types';
+2 -2
packages/openapi-ts/src/initConfigs.ts
··· 240 240 }; 241 241 242 242 export const initConfigs = async ( 243 - userConfig: UserConfig, 243 + userConfig: UserConfig | undefined, 244 244 ): Promise<Config[]> => { 245 245 let configurationFile: string | undefined = undefined; 246 - if (userConfig.configFile) { 246 + if (userConfig?.configFile) { 247 247 const parts = userConfig.configFile.split('.'); 248 248 configurationFile = parts.slice(0, parts.length - 1).join('.'); 249 249 }
+99 -102
packages/openapi-ts/test/openapi-ts.config.ts
··· 2 2 3 3 import { defineConfig } from '../src'; 4 4 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, 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', 53 36 }, 54 - // @ts-ignore 55 - { 56 - // name: '@hey-api/schemas', 57 - // type: 'json', 37 + logs: { 38 + // level: 'debug', 39 + path: './logs', 58 40 }, 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', 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'), 72 48 }, 73 - // @ts-ignore 74 - { 75 - bigInt: true, 76 - dates: true, 77 - // name: '@hey-api/transformers', 78 - }, 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, 89 - }, 90 - // @ts-ignore 91 - { 92 - // name: 'fastify', 93 - }, 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 + }; 110 107 });
+3 -4
packages/rollup-plugin/src/index.ts
··· 1 - import { createClient, type UserConfig } from '@hey-api/openapi-ts'; 1 + import { createClient } from '@hey-api/openapi-ts'; 2 2 3 3 export function heyApiPlugin(options?: { 4 4 /** 5 5 * `@hey-api/openapi-ts` configuration options. 6 6 */ 7 - config?: UserConfig; 7 + config?: Parameters<typeof createClient>[0]; 8 8 }) { 9 9 return { 10 10 buildStart: async () => { 11 - // @ts-expect-error 12 - await createClient(options?.config ?? {}); 11 + await createClient(options?.config); 13 12 }, 14 13 name: 'hey-api-plugin', 15 14 };