@chakra-ui/cli
Version:
Generate theme typings for autocomplete
127 lines (124 loc) • 4.16 kB
JavaScript
;
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';
import { z } from 'zod';
import { processEnvSchema, searchResultsSchema, compositionIndexSchema, compositionFileSchema, themeIndexSchema, proBlockResponseSchema, componentListSchema } from './schema.js';
const env = processEnvSchema.parse(process.env);
const agent = env.HTTPS_PROXY ? new HttpsProxyAgent(env.HTTPS_PROXY) : void 0;
const docsOrigin = env.CHAKRA_DOCS_URL.replace(/\/$/, "");
function docsUrl(path) {
return `${docsOrigin}${path.startsWith("/") ? path : `/${path}`}`;
}
function parseWithSchema(json, schema, context) {
const parsed = schema.safeParse(json);
if (!parsed.success) {
const detail = parsed.error.issues.map(
(i) => i.path.length ? `${i.path.join(".")}: ${i.message}` : i.message
).join("; ");
throw new Error(`Invalid ${context} response: ${detail}`);
}
return parsed.data;
}
async function fetchCompositions() {
const res = await fetch(`${env.REGISTRY_URL}/compositions/index.json`, {
agent
});
const json = await res.json();
return compositionIndexSchema.parse(json);
}
async function fetchComposition(id) {
try {
const res = await fetch(`${env.REGISTRY_URL}/compositions/${id}.json`, {
agent
});
const json = await res.json();
return compositionFileSchema.parse(json);
} catch (error) {
throw new Error(
`Failed to fetch snippet "${id}". Make sure the id is correct or run @chakra-ui/cli snippet list to see available snippets.`
);
}
}
async function fetchProBlocks() {
const res = await fetch("https://pro.chakra-ui.com/api/blocks", {
agent
});
if (!res.ok) {
throw new Error(
`Failed to fetch pro blocks: ${res.status} ${res.statusText}`
);
}
return res.json();
}
async function fetchComponentList() {
const res = await fetch(`${docsUrl("/api/types")}`, { agent });
if (!res.ok) {
throw new Error(
`Failed to fetch component list: ${res.status} ${res.statusText}`
);
}
const json = await res.json();
return parseWithSchema(json, componentListSchema, "component list");
}
async function fetchComponentProps(component) {
const res = await fetch(`${docsUrl(`/api/types/${component}`)}`, {
agent
});
if (!res.ok) {
throw new Error(
`Failed to fetch props for "${component}": ${res.status} ${res.statusText}`
);
}
const json = await res.json();
return parseWithSchema(json, z.unknown(), `props for "${component}"`);
}
async function fetchComponentExample(component) {
const res = await fetch(`${docsUrl(`/r/examples/${component}.json`)}`, {
agent
});
if (!res.ok) {
throw new Error(
`Failed to fetch example for "${component}": ${res.status} ${res.statusText}`
);
}
const json = await res.json();
return parseWithSchema(json, z.unknown(), `example for "${component}"`);
}
async function fetchTheme() {
const res = await fetch(`${docsUrl("/api/theme")}`, { agent });
if (!res.ok) {
throw new Error(`Failed to fetch theme: ${res.status} ${res.statusText}`);
}
const json = await res.json();
return parseWithSchema(json, themeIndexSchema, "theme");
}
async function searchDocs(query) {
const res = await fetch(
`${docsUrl("/api/search")}?query=${encodeURIComponent(query)}`,
{ agent }
);
if (!res.ok) {
throw new Error(`Failed to search docs: ${res.status} ${res.statusText}`);
}
const json = await res.json();
return parseWithSchema(json, searchResultsSchema, "search");
}
async function fetchProBlock(category, id, apiKey) {
const res = await fetch(
`https://pro.chakra-ui.com/api/blocks/${category}/${id}`,
{
agent,
headers: {
"x-api-key": apiKey
}
}
);
if (!res.ok) {
throw new Error(
`Failed to fetch pro block ${category}/${id}: ${res.status} ${res.statusText}`
);
}
const json = await res.json();
return parseWithSchema(json, proBlockResponseSchema, "pro block");
}
export { fetchComponentExample, fetchComponentList, fetchComponentProps, fetchComposition, fetchCompositions, fetchProBlock, fetchProBlocks, fetchTheme, searchDocs };