37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import fetch from 'node-fetch'
|
|
|
|
import {REGISTRY_URL, Registry} from '../const.js'
|
|
|
|
export async function getRegistry(
|
|
REGISTRY_OVERRIDE_URL?: string,
|
|
): Promise<{message: string; ok: false} | {ok: true; registry: Registry}> {
|
|
const registryResponse = await fetch(REGISTRY_OVERRIDE_URL ?? REGISTRY_URL)
|
|
|
|
if (registryResponse.ok) {
|
|
return {
|
|
ok: true,
|
|
registry: (await registryResponse.json()) as Registry,
|
|
}
|
|
}
|
|
|
|
return {
|
|
message: `Error while fetching registry: ${registryResponse.status} ${registryResponse.statusText}`,
|
|
ok: false,
|
|
}
|
|
}
|
|
|
|
export async function getAvailableComponentNames(registry: Registry): Promise<string[]> {
|
|
return Object.keys(registry.components)
|
|
}
|
|
|
|
export async function getComponentURL(registry: Registry, componentName: string): Promise<string> {
|
|
return registry.base + registry.paths.components.replace('{componentName}', registry.components[componentName].name)
|
|
}
|
|
|
|
export async function getComponentRealname(
|
|
registry: Registry,
|
|
componentName: keyof Registry['components'],
|
|
): Promise<string> {
|
|
return registry.components[componentName].name
|
|
}
|