npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dfinity/agent

v2.1.3

Published

JavaScript and TypeScript library to interact with the Internet Computer

Downloads

383,872

Readme

@dfinity/agent

JavaScript and TypeScript library to interact with the Internet Computer for Node.js and Client applications

Visit the Dfinity Forum and SDK Documentation for more information and support building on the Internet Computer.

Additional API Documentation can be found here.


Installation

Using agent:

npm i --save @dfinity/agent

In the browser:

import * as agent from "@dfinity/agent";

or using individual exports:

import { Actor, HttpAgent } from '@dfinity/agent';

In Node.js

const DfinityAgent = require('@dfinity/agent');

or using individual exports:

const { Actor, HttpAgent } = require('@dfinity/agent');

Using an Agent

The agent is a low-level interface that the Actor uses to encode and decode messages to the Internet Computer. It provides call, query and readState methods to the Actor, as well as a few additional utilities. For the most part, calls through the agent are intended to be structured through an Actor, configured with a canister interface that can be automatically generated from a Candid interface.

Initializing an Actor

The most common use for the agent is to create an actor. This is done by calling the Actor.createActor constructor:

Actor.createActor(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<T>

The interfaceFactory is a function that returns a runtime interface that the Actor uses to strucure calls to a canister. The interfaceFactory can be written manually, but it is recommended to use the dfx generate command to generate the interface for your project, or to use the didc tool to generate the interface for your project.

Actors can also be initialized to include the boundary node http headers, This is done by calling the Actor.createActor constructor:

Actor.createActorWithHttpDetails(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<ActorMethodMappedWithHttpDetails<T>>

Inspecting an actor's agent

Use the Actor.agentOf method to get the agent of an actor:

const defaultAgent = Actor.agentOf(defaultActor);

This is useful if you need to replace or invalidate the identity used by an actor's agent.

For example, if you want to replace the identity of an actor's agent with a newly authenticated identity from Internet Identity, you can do so by calling the Actor.replaceAgent method:

defaultAgent.replaceIdentity(await authClient.getIdentity());

Tips for using fetch

The agent uses the browser fetch API to make calls to the Internet Computer. If you are not using the agent in the browser, you can pass a custom fetch implementation to the agent's constructor. This is useful if you want to use a custom fetch implementation, such as one that adds authentication headers to the request. We recommend using the isomorphic-fetch package to provide a consistent fetch API across Node.js and the browser. You will also need to provide a host option to the agent's constructor, as the agent will not be able to determine the host from the global context.

For example,

import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';

const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';

const agent = new HttpAgent({ fetch, host });

You can also pass fetchOptions to the agent's constructor, which will be passed to the fetch implementation. This is useful if you want to pass additional options to the fetch implementation, such as a custom header.

For example,

import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';

const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://ic0.app';

/**
 * @type {RequestInit}
 */
const fetchOptions = {
  headers: {
    'X-Custom-Header': 'value',
  },
};

const agent = new HttpAgent({ fetch, host, fetchOptions });