11import { allSettled , fetchWithTimeout } from './utils' ;
2+ import { version } from '../package.json'
23
34import type * as Types from './types' ;
45export { Types } ;
56
67export class Optimade {
7- private providersUrl : string = '' ;
8- private corsProxyUrl : string = '' ;
8+ private providersUrl = '' ;
9+ private corsProxyUrl = '' ;
910 providers : Types . ProvidersMap | null = null ;
1011 apis : Types . ApisMap = { } ;
1112 private reqStack : string [ ] = [ ] ;
@@ -16,7 +17,7 @@ export class Optimade {
1617 }
1718
1819 async addProvider ( provider : Types . Provider ) {
19- if ( ! this . apis [ provider . id ] ) this . apis [ provider . id ] = [ ] ;
20+ if ( ! this . apis [ provider . id ] ) { this . apis [ provider . id ] = [ ] ; }
2021
2122 try {
2223 const ver = provider . attributes
@@ -28,7 +29,7 @@ export class Optimade {
2829 this . apis [ provider . id ] . push ( api ) ;
2930 }
3031
31- } catch ( ignore ) { }
32+ } catch ( ignore ) { console . log ( ignore ) }
3233
3334 if ( ! provider . attributes . query_limits ) {
3435 const formula = `chemical_formula_anonymous="A2B"` ;
@@ -37,7 +38,7 @@ export class Optimade {
3738 try {
3839 const res = await fetch ( url ) . then ( res => res . json ( ) ) ;
3940 const version = res . meta && res . meta . api_version || this . apis [ provider . id ] [ 0 ] . attributes . api_version ;
40- const detail = ( errors : { detail : any ; } [ ] ) => {
41+ const detail = ( errors ) => {
4142 return errors
4243 ? errors . length
4344 ? errors [ 0 ] . detail
@@ -71,18 +72,18 @@ export class Optimade {
7172 Optimade . getJSON ( this . providersUrl ) . catch ( ( ) => null )
7273 ) ;
7374
74- if ( ! providers ) return null ;
75- if ( ! this . providers ) this . providers = { } ;
75+ if ( ! providers ) { return null ; }
76+ if ( ! this . providers ) { this . providers = { } ; }
7677
7778 const data = providers . data . filter ( Optimade . isProviderValid ) ;
7879 const ver = providers . meta && providers . meta . api_version ?
7980 providers . meta . api_version . charAt ( 0 ) : '' ;
8081
8182 for ( const provider of data ) {
82- if ( ! this . apis [ provider . id ] ) this . apis [ provider . id ] = [ ] ;
83+ if ( ! this . apis [ provider . id ] ) { this . apis [ provider . id ] = [ ] ; }
8384 try {
8485 const api = await this . getApis ( provider , ver ? `v${ ver } ` : '' ) ;
85- if ( ! api ) continue ;
86+ if ( ! api ) { continue ; }
8687
8788 if ( api . attributes . available_endpoints . includes ( 'structures' ) ) {
8889 this . apis [ provider . id ] . push ( api ) ;
@@ -92,36 +93,36 @@ export class Optimade {
9293 } else {
9394 await this . getProviders ( api ) ;
9495 }
95- } catch ( ignore ) { }
96+ } catch ( ignore ) { console . log ( ignore ) }
9697 }
9798
9899 return this . providers ;
99100 }
100101
101- async getApis ( provider : Types . Provider | string , version : string = '' ) : Promise < Types . Api | null > {
102+ async getApis ( provider : Types . Provider | string , version = '' ) : Promise < Types . Api | null > {
102103 if ( typeof provider === 'string' ) {
103104 provider = this . providers [ provider ] ;
104105 }
105106
106- if ( ! provider ) throw new Error ( 'No provider found' ) ;
107+ if ( ! provider ) { throw new Error ( 'No provider found' ) ; }
107108
108109 const url : string = this . wrapUrl ( `${ provider . attributes . base_url } /${ version } ` , '/info' ) ;
109110
110- if ( this . isDuplicatedReq ( url ) ) return null ;
111+ if ( this . isDuplicatedReq ( url ) ) { return null ; }
111112
112113 const apis : Types . InfoResponse = await Optimade . getJSON ( url ) ;
113114 return Optimade . apiVersion ( apis ) ;
114115 }
115116
116- async getStructures ( providerId : string , filter : string = '' , page : number = 1 , limit : number ) : Promise < Types . StructuresResponse [ ] | Types . ResponseError > {
117+ async getStructures ( providerId : string , filter = '' , page = 1 , limit : number ) : Promise < Types . StructuresResponse [ ] | Types . ResponseError > {
117118
118- if ( ! this . apis [ providerId ] ) return null ;
119+ if ( ! this . apis [ providerId ] ) { return null ; }
119120
120121 const apis = this . apis [ providerId ] . filter ( api => api . attributes . available_endpoints . includes ( 'structures' ) ) ;
121122 const provider = this . providers [ providerId ] ;
122123
123124 const structures : Types . StructuresResponse [ ] = await allSettled ( apis . map ( async ( api : Types . Api ) => {
124- if ( page <= 0 ) page = 1 ;
125+ if ( page <= 0 ) { page = 1 ; }
125126 const pageLimit = limit ? `&page_limit=${ limit } ` : '' ;
126127 const pageNumber = page ? `&page_number=${ page - 1 } ` : '' ;
127128 const pageOffset = limit && page ? `&page_offset=${ limit * ( page - 1 ) } ` : '' ;
@@ -148,7 +149,7 @@ export class Optimade {
148149 } , [ ] ) ;
149150 }
150151
151- getStructuresAll ( providerIds : string [ ] , filter : string = '' , page : number = 1 , limit : number , batch : boolean = true ) : Promise < Promise < Types . StructuresResult > [ ] > | Promise < Types . StructuresResult > [ ] {
152+ getStructuresAll ( providerIds : string [ ] , filter = '' , page = 1 , limit : number , batch = true ) : Promise < Promise < Types . StructuresResult > [ ] > | Promise < Types . StructuresResult > [ ] {
152153
153154 const results = providerIds . reduce ( ( structures : Promise < any > [ ] , providerId : string ) => {
154155 const provider = this . providers [ providerId ] ;
@@ -165,7 +166,7 @@ export class Optimade {
165166 }
166167
167168 private async followLinks ( api : Types . Api ) : Promise < Types . LinksResponse | null > {
168- if ( ! api . attributes . available_endpoints . includes ( 'links' ) ) return null ;
169+ if ( ! api . attributes . available_endpoints . includes ( 'links' ) ) { return null ; }
169170
170171 const url = this . wrapUrl ( Optimade . apiVersionUrl ( api ) , '/links' ) ;
171172
@@ -181,7 +182,7 @@ export class Optimade {
181182 return this . reqStack . includes ( url ) || ! this . reqStack . unshift ( url ) ;
182183 }
183184
184- static async getJSON ( uri : string , params : { } = null , headers : { } = { } ) {
185+ static async getJSON ( uri : string , params = null , headers = { } ) {
185186
186187 const url = new URL ( uri ) ;
187188 const timeout = 10000 ;
@@ -190,6 +191,8 @@ export class Optimade {
190191 Object . entries ( params ) . forEach ( ( param : [ string , any ] ) => url . searchParams . append ( ...param ) ) ;
191192 }
192193
194+ Object . assign ( headers , { 'User-Agent' : `tilde-lab-optimade-client/${ version } ` } )
195+
193196 const res = await fetchWithTimeout ( url . toString ( ) , { headers } , timeout ) ;
194197
195198 if ( ! res . ok ) {
0 commit comments