Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract createAgent parameters and export them #735

21 changes: 11 additions & 10 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,26 @@ Get a default agent that connects to mainnet with the anonymous identity.
| -------------- | ------------- |
| `defaultAgent` | `() => Agent` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/agent.utils.ts#L9)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/agent.utils.ts#L10)

#### :gear: createAgent

Create an agent for a given identity

| Function | Type |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `createAgent` | `({ identity, host, fetchRootKey, verifyQuerySignatures, retryTimes, }: { identity: Identity; host?: string or undefined; fetchRootKey?: boolean or undefined; verifyQuerySignatures?: boolean or undefined; retryTimes?: number or undefined; }) => Promise<...>` |
| Function | Type |
| ------------- | ----------------------------------------------------------------------------------------------------------------- |
| `createAgent` | `({ identity, host, fetchRootKey, verifyQuerySignatures, retryTimes, }: CreateAgentParams) => Promise<HttpAgent>` |

Parameters:

- `identity`: A mandatory identity to use for the agent
- `host`: An optional host to connect to
- `fetchRootKey`: Fetch root key for certificate validation during local development or on testnet
- `verifyQuerySignatures`: Check for signatures in the state tree signed by the node that replies to queries - i.e. certify responses.
- `retryTimes`: Set the number of retries the agent should perform before errorring.
- `params`: The parameters to create a new HTTP agent
- `params.identity`: A mandatory identity to use for the agent
- `params.host`: An optional host to connect to
- `params.fetchRootKey`: Fetch root key for certificate validation during local development or on testnet
- `params.verifyQuerySignatures`: Check for signatures in the state tree signed by the node that replies to queries - i.e. certify responses.
- `params.retryTimes`: Set the number of retries the agent should perform before errorring.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/agent.utils.ts#L23)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/agent.utils.ts#L26)

#### :gear: createServices

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./enums/token.enums";
export * from "./parser/token";
export * from "./services/canister";
export * from "./types/actor-type.utils";
export type { CreateAgentParams } from "./types/agent.utils";
export type { CanisterOptions } from "./types/canister.options";
export type { QueryParams } from "./types/query.params";
export * from "./utils/actor.utils";
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/src/types/agent.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Identity } from "@dfinity/agent";

export interface CreateAgentParams {
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
identity: Identity;
host?: string;
fetchRootKey?: boolean;
// @deprecated Shipped as an opt-in feature but, will become the default in next major version
verifyQuerySignatures?: boolean;
retryTimes?: number;
}
24 changes: 10 additions & 14 deletions packages/utils/src/utils/agent.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Agent, Identity } from "@dfinity/agent";
import type { Agent } from "@dfinity/agent";
import { AnonymousIdentity, HttpAgent } from "@dfinity/agent";
import type { CreateAgentParams } from "../types/agent.utils";
import { nonNullish } from "./nullish.utils";

/**
Expand All @@ -14,26 +15,21 @@ export const defaultAgent = (): Agent =>

/**
* Create an agent for a given identity
* @param identity A mandatory identity to use for the agent
* @param host An optional host to connect to
* @param fetchRootKey Fetch root key for certificate validation during local development or on testnet
* @param verifyQuerySignatures Check for signatures in the state tree signed by the node that replies to queries - i.e. certify responses.
* @param retryTimes Set the number of retries the agent should perform before errorring.
*
* @param {CreateAgentParams} params The parameters to create a new HTTP agent
* @param {Identity} params.identity A mandatory identity to use for the agent
* @param {string} params.host An optional host to connect to
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} params.fetchRootKey Fetch root key for certificate validation during local development or on testnet
* @param {boolean} params.verifyQuerySignatures Check for signatures in the state tree signed by the node that replies to queries - i.e. certify responses.
* @param {number} params.retryTimes Set the number of retries the agent should perform before errorring.
*/
export const createAgent = async ({
identity,
host,
fetchRootKey = false,
verifyQuerySignatures = false,
retryTimes,
}: {
identity: Identity;
host?: string;
fetchRootKey?: boolean;
// @deprecated Shipped as an opt-in feature but, will become the default in next major version
verifyQuerySignatures?: boolean;
retryTimes?: number;
}): Promise<HttpAgent> => {
}: CreateAgentParams): Promise<HttpAgent> => {
return await HttpAgent.create({
identity,
...(nonNullish(host) && { host }),
Expand Down