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

feat(anvil.js): Add support for specifying headers in fork mode #44

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading