A comprehensive NestJS module providing client functionality for HSUITE applications, with built-in support for Web3 authentication, node communication, and network resilience.
- 🔐 Web3-based authentication flow
- 🌐 Dynamic node connection management
- 🔄 Automatic network resilience and failover
- 🍪 Session management with cookie support
- ⚡ HTTP client with axios integration
- 🛡️ Secure cryptographic signing
- 📡 Periodic health monitoring
- 🔌 Flexible module configuration
- 🎯 Global module scope support
npm install @hsuite/client
This module requires the following peer dependencies:
{
"@nestjs/common": "^10.4.2",
"@nestjs/core": "^10.4.2"
}
- Import the module in your
app.module.ts
:
import { ClientModule } from '@hsuite/client';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ClientModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
baseUrl: configService.get('CLIENT_BASE_URL'),
operator: {
accountId: configService.get('OPERATOR_ID'),
privateKey: configService.get('OPERATOR_KEY'),
publicKey: configService.get('OPERATOR_PUBLIC_KEY')
}
}),
inject: [ConfigService]
})
]
})
export class AppModule {}
- Inject and use the ClientService in your components:
@Injectable()
class YourService {
constructor(private clientService: ClientService) {}
async yourMethod() {
// Access authenticated client
const loginInfo = this.clientService.login;
// Make authenticated requests
const response = await this.clientService.axios.get('/your-endpoint');
}
}
The main module that provides client functionality.
-
forRootAsync(options: ClientModuleAsyncOptions)
: Configures the module asynchronously with dependency injection support.
Core service for managing client operations and authentication.
-
login
: Get current Web3 login credentials -
operator
: Get current operator information -
axios
: Get configured axios instance for making HTTP requests
-
onModuleInit()
: Initializes client connection and authentication - Private methods handle node connection, network resilience, and data signing
The module supports three configuration patterns:
- Factory Function Pattern:
ClientModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
baseUrl: config.get('API_URL'),
operator: {
accountId: config.get('OPERATOR_ID'),
privateKey: config.get('OPERATOR_KEY'),
publicKey: config.get('OPERATOR_PUBLIC_KEY')
}
}),
inject: [ConfigService]
});
- Existing Provider Pattern:
ClientModule.forRootAsync({
useExisting: [ExistingConfigService]
});
- Class Provider Pattern:
ClientModule.forRootAsync({
useClass: CustomClientOptionsFactory
});
The module implements a complete Web3 authentication flow:
- Requests authentication challenge
- Signs challenge with operator credentials
- Submits signed proof for verification
- Establishes authenticated session
- Sets up periodic health monitoring
Built-in support for handling network issues:
- Automatic failover to alternative nodes
- Connection error handling (ECONNREFUSED, ECONNABORTED, ECONNRESET)
- Dynamic node selection
- Session maintenance during node failures
- Cryptographic signing of requests
- Secure cookie handling
- Private key management
- Session-based authentication
Detailed documentation can be generated using Compodoc:
npm run compodoc
To check documentation coverage:
npm run compodoc:coverage
Core dependencies:
-
@hsuite/smart-network-types
: "2.0.0" -
@hsuite/auth-types
: "2.0.0" -
@hsuite/nestjs-swagger
: "2.0.0" -
@hsuite/smart-config
: "2.0.0"
Development dependencies:
-
@compodoc/compodoc
: "^1.1.23"
Current version: 2.0.0
This package is part of the HSuite Enterprise solution.
Built with ❤️ by the HbarSuite Team
Copyright © 2024 HbarSuite. All rights reserved.