workflow.service.ts
Frontend service for fetching config, active workflows, and extracting sections from the UpDoc backend API.
What it does
Section titled “What it does”Provides exported async functions that communicate with the backend API:
fetchActiveWorkflows— retrieves which workflows are complete (have destination + map + source)fetchConfig— retrieves the combined config (source, destination, map) for a blueprintextractSections— extracts structured sections from a media item using the config’s extraction rulesclearConfigCache— clears all in-memory caches
All functions require a bearer token for Umbraco Management API authentication.
Functions
Section titled “Functions”fetchActiveWorkflows
Section titled “fetchActiveWorkflows”export async function fetchActiveWorkflows( token: string): Promise<ActiveWorkflows>Fetches the list of document type aliases and blueprint IDs that have complete workflows from GET /umbraco/management/api/v1/updoc/workflows/active.
- Returns an
ActiveWorkflowsobject containing:documentTypeAliases— string array of document type aliases with complete workflowsblueprintIds— string array of blueprint IDs with complete workflows
- Uses a global singleton cache with deduplication (concurrent calls share one request)
- Returns
{ documentTypeAliases: [], blueprintIds: [] }on error (fails silently) - Cache persists for the browser session; cleared by
clearConfigCache()
fetchConfig
Section titled “fetchConfig”export async function fetchConfig( blueprintId: string, token: string): Promise<DocumentTypeConfig | null>Fetches the combined config for a blueprint from GET /umbraco/management/api/v1/updoc/config/{blueprintId}.
- Uses an in-memory
Map<string, DocumentTypeConfig>cache to avoid repeated network requests - Returns
nulland logs a warning if no config exists for the blueprint (404 response) - Returns the cached config on subsequent calls with the same
blueprintId
extractSections
Section titled “extractSections”export async function extractSections( mediaKey: string, blueprintId: string, token: string, sourceType: string = 'pdf'): Promise<ExtractSectionsResponse | null>Calls GET /umbraco/management/api/v1/updoc/extract-sections?mediaKey={mediaKey}&blueprintId={blueprintId}&sourceType={sourceType} to extract content from a media item using the config’s extraction rules. The sourceType parameter (default 'pdf') tells the backend which extraction service to use.
- Returns an
ExtractSectionsResponsecontaining:sections— aRecord<string, string>of extracted values keyed by section keyconfig— the fullDocumentTypeConfigfor property mapping
- Returns
nulland logs the error if the request fails - Does not cache results (each media item extraction is unique)
clearConfigCache
Section titled “clearConfigCache”export function clearConfigCache(): voidClears the in-memory config cache. Useful when configs have been modified and need to be reloaded.
saveSortOrder
Section titled “saveSortOrder”export async function saveSortOrder( workflowAlias: string, page: number, areaName: string | null, sortedIds: string[], token: string): Promise<TransformResult | null>Saves a new sort order for areas within a page (when areaName is null) or sections within an area (when areaName is provided). Calls PUT /umbraco/management/api/v1/updoc/workflows/{alias}/transform/sort-order.
- Returns the updated
TransformResultwith newsortOrdervalues applied - Returns
nullif the request fails - The backend updates
transform.jsonwithsortOrderindices (0-based)
Key concepts
Section titled “Key concepts”In-memory caches
Section titled “In-memory caches”Two module-level caches persist for the browser session:
const configCache = new Map<string, DocumentTypeConfig>();
let activeWorkflowsCache: ActiveWorkflows | null = null;let activeWorkflowsPromise: Promise<ActiveWorkflows> | null = null;The config cache avoids re-fetching per-blueprint configs. The active workflows cache avoids re-fetching the global workflow list — this is shared between the condition (visibility check) and the action (blueprint filtering).
The activeWorkflowsPromise ensures concurrent callers (e.g., condition evaluating on multiple nodes) share a single in-flight request rather than making duplicate calls.
Authentication
Section titled “Authentication”Both functions pass the bearer token in the Authorization header:
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`,}The token is obtained from UMB_AUTH_CONTEXT by the calling code.
Error handling
Section titled “Error handling”All functions handle non-OK responses gracefully:
fetchActiveWorkflowsreturns empty arrays (fails silently so the condition defaults to hidden)fetchConfigreturnsnullwith aconsole.warnextractSectionsparses the error body and logs it withconsole.error, then returnsnull
API response passthrough
Section titled “API response passthrough”The extractSections function passes through the API response directly:
// API returns { sections, config } - pass through directlyreturn response.json();This keeps the service simple — all extraction and config loading happens on the backend.
Imports
Section titled “Imports”import type { DocumentTypeConfig, ExtractSectionsResponse } from './workflow.types.js';Used by
Section titled “Used by”up-doc-has-workflows.condition.ts— callsfetchActiveWorkflowsto decide whether to show the entity actionup-doc-action.ts— callsfetchActiveWorkflowsto filter the blueprint picker; uses the config from the modal value to apply mappingsup-doc-modal.element.ts— callsextractSectionswhen a source document (PDF or Markdown) is selected