get-record-counts-sheet.ts•3.35 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_SearchFieldSchema,
commons_SearchValueSchema,
commons_SheetIdSchema,
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('Returns records that were changed in that version and only those records.'),
sinceVersionId: commons_VersionIdSchema.optional().describe("Deprecated, use 'sinceCommitId' instead."),
commitId: commons_CommitIdSchema
.optional()
.describe(
'Returns records that were changed in that version in addition to any records from versions after that version.',
),
sinceCommitId: commons_CommitIdSchema
.optional()
.describe('Listing a commit ID here will return all records since the specified commit.'),
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.'),
byField: z.boolean().optional().describe('If true, the counts for each field will also be returned'),
q: z.string().optional().describe('An FFQL query used to filter the result set to be counted'),
} as ZodRawShape
export const getRecordCountsSheet: Tool<typeof params> = {
name: 'getRecordCountsSheet',
description: 'Get record counts: Returns counts of records from a sheet',
params,
cb: async ({
sheetId,
versionId,
sinceVersionId,
commitId,
sinceCommitId,
filter,
filterField,
searchValue,
searchField,
byField,
q,
}) => {
try {
const searchParams = {
versionId,
sinceVersionId,
commitId,
sinceCommitId,
filter,
filterField,
searchValue,
searchField,
byField,
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}/counts${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',
}
}
},
}