sdk-node-apis-efi
Version:
Module for integration with Efi Bank API
97 lines (96 loc) • 3.53 kB
JavaScript
import axios from 'axios';
import fs from 'node:fs';
import https from 'node:https';
import { PACKAGE_VERSION } from './version.js';
export const SDK_IDENTIFIER = `efi-node-${PACKAGE_VERSION}`;
export function buildHttpsAgent(options) {
if (!options.certificate)
return undefined;
try {
if (options.cert_base64) {
if (options.pemKey) {
return new https.Agent({
cert: Buffer.from(options.certificate, 'base64'),
key: Buffer.from(options.pemKey, 'base64'),
passphrase: '',
});
}
return new https.Agent({
pfx: Buffer.from(options.certificate, 'base64'),
passphrase: '',
});
}
if (options.pemKey) {
return new https.Agent({
cert: fs.readFileSync(options.certificate),
key: fs.readFileSync(options.pemKey),
passphrase: '',
});
}
return new https.Agent({
pfx: fs.readFileSync(options.certificate),
passphrase: '',
});
}
catch (error) {
throw new Error('Erro ao ler o certificado ou chave: ' + error.message);
}
}
function resolveRoute(route, params = {}) {
const queryParams = { ...params };
const resolvedRoute = route.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
const value = queryParams[key];
if (value === undefined || value === null) {
throw new Error(`Parametro de rota ausente: ${key}`);
}
delete queryParams[key];
return encodeURIComponent(String(value));
});
return { route: resolvedRoute, queryParams };
}
function hasQueryParams(params) {
return Object.keys(params).some((key) => params[key] !== undefined);
}
function normalizeError(error) {
let errorData = error.response?.data ?? error;
const errorUrl = error.request?.res?.responseUrl || '';
if (errorUrl.includes('/v2/gn/pix/comprovantes')) {
try {
const decoder = new TextDecoder('utf-8');
const errorText = decoder.decode(errorData);
errorData = JSON.parse(errorText);
}
catch {
errorData = error.response?.data ?? error;
}
}
return errorData;
}
export async function httpRequest(options) {
const { baseUrl, endpoint, body, params = {}, headers = {}, partner_token, skipMtlsChecking, authorization, idempotencyKey, } = options;
const { route, queryParams } = resolveRoute(endpoint.route, params);
const agent = buildHttpsAgent(options);
const defaultHeaders = {
'api-sdk': SDK_IDENTIFIER,
...(authorization ? { Authorization: authorization } : {}),
...(skipMtlsChecking ? { 'x-skip-mtls-checking': 'true' } : {}),
...(partner_token ? { 'partner-token': partner_token } : {}),
...(idempotencyKey ? { 'x-idempotency-key': idempotencyKey } : {}),
};
const config = {
url: baseUrl + route,
method: endpoint.method.toLowerCase(),
data: body,
headers: { ...defaultHeaders, ...headers },
...(endpoint.responseType ? { responseType: endpoint.responseType } : {}),
...(hasQueryParams(queryParams) ? { params: queryParams } : {}),
...(agent ? { httpsAgent: agent } : {}),
};
try {
const response = await axios(config);
return response.data;
}
catch (error) {
throw normalizeError(error);
}
}