Skip to content

Commit

Permalink
feat: warn user about missing required dependencies in generated client
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 21, 2024
1 parent 7fc14c7 commit 9a84f4a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ 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 required by each client. These should be installed in the generated client package
const clientDependencies: Record<Config['client'], string[]> = {
angular: ['@angular/common', 'rxjs'],
axios: ['axios', 'form-data'],
fetch: [],
node: ['form-data', 'node-fetch'],
xhr: [],
};

const processOutput = (config: Config, dependencies: Dependencies) => {
if (config.format) {
if (dependencies.prettier) {
Expand All @@ -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';
};
Expand All @@ -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<UserConfig | undefined> => {
const configPath = configFiles
.map(file => pathToFileURL(path.resolve(process.cwd(), file)))
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 9a84f4a

Please sign in to comment.