@the-hashgraph-group-ag/identity-client-sdk-js-core
v0.6.0
Published
Core utilities and HTTP client for Identity Client SDK
Readme
Core Package
Core utilities and HTTP client for the Identity Client SDK.
Features
- HTTP Client: Modern HTTP client with interceptors
- Type Safety: Full TypeScript support
- Interceptors: Request/response middleware support
- Error Handling: Built-in HTTP error handling with standardized error classes
- Validation: Input validation utilities with typed errors
Installation
pnpm add @the-hashgraph-group-ag/identity-client-sdk-js-coreUsage
Basic HTTP Client
import { HttpClient } from '@the-hashgraph-group-ag/identity-client-sdk-js-core';
const client = new HttpClient({
baseUrl: 'https://api.example.com',
defaultHeaders: {
'User-Agent': 'MyApp/1.0',
},
});
// GET request
const response = await client.get('/users');
// POST request
const newUser = await client.post('/users', {
name: 'John Doe',
email: '[email protected]',
});With Interceptors
import {
HttpClient,
jsonHeaders,
withAuth,
throwOnHttpError,
} from '@the-hashgraph-group-ag/identity-client-sdk-js-core';
const client = new HttpClient({
baseUrl: 'https://api.example.com',
requestInterceptors: [jsonHeaders, withAuth(() => 'your-token-here')],
responseInterceptors: [throwOnHttpError],
});Custom Interceptors
import {
HttpClient,
RequestInterceptor,
ResponseInterceptor,
} from '@the-hashgraph-group-ag/identity-client-sdk-js-core';
const loggingInterceptor: RequestInterceptor = (ctx) => {
console.log(`Making ${ctx.request.method} request to ${ctx.request.url}`);
};
const client = new HttpClient({
requestInterceptors: [loggingInterceptor],
});API Reference
HttpClient
Main HTTP client class.
Constructor Options
baseUrl: Base URL for all requestsdefaultHeaders: Default headers for all requestsrequestInterceptors: Array of request interceptorsresponseInterceptors: Array of response interceptors
Methods
get<T>(url, query?, headers?): Make GET requestpost<T>(url, body?, headers?): Make POST requestput<T>(url, body?, headers?): Make PUT requestpatch<T>(url, body?, headers?): Make PATCH requestdelete<T>(url, headers?): Make DELETE requestrequest<T>(req): Make custom request
Built-in Interceptors
jsonHeaders: Adds JSON content-type headerswithAuth(getToken): Adds Authorization headerthrowOnHttpError: Throws error for HTTP 4xx/5xx responses
Error Classes
The SDK provides standardized error classes for consistent error handling:
HTTP Errors
HttpError: HTTP request errors with status code, data, and headersTimeoutError: Request timeout errorsNetworkError: Network connectivity errorsAbortedError: Request aborted errorsRetryableError: Retryable errors
SDK Errors
ValidationError: Input validation errors with field informationConfigurationError: SDK configuration errorsHandlerNotSetError: Handler not configured errors
Example:
import { ValidationError, HttpError } from '@the-hashgraph-group-ag/identity-client-sdk-js-core';
try {
await handler.createIssuanceRequest(params);
} catch (error) {
if (error instanceof ValidationError) {
console.error('Validation error:', error.message, 'Field:', error.field);
} else if (error instanceof HttpError) {
console.error('HTTP error:', error.status, error.message);
}
}License
MIT License
