Space Endpoint
These utilities provide essential functions for normalizing and retrieving DID Space endpoint information, ensuring that your interactions with DID Spaces are correctly routed and formatted. This section covers functions that help you handle DID Space URLs.
For more general utilities, refer to the Utilities Overview section.
normalizeEndpoint
#
The normalizeEndpoint
function is used to clean up and standardize a given DID Space endpoint URL. It ensures that the endpoint is a valid HTTP(s) URL and removes any redundant object/
suffix or query parameters, returning a base endpoint suitable for API calls.
Parameters
Name | Type | Description |
---|---|---|
|
| The DID Space endpoint URL to normalize. |
Returns
Name | Type | Description |
---|---|---|
|
| The normalized DID Space endpoint URL. |
Example
import { normalizeEndpoint } from '@did-spaces/client/libs/space';
async function demonstrateNormalizeEndpoint() {
const endpoint1 = 'https://example.did.abtnet.io/app/api/space/app/object/?query=test';
const normalized1 = normalizeEndpoint(endpoint1);
console.log(`Normalized endpoint 1: ${normalized1}`);
const endpoint2 = 'https://example.did.abtnet.io/app/api/space/app/';
const normalized2 = normalizeEndpoint(endpoint2);
console.log(`Normalized endpoint 2: ${normalized2}`);
try {
normalizeEndpoint('invalid-url');
} catch (error) {
console.error(`Error normalizing invalid URL: ${error.message}`);
}
}
demonstrateNormalizeEndpoint();
This example shows how normalizeEndpoint
processes URLs. It removes the object/
suffix and any query strings, resulting in a clean base URL. It also demonstrates the error handling for invalid URLs.
getSpaceServiceEndpoint
#
The getSpaceServiceEndpoint
function asynchronously retrieves the actual service endpoint for a given DID Space. It performs an API call to the normalized endpoint to get the authoritative service URL, providing a fallback default value if the API call fails or returns an empty body.
Parameters
Name | Type | Description |
---|---|---|
|
| The normalized DID Space endpoint URL (e.g., from |
|
| Optional. The default value to return if the service endpoint cannot be retrieved (defaults to |
Returns
Name | Type | Description |
---|---|---|
|
| A promise that resolves to the actual DID Space service endpoint URL. |
Example
import { getSpaceServiceEndpoint } from '@did-spaces/client/libs/space';
async function fetchServiceEndpoint() {
const baseEndpoint = 'https://73aa3e87-znkjt5vbgnezh4p6v4dsaye61e7pxxn3vk4j.did.abtnet.io/app/api/space/z3T6WZD3dBtaVUgwb3rtBey8BartrJDTLrmQr/app/zNKsSUmUVJntiVdAPpg8vYtEgnFvSppYLf4F/';
try {
const serviceEndpoint = await getSpaceServiceEndpoint(baseEndpoint);
console.log(`Retrieved service endpoint: ${serviceEndpoint}`);
} catch (error) {
console.error(`Error fetching service endpoint: ${error.message}`);
}
// Example with a custom default value
const customDefault = 'https://fallback.did.abtnet.io/app/api/space/';
try {
const serviceEndpointWithDefault = await getSpaceServiceEndpoint('https://invalid-space.com/', customDefault);
console.log(`Service endpoint (with default): ${serviceEndpointWithDefault}`);
} catch (error) {
console.error(`Error fetching service endpoint (with default): ${error.message}`);
}
}
fetchServiceEndpoint();
This example demonstrates how to use getSpaceServiceEndpoint
to obtain the operational URL for a DID Space. It shows how the function will attempt to fetch the endpoint and gracefully fall back to a provided default if the retrieval fails.
This section provided a clear overview of how to manage and resolve DID Space endpoint URLs using the SDK's utility functions. Next, explore how to use the Logger for debugging and monitoring SDK operations.