smartsuite-typescript-api
Version:
Typescript type generator and wrapper for the REST API provided by SmartSuite. Currently in pre 1.0 so no semver guarantees are given
77 lines (76 loc) • 3.45 kB
TypeScript
import { ZodSchema } from "zod";
import { TableFieldTypes, TableMetadata } from "./metadataModels.js";
import { SmartSuiteRecord } from "./index.js";
/**
* To be extended by classes generated in {@link generateTableTypings}
*/
export interface TableRecordMapper<T> {
tableID: string;
recordModel: new () => T;
mapKeys(originalObject: Record<string, any>): T;
}
export interface TableTypingTools<T = SmartSuiteRecord> {
tableID: string;
tableName: string;
mapper: TableRecordMapper<T>;
}
export interface TableTypingGenerationOptions {
/**
* SmartSuite labels(User defined names for most SmartSuite objects) can have spaces in the UI, this is not always practical in JS since to index an object
* with a key with a space you have to `record["Key With Spaces"]`.
*
* If this option is provided it will be used instead of spaces in labels with spaces e.g: `record.Key_With_Spaces`
*
* An empty string can be provided: `record.KeyWithSpaces`
*/
labelSpaceToken?: string;
/**
* If true, when mapping fetched data slug ids to labels the labels will be forced to camelCase where possible.
*
* If option this is true, {@link labelSpaceToken} will be implied to an empty string unless something else is
* provided.
*
* `record["Field name"]` -> `record.fieldName`
*
* `record["Yes / No"]` -> `record["Yes / No"]` (`/` is illegal in identifiers)
*/
forceCamelCase?: boolean;
/**
* An option to override the naming of the generated record type name,
* by default the name will be the user defined name of the table + the word
* "Record". By default, all spaces are also removed from the table name.
*
* If the result name(either with default crafting or user defined) is non-valid
* The name will be "TableRecord" + the slug of the table.
* @param tableName the user defined table name from SmartSuite
*/
recordTypeNameOverride?: (tableName: string) => string;
}
export declare function addToTypeMap(tableID: string, tableName: string, typeMapperName: string, typeDirPath: string): void;
/**
* Generates TS types for the given table, but with all slugs replaced with their labels for more readable code.
* Code needed for mapping slugs to labels is also provided for use on objects returned by the API.
*
* IMPORTANT: If any labels are non-valid identifiers after custom options have been applied(in the current runtime),
* string literals will be used in the final type.
*
* e.g: If a table has 3 records with labels "Record123", "Record with spaces", "test###",
* the resulting mapping will map to:
*
* `{
* Record123: <type>,
* "Record with spaces": <type>,
* "test###": <type>,
* }`
*
* For more reading on identifier names: https://mathiasbynens.be/notes/javascript-identifiers-es6
* @param metadata The metadata of the table to generate typings for
* @param options specifies if spaces in label names should be replaced and/or names forced to camelCase
* before the valid identifier check.
* @see SmartSuiteAPI#getTableMetadata
*/
export declare function generateTableTypings(metadata: TableMetadata, options?: TableTypingGenerationOptions): Promise<void>;
/**
* We sacrifice TS compiler help here, I can't imagine a better typed alternative is more practical...
*/
export declare const schemaBuilders: Record<TableFieldTypes, (mappingData?: any) => ZodSchema>;