All files / src/utils constants.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 3/3
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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                    17x           17x                     472x 472x                 8x                       17x           17x                 17x                       17x               24x               17x  
/**
 * System Constants
 *
 * Shared constants used across the application
 */
 
/**
 * Reserved entity IDs that cannot be used for user-created entities
 * These correspond to system entities with hardcoded schemas
 */
export const RESERVED_ENTITY_IDS = ['entity', 'role', 'user'] as const
 
/**
 * Visibility levels for entities. `restricted` entities are only reached by a
 * permission naming their id (or `*:*`) — see usePermissions' hasPermission.
 */
export const VISIBILITY_LEVELS = ['flow-members', 'restricted', 'public'] as const
 
export type VisibilityLevel = (typeof VISIBILITY_LEVELS)[number]
 
/**
 * Check if an entity name would conflict with a reserved system entity ID
 * @param name - The proposed entity name
 * @returns true if the name conflicts with a reserved ID
 */
export function isReservedEntityName(name: string): boolean {
  // Normalize the name the same way the backend does
  const proposedId = name.toLowerCase().replace(/[^a-z0-9-]/g, '-')
  return RESERVED_ENTITY_IDS.includes(proposedId as any)
}
 
/**
 * Get a user-friendly error message for reserved entity names
 * @param name - The proposed entity name
 * @returns An error message explaining the conflict
 */
export function getReservedNameError(name: string): string {
  return `Entity name "${name}" conflicts with reserved system entity. Reserved names: ${RESERVED_ENTITY_IDS.join(', ')}`
}
 
/**
 * Mirrors LARGE_ENTITY_ITEM_THRESHOLD in
 * flows-api/infrastructure/lambda/utils/constants.js — the item-list backend
 * only honors a `sort` request when the candidate set (the whole entity if
 * unfiltered, or the matched/filtered count otherwise) is within this
 * threshold. No shared module exists between flows-ui and flows-api (separate
 * deployables), so this is a deliberate, commented duplication — keep both
 * values in sync.
 */
export const LARGE_ENTITY_ITEM_THRESHOLD = 1000
 
/**
 * Mirrors TEXT_SEARCH_TYPES in flows-api/infrastructure/lambda/utils/textSearchIndex.js —
 * the only field types the item-list backend accepts as a `sort` fieldId.
 */
export const SORTABLE_FIELD_TYPES = ['text', 'textarea', 'email'] as const
 
/**
 * Mirrors CREATED_AT_SORT_FIELD_ID in flows-api/infrastructure/lambda/items/filtering.js —
 * a reserved `sort.fieldId` for the item's own `createdAt`, not a real entity
 * field. Unlike SORTABLE_FIELD_TYPES fields, it's always available regardless
 * of entity/filtered size — backed by a real DynamoDB index (createdAtIndex),
 * not an in-memory fetch-all.
 */
export const CREATED_AT_SORT_FIELD_ID = 'createdAt'
 
/**
 * A flow's slug (URL identifier) format — mirrors the `pattern` on
 * CreateFlowRequest.slug in flows-api/api-definition.yaml. That pattern is
 * enforced by API Gateway itself (the REST API is built directly from the
 * OpenAPI spec via SpecRestApi), before any request ever reaches a Lambda —
 * so a mismatch there can only ever surface as API Gateway's own generic
 * "Invalid request body" response, never a specific one. Checking the same
 * pattern here lets the UI catch it first with a message that actually says
 * what's wrong.
 */
export const FLOW_SLUG_PATTERN = /^[a-z0-9-]+$/
 
/**
 * Check whether a flow slug matches the format the backend requires.
 * @param slug - The proposed flow slug
 * @returns true if the slug is non-empty and made up only of lowercase letters, digits, and hyphens
 */
export function isValidFlowSlug(slug: string): boolean {
  return FLOW_SLUG_PATTERN.test(slug)
}
 
/**
 * Maximum length of a comment, counted in characters (Unicode code points), as
 * the user sees them in the composer counter. Mirrors MAX_COMMENT_LENGTH in
 * flows-api's utils/constants.js, which enforces it.
 */
export const MAX_COMMENT_LENGTH = 256