UNPKG

@spotable/attio-sdk

Version:
64 lines (53 loc) 2.11 kB
import { generateFileHeader } from "./fileHeader"; import { writeGeneratedFile } from "../../helpers/fs"; import logger from "../../helpers/logger"; import { AttioAttribute, AttioObject } from "../../helpers/fetchAttioSchema"; interface ImportInfo { types: Set<string>; file: string; } type ExtendedAttioAttribute = AttioAttribute & { tsOutputType: string; tsInputType: string; isArray: boolean; }; export function generateCustomObjectTypes( outputDir: string, { object, attributes, imports }: { object: AttioObject; attributes: ExtendedAttioAttribute[]; imports: ImportInfo[] }, ): void { logger.debug(`Generating object types file: ${object.api_slug.toLowerCase()}.ts`); const importLines = imports.length ? imports.map((importInfo) => `import { ${Array.from(importInfo.types).join(", ")} } from '${importInfo.file}';`).join("\n") : ""; const attributesLines = attributes .map( (attribute) => ` /** ${attribute.title} (${attribute.is_multiselect ? "multi " : ""}${attribute.type}) */\n` + ` ${attribute.api_slug}${attribute.is_required ? "" : "?"}: ${attribute.tsOutputType}${attribute.isArray ? "[]" : ""};`, ) .join("\n") .trim(); const inputAttributesLines = attributes .map( (attribute) => ` /** ${attribute.title} (${attribute.is_multiselect ? "multi " : ""}${attribute.type}) */\n` + ` ${attribute.api_slug}${attribute.is_required ? "" : "?"}: ${attribute.tsInputType}${attribute.isArray ? "[]" : ""};`, ) .join("\n") .trim(); const objectTypes = ` ${generateFileHeader(`${object.singular_noun.toLowerCase()}.ts`)} ${importLines ? importLines + "\n" : ""} export interface ${object.singular_noun}Attributes { ${attributesLines} } export interface ${object.singular_noun}InputAttributes { ${inputAttributesLines} } export interface ${object.singular_noun} extends ${object.singular_noun}Attributes { id: string; workspace_id: string; created_at: string; updated_at: string | null; } `.trim(); writeGeneratedFile(outputDir, `${object.api_slug.toLowerCase()}.ts`, objectTypes); }