@ensnode/ensrainbow-sdk
Version:
ENSRainbow SDK for interacting with the ENSRainbow API.
184 lines (182 loc) • 5.09 kB
JavaScript
// src/client.ts
import { LruCache } from "@ensnode/ensnode-sdk";
// src/consts.ts
var DEFAULT_ENSRAINBOW_URL = "https://api.ensrainbow.io";
var StatusCode = {
Success: "success",
Error: "error"
};
var ErrorCode = {
BadRequest: 400,
NotFound: 404,
ServerError: 500
};
// src/client.ts
var EnsRainbowApiClient = class _EnsRainbowApiClient {
options;
cache;
static DEFAULT_CACHE_CAPACITY = 1e3;
/**
* Create default client options.
*
* @returns default options
*/
static defaultOptions() {
return {
endpointUrl: new URL(DEFAULT_ENSRAINBOW_URL),
cacheCapacity: _EnsRainbowApiClient.DEFAULT_CACHE_CAPACITY
};
}
constructor(options = {}) {
this.options = {
..._EnsRainbowApiClient.defaultOptions(),
...options
};
this.cache = new LruCache(
this.options.cacheCapacity
);
}
/**
* Attempt to heal a labelHash to its original label.
*
* Note on returned labels: ENSRainbow returns labels exactly as they are
* represented in source rainbow table data. This means:
*
* - Labels may or may not be ENS-normalized
* - Labels can contain any valid string, including dots, null bytes, or be empty
* - Clients should handle all possible string values appropriately
*
* @param labelHash all lowercase 64-digit hex string with 0x prefix (total length of 66 characters)
* @returns a `HealResponse` indicating the result of the request and the healed label if successful
* @throws if the request fails due to network failures, DNS lookup failures, request timeouts, CORS violations, or Invalid URLs
*
* @example
* ```typescript
* const response = await client.heal(
* "0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"
* );
*
* console.log(response);
*
* // Output:
* // {
* // status: "success",
* // label: "vitalik"
* // }
*
* const notFoundResponse = await client.heal(
* "0xf64dc17ae2e2b9b16dbcb8cb05f35a2e6080a5ff1dc53ac0bc48f0e79111f264"
* );
*
* console.log(notFoundResponse);
*
* // Output:
* // {
* // status: "error",
* // error: "Label not found",
* // errorCode: 404
* // }
* ```
*/
async heal(labelHash) {
const cachedResult = this.cache.get(labelHash);
if (cachedResult) {
return cachedResult;
}
const response = await fetch(new URL(`/v1/heal/${labelHash}`, this.options.endpointUrl));
const healResponse = await response.json();
if (isCacheableHealResponse(healResponse)) {
this.cache.set(labelHash, healResponse);
}
return healResponse;
}
/**
* Get Count of Healable Labels
*
* @returns a `CountResponse` indicating the result and the timestamp of the request and the number of healable labels if successful
* @throws if the request fails due to network failures, DNS lookup failures, request timeouts, CORS violations, or Invalid URLs
*
* @example
*
* const response = await client.count();
*
* console.log(response);
*
* // {
* // "status": "success",
* // "count": 133856894,
* // "timestamp": "2024-01-30T11:18:56Z"
* // }
*
*/
async count() {
const response = await fetch(new URL("/v1/labels/count", this.options.endpointUrl));
return response.json();
}
/**
*
* Simple verification that the service is running, either in your local setup or for the provided hosted instance
*
* @returns a status of ENS Rainbow service
* @example
*
* const response = await client.health();
*
* console.log(response);
*
* // {
* // "status": "ok",
* // }
*/
async health() {
const response = await fetch(new URL("/health", this.options.endpointUrl));
return response.json();
}
/**
* Get the version information of the ENSRainbow service
*
* @returns the version information of the ENSRainbow service
* @throws if the request fails due to network failures, DNS lookup failures, request timeouts, CORS violations, or Invalid URLs
*
* @example
* ```typescript
* const response = await client.version();
*
* console.log(response);
*
* // {
* // "status": "success",
* // "version": "0.1.0",
* // "schema_version": 2
* // }
* ```
*/
async version() {
const response = await fetch(new URL("/v1/version", this.options.endpointUrl));
return response.json();
}
/**
* Get a copy of the current client options.
*
* @returns a copy of the current client options.
*/
getOptions() {
const deepCopy = {
cacheCapacity: this.options.cacheCapacity,
endpointUrl: new URL(this.options.endpointUrl.href)
};
return Object.freeze(deepCopy);
}
};
var isHealError = (response) => {
return response.status === StatusCode.Error;
};
var isCacheableHealResponse = (response) => {
return response.status === StatusCode.Success || response.errorCode !== ErrorCode.ServerError;
};
export {
EnsRainbowApiClient,
isCacheableHealResponse,
isHealError
};
//# sourceMappingURL=client.js.map