All files / flows list-spaces.js

100% Statements 12/12
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24              1x 1x 1x 1x   1x   1x 2x 2x 2x   1x 1x      
// @implements UC-SPACE-021.1
/**
 * GET /v1/spaces (public, no authentication): the slugs of the published
 * spaces of active flows, for the sitemap index (`/sitemap.xml`, built at the
 * edge by flows-ui). A Query on the flows table's sparse `publicSpaceIndex`
 * (utils/publicSpaces.js), never a Scan. Cached a minute by the spaces CDN.
 */
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { listPublicSpaceSlugs } = require('../utils/publicSpaces')
const { respondPublic } = require('../space-pages/shared')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
exports.handler = async () => {
  try {
    const slugs = await listPublicSpaceSlugs(docClient, process.env.FLOWS_TABLE_NAME)
    return respondPublic(200, { spaces: slugs.map((slug) => ({ slug })) })
  } catch (error) {
    console.error('Error listing the public spaces:', error)
    return respondPublic(500, { error: 'Failed to list spaces' })
  }
}