@@ -22,23 +22,23 @@ import Errors from './errors';
2222import arrayBufferToBase64 from '../helpers/array-buffer-to-base64' ;
2323import FCMDetails from './fcm-details' ;
2424
25+ interface IIDDetails {
26+ token : string ;
27+ pushSet : string ;
28+ }
29+
2530export default class IIDModel {
2631 private errorFactory_ : ErrorFactory < string > ;
2732
2833 constructor ( ) {
2934 this . errorFactory_ = new ErrorFactory ( 'messaging' , 'Messaging' , Errors . map ) ;
3035 }
3136
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 > {
4242 const p256dh = arrayBufferToBase64 ( subscription [ 'getKey' ] ( 'p256dh' ) ) ;
4343 const auth = arrayBufferToBase64 ( subscription [ 'getKey' ] ( 'auth' ) ) ;
4444
@@ -62,46 +62,43 @@ export default class IIDModel {
6262 body : fcmSubscribeBody
6363 } ;
6464
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+ }
9996 }
10097
10198 /**
10299 * Update the underlying token details for fcmToken.
103100 */
104- updateToken (
101+ async updateToken (
105102 senderId : string ,
106103 fcmToken : string ,
107104 fcmPushSet : string ,
@@ -133,33 +130,33 @@ export default class IIDModel {
133130 body : fcmUpdateBody
134131 } ;
135132
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+ }
157154 }
158155
159156 /**
160157 * Given a fcmToken, pushSet and messagingSenderId, delete an FCM token.
161158 */
162- deleteToken (
159+ async deleteToken (
163160 senderId : string ,
164161 fcmToken : string ,
165162 fcmPushSet : string
@@ -178,30 +175,20 @@ export default class IIDModel {
178175 body : fcmUnsubscribeBody
179176 } ;
180177
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+ } ) ;
204189 }
205- } ) ;
190+ } catch ( err ) {
191+ throw this . errorFactory_ . create ( Errors . codes . TOKEN_UNSUBSCRIBE_FAILED ) ;
192+ }
206193 }
207194}
0 commit comments