import { Command } from "@commander-js/extra-typings"; import { getApiKey } from "../zygonApi"; import { Entity, entityToTable, interactive } from "./ZQueryBuilder"; export const api = new Command("api").description( "Fetches entities from the Zygon API.", ); const API_ENDPOINT = process.env.API_ENDPOINT ?? "https://api.zygon.tech"; api .command("view") .description("Fetches a list of entries from a view.") .argument("viewId", "The id of the view to fetch") .action(async (viewId) => { const apiKey = getApiKey(); const response = await fetch(`${API_ENDPOINT}/view/${viewId}`, { method: "GET", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, }); const results = await response.json(); console.log(JSON.stringify(results, null, 2)); }); const supportedEntities = [ "app", "account", "user", "task", ] satisfies Array; const isSupportedEntity = (raw: string): raw is Entity => (supportedEntities as Array).includes(raw); api .command("list") .description("Fetches a list of entries from a set of filters and fields.") .argument("entity", "The entities to fetch") .argument("fields", "The fields to fetch") .argument("filters", "The filters to apply") .action(async (entity, fields, filters) => { if (!isSupportedEntity(entity)) { console.error( `The entity ${entity} is not supported. The supported entities are: ${supportedEntities.join(", ")}`, ); process.exit(1); } const apiKey = getApiKey(); const response = await fetch(`${API_ENDPOINT}/zquery`, { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ table: entityToTable(entity), fields: fields.split(","), filters: JSON.parse(filters), }), }); const results = await response.json(); console.log(JSON.stringify(results, null, 2)); }); api .command("create") .description("Creates a given entity.") .argument("entity", "The type of entity to create") .argument("data", "The JSON data of the entity to create") .action(async (entity, data) => { if (!isSupportedEntity(entity)) { console.error( `The entity ${entity} is not supported. The supported entities are: ${supportedEntities.join(", ")}`, ); process.exit(1); } const apiKey = getApiKey(); const response = await fetch(`${API_ENDPOINT}/${entityToTable(entity)}s`, { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: data, }); const results = await response.json(); console.log(JSON.stringify(results, null, 2)); }); api .command("edit") .description("Edits a given entity.") .argument("entity", "The type of entity to edit") .argument("data", "The JSON data of the entity to edit") .action(async (entity, data) => { if (!isSupportedEntity(entity)) { console.error( `The entity ${entity} is not supported. The supported entities are: ${supportedEntities.join(", ")}`, ); process.exit(1); } const apiKey = getApiKey(); const response = await fetch(`${API_ENDPOINT}/${entityToTable(entity)}s`, { method: "PUT", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: data, }); const results = await response.json(); console.log(JSON.stringify(results, null, 2)); }); api .command("delete") .description("Edits a given entity.") .argument("entity", "The type of entity to delete") .argument("id", "The id of the entity to delete") .action(async (entity, id) => { if (!isSupportedEntity(entity)) { console.error( `The entity ${entity} is not supported. The supported entities are: ${supportedEntities.join(", ")}`, ); process.exit(1); } const apiKey = getApiKey(); const response = await fetch(`${API_ENDPOINT}/${entityToTable(entity)}s`, { method: "DELETE", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ id }), }); const results = await response.json(); console.log(JSON.stringify(results, null, 2)); }); api .command("interactive") .description("Fetches a list of entries from a set of filters and fields.") .action(async () => { await interactive(); });