@@ -22,23 +22,23 @@ import Errors from './errors';
22
22
import arrayBufferToBase64 from '../helpers/array-buffer-to-base64' ;
23
23
import FCMDetails from './fcm-details' ;
24
24
25
+ interface IIDDetails {
26
+ token : string ;
27
+ pushSet : string ;
28
+ }
29
+
25
30
export default class IIDModel {
26
31
private errorFactory_ : ErrorFactory < string > ;
27
32
28
33
constructor ( ) {
29
34
this . errorFactory_ = new ErrorFactory ( 'messaging' , 'Messaging' , Errors . map ) ;
30
35
}
31
36
32
- /**
33
- * Given a PushSubscription and messagingSenderId, get an FCM token.
34
- * @public
35
- * @param {string } senderId The 'messagingSenderId' to tie the token to.
36
- * @param {PushSubscription } subscription The PushSusbcription to "federate".
37
- * @param {Uint8Array } publicVapidKey The public VAPID key.
38
- * @return {Promise<!Object> } Returns the FCM token to be used in place
39
- * of the PushSubscription.
40
- */
41
- getToken ( senderId , subscription , publicVapidKey ) : Promise < Object > {
37
+ async getToken (
38
+ senderId : string ,
39
+ subscription : PushSubscription ,
40
+ publicVapidKey : Uint8Array
41
+ ) : Promise < IIDDetails > {
42
42
const p256dh = arrayBufferToBase64 ( subscription [ 'getKey' ] ( 'p256dh' ) ) ;
43
43
const auth = arrayBufferToBase64 ( subscription [ 'getKey' ] ( 'auth' ) ) ;
44
44
@@ -62,46 +62,43 @@ export default class IIDModel {
62
62
body : fcmSubscribeBody
63
63
} ;
64
64
65
- return fetch (
66
- FCMDetails . ENDPOINT + '/fcm/connect/subscribe' ,
67
- subscribeOptions
68
- )
69
- . then ( response => response . json ( ) )
70
- . catch ( ( ) => {
71
- throw this . errorFactory_ . create ( Errors . codes . TOKEN_SUBSCRIBE_FAILED ) ;
72
- } )
73
- . then ( response => {
74
- const fcmTokenResponse = response ;
75
- if ( fcmTokenResponse [ 'error' ] ) {
76
- const message = fcmTokenResponse [ 'error' ] [ 'message' ] ;
77
- throw this . errorFactory_ . create ( Errors . codes . TOKEN_SUBSCRIBE_FAILED , {
78
- message : message
79
- } ) ;
80
- }
81
-
82
- if ( ! fcmTokenResponse [ 'token' ] ) {
83
- throw this . errorFactory_ . create (
84
- Errors . codes . TOKEN_SUBSCRIBE_NO_TOKEN
85
- ) ;
86
- }
87
-
88
- if ( ! fcmTokenResponse [ 'pushSet' ] ) {
89
- throw this . errorFactory_ . create (
90
- Errors . codes . TOKEN_SUBSCRIBE_NO_PUSH_SET
91
- ) ;
92
- }
93
-
94
- return {
95
- token : fcmTokenResponse [ 'token' ] ,
96
- pushSet : fcmTokenResponse [ 'pushSet' ]
97
- } ;
98
- } ) ;
65
+ try {
66
+ const response = await fetch (
67
+ FCMDetails . ENDPOINT + '/fcm/connect/subscribe' ,
68
+ subscribeOptions
69
+ ) ;
70
+
71
+ const responseData = await response . json ( ) ;
72
+ if ( responseData [ 'error' ] ) {
73
+ const message = responseData [ 'error' ] [ 'message' ] ;
74
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_SUBSCRIBE_FAILED , {
75
+ message : message
76
+ } ) ;
77
+ }
78
+
79
+ if ( ! responseData [ 'token' ] ) {
80
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_SUBSCRIBE_NO_TOKEN ) ;
81
+ }
82
+
83
+ if ( ! responseData [ 'pushSet' ] ) {
84
+ throw this . errorFactory_ . create (
85
+ Errors . codes . TOKEN_SUBSCRIBE_NO_PUSH_SET
86
+ ) ;
87
+ }
88
+
89
+ return {
90
+ token : responseData [ 'token' ] ,
91
+ pushSet : responseData [ 'pushSet' ]
92
+ } ;
93
+ } catch ( err ) {
94
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_SUBSCRIBE_FAILED ) ;
95
+ }
99
96
}
100
97
101
98
/**
102
99
* Update the underlying token details for fcmToken.
103
100
*/
104
- updateToken (
101
+ async updateToken (
105
102
senderId : string ,
106
103
fcmToken : string ,
107
104
fcmPushSet : string ,
@@ -133,33 +130,33 @@ export default class IIDModel {
133
130
body : fcmUpdateBody
134
131
} ;
135
132
136
- let updateFetchRes ;
137
- return fetch ( FCMDetails . ENDPOINT + '/fcm/connect/subscribe' , updateOptions )
138
- . then ( fetchResponse => {
139
- updateFetchRes = fetchResponse ;
140
- return fetchResponse . json ( ) ;
141
- } )
142
- . catch ( ( ) => {
143
- throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_FAILED ) ;
144
- } )
145
- . then ( fcmTokenResponse => {
146
- if ( ! updateFetchRes . ok ) {
147
- const message = fcmTokenResponse [ 'error' ] [ 'message' ] ;
148
- throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_FAILED , {
149
- message : message
150
- } ) ;
151
- }
152
- if ( ! fcmTokenResponse [ 'token' ] ) {
153
- throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_NO_TOKEN ) ;
154
- }
155
- return fcmTokenResponse [ 'token' ] ;
156
- } ) ;
133
+ try {
134
+ const response = await fetch (
135
+ FCMDetails . ENDPOINT + '/fcm/connect/subscribe' ,
136
+ updateOptions
137
+ ) ;
138
+ const responseData = await response . json ( ) ;
139
+ if ( responseData [ 'error' ] ) {
140
+ const message = responseData [ 'error' ] [ 'message' ] ;
141
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_FAILED , {
142
+ message : message
143
+ } ) ;
144
+ }
145
+
146
+ if ( ! responseData [ 'token' ] ) {
147
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_NO_TOKEN ) ;
148
+ }
149
+
150
+ return responseData [ 'token' ] ;
151
+ } catch ( err ) {
152
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UPDATE_FAILED ) ;
153
+ }
157
154
}
158
155
159
156
/**
160
157
* Given a fcmToken, pushSet and messagingSenderId, delete an FCM token.
161
158
*/
162
- deleteToken (
159
+ async deleteToken (
163
160
senderId : string ,
164
161
fcmToken : string ,
165
162
fcmPushSet : string
@@ -178,30 +175,20 @@ export default class IIDModel {
178
175
body : fcmUnsubscribeBody
179
176
} ;
180
177
181
- return fetch (
182
- FCMDetails . ENDPOINT + '/fcm/connect/unsubscribe' ,
183
- unsubscribeOptions
184
- ) . then ( fetchResponse => {
185
- if ( ! fetchResponse . ok ) {
186
- return fetchResponse . json ( ) . then (
187
- fcmTokenResponse => {
188
- if ( fcmTokenResponse [ 'error' ] ) {
189
- const message = fcmTokenResponse [ 'error' ] [ 'message' ] ;
190
- throw this . errorFactory_ . create (
191
- Errors . codes . TOKEN_UNSUBSCRIBE_FAILED ,
192
- {
193
- message : message
194
- }
195
- ) ;
196
- }
197
- } ,
198
- err => {
199
- throw this . errorFactory_ . create (
200
- Errors . codes . TOKEN_UNSUBSCRIBE_FAILED
201
- ) ;
202
- }
203
- ) ;
178
+ try {
179
+ const response = await fetch (
180
+ FCMDetails . ENDPOINT + '/fcm/connect/unsubscribe' ,
181
+ unsubscribeOptions
182
+ ) ;
183
+ const responseData = await response . json ( ) ;
184
+ if ( responseData [ 'error' ] ) {
185
+ const message = responseData [ 'error' ] [ 'message' ] ;
186
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UNSUBSCRIBE_FAILED , {
187
+ message : message
188
+ } ) ;
204
189
}
205
- } ) ;
190
+ } catch ( err ) {
191
+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UNSUBSCRIBE_FAILED ) ;
192
+ }
206
193
}
207
194
}
0 commit comments