2024-04-15 18:22:34 +00:00
declare const window : Window & typeof globalThis ;
2022-12-21 11:06:09 -05:00
const { port , hostname } = window . location ;
2022-10-17 17:18:24 -04:00
let protocol = 'https' ;
2022-12-21 11:06:09 -05:00
2023-02-07 17:21:54 -05:00
declare global {
interface SpiffworkflowFrontendJsenvObject {
[ key : string ] : string ;
}
interface Window {
spiffworkflowFrontendJsenv : SpiffworkflowFrontendJsenvObject ;
}
}
2023-02-17 14:35:13 -05:00
let spiffEnvironment = '' ;
2023-02-07 17:21:54 -05:00
let appRoutingStrategy = 'subdomain_based' ;
2023-04-26 13:31:12 -04:00
let backendBaseUrl = null ;
2023-05-23 16:58:58 -04:00
let documentationUrl = null ;
2023-02-17 14:35:13 -05:00
if ( 'spiffworkflowFrontendJsenv' in window ) {
if ( 'APP_ROUTING_STRATEGY' in window . spiffworkflowFrontendJsenv ) {
appRoutingStrategy = window . spiffworkflowFrontendJsenv . APP_ROUTING_STRATEGY ;
}
if ( 'ENVIRONMENT_IDENTIFIER' in window . spiffworkflowFrontendJsenv ) {
spiffEnvironment = window . spiffworkflowFrontendJsenv . ENVIRONMENT_IDENTIFIER ;
}
2023-04-26 13:31:12 -04:00
if ( 'BACKEND_BASE_URL' in window . spiffworkflowFrontendJsenv ) {
backendBaseUrl = window . spiffworkflowFrontendJsenv . BACKEND_BASE_URL ;
}
2023-05-23 16:58:58 -04:00
if ( 'DOCUMENTATION_URL' in window . spiffworkflowFrontendJsenv ) {
documentationUrl = window . spiffworkflowFrontendJsenv . DOCUMENTATION_URL ;
}
2023-02-07 17:21:54 -05:00
}
2023-04-26 13:31:12 -04:00
if ( ! backendBaseUrl ) {
let hostAndPortAndPathPrefix ;
if ( appRoutingStrategy === 'subdomain_based' ) {
hostAndPortAndPathPrefix = ` api. ${ hostname } ` ;
} else if ( appRoutingStrategy === 'path_based' ) {
hostAndPortAndPathPrefix = ` ${ hostname } /api ` ;
} else {
throw new Error ( ` Invalid app routing strategy: ${ appRoutingStrategy } ` ) ;
2022-12-21 11:06:09 -05:00
}
2023-02-17 14:35:13 -05:00
2023-04-26 13:31:12 -04:00
if ( /^\d+\./ . test ( hostname ) || hostname === 'localhost' ) {
let serverPort = 7000 ;
if ( ! Number . isNaN ( Number ( port ) ) ) {
serverPort = Number ( port ) - 1 ;
}
hostAndPortAndPathPrefix = ` ${ hostname } : ${ serverPort } ` ;
protocol = 'http' ;
if ( spiffEnvironment === '' ) {
// using destructuring on an array where we only want the first element
// seems super confusing for non-javascript devs to read so let's NOT do that.
// eslint-disable-next-line prefer-destructuring
spiffEnvironment = hostname . split ( '.' ) [ 0 ] ;
}
2023-02-17 14:35:13 -05:00
}
2023-04-26 13:31:12 -04:00
backendBaseUrl = ` ${ protocol } :// ${ hostAndPortAndPathPrefix } /v1.0 ` ;
}
2023-02-07 17:21:54 -05:00
2023-04-26 13:31:12 -04:00
if ( ! backendBaseUrl . endsWith ( '/v1.0' ) ) {
backendBaseUrl += '/v1.0' ;
2022-12-21 11:06:09 -05:00
}
2024-04-15 18:22:34 +00:00
const BACKEND_BASE_URL = backendBaseUrl ;
const DOCUMENTATION_URL = documentationUrl ;
2022-10-12 10:21:49 -04:00
2024-04-15 18:22:34 +00:00
const PROCESS_STATUSES = [
2022-10-12 10:21:49 -04:00
'complete' ,
2022-11-10 15:55:17 -05:00
'error' ,
2023-12-05 11:41:59 -05:00
'not_started' ,
'running' ,
2022-10-12 10:21:49 -04:00
'suspended' ,
2022-12-02 13:47:04 -05:00
'terminated' ,
2023-12-05 11:41:59 -05:00
'user_input_required' ,
'waiting' ,
2022-10-12 10:21:49 -04:00
] ;
2022-11-02 12:42:49 -04:00
// with time: yyyy-MM-dd HH:mm:ss
2023-07-14 11:33:12 -04:00
let generalDateFormat = 'yyyy-MM-dd' ;
2023-04-11 17:25:46 -04:00
if (
'spiffworkflowFrontendJsenv' in window &&
'DATE_FORMAT' in window . spiffworkflowFrontendJsenv
) {
generalDateFormat = window . spiffworkflowFrontendJsenv . DATE_FORMAT ;
}
2024-01-02 10:16:00 -05:00
const splitDateFormat = generalDateFormat . split ( '-' ) ;
2024-01-03 10:42:54 -05:00
// https://date-fns.org/v3.0.6/docs/format
const supportedDateFormatTypes = {
yyyy : '2024' ,
MM : '01' ,
MMM : 'Jan' ,
MMMM : 'January' ,
dd : '01' ,
} ;
2024-01-02 10:16:00 -05:00
const unsupportedFormatTypes = splitDateFormat . filter (
2024-06-04 14:26:40 -04:00
( x ) = > ! Object . keys ( supportedDateFormatTypes ) . includes ( x ) ,
2024-01-02 10:16:00 -05:00
) ;
2024-01-03 10:42:54 -05:00
const formattedSupportedDateTypes : string [ ] = [ ] ;
Object . entries ( supportedDateFormatTypes ) . forEach ( ( [ key , value ] ) = > {
formattedSupportedDateTypes . push ( ` ${ key } : ${ value } ` ) ;
} ) ;
2024-01-02 10:16:00 -05:00
if ( unsupportedFormatTypes . length > 0 ) {
2023-04-11 17:25:46 -04:00
throw new Error (
2024-01-02 10:16:00 -05:00
` Given SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_DATE_FORMAT is not supported. Given: ${ generalDateFormat } with invalid options: ${ unsupportedFormatTypes . join (
2024-06-04 14:26:40 -04:00
', ' ,
) } . Valid options are : $ { formattedSupportedDateTypes . join ( ', ' ) } ` ,
2023-04-11 17:25:46 -04:00
) ;
}
const carbonDateFormat = generalDateFormat
2024-01-02 10:16:00 -05:00
. replace ( /\byyyy\b/ , 'Y' )
. replace ( /\bMM\b/ , 'm' )
. replace ( /\bMMM\b/ , 'M' )
. replace ( /\bMMMM\b/ , 'F' )
. replace ( /\bdd\b/ , 'd' ) ;
2024-04-15 18:22:34 +00:00
const DATE_TIME_FORMAT = ` ${ generalDateFormat } HH:mm:ss ` ;
const TIME_FORMAT_HOURS_MINUTES = 'HH:mm' ;
const DATE_FORMAT = generalDateFormat ;
const DATE_FORMAT_CARBON = carbonDateFormat ;
const DATE_FORMAT_FOR_DISPLAY = generalDateFormat . toLowerCase ( ) ;
const DATE_RANGE_DELIMITER = ':::' ;
2023-02-17 14:35:13 -05:00
2024-04-15 18:22:34 +00:00
const SPIFF_ENVIRONMENT = spiffEnvironment ;
export {
DATE_TIME_FORMAT ,
TIME_FORMAT_HOURS_MINUTES ,
DATE_FORMAT ,
DATE_FORMAT_CARBON ,
DATE_FORMAT_FOR_DISPLAY ,
DATE_RANGE_DELIMITER ,
BACKEND_BASE_URL ,
DOCUMENTATION_URL ,
PROCESS_STATUSES ,
SPIFF_ENVIRONMENT ,
} ;