Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/airtable

src/models.ts

94 lines · 3.2 KB · TypeScript
import type {
	AirtableBaseSummary,
	AirtableCommentAuthor,
	AirtableCommentSummary,
	AirtableFieldSummary,
	AirtableRecordSummary,
	AirtableTableSummary,
	AirtableViewSummary,
	JsonRecord,
} from './types.ts'

export function asArray<T>(value: T | Array<T> | null | undefined): Array<T> {
	if (value === undefined || value === null) return []
	return Array.isArray(value) ? value : [value]
}

export function mapBase(value: unknown): AirtableBaseSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	return {
		id: String(record.id || ''),
		name: String(record.name || ''),
		permissionLevel: typeof record.permissionLevel === 'string' ? record.permissionLevel : null,
	}
}

export function mapField(value: unknown): AirtableFieldSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	return {
		id: String(record.id || ''),
		name: String(record.name || ''),
		type: typeof record.type === 'string' ? record.type : null,
		description: typeof record.description === 'string' ? record.description : null,
	}
}

export function mapView(value: unknown): AirtableViewSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	return {
		id: String(record.id || ''),
		name: String(record.name || ''),
		type: typeof record.type === 'string' ? record.type : null,
	}
}

export function mapTable(value: unknown): AirtableTableSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	const fields = asArray(record.fields).map(mapField)
	const views = asArray(record.views).map(mapView)
	return {
		id: String(record.id || ''),
		name: String(record.name || ''),
		description: typeof record.description === 'string' ? record.description : null,
		primaryFieldId: typeof record.primaryFieldId === 'string' ? record.primaryFieldId : null,
		fieldCount: fields.length,
		viewCount: views.length,
		fields,
		views,
	}
}

export function mapRecord(value: unknown): AirtableRecordSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	const fields =
		record.fields && typeof record.fields === 'object' && !Array.isArray(record.fields)
			? (record.fields as JsonRecord)
			: {}
	return {
		id: String(record.id || ''),
		createdTime: typeof record.createdTime === 'string' ? record.createdTime : null,
		commentCount: typeof record.commentCount === 'number' ? record.commentCount : null,
		fields,
	}
}

export function mapCommentAuthor(value: unknown): AirtableCommentAuthor | null {
	if (!value || typeof value !== 'object') return null
	const record = value as JsonRecord
	if (typeof record.id !== 'string' || record.id.length === 0) return null
	return {
		id: record.id,
		name: typeof record.name === 'string' ? record.name : null,
	}
}

export function mapComment(value: unknown): AirtableCommentSummary {
	const record = (value && typeof value === 'object' ? value : {}) as JsonRecord
	return {
		id: String(record.id || ''),
		text: typeof record.text === 'string' ? record.text : null,
		createdTime: typeof record.createdTime === 'string' ? record.createdTime : null,
		lastUpdatedTime: typeof record.lastUpdatedTime === 'string' ? record.lastUpdatedTime : null,
		author: mapCommentAuthor(record.author),
	}
}