get-records.ts•5.11 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_EventIdSchema,
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: commons_VersionIdSchema.optional().describe("Deprecated, use 'commitId' instead."),
commitId: commons_CommitIdSchema.optional(),
sinceVersionId: commons_VersionIdSchema.optional().describe("Deprecated, use 'sinceCommitId' instead."),
sinceCommitId: commons_CommitIdSchema.optional(),
sortField: commons_SortFieldSchema.optional(),
sortDirection: commons_SortDirectionSchema.optional(),
filter: commons_FilterSchema.optional(),
filterField: commons_FilterFieldSchema.optional().describe('Name of field by which to filter records'),
searchValue: commons_SearchValueSchema.optional(),
searchField: commons_SearchFieldSchema.optional(),
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. Maximum of 100 allowed. ",
),
pageSize: z.number().int().optional().describe('Number of records to return in a page (default 10,000)'),
pageNumber: z
.number()
.int()
.optional()
.describe('Based on pageSize, which page of records to return (Note - numbers start at 1)'),
includeCounts: z.boolean().optional().describe('**DEPRECATED** Use GET /sheets/:sheetId/counts'),
includeLength: z.boolean().optional().describe('The length of the record result set, returned as counts.total'),
includeLinks: z
.boolean()
.optional()
.describe('If true, linked records will be included in the results. Defaults to false.'),
includeMessages: z.boolean().optional().describe('Include error messages, defaults to false.'),
fields: z
.string()
.optional()
.describe('A list of field keys to include in the response. If not provided, all fields will be included.'),
for: commons_EventIdSchema
.optional()
.describe('if "for" is provided, the query parameters will be pulled from the event payload'),
q: z.string().optional().describe('An FFQL query used to filter the result set'),
} as ZodRawShape
export const getRecords: Tool<typeof params> = {
name: 'getRecords',
description: 'Get records: Returns records from a sheet in a workbook',
params,
cb: async ({
sheetId,
versionId,
commitId,
sinceVersionId,
sinceCommitId,
sortField,
sortDirection,
filter,
filterField,
searchValue,
searchField,
ids,
pageSize,
pageNumber,
includeCounts,
includeLength,
includeLinks,
includeMessages,
fields,
forParam,
q,
}) => {
try {
const searchParams = {
versionId,
commitId,
sinceVersionId,
sinceCommitId,
sortField,
sortDirection,
filter,
filterField,
searchValue,
searchField,
ids,
pageSize,
pageNumber,
includeCounts,
includeLength,
includeLinks,
includeMessages,
fields,
for: forParam,
q,
}
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}/records${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',
}
}
},
}