tuil
ReferencePackages@mwillbanks/tuil-registryAPI

HttpRegistrySource

class exported by @mwillbanks/tuil-registry.

View rawEdit

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

MemberTypeRequiredDescriptionRelated types
idstringYesThe id member uses the string contract.
baseUrlstringYesThe baseUrl member uses the string contract.
__constructoranyYesThe __constructor member uses the any contract.
get(name: string, signal?: AbortSignal) => Promise<RegistryItem | undefined>YesThe get member uses the (name: string, signal?: AbortSignal) => Promise<RegistryItem | undefined> contract.RegistryItem
list(signal?: AbortSignal) => Promise<readonly RegistryIndexEntry[]>YesThe 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.

Source

View the secondary source reference

Package

@mwillbanks/tuil-registry

On this page