1- import { ArcGISIdentityManager } from '@esri/arcgis-rest-request'
2- import { FeatureServiceConfig } from './ArcGISConfig'
3- import { PluginStateRepository } from '@ngageoint/mage.service/lib/plugins.api'
1+ import { ArcGISIdentityManager } from '@esri/arcgis-rest-request' ;
2+ import { FeatureServiceConfig } from './ArcGISConfig' ;
3+ import { PluginStateRepository } from '@ngageoint/mage.service/lib/plugins.api' ;
4+ import { ArcGISPluginConfig } from './ArcGISPluginConfig' ;
45
6+ /**
7+ * Interface for managing ArcGIS identity and authentication.
8+ */
59export interface ArcGISIdentityService {
10+ /**
11+ * Sign in to ArcGIS with the feature service credentials.
12+ * @param {FeatureServiceConfig } featureService The feature service configuration.
13+ * @returns {Promise<ArcGISIdentityManager> } The ArcGIS identity manager.
14+ */
615 signin ( featureService : FeatureServiceConfig ) : Promise < ArcGISIdentityManager >
16+
17+ /**
18+ * Update the identity managers with latest tokens.
19+ * @returns {Promise<void> }
20+ */
721 updateIndentityManagers ( ) : Promise < void >
822}
923
24+ /**
25+ * Creates a new ArcGIS identity service.
26+ * @param {PluginStateRepository<ArcGISPluginConfig> } stateRepo The plugin state repository.
27+ * @returns {ArcGISIdentityService } The ArcGIS identity service.
28+ */
1029export function createArcGISIdentityService (
11- stateRepo : PluginStateRepository < any >
30+ stateRepo : PluginStateRepository < ArcGISPluginConfig >
1231) : ArcGISIdentityService {
13- const identityManagerCache : Map < string , Promise < ArcGISIdentityManager > > = new Map ( )
32+ const identityManagerCache : Map < string , Promise < ArcGISIdentityManager > > = new Map ( ) ;
1433
1534 return {
1635 async signin ( featureService : FeatureServiceConfig ) : Promise < ArcGISIdentityManager > {
17- let cached = await identityManagerCache . get ( featureService . url )
36+ const cached = await identityManagerCache . get ( featureService . url ) ;
1837 if ( ! cached ) {
19- const identityManager = ArcGISIdentityManager . deserialize ( featureService . identityManager )
20- const promise = identityManager . getUser ( ) . then ( ( ) => identityManager )
21- identityManagerCache . set ( featureService . url , promise )
22- return promise
38+ const identityManager = ArcGISIdentityManager . deserialize ( featureService . identityManager ) ;
39+ const promise = identityManager . getUser ( ) . then ( ( ) => identityManager ) ;
40+ identityManagerCache . set ( featureService . url , promise ) ;
41+ return promise ;
2342 } else {
24- return cached
43+ return cached ;
2544 }
2645 } ,
46+
2747 async updateIndentityManagers ( ) {
28- const config = await stateRepo . get ( )
29- for ( let [ url , persistedIdentityManagerPromise ] of identityManagerCache ) {
30- const persistedIdentityManager = await persistedIdentityManagerPromise
31- const featureService : FeatureServiceConfig | undefined = config . featureServices . find ( ( service : FeatureServiceConfig ) => service . url === url )
32- if ( featureService ) {
33- const identityManager = ArcGISIdentityManager . deserialize ( featureService . identityManager )
48+ const config = await stateRepo . get ( ) as ArcGISPluginConfig | null ;
49+ for ( const [ url , persistedIdentityManagerPromise ] of identityManagerCache ) {
50+ const persistedIdentityManager = await persistedIdentityManagerPromise ;
51+ const featureService : FeatureServiceConfig | undefined = config ? .featureServices . find ( ( service : FeatureServiceConfig ) => service . url === url ) ;
52+ if ( featureService && config ) {
53+ const identityManager = ArcGISIdentityManager . deserialize ( featureService . identityManager ) ;
3454 if ( identityManager . token !== persistedIdentityManager . token || identityManager . refreshToken !== persistedIdentityManager . refreshToken ) {
35- featureService . identityManager = persistedIdentityManager . serialize ( )
36- await stateRepo . put ( config )
55+ featureService . identityManager = persistedIdentityManager . serialize ( ) ;
56+ await stateRepo . patch ( config as never ) ;
3757 }
3858 }
3959 }
4060 }
41- }
61+ } ;
4262}
4363
64+ /**
65+ * Gets the portal URL for an ArcGIS feature service.
66+ * @param {FeatureServiceConfig | string } featureService The feature service config or URL.
67+ * @returns {string } The portal URL.
68+ */
4469export function getPortalUrl ( featureService : FeatureServiceConfig | string ) : string {
45- const url = getFeatureServiceUrl ( featureService )
46- return `https://${ url . hostname } /arcgis/sharing/rest`
70+ const url = getFeatureServiceUrl ( featureService ) ;
71+ return `https://${ url . hostname } /arcgis/sharing/rest` ;
4772}
4873
74+ /**
75+ * Gets the server URL for an ArcGIS feature service.
76+ * @param {FeatureServiceConfig | string } featureService The feature service config or URL.
77+ * @returns {string } The server URL.
78+ */
4979export function getServerUrl ( featureService : FeatureServiceConfig | string ) : string {
50- const url = getFeatureServiceUrl ( featureService )
51- return `https://${ url . hostname } /arcgis`
80+ const url = getFeatureServiceUrl ( featureService ) ;
81+ return `https://${ url . hostname } /arcgis` ;
5282}
5383
84+ /**
85+ * Gets the URL object for an ArcGIS feature service.
86+ * @param {FeatureServiceConfig | string } featureService The feature service config or URL string.
87+ * @returns {URL } The feature service URL.
88+ */
5489export function getFeatureServiceUrl ( featureService : FeatureServiceConfig | string ) : URL {
55- const url = typeof featureService === 'string' ? featureService : featureService . url
56- return new URL ( url )
90+ const url = typeof featureService === 'string' ? featureService : featureService . url ;
91+ return new URL ( url ) ;
5792}
0 commit comments