From 5fb4bbdcc1b225bc32b5a05518e42ba1686b5f3a Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Fri, 22 Mar 2024 10:39:40 +1100 Subject: [PATCH 1/3] feat: warn user about missing required dependencies in generated client --- src/index.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index ea03e0ea4..9059c5b5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,15 @@ type Dependencies = Record; // 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 required by each client. These should be installed in the generated client package +const clientDependencies: Record = { + angular: ['@angular/common', 'rxjs'], + axios: ['axios'], + fetch: [], + node: ['node-fetch'], + xhr: [], +}; + const processOutput = (config: Config, dependencies: Dependencies) => { if (config.format) { if (dependencies.prettier) { @@ -35,11 +44,10 @@ const processOutput = (config: Config, dependencies: Dependencies) => { }; const inferClient = (dependencies: Dependencies): Config['client'] => { - if (dependencies['@angular/cli']) { - return 'angular'; - } - if (dependencies.axios) { - return 'axios'; + for (const [c, deps] of Object.entries(clientDependencies)) { + if (deps.every(d => dependencies[d])) { + return c as Config['client']; + } } return 'fetch'; }; @@ -59,6 +67,13 @@ const logClientMessage = (client: Config['client']) => { } }; +const logMissingDependenciesWarning = (client: Config['client'], dependencies: Dependencies) => { + const missing = clientDependencies[client].filter(d => dependencies[d] === undefined); + if (missing.length > 0) { + console.log('❗️ Missing required dependencies: ' + missing.join(' ')); + } +}; + const getConfigFromFile = async (): Promise => { const configPath = configFiles .map(file => pathToFileURL(path.resolve(process.cwd(), file))) @@ -111,6 +126,9 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) => } const client = userConfig.client || inferClient(dependencies); + + logMissingDependenciesWarning(client, dependencies); + const output = path.resolve(process.cwd(), userConfig.output); const config: Config = { From 78fbf11813550f267ab25a5569ab40340efaecf8 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Fri, 22 Mar 2024 13:00:31 +1100 Subject: [PATCH 2/3] chore: address pr comments about inferring client type --- src/index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9059c5b5d..3b73aff36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,10 +44,14 @@ const processOutput = (config: Config, dependencies: Dependencies) => { }; const inferClient = (dependencies: Dependencies): Config['client'] => { - for (const [c, deps] of Object.entries(clientDependencies)) { - if (deps.every(d => dependencies[d])) { - return c as Config['client']; - } + if (Object.keys(dependencies).some(d => d.startsWith('@angular'))) { + return 'angular'; + } + if (dependencies.axios) { + return 'axios'; + } + if (dependencies['node-fetch']) { + return 'node'; } return 'fetch'; }; From de9b824491e00613cd2bbf82e2ba1de822438100 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Fri, 22 Mar 2024 13:21:58 +1100 Subject: [PATCH 3/3] chore: add angular dependency that was missing from list --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 3b73aff36..57f5128a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ const configFiles = ['openapi-ts.config.js', 'openapi-ts.config.cjs', 'openapi-t // Mapping of all dependencies required by each client. These should be installed in the generated client package const clientDependencies: Record = { - angular: ['@angular/common', 'rxjs'], + angular: ['@angular/common', '@angular/core', 'rxjs'], axios: ['axios'], fetch: [], node: ['node-fetch'],