@pfacheris/implementation-utils-known-resources
v0.0.6
Published
Provides wrapper classes for common Flatfile API resources with convenient methods for common operations
Readme
@flatfile/implementations-utils-known-resources
Provides wrapper classes for common Flatfile API resources with convenient methods for common operations. These classes simplify interactions with Workbooks and Sheets.
Features
KnownWorkbook: A wrapper aroundFlatfile.Workbookthat provides helpful methods for accessing sheets.KnownSheet: A wrapper aroundFlatfile.Sheetthat provides easy access to sheet properties and fields.
Usage
Instead of working directly with the raw API response objects, you can use these classes to make your code more readable and robust.
Working with KnownWorkbook
Here's how you can get a workbook and interact with its sheets:
import { FlatfileClient } from "@flatfile/api";
import { KnownWorkbook } from "@flatfile/implementations-utils-known-resources";
const api = new FlatfileClient();
async function getWorkbookAndSheets(workbookId: string) {
// Fetch the raw workbook object from the API
const { data: rawWorkbook } = await api.workbooks.get(workbookId);
// Wrap it in a KnownWorkbook
const workbook = new KnownWorkbook(rawWorkbook);
// Access sheets easily
const allSheets = workbook.sheets; // Returns an array of KnownSheet objects
// Check if a sheet exists by its slug
if (workbook.hasSheet("contacts")) {
// Get a specific sheet by its slug
const contactsSheet = workbook.sheet("contacts");
console.log(`Sheet Name: ${contactsSheet.name}`);
console.log(`Sheet ID: ${contactsSheet.id}`);
// Get all field keys for the sheet
const fieldKeys = contactsSheet.keys();
console.log("Fields:", fieldKeys);
}
}