Skip to content

Commit

Permalink
feat: update docs and standardization of methods in ApiClient (#75)
Browse files Browse the repository at this point in the history
Co-authored-by: Ariel Gentile <[email protected]>
  • Loading branch information
lotharking and genaris authored Feb 3, 2025
1 parent c570ca4 commit e8d858d
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/chatbot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ const server = app.listen(PORT, async () => {
logger.info(`phoneNumberCredentialDefinitionId: ${phoneNumberCredentialDefinitionId}`)

phoneNumberRevocationDefinitionId =
(await apiClient.revocationRegistry.get(phoneNumberCredentialDefinitionId))[0] ??
(await apiClient.revocationRegistry.create({
(await apiClient.revocationRegistries.get(phoneNumberCredentialDefinitionId))[0] ??
(await apiClient.revocationRegistries.create({
credentialDefinitionId: phoneNumberCredentialDefinitionId,
maximumCredentialNumber: 1000,
}))
Expand Down
2 changes: 1 addition & 1 deletion examples/verifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ expressHandler.messageReceived(async (req, res) => {
app.get('/invitation/:ref', async (req, res) => {
logger.info(`Generate invitation`)

const presReq = await apiClient.invitation.createPresentationRequest({
const presReq = await apiClient.invitations.createPresentationRequest({
ref: req.params.ref,
callbackUrl: `${PUBLIC_BASE_URL}/presentation`,
requestedCredentials: [
Expand Down
131 changes: 131 additions & 0 deletions packages/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
`@2060.io/service-agent-client`

# Service Agent Client
**Service Agent Client** is a TypeScript library designed to simplify the interaction with **Service Agent** (`@2060.io/service-agent-main`). It provides an abstraction layer for communicating with the agent, handling messaging, credential management, and event handling efficiently.

This package ensures that the client stays updated with the latest API versioning of the **Service Agent** to maintain compatibility with evolving endpoints.

## Features
- **Simplified API interaction:** Provides an easy-to-use client to interact with the Service Agent.
- **Event handling:** Includes an ExpressEventHandler for quick integration into Express-based applications.
- **Service-based structure:** Dedicated services for messaging, credentials, invitations, and revocation registries.
- **Versioning support:** Supports API versioning to maintain compatibility with different Service Agent versions.

## Repository
Find the public repository here: [2060 Service Agent](../../README.md)

## How to work
```plantuml
@startuml
package "2060 Ecosystem" {
package "Service Agent (SA)" {
class ServiceAgent {
+ Handles DIDComm communication
+ Manages agent wallet and credentials
+ Exposes API for client interactions
}
}
package "Libraries" {
class NestJSClient {
+ Plug-and-play integration
+ Selectable modules for various services
+ Modules:
-- MessageEventOptions: Configures message event handling
-- ConnectionEventOptions: Configures connection event handling
-- CredentialOptions: Configures credential management
}
class Client ##red {
+ Directly manages requests to SA
+ Facilitates reception of requests from modules
+ Provides an abstraction for service communication
+ Interfaces:
-- messages
-- credentialTypes
-- revocationRegistries
-- invitations
}
class ModelLibrary {
+ Defines required data models
+ Ensures type safety across services
}
}
}
NestJSClient --> ServiceAgent : Uses
Client --> ServiceAgent : Sends requests
Client --> ServiceAgent : Receives requests
Client --> ModelLibrary : Uses models
ModelLibrary --> ServiceAgent : Provides data models
NestJSClient --> ModelLibrary : Uses models
@enduml
```

## Installation
```sh
npm install @2060.io/service-agent-client
```
or
```sh
yarn add @2060.io/service-agent-client
```
## `ApiClient.ts`
`ApiClient` class provides a simple interface for interacting with the Service Agent. It abstracts API calls and offers dedicated service methods for messaging, credential types, invitations, and revocation registries.

### Example Usage
```ts
import { ApiClient, ApiVersion } from '@2060.io/service-agent-client'

const apiClient = new ApiClient('http://localhost:3000', ApiVersion.V1)

// Sending a message
const messageId = await apiClient.messages.send(
new TextMessage({
connectionId: 'connectionId',
content: 'Hello, world!',
})
)

console.log(`Message sent with ID: ${messageId}`)
```

### Services
- `messages:` Handles message sending and receiving.
- `credentialTypes:` Manages credential types.
- `revocationRegistries:` Manages revocation registries.
- `invitations:` Handles connection invitations.

## `ExpressEventHandler.ts`
The `ExpressEventHandler` simplifies event handling for Express applications, mapping API endpoints to event handlers.
### Example Usage
```ts
import express from 'express'
import { ExpressEventHandler } from '@2060.io/service-agent-client'

const app = express()
const eventHandler = new ExpressEventHandler(app)

expressHandler.messageStateUpdated(async (req, res) => {
const obj = req.body
logger.info(`message state updated: ${JSON.stringify(obj)}`)
res.json({ message: 'ok' })
})

app.listen(3000, () => console.log('Server running on port 3000'))
```
### Available Event Handlers
- `connectionStateUpdated` (`POST /connection-state-updated`)
- `messageStateUpdated` (`POST /message-state-updated`)
- `messageReceived` (`POST /message-received`)

## Contributing
This library evolves alongside the Service Agent. Contributions are welcome!
- Fork the repository.
- Create a feature branch.
- Commit changes.
- Open a pull request.

For more details, visit the [official repository](https://github.com/2060-io/2060-service-agent).
18 changes: 9 additions & 9 deletions packages/client/src/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ import { ApiVersion } from './types/enums'
* const apiClient = new ApiClient('http://localhost', ApiVersion.V1)
*
* // Example to query available credentials
* await apiClient.credentialType.getAll()
* await apiClient.credentialTypes.getAll()
*
* // Example to send a message
* apiClient.message.send(message: BaseMessage)
* apiClient.messages.send(message: BaseMessage)
*
* The `ApiClient` class provides easy methods for interacting with:
* - `message`: Send and manage messages.
* - `credentialType`: Query and manage credential types.
* - `revocationRegistry`: Query and manage the revocation registry for credential definitions.
* - `messages`: Send and manage messages.
* - `credentialTypes`: Query and manage credential types.
* - `revocationRegistries`: Query and manage the revocation registry for credential definitions.
*/
export class ApiClient {
public readonly messages: MessageService
public readonly credentialTypes: CredentialTypeService
public readonly revocationRegistry: RevocationRegistryService
public readonly invitation: InvitationService
public readonly revocationRegistries: RevocationRegistryService
public readonly invitations: InvitationService

constructor(
private baseURL: string,
private version: ApiVersion = ApiVersion.V1,
) {
this.invitation = new InvitationService(baseURL, version)
this.invitations = new InvitationService(baseURL, version)
this.messages = new MessageService(baseURL, version)
this.credentialTypes = new CredentialTypeService(baseURL, version)
this.revocationRegistry = new RevocationRegistryService(baseURL, version)
this.revocationRegistries = new RevocationRegistryService(baseURL, version)
}
}
93 changes: 93 additions & 0 deletions packages/model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
`@2060.io/service-agent-model`
# Service Agent Model

This package provides the data models used by **Service Agent** and its related services and libraries within the `@2060.io` ecosystem. These models are essential for chatbot development and facilitate integration with the broader system.

## Packages Using This Library

The models in this package are used by the following services and libraries:

- **[@2060.io/service-agent-main](../../README.md)** – The core Service Agent.
- **[@2060.io/service-agent-nestjs-client](../nestjs-client/README.md)** – A NestJS client for interacting with the Service Agent.
- **[@2060.io/service-agent-client](../client/README.md)** – A general-purpose api client for the Service Agent.


## Usage

This package defines essential models that support structured communication and event handling within the Service Agent ecosystem. It is designed to be used by chatbot services and other integrations that rely on a standardized message format.

## How to work
```plantuml
@startuml
package "2060 Ecosystem" {
package "Service Agent (SA)" {
class ServiceAgent {
+ Handles DIDComm communication
+ Manages agent wallet and credentials
+ Exposes API for client interactions
}
}
package "Libraries" {
class NestJSClient {
+ Plug-and-play integration
+ Selectable modules for various services
+ Modules:
-- MessageEventOptions: Configures message event handling
-- ConnectionEventOptions: Configures connection event handling
-- CredentialOptions: Configures credential management
}
class Client {
+ Directly manages requests to SA
+ Facilitates reception of requests from modules
+ Provides an abstraction for service communication
+ Interfaces:
-- messages
-- credentialTypes
-- revocationRegistries
-- invitations
}
class ModelLibrary ##red {
+ Defines required data models
+ Ensures type safety across services
}
}
}
NestJSClient --> ServiceAgent : Uses
Client --> ServiceAgent : Sends requests
Client --> ServiceAgent : Receives requests
Client --> ModelLibrary : Uses models
ModelLibrary --> ServiceAgent : Provides data models
NestJSClient --> ModelLibrary : Uses models
@enduml
```

### Installation

```sh
npm install @2060.io/service-agent-model
```
or
```sh
yarn add @2060.io/service-agent-model
```

## Example

Importing and using a message model:

```typescript
import { CallOfferRequestMessage } from '@2060.io/service-agent-model';

const callOffer = new CallOfferRequestMessage({
connectionId: 'connectionId',
description: 'Start call',
parameters: { wsUrl, roomId, peerId },
});

console.log(callOffer);
```
Loading

0 comments on commit e8d858d

Please sign in to comment.