CI HUBCI HUB SDK
Guides

Browse a folder tree

Walk a connected DAM from the root, listing subfolders and assets at each level.

This guide walks a DAM folder tree from the root down, printing each folder and the assets inside it. It assumes you completed the authentication flow: a seeded TokenManager on the client-library path, or a CI HUB access token plus a DAM connection token on the HTTP path.

Read one folder

Folder browse returns the subfolders and assets directly inside a folder. Subfolders arrive on the first page; assets page with the more cursor (see Pagination). Read the folders once, then keep pulling assets until more is absent.

import { CiHubAccessClient, TokenManager } from '@ci-hub/access-sdk'

const client = new CiHubAccessClient({ baseUrl: 'https://stage.ci-hub.com/api/v1' })
const tokens = new TokenManager(client) // seeded during authentication

const getFolderPage = (folderId: string, more?: string) =>
  tokens.withDamAuth('dropbox', (accessToken, damToken) =>
    client.getFolder({ accessToken, damToken, folderId, options: { more } }),
  )
const BASE_URL = 'https://stage.ci-hub.com/api/v1'

const authHeaders = {
  Authorization: `Bearer ${ciHubAccessToken}`,
  'provider-authorization': `Bearer ${damProviderToken}`,
}

async function getFolderPage(folderId: string, more?: string) {
  const url = new URL(`${BASE_URL}/assets/folder/${encodeURIComponent(folderId)}`)
  if (more !== undefined) url.searchParams.set('more', more)

  const response = await fetch(url, { headers: authHeaders })
  if (!response.ok) {
    const { error } = await response.json()
    throw new Error(`${error.code}: ${error.message}`)
  }
  return response.json()
}

The aggregation on top is identical on both paths:

async function readFolder(folderId: string) {
  const first = await getFolderPage(folderId)
  const assets = [...first.assets]

  let more = first.more
  while (more !== undefined) {
    const page = await getFolderPage(folderId, more)
    assets.push(...page.assets)
    more = page.more
  }

  return { folders: first.folders, assets }
}

Walk the tree

Start at root and recurse into each subfolder by its id. IDs are opaque and can contain slashes; pass them through as received.

async function walkTree(folderId = 'root', depth = 0) {
  const { folders, assets } = await readFolder(folderId)
  const indent = '  '.repeat(depth)

  for (const asset of assets) {
    console.log(`${indent}- ${asset.name}`)
  }
  for (const folder of folders) {
    console.log(`${indent}+ ${folder.name}/`)
    await walkTree(folder.id, depth + 1)
  }
}

await walkTree()

Each asset carries the metadata and the thumbnailUrl / downloadUrl you need without a second call. See Asset model for the field reference, and Download an asset to fetch the bytes.

Next

On this page