1
1
import AsyncStorage from '@react-native-async-storage/async-storage' ;
2
2
import { User } from '@utils' ;
3
- import { CioConfig } from 'customerio-reactnative' ;
3
+ import { CioConfig , CioLogLevel , CioRegion } from 'customerio-reactnative' ;
4
4
5
5
const USER_STORAGE_KEY = 'user' ;
6
6
const CIO_CONFIG_STORAGE_KEY = 'cioConfig' ;
7
7
8
+ export type QASettings = {
9
+ cdnHost : string ;
10
+ apiHost : string ;
11
+ } ;
12
+
13
+ type Config = Partial < CioConfig > & { qa : QASettings } ;
14
+
15
+ const defaultConfig : Config = {
16
+ region : CioRegion . US ,
17
+ logLevel : CioLogLevel . Error ,
18
+ trackApplicationLifecycleEvents : true ,
19
+ qa : {
20
+ cdnHost : 'https://cdp.customer.io/v1' ,
21
+ apiHost : 'https://cdp.customer.io/v1' ,
22
+ } ,
23
+ } ;
24
+
8
25
export class Storage {
9
26
private user : User | null = null ;
10
- private cioConfig : CioConfig | null = null ;
27
+ private config : Config | null = null ;
11
28
12
29
static readonly instance : Storage = new Storage ( ) ;
13
30
14
31
private constructor ( ) { }
15
32
16
33
readonly loadAll = async ( ) => {
17
- if ( ! this . user || ! this . cioConfig ) {
34
+ if ( ! this . user || ! this . config ) {
18
35
await this . loadFromStorage ( ) ;
19
36
}
20
37
} ;
@@ -24,8 +41,9 @@ export class Storage {
24
41
const cioConfigJsonPayload = await AsyncStorage . getItem (
25
42
CIO_CONFIG_STORAGE_KEY
26
43
) ;
44
+
27
45
this . user = userJsonPayload ? JSON . parse ( userJsonPayload ) : null ;
28
- this . cioConfig = cioConfigJsonPayload
46
+ this . config = cioConfigJsonPayload
29
47
? JSON . parse ( cioConfigJsonPayload )
30
48
: null ;
31
49
} ;
@@ -40,17 +58,55 @@ export class Storage {
40
58
} ;
41
59
42
60
readonly removeUser = async ( ) => {
43
- await AsyncStorage . removeItem ( USER_STORAGE_KEY ) ;
44
61
this . user = null ;
62
+ await AsyncStorage . removeItem ( USER_STORAGE_KEY ) ;
63
+ } ;
64
+
65
+ readonly setCioConfig = async ( cioConfig : CioConfig ) => {
66
+ const config = cioConfig as Config ;
67
+ this . config = { ...config , qa : config . qa ?? defaultConfig . qa } ;
68
+ await AsyncStorage . setItem (
69
+ CIO_CONFIG_STORAGE_KEY ,
70
+ JSON . stringify ( this . config )
71
+ ) ;
72
+ } ;
73
+
74
+ readonly getCioConfig = ( ) : CioConfig => {
75
+ return this . config as CioConfig ;
45
76
} ;
46
77
47
- readonly setCioConfig = async ( config : CioConfig ) => {
48
- await AsyncStorage . setItem ( CIO_CONFIG_STORAGE_KEY , JSON . stringify ( config ) ) ;
49
- this . cioConfig = config ;
78
+ readonly getDefaultCioConfig = ( ) : Partial < CioConfig > => {
79
+ return defaultConfig ;
50
80
} ;
51
81
52
- readonly getCioConfig = ( ) => {
53
- return this . cioConfig ;
82
+ readonly resetCioConfig = async ( ) => {
83
+ if ( this . config === null ) {
84
+ this . config = defaultConfig ;
85
+ } else {
86
+ this . config = { ...this . config , ...defaultConfig , qa : this . config . qa } ;
87
+ }
88
+
89
+ await AsyncStorage . setItem (
90
+ CIO_CONFIG_STORAGE_KEY ,
91
+ JSON . stringify ( this . config )
92
+ ) ;
93
+ } ;
94
+
95
+ readonly getQaConfig = ( ) : QASettings => {
96
+ return this . config ?. qa ?? defaultConfig . qa ;
97
+ } ;
98
+
99
+ readonly setQaConfig = async ( qa : QASettings ) => {
100
+ if ( this . config === null ) {
101
+ this . config = { ...defaultConfig , qa } ;
102
+ } else {
103
+ this . config = { ...this . config , qa } ;
104
+ }
105
+
106
+ await AsyncStorage . setItem (
107
+ CIO_CONFIG_STORAGE_KEY ,
108
+ JSON . stringify ( this . config )
109
+ ) ;
54
110
} ;
55
111
56
112
readonly clear = async ( ) => {
0 commit comments