@ucdjs/client
v0.3.0
Published
[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href]
Downloads
159
Readme
@ucdjs/client
A TypeScript-first client for the UCD.js API with resource-based helpers for files, reports, versions, version manifests, and published server configuration.
Installation
npm install @ucdjs/clientExports
The package exports:
createUCDClient(baseUrl)for well-known discoverycreateUCDClientWithConfig(baseUrl, config)for explicit endpoint configurationdiscoverEndpointsFromConfig(baseUrl)if you want to fetch the well-known config yourselfgetDefaultUCDEndpointConfig()for the library's built-in fallback config
Usage
Automatic discovery with createUCDClient
createUCDClient() fetches /.well-known/ucd-config.json from the provided origin, validates it, and then creates resource wrappers from the discovered endpoint paths.
This function is async and will throw if discovery fails or the well-known config is invalid.
import { createUCDClient } from "@ucdjs/client";
const client = await createUCDClient("https://api.ucdjs.dev");
const { data: versions, error } = await client.versions.list();
if (error) {
console.error("Failed to fetch versions:", error.message);
} else {
console.log("Available versions:", versions);
}Explicit configuration with createUCDClientWithConfig
createUCDClientWithConfig() skips the discovery request. You pass the same UCDWellKnownConfig shape that the well-known endpoint would return.
This is useful when:
- you already have the config available at build time
- you want to avoid an extra startup request
- your API is hosted on a different path, but still serves the same response types
import { createUCDClientWithConfig } from "@ucdjs/client";
const client = createUCDClientWithConfig("https://example.com", {
version: "0.1",
endpoints: {
files: "/custom/api/files",
manifest: "/custom/api/versions/{version}/manifest",
reports: "/custom/api/reports",
versions: "/custom/api/versions",
},
});
const { data: fileTree, error } = await client.versions.getFileTree("16.0.0");
if (error) {
console.error("Failed to fetch file tree:", error.message);
} else {
console.log("File tree:", fileTree);
}Well-known discovery vs. explicit config
The client supports two ways to arrive at the same endpoint shape:
createUCDClient(baseUrl)readshttps://<origin>/.well-known/ucd-config.jsoncreateUCDClientWithConfig(baseUrl, config)accepts that config object directly
In other words, the difference is the source of truth:
- well-known discovery loads the config from the server at runtime
- explicit config provides the same data in code
This makes it possible to host the API under a different path without changing the client-facing types. For example, a deployment may expose:
/.well-known/ucd-config.jsonon the main origin/edge/api/filesfor files/edge/api/versionsfor version endpoints
As long as the config points to those paths, the same client.files.* and client.versions.* methods continue to work.
Regardless of whether you created the client with createUCDClient() or createUCDClientWithConfig(), client.config.get() always reads the published well-known config from the origin:
/.well-known/ucd-config.json
Version manifest requests follow the configured endpoints.manifest value. By default that points to the canonical API route:
/api/v1/versions/{version}/manifest
Resources
The returned client exposes resource helpers:
client.files.get(path, query?)to fetch a Unicode file or directory listingclient.files.head(path, query?)to read file or directory metadata without downloading the bodyclient.versions.list()to list available Unicode versionsclient.versions.get(version)to fetch metadata and statistics for a specific Unicode versionclient.versions.getFileTree(version)to fetch a version's file treeclient.versions.getManifest(version)to read the canonical per-version manifestclient.reports.list()to list Unicode reports and adjacent revisionsclient.reports.get(reportId)to read the latest revision metadata for a reportclient.reports.getRevision(reportId, revId)to read metadata for a specific revisionclient.reports.getHtml(reportId, revId)to fetch the HTML document for a revisionclient.config.get()to read/.well-known/ucd-config.json
Fetch a file
const { data: fileContent, error } = await client.files.get("16.0.0/ucd/UnicodeData.txt");
if (error) {
console.error("Failed to fetch file:", error.message);
} else {
console.log("File content:", fileContent);
}Filter a directory listing
client.files.get() and client.files.head() both accept the typed file query parameters generated from OpenAPI:
queryfor prefix matchingpatternfor glob filteringsortfornameorlastModifiedorderforascordesctypeforall,files, ordirectories
const { data: files, error } = await client.files.get("16.0.0/ucd", {
query: "Uni",
pattern: "*.txt",
sort: "lastModified",
order: "desc",
type: "files",
});
if (error) {
console.error("Failed to fetch filtered directory listing:", error.message);
} else {
console.log("Filtered files:", files);
}Read file metadata with HEAD
const { data, error, response } = await client.files.head("16.0.0/ucd/UnicodeData.txt");
if (error) {
console.error("Failed to read file metadata:", error.message);
} else {
console.log("No response body:", data); // null
console.log("Content-Type:", response?.headers.get("Content-Type"));
console.log("Content-Length:", response?.headers.get("Content-Length"));
}Read version details
const { data: version, error } = await client.versions.get("16.0.0");
if (error) {
console.error("Failed to fetch version details:", error.message);
} else {
console.log("Version type:", version.type);
console.log("Statistics:", version.statistics);
}Read the published config
const { data: config, error } = await client.config.get();
if (error) {
console.error("Failed to fetch config:", error.message);
} else {
console.log("Published config:", config);
}Read a version manifest
const { data: manifest, error } = await client.versions.getManifest("16.0.0");
if (error) {
console.error("Failed to fetch manifest:", error.message);
} else {
console.log("Expected files:", manifest.expectedFiles);
}[!NOTE] Full documentation is available at docs.ucdjs.dev/api-reference/client.
License
Published under MIT License.
