Skip to content
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
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ git tag "v0.20.0"
git push --force-with-lease

RELEASE_URL=$(gh release create v0.20.0 --generate-notes)
gh pr create --base main --title 'chore: release $VERSION' --body 'GitHub Release: $RELEASE_URL\nNPM release: https://www.npmjs.com/package/@dfinity/agent/v/${version}'"
gh pr create --base main --title 'chore: release $VERSION' --body 'GitHub Release: $RELEASE_URL\nNPM release: https://www.npmjs.com/package/@icp-sdk/core/v/${version}'"
git checkout main
```

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"bundle": "pnpm build:packages && pnpm -F './packages/core' bundle",
"e2e": "pnpm -r e2e",
"test": "pnpm -r -F './packages/core' test",
"publish:dfinity-packages": "pnpm -r -F './packages/{agent,candid,principal}' publish",
"publish:dfinity-packages": "pnpm -r -F './packages/{candid,principal}' publish",
"publish:core-package": "pnpm -F './packages/core' publish",
"version": "pnpm dlx tsx bin/version.ts",
"attw": "pnpm -r attw",
Expand Down Expand Up @@ -123,7 +123,7 @@
"git push --set-upstream origin release/${version}"
],
"after:release": [
"gh pr create --base main --title 'chore: release ${version}' --body 'GitHub Release: ${releaseUrl}\nNPM release: https://www.npmjs.com/package/@dfinity/agent/v/${version}\n- [ ] Check this box to trigger CI'"
"gh pr create --base main --title 'chore: release ${version}' --body 'GitHub Release: ${releaseUrl}\nNPM release: https://www.npmjs.com/package/@icp-sdk/core/v/${version}\n- [ ] Check this box to trigger CI'"
]
},
"git": {
Expand Down
3 changes: 0 additions & 3 deletions packages/agent/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions packages/agent/.npmignore

This file was deleted.

1 change: 0 additions & 1 deletion packages/agent/LICENSE

This file was deleted.

102 changes: 8 additions & 94 deletions packages/agent/README.md
Original file line number Diff line number Diff line change
@@ -1,94 +1,8 @@
# @icp-sdk/core/agent

JavaScript and TypeScript module to interact with the [Internet Computer](https://internetcomputer.org/) for Node.js and browser applications.

## Usage

```ts
import { HttpAgent } from '@icp-sdk/core/agent';

const agent = await HttpAgent.create({
host: 'https://icp-api.io',
});

agent.call(..) // Send an update call to the canister
agent.query(..) // Send a query call to the canister
agent.readState(..) // Read the state of the canister
```

### 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](https://github.com/dfinity/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:

```ts
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:

```ts
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:

```ts
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](https://identity.internetcomputer.org), you can do so by calling the `Actor.replaceAgent` method:

```ts
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](https://www.npmjs.com/package/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,

```ts
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@icp-sdk/core/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,

```ts
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@icp-sdk/core/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 });
```

## API Reference

Additional API Documentation can be found [here](https://js.icp.build/core/latest/libs/agent/api).
# @dfinity/agent

> [!WARNING]
> This package has been moved to [`@icp-sdk/core`](https://js.icp.build/core/), under the `agent` submodule.
>
> The source code is now inside the [`packages/core/src/agent`](../core/src/agent/) directory.
>
> The `@dfinity/agent` package will be deprecated soon.
19 changes: 0 additions & 19 deletions packages/agent/jest.config.mjs

This file was deleted.

4 changes: 0 additions & 4 deletions packages/agent/lib/esm/package.json

This file was deleted.

69 changes: 0 additions & 69 deletions packages/agent/package.json

This file was deleted.

39 changes: 0 additions & 39 deletions packages/agent/src/__snapshots__/actor.test.ts.snap

This file was deleted.

Loading
Loading