upload-file.ts•2.38 kB
/**
* This file was generated. Do NOT edit this file.
*/
import fetch from 'node-fetch'
import { type ZodRawShape, z } from 'zod'
import {
commons_ActionSchema,
commons_EnvironmentIdSchema,
commons_SpaceIdSchema,
files_FileOriginSchema,
files_FileSchema,
files_ModeSchema,
} from '../schemas.js'
import type { Tool } from './index.js'
const params = {
spaceId: commons_SpaceIdSchema,
environmentId: commons_EnvironmentIdSchema,
mode: files_ModeSchema.optional().describe('The storage mode of file to insert, defaults to "import"'),
file: files_FileSchema.describe('A binary payload containing the file contents'),
actions: z.array(commons_ActionSchema).optional().describe('The actions attached to the file'),
origin: files_FileOriginSchema.optional().describe('The origin of the file, ie filesystem'),
} as ZodRawShape
export const uploadFile: Tool<typeof params> = {
name: 'uploadFile',
description: 'Upload a file: Upload a file',
params,
cb: async ({ spaceId, environmentId, mode, file, actions, origin }) => {
try {
const searchParams = {}
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}/files${searchParamsString ? `?${searchParamsString}` : ''}`
const response = await fetch(url, {
method: 'POST',
headers: {
'X-Disable-Hooks': 'true',
Authorization: `Bearer ${process.env.FLATFILE_BEARER_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
spaceId: spaceId,
environmentId: environmentId,
mode: mode,
file: file,
actions: actions,
origin: origin,
}),
})
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',
}
}
},
}