@@ -21,15 +21,15 @@ import type { SubscriptionTypeValue } from 'src/shared/subscriptions/types';
2121import { logMethodCall } from 'src/shared/utils/utils' ;
2222
2323export default class User {
24- static singletonInstance ?: User ;
24+ static _singletonInstance ?: User ;
2525
2626 /**
2727 * Creates a user singleton
2828 * @returns - User singleton
2929 */
30- static createOrGetInstance ( ) : User {
31- if ( ! User . singletonInstance ) {
32- User . singletonInstance = new User ( ) ;
30+ static _createOrGetInstance ( ) : User {
31+ if ( ! User . _singletonInstance ) {
32+ User . _singletonInstance = new User ( ) ;
3333 const identityModel = OneSignal . _coreDirector . _getIdentityModel ( ) ;
3434 const propertiesModel = OneSignal . _coreDirector . _getPropertiesModel ( ) ;
3535
@@ -52,46 +52,10 @@ export default class User {
5252 }
5353 }
5454
55- return User . singletonInstance ;
55+ return User . _singletonInstance ;
5656 }
5757
58- get onesignalId ( ) : string | undefined {
59- const onesignalId = OneSignal . _coreDirector . _getIdentityModel ( ) . onesignalId ;
60- return IDManager . _isLocalId ( onesignalId ) ? undefined : onesignalId ;
61- }
62-
63- private validateStringLabel ( label : string , labelName : string ) : void {
64- if ( typeof label !== 'string' ) throw WrongTypeArgumentError ( labelName ) ;
65-
66- if ( ! label ) throw EmptyArgumentError ( labelName ) ;
67- }
68-
69- private validateArray ( array : string [ ] , arrayName : string ) : void {
70- if ( ! Array . isArray ( array ) ) throw WrongTypeArgumentError ( arrayName ) ;
71-
72- if ( array . length === 0 ) throw EmptyArgumentError ( arrayName ) ;
73-
74- for ( const label of array ) {
75- this . validateLabel ( label , 'label' ) ;
76- }
77- }
78-
79- private validateObject ( object : unknown , objectName : string ) : void {
80- if ( ! isObject ( object ) ) throw WrongTypeArgumentError ( objectName ) ;
81-
82- if ( ! object || Object . keys ( object ) . length === 0 )
83- throw EmptyArgumentError ( objectName ) ;
84- }
85-
86- private validateLabel ( label : string , labelName : string ) : void {
87- this . validateStringLabel ( label , labelName ) ;
88-
89- if ( label === 'external_id' || label === 'onesignal_id' ) {
90- throw ReservedArgumentError ( label ) ;
91- }
92- }
93-
94- private updateIdentityModel ( aliases : {
58+ private _updateIdentityModel ( aliases : {
9559 [ key : string ] : string | undefined ;
9660 } ) : void {
9761 const identityModel = OneSignal . _coreDirector . _getIdentityModel ( ) ;
@@ -101,12 +65,17 @@ export default class User {
10165 }
10266
10367 /* PUBLIC API METHODS */
68+ get onesignalId ( ) : string | undefined {
69+ const onesignalId = OneSignal . _coreDirector . _getIdentityModel ( ) . onesignalId ;
70+ return IDManager . _isLocalId ( onesignalId ) ? undefined : onesignalId ;
71+ }
72+
10473 public addAlias ( label : string , id : string ) : void {
10574 logMethodCall ( 'addAlias' , { label, id } ) ;
10675 if ( isConsentRequiredButNotGiven ( ) ) return ;
10776
108- this . validateStringLabel ( label , 'label' ) ;
109- this . validateStringLabel ( id , 'id' ) ;
77+ validateStringLabel ( label , 'label' ) ;
78+ validateStringLabel ( id , 'id' ) ;
11079
11180 this . addAliases ( { [ label ] : id } ) ;
11281 }
@@ -115,14 +84,14 @@ export default class User {
11584 logMethodCall ( 'addAliases' , { aliases } ) ;
11685 if ( isConsentRequiredButNotGiven ( ) ) return ;
11786
118- this . validateObject ( aliases , 'aliases' ) ;
87+ validateObject ( aliases , 'aliases' ) ;
11988
12089 for ( const label of Object . keys ( aliases ) ) {
121- this . validateStringLabel ( aliases [ label ] , `key: ${ label } ` ) ;
122- this . validateLabel ( label , `key: ${ label } ` ) ;
90+ validateStringLabel ( aliases [ label ] , `key: ${ label } ` ) ;
91+ validateLabel ( label , `key: ${ label } ` ) ;
12392 }
12493
125- this . updateIdentityModel ( aliases ) ;
94+ this . _updateIdentityModel ( aliases ) ;
12695 }
12796
12897 public removeAlias ( label : string ) : void {
@@ -136,19 +105,19 @@ export default class User {
136105 logMethodCall ( 'removeAliases' , { aliases } ) ;
137106 if ( isConsentRequiredButNotGiven ( ) ) return ;
138107
139- this . validateArray ( aliases , 'aliases' ) ;
108+ validateArray ( aliases , 'aliases' ) ;
140109
141110 const newAliases = Object . fromEntries (
142111 aliases . map ( ( key ) => [ key , undefined ] ) ,
143112 ) ;
144- this . updateIdentityModel ( newAliases ) ;
113+ this . _updateIdentityModel ( newAliases ) ;
145114 }
146115
147116 public addEmail ( email : string ) : void {
148117 logMethodCall ( 'addEmail' , { email } ) ;
149118 if ( isConsentRequiredButNotGiven ( ) ) return ;
150119
151- this . validateStringLabel ( email , 'email' ) ;
120+ validateStringLabel ( email , 'email' ) ;
152121
153122 if ( ! isValidEmail ( email ) ) throw MalformedArgumentError ( 'email' ) ;
154123
@@ -162,7 +131,7 @@ export default class User {
162131 logMethodCall ( 'addSms' , { sms } ) ;
163132 if ( isConsentRequiredButNotGiven ( ) ) return ;
164133
165- this . validateStringLabel ( sms , 'sms' ) ;
134+ validateStringLabel ( sms , 'sms' ) ;
166135
167136 addSubscriptionToModels ( {
168137 type : SubscriptionType . SMS ,
@@ -174,7 +143,7 @@ export default class User {
174143 logMethodCall ( 'removeEmail' , { email } ) ;
175144 if ( isConsentRequiredButNotGiven ( ) ) return ;
176145
177- this . validateStringLabel ( email , 'email' ) ;
146+ validateStringLabel ( email , 'email' ) ;
178147
179148 const emailSubscriptions =
180149 OneSignal . _coreDirector . _getEmailSubscriptionModels ( ) ;
@@ -190,7 +159,7 @@ export default class User {
190159 logMethodCall ( 'removeSms' , { smsNumber } ) ;
191160 if ( isConsentRequiredButNotGiven ( ) ) return ;
192161
193- this . validateStringLabel ( smsNumber , 'smsNumber' ) ;
162+ validateStringLabel ( smsNumber , 'smsNumber' ) ;
194163
195164 const smsSubscriptions =
196165 OneSignal . _coreDirector . _getSmsSubscriptionModels ( ) ;
@@ -205,8 +174,8 @@ export default class User {
205174 logMethodCall ( 'addTag' , { key, value } ) ;
206175 if ( isConsentRequiredButNotGiven ( ) ) return ;
207176
208- this . validateStringLabel ( key , 'key' ) ;
209- this . validateStringLabel ( value , 'value' ) ;
177+ validateStringLabel ( key , 'key' ) ;
178+ validateStringLabel ( value , 'value' ) ;
210179
211180 this . addTags ( { [ key ] : value } ) ;
212181 }
@@ -223,7 +192,7 @@ export default class User {
223192 logMethodCall ( 'removeTag' , { tagKey } ) ;
224193 if ( isConsentRequiredButNotGiven ( ) ) return ;
225194
226- this . validateStringLabel ( tagKey , 'tagKey' ) ;
195+ validateStringLabel ( tagKey , 'tagKey' ) ;
227196
228197 this . removeTags ( [ tagKey ] ) ;
229198 }
@@ -232,7 +201,7 @@ export default class User {
232201 logMethodCall ( 'removeTags' , { tagKeys } ) ;
233202 if ( isConsentRequiredButNotGiven ( ) ) return ;
234203
235- this . validateArray ( tagKeys , 'tagKeys' ) ;
204+ validateArray ( tagKeys , 'tagKeys' ) ;
236205
237206 const propertiesModel = OneSignal . _coreDirector . _getPropertiesModel ( ) ;
238207 const newTags = { ...propertiesModel . tags } ;
@@ -253,7 +222,7 @@ export default class User {
253222 logMethodCall ( 'setLanguage' , { language } ) ;
254223 if ( isConsentRequiredButNotGiven ( ) ) return ;
255224
256- this . validateStringLabel ( language , 'language' ) ;
225+ validateStringLabel ( language , 'language' ) ;
257226
258227 const propertiesModel = OneSignal . _coreDirector . _getPropertiesModel ( ) ;
259228 propertiesModel . language = language ;
@@ -347,3 +316,34 @@ function isObjectSerializable(value: unknown): boolean {
347316 return false ;
348317 }
349318}
319+
320+ function validateStringLabel ( label : string , labelName : string ) : void {
321+ if ( typeof label !== 'string' ) throw WrongTypeArgumentError ( labelName ) ;
322+
323+ if ( ! label ) throw EmptyArgumentError ( labelName ) ;
324+ }
325+
326+ function validateArray ( array : string [ ] , arrayName : string ) : void {
327+ if ( ! Array . isArray ( array ) ) throw WrongTypeArgumentError ( arrayName ) ;
328+
329+ if ( array . length === 0 ) throw EmptyArgumentError ( arrayName ) ;
330+
331+ for ( const label of array ) {
332+ validateLabel ( label , 'label' ) ;
333+ }
334+ }
335+
336+ function validateObject ( object : unknown , objectName : string ) : void {
337+ if ( ! isObject ( object ) ) throw WrongTypeArgumentError ( objectName ) ;
338+
339+ if ( ! object || Object . keys ( object ) . length === 0 )
340+ throw EmptyArgumentError ( objectName ) ;
341+ }
342+
343+ function validateLabel ( label : string , labelName : string ) : void {
344+ validateStringLabel ( label , labelName ) ;
345+
346+ if ( label === 'external_id' || label === 'onesignal_id' ) {
347+ throw ReservedArgumentError ( label ) ;
348+ }
349+ }
0 commit comments