diff --git a/src/blocks/publishBlocks.ts b/src/blocks/publishBlocks.ts index 0d8120c7..d20f3da0 100644 --- a/src/blocks/publishBlocks.ts +++ b/src/blocks/publishBlocks.ts @@ -43,6 +43,7 @@ const uploadBlock = async ( const url = `${config.blockstoreApiUrl}/blocks/publish`; return fetch(url, { + agent: config.agent, method: 'POST', body: form, headers: { diff --git a/src/functions/config.ts b/src/functions/config.ts index 41227944..7e427264 100644 --- a/src/functions/config.ts +++ b/src/functions/config.ts @@ -2,7 +2,8 @@ import fs from 'fs-extra'; import path from 'path'; import os from 'os'; import prompts from 'prompts'; -import { AgentOptions } from 'https'; +import https, { AgentOptions } from 'https'; +import { setHttpsAgent } from './utils'; export type GlobalConfig = { auth: { @@ -29,6 +30,7 @@ export type LocalConfig = { skipCompile?: boolean; includes?: string[]; tenantId?: string; + agent?: https.Agent; }; export type CustomConfig = { @@ -160,6 +162,10 @@ class Config { return !!this.config.skipCompile; } + get agent(): https.Agent | undefined { + return setHttpsAgent(this.config.agentOptions); + } + get identifier(): string { if (!this._identifier) { this._identifier = diff --git a/src/functions/publishAppFunctions.ts b/src/functions/publishAppFunctions.ts index e940f14a..6dc67dbe 100644 --- a/src/functions/publishAppFunctions.ts +++ b/src/functions/publishAppFunctions.ts @@ -80,6 +80,7 @@ const uploadAppFunctions = async ( } const url = `${config.builderApiUrl}/artifacts/actions/${applicationId}/functions`; return fetch(url, { + agent: config.agent, method: 'POST', body: form, headers: { diff --git a/src/functions/utils.ts b/src/functions/utils.ts new file mode 100644 index 00000000..e0b3421b --- /dev/null +++ b/src/functions/utils.ts @@ -0,0 +1,29 @@ +import path from 'path'; +import fs from 'fs-extra'; +import https, { AgentOptions } from 'https'; + +export function setHttpsAgent( + agentOptions?: AgentOptions, +): https.Agent | undefined { + let options: AgentOptions | undefined; + + if (agentOptions) { + options = (['ca', 'cert', 'key'] as const).reduce( + (acc, key) => { + if (typeof agentOptions[key] === 'string') { + return { + ...acc, + [key]: fs.readFileSync(path.resolve(agentOptions[key] as string)), + }; + } + + return acc; + }, + agentOptions, + ); + } + + const agent = agentOptions && options ? new https.Agent(options) : undefined; + + return agent; +} diff --git a/src/functions/validations.ts b/src/functions/validations.ts index c2515398..44c0519b 100644 --- a/src/functions/validations.ts +++ b/src/functions/validations.ts @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/restrict-template-expressions */ import fetch from 'node-fetch'; import path from 'path'; -import https, { AgentOptions } from 'https'; -import fs from 'fs-extra'; import chalk from 'chalk'; import { Validator, ValidatorResult, ValidationError } from 'jsonschema'; @@ -26,28 +24,9 @@ export type ValidationResult = { const fetchRemoteSchema = async ( schemaUrl: string, - agentOptions?: AgentOptions, + config: Config, ): Promise => { - let options: AgentOptions | undefined; - if (agentOptions) { - options = (['ca', 'cert', 'key'] as const).reduce( - (acc, key) => { - if (typeof agentOptions[key] === 'string') { - console.log(path.resolve(agentOptions[key] as string)); - return { - ...acc, - [key]: fs.readFileSync(path.resolve(agentOptions[key] as string)), - }; - } - - return acc; - }, - agentOptions, - ); - } - - const agent = agentOptions && options ? new https.Agent(options) : undefined; - const res = await fetch(schemaUrl, { agent }); + const res = await fetch(schemaUrl, { agent: config.agent }); const json = await res.json(); return json as Schema; }; @@ -57,8 +36,7 @@ const importNextSchema = async ( schemaId: string, config: Config, ): Promise => { - const { agentOptions } = config; - const schemaJSON = await fetchRemoteSchema(schemaId, agentOptions); + const schemaJSON = await fetchRemoteSchema(schemaId, config); validator.addSchema(schemaJSON, schemaId); const nextSchemaId = validator.unresolvedRefs.shift(); diff --git a/src/utils/login.ts b/src/utils/login.ts index 7a8cb784..8bbedb32 100644 --- a/src/utils/login.ts +++ b/src/utils/login.ts @@ -76,6 +76,7 @@ class FusionAuth { const additionalHeaders = this.config.additionalHeaders(); return fetch(`${this.config.fusionAuthUrl}/api/login`, { + agent: this.config.agent, method: 'POST', body: JSON.stringify({ loginId: email, @@ -110,6 +111,7 @@ class FusionAuth { ])) as { code: string }; return fetch(`${this.config.fusionAuthUrl}/api/two-factor/login`, { + agent: this.config.agent, method: 'POST', body: JSON.stringify({ code,