Skip to content

Commit

Permalink
restructure type generation files
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellocurto committed Nov 5, 2024
1 parent 33b0a51 commit 2bd1305
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 88 deletions.
83 changes: 83 additions & 0 deletions src/generate-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { getBaseSchema } from './requests';

export async function generateTypeScriptDefinitions({
apiKey,
baseId,
tableNameOrId,
}: {
apiKey: string;
baseId: string;
tableNameOrId: string;
}): Promise<string> {
const schema = await getBaseSchema({ apiKey, baseId });

const table = schema.tables.find(
(t) => t.id === tableNameOrId || t.name === tableNameOrId
);

if (!table) {
throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
}

let typeDefinitions = `type ${table.name}Fields = {\n`;

table.fields.forEach((field) => {
typeDefinitions += ` "${field.name}"?: ${mapAirtableTypeToTypeScript(
field.type
)};\n`;
});

typeDefinitions += '};\n';

return typeDefinitions;
}

function mapAirtableTypeToTypeScript(airtableType: string): string {
switch (airtableType) {
case 'aiText':
case 'singleLineText':
case 'email':
case 'url':
case 'phoneNumber':
case 'multilineText':
case 'richText':
case 'button':
case 'barcode':
case 'createdTime':
case 'lastModifiedTime':
return 'string';
case 'number':
case 'currency':
case 'percent':
case 'rating':
case 'duration':
case 'autoNumber':
case 'count':
return 'number';
case 'checkbox':
return 'boolean';
case 'date':
case 'dateTime':
return 'Date';
case 'singleSelect':
case 'multipleSelects':
return 'string[]';
case 'attachment':
return 'any[]';
case 'collaborator':
case 'multipleCollaborators':
return '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]';
case 'linkToAnotherRecord':
case 'multipleRecordLinks':
return 'string[]';
case 'lookup':
case 'rollup':
return 'any';
case 'formula':
return 'string | number | boolean | any[]';
case 'syncSource':
return 'string';
default:
return 'any';
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export {
updateRecords,
updateRecordsUpsert,
deleteRecords,
generateTypeScriptDefinitions,
} from './requests.js';

export { generateTypeScriptDefinitions } from './generate-types.js';
83 changes: 0 additions & 83 deletions src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { URL } from 'url';
import {
AirtableRecord,
DeleteRecordResponse,
DeleteRecordsQueryParameters,
DeleteRecordsResponse,
GetRecordsQueryParameters,
} from './types/records';
Expand Down Expand Up @@ -456,85 +455,3 @@ export async function getBaseSchema({

return response.json();
}

export async function generateTypeScriptDefinitions({
apiKey,
baseId,
tableNameOrId,
}: {
apiKey: string;
baseId: string;
tableNameOrId: string;
}): Promise<string> {
const schema = await getBaseSchema({ apiKey, baseId });

const table = schema.tables.find(
(t) => t.id === tableNameOrId || t.name === tableNameOrId
);

if (!table) {
throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
}

let typeDefinitions = `type ${table.name}Fields = {\n`;

table.fields.forEach((field) => {
typeDefinitions += ` "${field.name}"?: ${mapAirtableTypeToTypeScript(
field.type
)};\n`;
});

typeDefinitions += '};\n';

return typeDefinitions;
}

function mapAirtableTypeToTypeScript(airtableType: string): string {
switch (airtableType) {
case 'aiText':
case 'singleLineText':
case 'email':
case 'url':
case 'phoneNumber':
case 'multilineText':
case 'richText':
case 'button':
case 'barcode':
case 'createdTime':
case 'lastModifiedTime':
return 'string';
case 'number':
case 'currency':
case 'percent':
case 'rating':
case 'duration':
case 'autoNumber':
case 'count':
return 'number';
case 'checkbox':
return 'boolean';
case 'date':
case 'dateTime':
return 'Date';
case 'singleSelect':
case 'multipleSelects':
return 'string[]';
case 'attachment':
return 'any[]';
case 'collaborator':
case 'multipleCollaborators':
return '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]';
case 'linkToAnotherRecord':
case 'multipleRecordLinks':
return 'string[]';
case 'lookup':
case 'rollup':
return 'any';
case 'formula':
return 'string | number | boolean | any[]';
case 'syncSource':
return 'string';
default:
return 'any';
}
}
4 changes: 0 additions & 4 deletions src/types/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ export interface DeleteRecordsPathParameters {
tableIdOrName: TableIdOrName;
}

export interface DeleteRecordsQueryParameters {
records: string[];
}

export interface DeleteRecordResponse {
id: string;
deleted: true;
Expand Down

0 comments on commit 2bd1305

Please sign in to comment.