get-records-as-csv-sheet.ts•4.19 kB
/**
* This file was generated. Do NOT edit this file.
*/
import fetch from 'node-fetch'
import { type ZodRawShape, z } from 'zod'
import {
commons_CommitIdSchema,
commons_FilterFieldSchema,
commons_FilterSchema,
commons_RecordIdSchema,
commons_SearchFieldSchema,
commons_SearchValueSchema,
commons_SheetIdSchema,
commons_SortDirectionSchema,
commons_SortFieldSchema,
commons_VersionIdSchema,
} from '../schemas.js'
import type { Tool } from './index.js'
const params = {
sheetId: commons_SheetIdSchema.describe('ID of sheet'),
versionId: z.string().optional().describe("Deprecated, use 'sinceCommitId' instead."),
commitId: commons_CommitIdSchema
.optional()
.describe('Returns records that were changed in that version in that version and only those records.'),
sinceVersionId: commons_VersionIdSchema.optional().describe("Deprecated, use 'sinceCommitId' instead."),
sinceCommitId: commons_CommitIdSchema
.optional()
.describe(
'Returns records that were changed in that version in addition to any records from versions after that version.',
),
sortField: commons_SortFieldSchema.optional().describe('The field to sort the data on.'),
sortDirection: commons_SortDirectionSchema
.optional()
.describe('Sort direction - asc (ascending) or desc (descending)'),
filter: commons_FilterSchema.optional().describe('Options to filter records'),
filterField: commons_FilterFieldSchema.optional().describe('The field to filter the data on.'),
searchValue: commons_SearchValueSchema.optional().describe('The value to search for data on.'),
searchField: commons_SearchFieldSchema.optional().describe('The field to search for data on.'),
ids: commons_RecordIdSchema
.optional()
.describe(
"The Record Ids param (ids) is a list of record ids that can be passed to several record endpoints allowing the user to identify specific records to INCLUDE in the query, or specific records to EXCLUDE, depending on whether or not filters are being applied. When passing a query param that filters the record dataset, such as 'searchValue', or a 'filter' of 'valid' | 'error' | 'all', the 'ids' param will EXCLUDE those records from the filtered results. For basic queries that do not filter the dataset, passing record ids in the 'ids' param will limit the dataset to INCLUDE just those specific records ",
),
} as ZodRawShape
export const getRecordsAsCsvSheet: Tool<typeof params> = {
name: 'getRecordsAsCsvSheet',
description: 'Download records as a CSV file: Returns records from a sheet in a workbook as a csv file',
params,
cb: async ({
sheetId,
versionId,
commitId,
sinceVersionId,
sinceCommitId,
sortField,
sortDirection,
filter,
filterField,
searchValue,
searchField,
ids,
}) => {
try {
const searchParams = {
versionId,
commitId,
sinceVersionId,
sinceCommitId,
sortField,
sortDirection,
filter,
filterField,
searchValue,
searchField,
ids,
}
const searchParamsString = new URLSearchParams(JSON.parse(JSON.stringify(searchParams))).toString()
const baseUrl = process.env.FLATFILE_API_URL || 'https://platform.flatfile.com/api/v1'
const url = `${baseUrl}/sheets/${sheetId}/download${searchParamsString ? `?${searchParamsString}` : ''}`
const response = await fetch(url, {
method: 'GET',
headers: {
'X-Disable-Hooks': 'true',
Authorization: `Bearer ${process.env.FLATFILE_BEARER_TOKEN}`,
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status} - ${response.statusText}`)
}
const data = (await response.json()) as { data: unknown }
return {
content: [
{
type: 'text',
text: JSON.stringify(data.data, null, 2),
},
],
status: 'success',
}
} catch (error) {
return {
content: [{ type: 'text', text: `Error: ${error}` }],
status: 'failed',
}
}
},
}