Skip to content

Commit

Permalink
Define headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Dec 16, 2024
1 parent 2693dcc commit 43ba201
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/brain/src/modules/apiClients/signer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ export class Web3SignerApi extends StandardApi {
await this.request({
method: "GET",
endpoint: this.serverUpcheckEndpoint,
headers: { ...this.originHeader, "Content-Type": "text/plain; charset=utf-8" }
headers: {
...this.originHeader,
"Content-Type": "text/plain; charset=utf-8",
Accept: "text/plain" // Specify the expected response content type
}
});
} catch (e) {
throw new SignerApiError(`Error getting (GET) server upcheck. Is Web3Signer running?: ${e.message}`);
Expand Down
48 changes: 33 additions & 15 deletions packages/brain/src/modules/apiClients/standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { Network } from "@stakingbrain/common";
export class StandardApi {
private useTls = false;
private requestOptions: https.RequestOptions;
private authToken?: string; // Store auth token for headers
private host?: string; // Store optional host for headers
protected network: Network;

constructor(apiParams: ApiParams, network: Network) {
Expand All @@ -22,20 +24,20 @@ export class StandardApi {
protocol: urlOptions.protocol
};

this.requestOptions.headers = {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: "Bearer " + apiParams.authToken,
...(apiParams.host && { Host: apiParams.host })
};
this.authToken = apiParams.authToken;
if (apiParams.host) {
this.host = apiParams.host; // Save host for later use
}

if (apiParams.tlsCert) {
this.requestOptions.pfx = apiParams.tlsCert;
this.requestOptions.passphrase = "dappnode";
this.useTls = true;
}

if (this.requestOptions.protocol?.includes("https")) this.useTls = true;
if (this.requestOptions.protocol?.includes("https")) {
this.useTls = true;
}
}

/**
Expand All @@ -50,7 +52,7 @@ export class StandardApi {
method,
endpoint,
body,
headers,
headers = {}, // Default to an empty object
timeout
}: {
method: AllowedMethods;
Expand All @@ -65,10 +67,32 @@ export class StandardApi {
this.requestOptions.method = method;
this.requestOptions.path = endpoint;

// Add default headers only if they are not provided in `headers`
const defaultHeaders: Record<string, string> = {
...(headers["Content-Type"] ? {} : { "Content-Type": "application/json" }),
...(headers["Accept"] ? {} : { Accept: "application/json" }),
Authorization: `Bearer ${this.authToken}`
};

// Add optional host header if it exists
if (this.host) {
defaultHeaders.Host = this.host;
}

// Merge headers (custom headers take precedence for defaults)
const mergedHeaders = {
...defaultHeaders,
...headers
};

this.requestOptions.headers = mergedHeaders;

if (this.useTls) {
this.requestOptions.rejectUnauthorized = false;
req = https.request(this.requestOptions);
} else req = http.request(this.requestOptions);
} else {
req = http.request(this.requestOptions);
}

if (timeout) {
req.setTimeout(timeout, () => {
Expand All @@ -77,12 +101,6 @@ export class StandardApi {
});
}

if (headers) {
for (const [key, value] of Object.entries(headers)) {
req.setHeader(key, value);
}
}

if (body) {
req.setHeader("Content-Length", Buffer.byteLength(body));
req.write(body);
Expand Down

0 comments on commit 43ba201

Please sign in to comment.