HttpRegistrySource
class exported by @mwillbanks/tuil-registry.
class
Public class exported by @mwillbanks/tuil-registry.
export class HttpRegistrySource implements RegistrySource {
readonly id: string;
readonly baseUrl: string;
constructor(id: string, baseUrl: string) {
if (!id.trim()) {
throw new TypeError("Registry source id cannot be empty");
}
this.id = id;
this.baseUrl = validateRegistryBaseUrl(baseUrl);
}
async get(
name: string,
signal?: AbortSignal,
): Promise<RegistryItem | undefined> {
const itemPath = validateRegistryPath(name)
.split("/")
.map(encodeURIComponent)
.join("/");
const response = await fetch(`${this.baseUrl}/${itemPath}.json`, {
redirect: "error",
signal,
});
if (response.status === 404) {
return undefined;
}
if (!response.ok) {
throw new Error(
`Registry "${this.id}" returned ${response.status} for "${name}"`,
);
}
const item = parseRegistrySourceItem(
await readRegistryResponseJson(response, this.id),
this.id,
name,
);
assertPublishedRegistryMetadata(item, this.id);
return item;
}
async list(signal?: AbortSignal): Promise<readonly RegistryIndexEntry[]> {
const response = await fetch(`${this.baseUrl}/registry.json`, {
redirect: "error",
signal,
});
if (!response.ok) {
throw new Error(`Registry "${this.id}" returned ${response.status}`);
}
const value = await readRegistryResponseJson(response, this.id);
const items =
value && typeof value === "object" && "items" in value
? (value as { items: unknown }).items
: value;
if (!Array.isArray(items)) {
throw new TypeError(`Registry "${this.id}" index must be an array`);
}
return items.map((entry) => parseRegistryIndexEntry(entry, this.id));
}
}Members
| Member | Type | Required | Description | Related types |
|---|---|---|---|---|
id | string | Yes | The id member uses the string contract. | — |
baseUrl | string | Yes | The baseUrl member uses the string contract. | — |
__constructor | any | Yes | The __constructor member uses the any contract. | — |
get | (name: string, signal?: AbortSignal) => Promise<RegistryItem | undefined> | Yes | The get member uses the (name: string, signal?: AbortSignal) => Promise<RegistryItem | undefined> contract. | RegistryItem |
list | (signal?: AbortSignal) => Promise<readonly RegistryIndexEntry[]> | Yes | The list member uses the (signal?: AbortSignal) => Promise<readonly RegistryIndexEntry[]> contract. | RegistryIndexEntry |
Parameters
This declaration has no public members.
Returns
This declaration does not return a value.
Throws
No thrown errors are documented for this declaration.
Related types
Source
View the secondary source reference