From a4ef67efe3aff663f97abe3cff909f0abef11097 Mon Sep 17 00:00:00 2001 From: Jontze Date: Tue, 5 Mar 2024 13:32:45 +0100 Subject: [PATCH] fix(app): configure http agent with proxy url if present in environment Fixes #OpenAPITools/openapi-generator-cli#714 --- apps/generator-cli/src/app/app.module.ts | 60 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/apps/generator-cli/src/app/app.module.ts b/apps/generator-cli/src/app/app.module.ts index 3abf47676..f67633860 100644 --- a/apps/generator-cli/src/app/app.module.ts +++ b/apps/generator-cli/src/app/app.module.ts @@ -1,16 +1,33 @@ -import {Inject, Module, OnApplicationBootstrap} from '@nestjs/common'; -import {HttpModule} from '@nestjs/axios'; -import {Command} from 'commander'; +import { Inject, Module, OnApplicationBootstrap } from '@nestjs/common'; +import { HttpModule, HttpModuleOptions } from '@nestjs/axios'; +import { Command } from 'commander'; -import {COMMANDER_PROGRAM, LOGGER} from './constants'; -import {VersionManagerController} from './controllers/version-manager.controller'; -import {ConfigService, GeneratorService, PassThroughService, UIService, VersionManagerService} from './services'; +import { COMMANDER_PROGRAM, LOGGER } from './constants'; +import { VersionManagerController } from './controllers/version-manager.controller'; +import { + ConfigService, + GeneratorService, + PassThroughService, + UIService, + VersionManagerService, +} from './services'; +import { HttpsProxyAgent } from 'https-proxy-agent'; + +const proxyUrl = process.env.HTTP_PROXY || process.env.HTTPS_PROXY; +const httpModuleConfig: HttpModuleOptions = {}; + +if (proxyUrl) { + httpModuleConfig.proxy = false; + httpModuleConfig.httpsAgent = new HttpsProxyAgent(proxyUrl); +} @Module({ - imports: [HttpModule], - controllers: [ - VersionManagerController + imports: [ + HttpModule.register({ + ...httpModuleConfig, + }), ], + controllers: [VersionManagerController], providers: [ UIService, ConfigService, @@ -19,26 +36,31 @@ import {ConfigService, GeneratorService, PassThroughService, UIService, VersionM VersionManagerService, { provide: COMMANDER_PROGRAM, - useValue: new Command('openapi-generator-cli').helpOption(false).usage(' []').option( '--openapitools ', 'Use the specified openapi-generator-cli configuration file') + useValue: new Command('openapi-generator-cli') + .helpOption(false) + .usage(' []') + .option( + '--openapitools ', + 'Use the specified openapi-generator-cli configuration file', + ), }, - {provide: LOGGER, useValue: console} - ] + { provide: LOGGER, useValue: console }, + ], }) export class AppModule implements OnApplicationBootstrap { - constructor( @Inject(COMMANDER_PROGRAM) private readonly program: Command, private readonly versionManager: VersionManagerService, - private readonly passThroughService: PassThroughService - ) { - } + private readonly passThroughService: PassThroughService, + ) {} onApplicationBootstrap = async () => { - let selectedVersion = this.versionManager.getSelectedVersion(); if (!selectedVersion) { - const [{version}] = await this.versionManager.search(['latest']).toPromise(); + const [{ version }] = await this.versionManager + .search(['latest']) + .toPromise(); await this.versionManager.setSelectedVersion(version); selectedVersion = version; } @@ -46,7 +68,5 @@ export class AppModule implements OnApplicationBootstrap { await this.versionManager.downloadIfNeeded(selectedVersion); await this.passThroughService.init(); this.program.parse(process.argv); - }; - }