list-users.ts•2.24 kB
/**
* This file was generated. Do NOT edit this file.
*/
import fetch from 'node-fetch'
import { type ZodRawShape, z } from 'zod'
import { commons_SortDirectionSchema, users_ListUsersSortFieldSchema } from '../schemas.js'
import type { Tool } from './index.js'
const params = {
email: z.string().optional().describe('Email of guest to return'),
search: z.string().optional().describe('String to search for users by name and email'),
sortField: users_ListUsersSortFieldSchema.optional().describe('Field to sort users by'),
sortDirection: commons_SortDirectionSchema.optional().describe('Direction of sorting'),
pageSize: z.number().int().optional().describe('Number of users to return in a page (default 20)'),
pageNumber: z.number().int().optional().describe('Based on pageSize, which page of users to return'),
} as ZodRawShape
export const listUsers: Tool<typeof params> = {
name: 'listUsers',
description: 'List users: Gets a list of users',
params,
cb: async ({ email, search, sortField, sortDirection, pageSize, pageNumber }) => {
try {
const searchParams = { email, search, sortField, sortDirection, pageSize, pageNumber }
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}/users${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',
}
}
},
}