Skip to content

Commit

Permalink
Use SecretsManagerService class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronaldo Macapobre committed Nov 25, 2024
1 parent 081f498 commit 8b4bd11
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions aws/services/httpService.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
import axios, { AxiosInstance, AxiosResponse } from "axios"
import * as https from "https"
import { getSecret } from "../helpers/getSecret"
import axios, { AxiosInstance, AxiosResponse } from "axios";
import * as https from "https";
import SecretsManagerService from "./secretsManagerService";

class HttpService {
private axios: AxiosInstance
private axios: AxiosInstance;

constructor() {}

async init(baseURL: string) {
const httpsAgent = await this.initHttpsAgent()
const httpsAgent = await this.initHttpsAgent();

this.axios = axios.create({
baseURL,
timeout: 5000,
httpsAgent
})
httpsAgent,
});
}

async initHttpsAgent(): Promise<https.Agent> {
const mtlsCertJson = await getSecret(process.env.MTLS_SECRET_NAME)
const sm = new SecretsManagerService();

const mtlsCertJson = await sm.getSecret(process.env.MTLS_SECRET_NAME);

// Get and parse mTLS Cert
const { key, ca, cert } = JSON.parse(mtlsCertJson)
const certUtf8 = Buffer.from(cert, "base64").toString("utf-8")
const keyUtf8 = Buffer.from(key, "base64").toString("utf-8")
const caUtf8 = ca ? Buffer.from(ca, "base64").toString("utf-8") : undefined
const { key, ca, cert } = JSON.parse(mtlsCertJson);
const certUtf8 = Buffer.from(cert, "base64").toString("utf-8");
const keyUtf8 = Buffer.from(key, "base64").toString("utf-8");
const caUtf8 = ca ? Buffer.from(ca, "base64").toString("utf-8") : undefined;

// Create the HTTPS Agent with the decoded cert and key
return new https.Agent({
cert: certUtf8,
key: keyUtf8,
ca: caUtf8 ? caUtf8 : undefined,
rejectUnauthorized: true
})
rejectUnauthorized: true,
});
}

async get<T>(url: string, params?: Record<string, unknown>): Promise<T> {
try {
const response: AxiosResponse<T> = await this.axios.get(url, {
params
})
return response.data
params,
});
return response.data;
} catch (error) {
this.handleError(error)
this.handleError(error);
}
}

async post<T>(url: string, data?: Record<string, unknown>): Promise<T> {
try {
const response: AxiosResponse<T> = await this.axios.post(url, data)
return response.data
const response: AxiosResponse<T> = await this.axios.post(url, data);
return response.data;
} catch (error) {
this.handleError(error)
this.handleError(error);
}
}

async put<T>(url: string, data?: Record<string, unknown>): Promise<T> {
try {
const response: AxiosResponse<T> = await this.axios.put(url, data)
return response.data
const response: AxiosResponse<T> = await this.axios.put(url, data);
return response.data;
} catch (error) {
this.handleError(error)
this.handleError(error);
}
}

private handleError(error: unknown): never {
if (axios.isAxiosError(error)) {
console.error("Axios error:", error.message)
console.error("Axios error:", error.message);
throw new Error(
`HTTP Error: ${error.response?.status || "Unknown status"}`
)
);
} else {
console.error("Unexpected error:", error)
throw new Error("Unexpected error occurred")
console.error("Unexpected error:", error);
throw new Error("Unexpected error occurred");
}
}
}

export default HttpService
export default HttpService;

0 comments on commit 8b4bd11

Please sign in to comment.