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(generate): warn about missing dependencies used in generated client #125

Merged
merged 2 commits into from
Mar 23, 2024
Merged
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
23 changes: 22 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 used in each client. These should be installed in the generated client package
const clientDependencies: Record<Config['client'], string[]> = {
angular: ['@angular/common', '@angular/core', 'rxjs'],
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);
jordanshatford marked this conversation as resolved.
Show resolved Hide resolved
if (missing.length > 0) {
console.log('⚠️ Dependencies used in generated client are missing: ' + missing.join(' '));
}
};

const getConfigFromFile = async (): Promise<UserConfig | undefined> => {
const configPath = configFiles
.map(file => pathToFileURL(path.resolve(process.cwd(), file)))
Expand Down Expand Up @@ -174,11 +193,13 @@ export async function createClient(userConfig: UserConfig): Promise<Client> {

if (config.write) {
logClientMessage(config.client);
logMissingDependenciesWarning(config.client, dependencies);
await writeClient(client, templates, config);
processOutput(config, dependencies);
}

console.log('✨ Done! Your client is located in:', config.output);

return client;
}

Expand Down