Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn user about missing required dependencies in generated client #110

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion 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', '@angular/core', 'rxjs'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought CLI is used for Angular projects? https://angular.io/guide/setup-local

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but the generated client for angular only imports code from these packages (so cli is not required). These are a map of dependencies that the client we generate needs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. In that case, can you update the warning message to clarify those are the dependencies that need to be installed in order for the client to work? Right now it just says they're required, but not why

axios: ['axios'],
fetch: [],
node: ['node-fetch'],
xhr: [],
};

const processOutput = (config: Config, dependencies: Dependencies) => {
if (config.format) {
if (dependencies.prettier) {
Expand All @@ -35,12 +44,15 @@ const processOutput = (config: Config, dependencies: Dependencies) => {
};

const inferClient = (dependencies: Dependencies): Config['client'] => {
if (dependencies['@angular/cli']) {
if (Object.keys(dependencies).some(d => d.startsWith('@angular'))) {
return 'angular';
}
if (dependencies.axios) {
return 'axios';
}
if (dependencies['node-fetch']) {
return 'node';
}
return 'fetch';
};

Expand All @@ -59,6 +71,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 +130,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