import { createSync } from 'nango';
import { z } from 'zod';
const BoxMetadata = z.object({
files: z.array(z.string()),
folders: z.array(z.string())
});
const BoxDocument = z.object({
id: z.string(),
name: z.string(),
modified_at: z.string(),
download_url: z.string().optional()
});
const sync = createSync({
description: 'Sync files from specific folders or individual files',
version: '1.0.0',
frequency: 'every day',
autoStart: false,
syncType: 'full',
endpoints: [
{
method: 'GET',
path: '/files',
group: 'Files'
}
],
models: {
BoxDocument: BoxDocument
},
metadata: BoxMetadata,
exec: async (nango) => {
const metadata = await nango.getMetadata<z.infer<typeof BoxMetadata>>();
const files = metadata?.files ?? [];
const folders = metadata?.folders ?? [];
const batchSize = 100;
if (files.length === 0 && folders.length === 0) {
throw new Error('Metadata for files or folders is required.');
}
// Process folders first
for (const folder of folders) {
await fetchFolder(nango, folder);
}
// Then process individual files
let batch: z.infer<typeof BoxDocument>[] = [];
for (const file of files) {
const metadata = await getFileMetadata(nango, file);
batch.push({
id: metadata.id,
name: metadata.name,
modified_at: metadata.modified_at,
download_url: metadata.shared_link?.download_url
});
if (batch.length >= batchSize) {
await nango.batchSave(batch, 'BoxDocument');
batch = [];
}
}
if (batch.length > 0) {
await nango.batchSave(batch, 'BoxDocument');
}
}
});
async function fetchFolder(nango: any, folderId: string) {
const proxy = {
endpoint: `/2.0/folders/${folderId}/items`,
params: {
fields: 'id,name,modified_at,shared_link'
},
paginate: {
type: 'cursor',
response_path: 'entries'
}
};
let batch: z.infer<typeof BoxDocument>[] = [];
const batchSize = 100;
for await (const items of nango.paginate(proxy)) {
for (const item of items) {
if (item.type === 'folder') {
await fetchFolder(nango, item.id);
}
if (item.type === 'file') {
batch.push({
id: item.id,
name: item.name,
modified_at: item.modified_at,
download_url: item.shared_link?.download_url
});
if (batch.length >= batchSize) {
await nango.batchSave(batch, 'BoxDocument');
batch = [];
}
}
}
}
if (batch.length > 0) {
await nango.batchSave(batch, 'BoxDocument');
}
}