@sonr.io/sdk
v0.0.11
Published
This is the TypeScript SDK for Highway WebAuthn authentication gateway. It provides a comprehensive client library for interacting with Highway APIs, including WebAuthn authentication, session management, and blockchain operations.
Downloads
6
Readme
@sonr.io/sdk
This is the TypeScript SDK for Highway WebAuthn authentication gateway. It provides a comprehensive client library for interacting with Highway APIs, including WebAuthn authentication, session management, and blockchain operations.
Features
- WebAuthn Client: Complete WebAuthn registration and authentication flows
- Session Management: Automatic session handling and token management
- Type Safety: Full TypeScript support with comprehensive type definitions
- HTTP Client: Configured HTTP client with automatic retries and error handling
- Blockchain Integration: Client methods for DID and blockchain operations
- Environment Support: Configurable for development, staging, and production
Installation
npm install @sonr.io/sdk
# or
yarn add @sonr.io/sdk
# or
pnpm add @sonr.io/sdkUsage
Basic Setup
import { HighwaySDK } from "@sonr.io/sdk";
const sdk = new HighwaySDK({
apiUrl: "https://api.yourdomain.com",
environment: "production",
});WebAuthn Authentication
// Register a new user
try {
const registrationResult = await sdk.auth.register({
username: "[email protected]",
displayName: "John Doe",
});
console.log("Registration successful:", registrationResult);
} catch (error) {
console.error("Registration failed:", error);
}
// Authenticate existing user
try {
const authResult = await sdk.auth.authenticate({
username: "[email protected]",
});
console.log("Authentication successful:", authResult);
} catch (error) {
console.error("Authentication failed:", error);
}Session Management
// Get current session
const session = await sdk.session.getCurrent();
// Validate session
const isValid = await sdk.session.validate();
// Logout
await sdk.session.logout();Blockchain Operations
// Create DID
const didResult = await sdk.blockchain.createDID({
username: "[email protected]",
credentialId: "webauthn-credential-id",
publicKey: "public-key-data",
});
// Resolve DID
const didDocument = await sdk.blockchain.resolveDID("did:sonr:123456789");API Reference
HighwaySDK
The main SDK class that provides access to all Highway services.
Constructor Options
interface HighwaySDKOptions {
apiUrl: string;
environment?: "development" | "staging" | "production";
timeout?: number;
retries?: number;
}Methods
auth: WebAuthn authentication methodssession: Session management methodsblockchain: Blockchain and DID operationsprofile: User profile management
Authentication (sdk.auth)
register(options): Register new WebAuthn credentialauthenticate(options): Authenticate with WebAuthngetProfile(): Get current user profile
Session (sdk.session)
getCurrent(): Get current session informationvalidate(): Validate current sessionlogout(): End current session
Blockchain (sdk.blockchain)
createDID(options): Create new DID documentresolveDID(id): Resolve DID document by IDgetHealth(): Get blockchain service health
Development Roadmap
🔮 Future Implementation (Planned)
- [ ] SDK Core: TypeScript SDK with HTTP client configuration
- [ ] Authentication Module: WebAuthn registration and authentication methods
- [ ] Session Management: Automatic session handling and token management
- [ ] Blockchain Module: DID creation and resolution methods
- [ ] Profile Management: User profile operations and management
- [ ] Type Definitions: Comprehensive TypeScript type definitions
- [ ] Error Handling: Robust error handling and retry logic
🚧 Production Readiness (Next)
- [ ] Testing Suite: Unit tests for all SDK methods
- [ ] Documentation: Complete API documentation and examples
- [ ] Performance Optimization: Request caching and optimization
- [ ] Bundle Optimization: Tree-shaking and minimal bundle size
- [ ] Browser Support: Cross-browser compatibility testing
- [ ] Node.js Support: Server-side usage support
🔮 Future Enhancements
- [ ] React Hooks: React hooks for easy integration
- [ ] Vue Composables: Vue.js composables for Vue applications
- [ ] Offline Support: Offline capability with background sync
- [ ] Real-time Features: WebSocket support for real-time updates
- [ ] Advanced Caching: Intelligent caching strategies
- [ ] Plugin System: Extensible plugin architecture
Error Handling
The SDK provides comprehensive error handling:
import { HighwayError, WebAuthnError, SessionError } from "@sonr.io/sdk";
try {
await sdk.auth.register(options);
} catch (error) {
if (error instanceof WebAuthnError) {
console.error("WebAuthn error:", error.message);
} else if (error instanceof SessionError) {
console.error("Session error:", error.message);
} else if (error instanceof HighwayError) {
console.error("Highway error:", error.message);
}
}Configuration
Environment Variables
# Default API URL
HIGHWAY_API_URL=https://api.yourdomain.com
# Development
HIGHWAY_API_URL=http://localhost:8080SDK Configuration
const sdk = new HighwaySDK({
apiUrl: process.env.HIGHWAY_API_URL || "https://api.yourdomain.com",
environment: process.env.NODE_ENV || "production",
timeout: 10000,
retries: 3,
});Development
# Install dependencies
pnpm install
# Build the SDK
pnpm build
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watchIntegration Examples
React Application
import { HighwaySDK } from "@sonr.io/sdk";
import { useEffect, useState } from "react";
function useHighway() {
const [sdk] = useState(
() =>
new HighwaySDK({
apiUrl: process.env.NEXT_PUBLIC_API_URL!,
})
);
return sdk;
}
function AuthComponent() {
const sdk = useHighway();
const handleRegister = async () => {
try {
await sdk.auth.register({
username: "[email protected]",
displayName: "John Doe",
});
} catch (error) {
console.error("Registration failed:", error);
}
};
return <button onClick={handleRegister}>Register with WebAuthn</button>;
}Node.js Application
import { HighwaySDK } from "@sonr.io/sdk";
const sdk = new HighwaySDK({
apiUrl: "https://api.yourdomain.com",
environment: "production",
});
async function createUser() {
try {
const result = await sdk.auth.register({
username: "[email protected]",
displayName: "John Doe",
});
console.log("User created:", result);
} catch (error) {
console.error("Failed to create user:", error);
}
}Dependencies
- TypeScript: ^5.3.0
- Axios: ^1.6.0 (HTTP client)
- Zod: ^3.22.0 (Schema validation)
- @simplewebauthn/browser: ^9.0.0 (WebAuthn client)
Contributing
When contributing to the SDK:
- Follow TypeScript best practices
- Add comprehensive type definitions
- Include unit tests for new features
- Update documentation and examples
- Ensure backward compatibility
