@spotable/attio-sdk
Version:
Client for Attio REST API
114 lines • 6.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = registerGenerateCommand;
const logger_1 = require("../helpers/logger");
const types_1 = require("../generators/types");
const fs_1 = require("../helpers/fs");
const fetchAttioSchema_1 = require("../helpers/fetchAttioSchema");
const node_path_1 = require("node:path");
const client_1 = require("../generators/client");
const ora_1 = require("ora");
const indexFile_1 = require("../generators/indexFile");
const nestjsFiles_1 = require("../generators/nestjsFiles");
const ATTIO_TYPE_MAP = {
text: { tsOutputType: "string", isArray: false, needsImport: false },
boolean: { tsOutputType: "boolean", isArray: false, needsImport: false },
number: { tsOutputType: "number", isArray: false, needsImport: false },
date: { tsOutputType: "Date", isArray: false, needsImport: false },
email: { tsOutputType: "string", isArray: false, needsImport: false },
phone: { tsOutputType: "string", isArray: false, needsImport: false },
currency: { tsOutputType: "number", isArray: false, needsImport: false },
dropdown: { tsOutputType: "string", isArray: false, needsImport: false },
url: { tsOutputType: "string", isArray: false, needsImport: false },
person: { tsOutputType: "string", isArray: false, needsImport: false },
company: { tsOutputType: "string", isArray: false, needsImport: false },
deal: { tsOutputType: "string", isArray: false, needsImport: false },
user: { tsOutputType: "string", isArray: false, needsImport: false },
list_entry: { tsOutputType: "string", isArray: false, needsImport: false },
object: { tsOutputType: "string", isArray: false, needsImport: false },
status: { tsOutputType: "string", isArray: false, needsImport: false },
"personal-name": { tsOutputType: "string", isArray: false, needsImport: false },
"email-address": { tsOutputType: "string", isArray: false, needsImport: false },
"phone-number": { tsOutputType: "PhoneNumber", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
location: { tsOutputType: "Location", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
domain: { tsOutputType: "Domain", tsInputType: "DomainInput", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
select: { tsOutputType: "SelectOption", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
"record-reference": { tsOutputType: "RecordReference", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
"actor-reference": { tsOutputType: "ActorReference", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
interaction: { tsOutputType: "Interaction", isArray: false, needsImport: true, importFrom: "./systemDataTypes" },
timestamp: { tsOutputType: "string", isArray: false, needsImport: false },
};
async function generateCommandHandler(options) {
logger_1.default.level = options.verbose ? "debug" : "info";
const apiKey = process.env[options.apiKeyEnvVar];
if (!apiKey) {
logger_1.default.error(`API key not found in environment variable: ${options.apiKeyEnvVar}`);
process.exit(1);
}
(0, fs_1.removeDirectoryRecursive)(options.output);
const clientOutputDir = options.output;
const typesOutputDir = (0, node_path_1.join)(options.output, "types");
const generateLoader = (0, ora_1.default)("Generating standard types").start();
(0, fs_1.ensureDirectoryExists)(typesOutputDir);
if (options.standardTypes) {
(0, types_1.generateBaseTypes)(typesOutputDir);
(0, types_1.generateAttributeTypes)(typesOutputDir);
(0, types_1.generateCommentTypes)(typesOutputDir);
(0, types_1.generateEntryTypes)(typesOutputDir);
(0, types_1.generateListTypes)(typesOutputDir);
(0, types_1.generateNoteTypes)(typesOutputDir);
(0, types_1.generateObjectTypes)(typesOutputDir);
(0, types_1.generateRecordTypes)(typesOutputDir);
(0, types_1.generateTaskTypes)(typesOutputDir);
(0, types_1.generateWebhookTypes)(typesOutputDir);
(0, types_1.generateWorkspaceMemberTypes)(typesOutputDir);
}
const { objects, attributes } = await (0, fetchAttioSchema_1.fetchAttioSchema)(apiKey);
(0, types_1.generateSystemDataTypes)(typesOutputDir);
(0, types_1.generateSdkUtilityTypes)(objects, options.standardTypes, typesOutputDir);
generateLoader.text = "Generating custom object types";
for (const object of objects) {
const imports = {};
const attributesWithTSTypes = (attributes[object.api_slug] || []).map((attribute) => {
const typeInfo = ATTIO_TYPE_MAP[attribute.type] || { tsOutputType: "any", needsImport: false };
if (typeInfo.needsImport) {
if (!imports[typeInfo.importFrom]) {
imports[typeInfo.importFrom] = {
file: typeInfo.importFrom,
types: new Set(),
};
}
imports[typeInfo.importFrom].types.add(typeInfo.tsOutputType);
if (typeInfo.tsInputType) {
imports[typeInfo.importFrom].types.add(typeInfo.tsInputType);
}
}
return {
...attribute,
tsOutputType: typeInfo.tsOutputType,
tsInputType: typeInfo.tsInputType || typeInfo.tsOutputType,
isArray: typeInfo.isArray || attribute.is_multiselect,
};
});
(0, types_1.generateCustomObjectTypes)(typesOutputDir, { object, attributes: attributesWithTSTypes, imports: Object.values(imports) });
}
(0, indexFile_1.generateIndexFile)(typesOutputDir);
generateLoader.succeed(`TypeScript types generated successfully in ${typesOutputDir}`);
(0, client_1.generateClient)(clientOutputDir, objects, options.standardTypes);
if (options.nestjs) {
(0, nestjsFiles_1.generateNestjsFiles)(options.output);
}
(0, indexFile_1.generateIndexFile)(clientOutputDir);
}
function registerGenerateCommand(program) {
program
.command("generate")
.description("Generate a TypeScript client from your Attio workspace")
.option("-a, --api-key-env-var <key>", "Environment variable containing the Attio API key", "ATTIO_API_KEY")
.option("-o, --output <dir>", "Output directory for generated types", "./attio")
.option("-s, --standard-types", "Generate types for standard Attio entities (lists, notes, tasks, etc.)", false)
.option("-n, --nestjs", "Generate NestJS-style client", false)
.option("--verbose", "Enable verbose logging", false)
.action(generateCommandHandler);
}
//# sourceMappingURL=generate.js.map