11import { Okta } from './Okta.js'
22// import { Http } from '@capacitor-community/http'
33import { Http } from './CancelableHttp.js'
4+ import urlJoin from 'url-join'
45import { SecureStoragePlugin } from 'capacitor-secure-storage-plugin'
56import { DateTime } from 'luxon'
7+ import { isFunction } from 'lodash'
68import { blobToBase64 } from '@/helpers/utils.js'
79
8- const USER_AGENT = 'APMConnect/1 CFNetwork/1329 Darwin/21.3 .0'
10+ const USER_AGENT = 'APMConnect/1 CFNetwork/1331.0.7 Darwin/21.4 .0'
911const STORE_PREFIX = 'tosync_cctoken'
1012const READ_TIMEOUT = 15 * 1000
1113const CONNECT_TIMEOUT = 15 * 1000
@@ -15,7 +17,7 @@ const defaultOptions = {
1517 readTimeout : READ_TIMEOUT
1618}
1719
18- const headers = {
20+ const DEFAULT_HEADERS = {
1921 'User-Agent' : USER_AGENT ,
2022 'Content-Type' : 'application/json'
2123}
@@ -25,6 +27,7 @@ export class CrewConnect {
2527 this . _serverUrl = serverUrl
2628 this . _userId = null
2729 this . _token = null
30+ this . _pendingRequests = new Map ( )
2831 this . okta = new Okta ( )
2932 }
3033
@@ -35,7 +38,6 @@ export class CrewConnect {
3538 }
3639
3740 async signIn ( userId , { silent = false } ) {
38- console . log ( silent )
3941 if ( userId ) {
4042 const result = await this . tryTokenLogin ( userId )
4143 if ( result ?. success ) {
@@ -63,10 +65,8 @@ export class CrewConnect {
6365 this . _checkServerURL ( )
6466 if ( ! userId || ! accessToken ) throw new Error ( 'Invalid userId or accessToken' )
6567
66- const { status, data } = await Http . post ( {
67- ...defaultOptions ,
68+ const { status, data } = await this . _httpPost ( {
6869 url : this . loginUrl ,
69- headers,
7070 data : {
7171 userId,
7272 accessToken
@@ -129,10 +129,8 @@ export class CrewConnect {
129129
130130 async checkToken ( token ) {
131131 this . _checkServerURL ( )
132- let { data } = await Http . get ( {
133- ...defaultOptions ,
134- url : `${ this . apiUrl } /token/introspect` ,
135- headers,
132+ let { data } = await this . _httpGet ( {
133+ url : urlJoin ( this . apiUrl , '/token/introspect' ) ,
136134 params : {
137135 token,
138136 '_' : Date . now ( ) . toString ( )
@@ -148,19 +146,16 @@ export class CrewConnect {
148146
149147 async getApiConfig ( ) {
150148 this . _checkServerURL ( )
151- const { data } = await Http . get ( {
152- ...defaultOptions ,
153- url : this . apiUrl + '/config' ,
154- headers
149+ const { data } = await this . _httpGet ( {
150+ url : urlJoin ( this . apiUrl , '/config' )
155151 } )
156152 console . log ( 'config' , data )
157153 return data
158154 }
159155
160156 async getCrewsIndex ( ) {
161157 this . _checkServerURL ( )
162- const { data } = await Http . get ( {
163- ...defaultOptions ,
158+ const { data } = await this . _httpGet ( {
164159 url : this . crewsUrl ,
165160 params : {
166161 '_' : Date . now ( ) . toString ( )
@@ -173,9 +168,8 @@ export class CrewConnect {
173168
174169 async getCrewPhoto ( path , { format = 'dataUrl' } = { } ) {
175170 this . _checkServerURL ( )
176- const { data } = await Http . get ( {
177- ...defaultOptions ,
178- url : `${ this . serverUrl } /${ path } ` ,
171+ const { data } = await this . _httpGet ( {
172+ url : urlJoin ( this . serverUrl , path ) ,
179173 responseType : 'blob' ,
180174 headers : {
181175 ...this . _headersWithToken ( ) ,
@@ -193,9 +187,8 @@ export class CrewConnect {
193187 async getRosterChanges ( ) {
194188 this . _checkServerURL ( )
195189 if ( ! this . userId ) throw new Error ( 'You must be logged in to access this ressource.' )
196- const { data } = await Http . get ( {
197- ...defaultOptions ,
198- url : `${ this . crewsUrl } /${ this . userId } /roster-changes` ,
190+ const { data } = await this . _httpGet ( {
191+ url : urlJoin ( this . crewsUrl , this . userId , '/roster-changes' ) ,
199192 params : {
200193 '_' : Date . now ( ) . toString ( )
201194 } ,
@@ -216,9 +209,8 @@ export class CrewConnect {
216209 this . _checkServerURL ( )
217210 console . log ( 'getRosterCalendars' , this . userId )
218211 if ( ! this . userId ) throw new Error ( 'You must be logged in to access this ressource.' )
219- const { data } = await Http . get ( {
220- ...defaultOptions ,
221- url : `${ this . crewsUrl } /${ this . userId } /roster-calendars` ,
212+ const { data } = await this . _httpGet ( {
213+ url : urlJoin ( this . crewsUrl , this . userId , '/roster-calendars' ) ,
222214 params : {
223215 dateFrom,
224216 dateTo,
@@ -233,9 +225,8 @@ export class CrewConnect {
233225 async signRoster ( rosterState ) {
234226 this . _checkServerURL ( )
235227 console . log ( 'signRoster' , this . userId , rosterState )
236- const { status, data } = await Http . post ( {
237- ...defaultOptions ,
238- url : `${ this . crewsUrl } /${ this . userId } /sign-roster` ,
228+ const { status, data } = await this . _httpPost ( {
229+ url : urlJoin ( this . crewsUrl , this . userId , '/sign-roster' ) ,
239230 headers : this . _headersWithToken ( ) ,
240231 data : rosterState ,
241232 responseType : 'text'
@@ -247,9 +238,8 @@ export class CrewConnect {
247238 async signRosterChanges ( changes ) {
248239 this . _checkServerURL ( )
249240 console . log ( 'signRosterChanges' , this . userId , changes )
250- const { status } = await Http . post ( {
251- ...defaultOptions ,
252- url : `${ this . crewsUrl } /${ this . userId } /sign-roster-changes` ,
241+ const { status } = await this . _httpPost ( {
242+ url : urlJoin ( this . crewsUrl , this . userId , '/sign-roster-changes' ) ,
253243 headers : this . _headersWithToken ( ) ,
254244 data : changes ,
255245 responseType : 'text'
@@ -258,10 +248,18 @@ export class CrewConnect {
258248 return status === 200
259249 }
260250
251+ cancel ( ) {
252+ for ( const request of this . _pendingRequests . values ( ) ) {
253+ if ( isFunction ( request ?. cancel ) ) {
254+ request . cancel ( )
255+ }
256+ }
257+ }
258+
261259 _headersWithToken ( ) {
262260 if ( ! this . _token ) throw new Error ( 'No token defined' )
263261 return {
264- ...headers ,
262+ ...DEFAULT_HEADERS ,
265263 Authorization : `Bearer ${ this . _token } `
266264 }
267265 }
@@ -270,6 +268,42 @@ export class CrewConnect {
270268 if ( ! this . serverUrl ) throw new Error ( 'No server URL provided' )
271269 }
272270
271+ _httpGet ( options ) {
272+ return this . _request ( {
273+ method : 'get' ,
274+ ...options
275+ } )
276+ }
277+
278+ _httpPost ( options ) {
279+ return this . _request ( {
280+ method : 'post' ,
281+ ...options
282+ } )
283+ }
284+
285+ async _request ( options ) {
286+ const requestOptions = {
287+ headers : DEFAULT_HEADERS ,
288+ ...defaultOptions ,
289+ ...options
290+ }
291+ console . log ( '[CrewConnect._request]' , requestOptions . method , requestOptions . url , requestOptions )
292+
293+ let result
294+ const requestKey = Symbol ( options . method )
295+ try {
296+ const request = Http . request ( requestOptions )
297+ this . _pendingRequests . set ( requestKey , request )
298+ result = await request
299+ } catch ( error ) {
300+ throw error
301+ } finally {
302+ this . _pendingRequests . delete ( requestKey )
303+ }
304+ return result
305+ }
306+
273307 get userId ( ) {
274308 return this . _userId
275309 }
@@ -297,14 +331,14 @@ export class CrewConnect {
297331 }
298332
299333 get loginUrl ( ) {
300- return ` ${ this . serverUrl } / login`
334+ return urlJoin ( this . serverUrl , ' login' )
301335 }
302336
303337 get apiUrl ( ) {
304- return ` ${ this . serverUrl } / api`
338+ return urlJoin ( this . serverUrl , ' api' )
305339 }
306340
307341 get crewsUrl ( ) {
308- return ` ${ this . apiUrl } / crews`
342+ return urlJoin ( this . apiUrl , ' crews' )
309343 }
310344}
0 commit comments