Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
feat: add support for specifying headers in fork mode (#44)
Browse files Browse the repository at this point in the history
* feat(anvil.js): Add support for specifying headers in fork mode

* Update toArgs.ts

* Create odd-bears-relax.md

---------

Co-authored-by: jxom <jakemoxey@gmail.com>
  • Loading branch information
jnsdls and jxom committed Mar 5, 2024
1 parent 19123dd commit 5b39862
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/odd-bears-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@viem/anvil": patch
---

Added support for specifying headers in fork mode
8 changes: 8 additions & 0 deletions packages/anvil.js/src/anvil/createAnvil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ export type AnvilOptions = {
* on disk, anything missing locally would be fetched from the remote.
*/
forkChainId?: number | undefined;
/**
* Specify headers to send along with any request to the remote JSON-RPC server in forking mode.
*
* e.g. "User-Agent: test-agent"
*
* Requires `forkUrl` to be set.
*/
forkHeader?: Record<string, string> | undefined;
/**
* Initial retry backoff on encountering errors.
*/
Expand Down
16 changes: 15 additions & 1 deletion packages/anvil.js/src/anvil/toArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ import { toFlagCase } from "./toFlagCase.js";
* @returns The command line arguments.
*/
export function toArgs(options: {
[key: string]: string | boolean | number | bigint | undefined;
[key: string]: Record<string, string> | string | boolean | number | bigint | undefined;
}) {
return Object.entries(options).flatMap(([key, value]) => {
if (value === undefined) {
return [];
}

if (typeof value === "object" && value !== null) {
return Object.entries(value).flatMap(([subKey, subValue]) => {
if (subValue === undefined) {
return [];
}

const flag = toFlagCase(key);

const value = `${subKey}: ${subValue}`;

return [flag, value];
});
}

const flag = toFlagCase(key);

if (value === false) {
Expand Down

0 comments on commit 5b39862

Please sign in to comment.