Skip to content

Commit

Permalink
Validator can accept api key
Browse files Browse the repository at this point in the history
  • Loading branch information
joewagner committed Aug 21, 2023
1 parent 1c39536 commit 46cd2d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type Signer, type ExternalProvider, getSigner } from "./ethers.js";
export interface ReadConfig {
baseUrl: string;
aliases?: AliasesNameMap;
apiKey?: string;
}

export interface SignerConfig {
Expand Down
16 changes: 15 additions & 1 deletion packages/sdk/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type TransactionReceipt,
type Params as ReceiptParams,
} from "./receipt.js";
import { type FetchConfig } from "./client/index.js";

export { ApiError } from "./client/index.js";
export {
Expand Down Expand Up @@ -65,7 +66,7 @@ export class Validator {
* @description Returns OK if the validator considers itself healthy
*/
async health(opts: Signal = {}): Promise<boolean> {
return await getHealth(this.config, opts);
return await getHealth(prepReadConfig(this.config), opts);
}

/**
Expand Down Expand Up @@ -131,3 +132,16 @@ export class Validator {
return await pollTransactionReceipt(this.config, params, opts);
}
}

function prepReadConfig(config: Partial<ReadConfig>): FetchConfig {
const conf: FetchConfig = {};
if (config.apiKey) {
conf.init = {
headers: {
"Api-Key": config.apiKey
}
};
}

return { ...config, ...conf };
}
8 changes: 6 additions & 2 deletions packages/sdk/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,15 @@ describe("validator", function () {
});

test("when valid api key is used there is no exeption", async function () {
const api = new Validator({ baseUrl });
const apiKey = "foo";
const api = new Validator({
baseUrl,
apiKey,
});
const responses = await Promise.all(getRange(15).map(async () => await api.health()));

equal(responses.length, 15);
console.log(JSON.stringify(responses));
equal(responses.every((r: unknown) => r === true), true);
});
});
});
3 changes: 2 additions & 1 deletion packages/sdk/test/validator/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"HTTP": {
"Port": "8081",
"RateLimInterval": "1s",
"MaxRequestPerInterval": 10
"MaxRequestPerInterval": 10,
"APIKey": "foo"
},
"Gateway": {
"ExternalURIPrefix": "http://localhost:8081",
Expand Down

0 comments on commit 46cd2d4

Please sign in to comment.