Skip to content

Commit

Permalink
feat: add setHttpsAgent function to set agent for https requests (#458)
Browse files Browse the repository at this point in the history
* feat: add setHttpsAgent function to set agent for https requests

* fix: configure https agent in config class

---------

Co-authored-by: Emiel Wit <emiel.wit@bettyblocks.com>
  • Loading branch information
emielwit and emielwit committed Nov 20, 2023
1 parent 0da9b49 commit 5332745
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/blocks/publishBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const uploadBlock = async (
const url = `${config.blockstoreApiUrl}/blocks/publish`;

return fetch(url, {
agent: config.agent,
method: 'POST',
body: form,
headers: {
Expand Down
8 changes: 7 additions & 1 deletion src/functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -29,6 +30,7 @@ export type LocalConfig = {
skipCompile?: boolean;
includes?: string[];
tenantId?: string;
agent?: https.Agent;
};

export type CustomConfig = {
Expand Down Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions src/functions/publishAppFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
29 changes: 29 additions & 0 deletions src/functions/utils.ts
Original file line number Diff line number Diff line change
@@ -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<AgentOptions>(
(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;
}
28 changes: 3 additions & 25 deletions src/functions/validations.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -26,28 +24,9 @@ export type ValidationResult = {

const fetchRemoteSchema = async (
schemaUrl: string,
agentOptions?: AgentOptions,
config: Config,
): Promise<Schema> => {
let options: AgentOptions | undefined;
if (agentOptions) {
options = (['ca', 'cert', 'key'] as const).reduce<AgentOptions>(
(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;
};
Expand All @@ -57,8 +36,7 @@ const importNextSchema = async (
schemaId: string,
config: Config,
): Promise<Validator> => {
const { agentOptions } = config;
const schemaJSON = await fetchRemoteSchema(schemaId, agentOptions);
const schemaJSON = await fetchRemoteSchema(schemaId, config);
validator.addSchema(schemaJSON, schemaId);

const nextSchemaId = validator.unresolvedRefs.shift();
Expand Down
2 changes: 2 additions & 0 deletions src/utils/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5332745

Please sign in to comment.