1- import { Action , UserAttributes } from './types/public'
1+ import { Action , UserAttributes , ActionContext , RenderOptions } from './types/public'
22import Constants from './Constants'
33import ArgsBuilder from './ArgsBuilder'
44import { CreateRequestFunction , Message , MessageVariables } from './types/internal'
55import EventEmitter from './EventEmitter'
66import Network from './Network'
77import isEqual from 'lodash.isequal'
88import LocalStorageManager from './LocalStorageManager'
9+ import ValueTransforms from './ValueTransforms'
910
1011/* eslint-disable @typescript-eslint/ban-types */
1112
1213type MessageId = string
1314type Timestamp = number
1415type MessageHash = { [ key : string ] : Message }
15- type ActionContext = {
16- // matches the ActionContext API in Android/iOS
17- // https://docs.leanplum.com/reference#section-android-custom-templates
18- track : ( event ?: string , value ?: number , info ?: string , params ?: Object ) => void ;
19- runActionNamed : ( actionName : string ) => void ;
20- runTrackedActionNamed : ( actionName : string ) => void ;
21- }
2216type TriggerContext =
17+ // TODO: 'install' trigger for first session
2318 { trigger : 'start' } |
2419 { trigger : 'resume' } |
2520 { trigger : 'userAttribute' ; attributes : UserAttributes } |
@@ -34,11 +29,6 @@ type FilterConfig = {
3429 objects ?: Array < string | number > ;
3530 } > ;
3631}
37- type RenderOptions = {
38- isPreview ?: boolean ;
39- context : ActionContext ;
40- message : MessageVariables ;
41- }
4232type TrackOptions = {
4333 event ?: string ;
4434 value ?: number ;
@@ -123,7 +113,6 @@ const verbToInterval = (verb: string): number => {
123113}
124114
125115export default class Messages {
126- private _files : { [ key : string ] : string } = { }
127116 private _messageCache : MessageHash = { }
128117 private occurrenceTracker = new OccurrenceTracker ( )
129118
@@ -200,10 +189,10 @@ export default class Messages {
200189 this . handleMessage ( {
201190 isPreview : true ,
202191
203- message : {
192+ message : this . addDefaults ( {
204193 messageId : message . messageId ,
205194 ...vars ,
206- } ,
195+ } ) ,
207196
208197 context,
209198 } )
@@ -452,35 +441,36 @@ export default class Messages {
452441 return this . _messageCache || { }
453442 }
454443
455- private colorToHex ( color : number ) : string {
456- const b = color & 0xff ; color >>= 8
457- const g = color & 0xff ; color >>= 8
458- const r = color & 0xff ; color >>= 8
459- const a = ( color & 0xff ) / 255
460- return `rgba(${ r } ,${ g } ,${ b } ,${ a } )`
461- }
462-
463444 private addDefaults ( vars : MessageVariables ) : MessageVariables {
464- const kinds = this . getMessages ( ) . actionDefinitions || { }
465- const defaults = kinds [ vars . __name__ ]
445+ const definitions = this . getMessages ( ) . actionDefinitions || { }
446+ const definition = definitions [ vars . __name__ ]
447+ const kinds = definition ?. kinds
466448
467- if ( ! defaults ) {
449+ if ( ! definition ) {
468450 return vars
469451 }
470452
471- function useDefaults ( obj : MessageVariables , defaultValues : MessageVariables ) : MessageVariables {
453+ const useDefaults = (
454+ obj : MessageVariables ,
455+ defaultValues : MessageVariables ,
456+ path = ''
457+ ) : MessageVariables => {
472458 for ( const key of Object . keys ( defaultValues ) ) {
473459 const value = defaultValues [ key ]
474460 if ( typeof value === 'object' ) {
475- obj [ key ] = useDefaults ( obj [ key ] || { } , value )
461+ obj [ key ] = useDefaults ( obj [ key ] || { } , value , ` ${ path } ${ key } .` )
476462 } else if ( typeof obj [ key ] === 'undefined' ) {
477463 obj [ key ] = value
478464 }
465+
466+ if ( kinds [ `${ path } ${ key } ` ] === 'FILE' ) {
467+ obj [ key ] = this . getFileUrl ( obj [ key ] )
468+ }
479469 }
480470 return obj
481471 }
482472
483- return useDefaults ( { ...vars } , defaults . values )
473+ return useDefaults ( { ...vars } , definition . values )
484474 }
485475
486476 private resolveFiles ( vars : MessageVariables ) : MessageVariables {
@@ -505,7 +495,7 @@ export default class Messages {
505495 const name = key . replace ( filePrefix , '' )
506496 vars [ name + ' URL' ] = this . getFileUrl ( vars [ key ] )
507497 } else if ( colorSuffix . test ( key ) ) {
508- vars [ key ] = this . colorToHex ( vars [ key ] )
498+ vars [ key ] = ValueTransforms . decodeColor ( vars [ key ] )
509499 } else if ( typeof vars [ key ] === 'object' ) {
510500 vars [ key ] = this . resolveFields ( vars [ key ] )
511501 }
0 commit comments