@@ -44,62 +44,62 @@ export default class SkohubProvider extends BaseProvider {
44
44
}
45
45
46
46
_setup ( ) {
47
- this . _jskos . schemes = this . schemes || [ ]
48
47
this . _index = { }
49
48
this . _conceptCache = { }
49
+ this . _schemeCache = { }
50
50
}
51
51
52
- async _loadScheme ( scheme , config ) {
53
- const { uri, topConcepts } = scheme
52
+ async _loadScheme ( { scheme, ...config } ) {
53
+ const uris = jskos . getAllUris ( scheme )
54
+ for ( let uri of uris ) {
55
+ if ( this . _schemeCache [ uri ] ) {
56
+ return this . _schemeCache [ uri ]
57
+ }
58
+ }
59
+ // Find main URI from this.schemes
60
+ const { uri } = this . schemes . find ( s => jskos . compare ( s , scheme ) ) || { }
54
61
55
- if ( ! uri || topConcepts ) {
56
- return scheme
62
+ if ( ! uri ) {
63
+ throw new errors . InvalidRequestError ( { message : `Tried to load unsupported scheme ( ${ scheme && scheme . uri } )` } )
57
64
}
58
65
59
66
let postfix = ".json"
60
67
if ( uri . endsWith ( "/" ) ) {
61
68
postfix = "index.json"
62
69
}
70
+ // Errors for this request will trickle upwards of the call chain
63
71
const data = await this . axios ( { ...config , url : `${ uri } ${ postfix } ` , _skipAdditionalParameters : true } )
64
72
65
- // TODO: if not found
66
-
67
73
if ( data . id !== uri ) {
68
74
throw new errors . InvalidRequestError ( { message : "Skohub URL did not return expected concept scheme" } )
69
75
}
70
76
71
- const { title, preferredNamespaceUri, hasTopConcept, description } = data //, issued, created, modified, creator, publisher } = data
77
+ const { title, preferredNamespaceUri, hasTopConcept, description } = data
72
78
73
79
scheme . prefLabel = title
74
80
Object . keys ( scheme . prefLabel || { } ) . forEach ( key => {
75
81
scheme . prefLabel [ key ] = decodeUnicode ( scheme . prefLabel [ key ] )
76
82
} )
77
83
scheme . namespace = preferredNamespaceUri
78
84
scheme . topConcepts = ( hasTopConcept || [ ] ) . map ( c => this . _toJskosConcept ( c ) )
79
-
80
- // const hasNarrower = scheme.topConcepts.find(c => c.narrower && c.narrower.length)
81
-
82
85
scheme . concepts = [ null ]
83
- // scheme.concepts = [...scheme.topConcepts]
84
- // if (hasNarrower) {
85
- // scheme.concepts.push(null)
86
- // }
87
-
88
- // TODO: map remaining fields
89
-
90
86
if ( description ) {
91
87
scheme . definition = description
92
88
// scopeNote values in JSKOS are arrays
93
89
Object . keys ( scheme . definition ) . forEach ( key => {
94
90
scheme . definition [ key ] = [ decodeUnicode ( scheme . definition [ key ] ) ]
95
91
} )
96
92
}
97
-
98
- // remove fields without value
93
+ // Remove fields without value
99
94
for ( let key of Object . keys ( scheme ) . filter ( key => ! scheme [ key ] ) ) {
100
95
delete scheme [ key ]
101
96
}
102
97
98
+ // Add to cache
99
+ for ( let uri of uris ) {
100
+ this . _schemeCache [ uri ] = scheme
101
+ }
102
+
103
103
return scheme
104
104
}
105
105
@@ -150,27 +150,16 @@ export default class SkohubProvider extends BaseProvider {
150
150
}
151
151
152
152
async getSchemes ( { ...config } ) {
153
- const { schemes } = this . _jskos
154
-
155
- for ( let i = 0 ; i < schemes . length ; i ++ ) {
156
- schemes [ i ] = await this . _loadScheme ( schemes [ i ] , config )
157
- }
158
-
159
- return schemes
153
+ return Promise . all ( this . schemes . map ( scheme => this . _loadScheme ( { ...config , scheme } ) ) )
160
154
}
161
155
162
156
async getTop ( { scheme, ...config } ) {
163
157
if ( ! scheme || ! scheme . uri ) {
164
158
throw new errors . InvalidOrMissingParameterError ( { parameter : "scheme" , message : "Missing scheme URI" } )
165
159
}
166
160
167
- scheme = this . _jskos . schemes . find ( s => s . uri === scheme . uri )
168
- if ( scheme ) {
169
- scheme = await this . _loadScheme ( scheme , config )
170
- return scheme . topConcepts
171
- } else {
172
- return [ ]
173
- }
161
+ scheme = await this . _loadScheme ( { scheme, ...config } )
162
+ return scheme . topConcepts || [ ]
174
163
}
175
164
176
165
async getConcepts ( { concepts, ...config } ) {
0 commit comments