@@ -40,16 +40,17 @@ export default class SuppressionClient
4040 extends NavigationThruPages < SuppressionList >
4141 implements ISuppressionClient {
4242 request : Request ;
43- models : Map < string , any > ;
43+ models : object ;
4444
4545 constructor ( request : Request ) {
4646 super ( request ) ;
4747 this . request = request ;
48- this . models = new Map ( ) ;
49- this . models . set ( 'bounces' , Bounce ) ;
50- this . models . set ( 'complaints' , Complaint ) ;
51- this . models . set ( 'unsubscribes' , Unsubscribe ) ;
52- this . models . set ( 'whitelists' , WhiteList ) ;
48+ this . models = {
49+ bounces : Bounce ,
50+ complaints : Complaint ,
51+ unsubscribes : Unsubscribe ,
52+ whitelists : WhiteList ,
53+ } ;
5354 }
5455
5556 protected parseList (
@@ -78,9 +79,10 @@ export default class SuppressionClient
7879
7980 private createWhiteList (
8081 domain : string ,
81- data : SuppressionCreationData | SuppressionCreationData [ ]
82+ data : SuppressionCreationData | SuppressionCreationData [ ] ,
83+ isDataArray : boolean
8284 ) : Promise < SuppressionCreationResult > {
83- if ( Array . isArray ( data ) ) {
85+ if ( isDataArray ) {
8486 throw new APIError ( {
8587 status : 400 ,
8688 statusText : 'Data property should be an object' ,
@@ -94,14 +96,59 @@ export default class SuppressionClient
9496 . then ( this . prepareResponse ) ;
9597 }
9698
97- private checkType ( type : string ) {
98- if ( ! this . models . has ( type ) ) {
99+ private createUnsubscribe (
100+ domain : string ,
101+ data : SuppressionCreationData | SuppressionCreationData [ ]
102+ ) : Promise < SuppressionCreationResult > {
103+ if ( Array . isArray ( data ) ) { // User provided an array
104+ const isContainsTag = data . some ( ( unsubscribe : SuppressionCreationData ) => unsubscribe . tag ) ;
105+ if ( isContainsTag ) {
106+ throw new APIError ( {
107+ status : 400 ,
108+ statusText : 'Tag property should not be used for creating multiple unsubscribes.' ,
109+ body : {
110+ message : 'Tag property can be used only if one unsubscribe provided as second argument of create method. Please use tags instead.'
111+ }
112+ } as APIErrorOptions ) ;
113+ }
114+ return this . request
115+ . post ( urljoin ( 'v3' , domain , 'unsubscribes' ) , JSON . stringify ( data ) , createOptions )
116+ . then ( this . prepareResponse ) ;
117+ }
118+
119+ if ( data ?. tags ) {
120+ throw new APIError ( {
121+ status : 400 ,
122+ statusText : 'Tags property should not be used for creating one unsubscribe.' ,
123+ body : {
124+ message : 'Tags property can be used if you provides an array of unsubscribes as second argument of create method. Please use tag instead'
125+ }
126+ } as APIErrorOptions ) ;
127+ }
128+ if ( Array . isArray ( data . tag ) ) {
99129 throw new APIError ( {
100130 status : 400 ,
101- statusText : 'Unknown type value' ,
102- body : { message : 'Type may be only one of [bounces, complaints, unsubscribes, whitelists]' }
131+ statusText : 'Tag property can not be an array' ,
132+ body : {
133+ message : 'Please use array of unsubscribes as second argument of create method to be able to provide few tags'
134+ }
103135 } as APIErrorOptions ) ;
104136 }
137+ /* We need Form Data for unsubscribes if we want to support the "tag" property */
138+ return this . request
139+ . postWithFD ( urljoin ( 'v3' , domain , 'unsubscribes' ) , data )
140+ . then ( this . prepareResponse ) ;
141+ }
142+
143+ private getModel ( type : string ) {
144+ if ( type in this . models ) {
145+ return this . models [ type as keyof typeof this . models ] ;
146+ }
147+ throw new APIError ( {
148+ status : 400 ,
149+ statusText : 'Unknown type value' ,
150+ body : { message : 'Type may be only one of [bounces, complaints, unsubscribes, whitelists]' }
151+ } as APIErrorOptions ) ;
105152 }
106153
107154 private prepareResponse ( response : SuppressionCreationResponse ) : SuppressionCreationResult {
@@ -118,8 +165,7 @@ export default class SuppressionClient
118165 type : string ,
119166 query ?: SuppressionListQuery
120167 ) : Promise < SuppressionList > {
121- this . checkType ( type ) ;
122- const model = this . models . get ( type ) ;
168+ const model = this . getModel ( type ) ;
123169 return this . requestListWithPages ( urljoin ( 'v3' , domain , type ) , query , model ) ;
124170 }
125171
@@ -128,9 +174,7 @@ export default class SuppressionClient
128174 type : string ,
129175 address : string
130176 ) : Promise < IBounce | IComplaint | IUnsubscribe | IWhiteList > {
131- this . checkType ( type ) ;
132-
133- const model = this . models . get ( type ) ;
177+ const model = this . getModel ( type ) ;
134178 return this . request
135179 . get ( urljoin ( 'v3' , domain , type , encodeURIComponent ( address ) ) )
136180 . then ( ( response : SuppressionResponse ) => this . _parseItem < typeof model > ( response . body , model ) ) ;
@@ -141,14 +185,20 @@ export default class SuppressionClient
141185 type : string ,
142186 data : SuppressionCreationData | SuppressionCreationData [ ]
143187 ) : Promise < SuppressionCreationResult > {
144- this . checkType ( type ) ;
188+ this . getModel ( type ) ;
145189 // supports adding multiple suppressions by default
146190 let postData ;
191+ const isDataArray = Array . isArray ( data ) ;
192+
147193 if ( type === 'whitelists' ) {
148- return this . createWhiteList ( domain , data ) ;
194+ return this . createWhiteList ( domain , data , isDataArray ) ;
195+ }
196+
197+ if ( type === 'unsubscribes' ) {
198+ return this . createUnsubscribe ( domain , data ) ;
149199 }
150200
151- if ( ! Array . isArray ( data ) ) {
201+ if ( ! isDataArray ) {
152202 postData = [ data ] ;
153203 } else {
154204 postData = [ ...data ] ;
@@ -164,7 +214,7 @@ export default class SuppressionClient
164214 type : string ,
165215 address : string
166216 ) : Promise < SuppressionDestroyResult > {
167- this . checkType ( type ) ;
217+ this . getModel ( type ) ;
168218 return this . request
169219 . delete ( urljoin ( 'v3' , domain , type , encodeURIComponent ( address ) ) )
170220 . then ( ( response : SuppressionDestroyResponse ) => ( {
0 commit comments