@iqb/metadata-resolver 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -64
- package/dist/index.cjs +35 -74
- package/dist/index.d.cts +11 -52
- package/dist/index.d.mts +11 -52
- package/dist/index.mjs +35 -74
- package/package.json +10 -31
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @iqb/metadata-resolver
|
|
2
2
|
|
|
3
|
-
TypeScript library for loading metadata profiles and vocabularies.
|
|
3
|
+
Framework-agnostic TypeScript library for loading metadata profiles and vocabularies.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
```bash
|
|
@@ -28,68 +28,6 @@ See [GETTING_STARTED.md](./GETTING_STARTED.md) for detailed usage guide.
|
|
|
28
28
|
|
|
29
29
|
**0.1.0** - Testing phase
|
|
30
30
|
|
|
31
|
-
## Development
|
|
32
|
-
|
|
33
|
-
### Setup
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
npm install
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Quality Checks
|
|
40
|
-
|
|
41
|
-
We use **ESLint** and **Prettier** to ensure code quality.
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
# Run linting
|
|
45
|
-
npm run lint
|
|
46
|
-
|
|
47
|
-
# Run type check
|
|
48
|
-
npm run typecheck
|
|
49
|
-
|
|
50
|
-
# Run tests
|
|
51
|
-
npm run test
|
|
52
|
-
|
|
53
|
-
# Format code
|
|
54
|
-
npm run format
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Build
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
npm run build
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Release
|
|
64
|
-
|
|
65
|
-
To publish a new version to NPM:
|
|
66
|
-
|
|
67
|
-
1. **Update Version**:
|
|
68
|
-
```bash
|
|
69
|
-
npm version patch # or minor, major
|
|
70
|
-
```
|
|
71
|
-
This updates `package.json` and creates a git tag.
|
|
72
|
-
|
|
73
|
-
2. **Push Changes**:
|
|
74
|
-
```bash
|
|
75
|
-
git push && git push --tags
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
3. **Publish**:
|
|
79
|
-
```bash
|
|
80
|
-
npm run release
|
|
81
|
-
```
|
|
82
|
-
The `prepublishOnly` script will automatically run tests, linting, and build before publishing ensures only valid code is released.
|
|
83
|
-
|
|
84
|
-
### Configuration
|
|
85
|
-
|
|
86
|
-
- **Registry**: Configured in `.npmrc` (default is npmjs.org).
|
|
87
|
-
- **Access**: defaults to `public` in `package.json`.
|
|
88
|
-
- **Tags**: pass flags to the release script:
|
|
89
|
-
```bash
|
|
90
|
-
npm run release -- --tag beta
|
|
91
|
-
```
|
|
92
|
-
|
|
93
31
|
## License
|
|
94
32
|
|
|
95
|
-
MIT © IQB Berlin
|
|
33
|
+
MIT © IQB Berlin
|
package/dist/index.cjs
CHANGED
|
@@ -13,29 +13,21 @@ var MetadataResolver = class {
|
|
|
13
13
|
this.preferredLanguage = options?.preferredLanguage || "de";
|
|
14
14
|
this.requestTimeout = options?.requestTimeout || 1e4;
|
|
15
15
|
}
|
|
16
|
-
setCorsProxy(proxy) {
|
|
17
|
-
this.corsProxy = proxy;
|
|
18
|
-
}
|
|
19
16
|
/**
|
|
20
17
|
* Load a profile from a URL
|
|
21
18
|
*/
|
|
22
19
|
async loadProfile(url) {
|
|
23
20
|
const cacheKey = `profile:${url}`;
|
|
24
21
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
25
|
-
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
26
|
-
let response;
|
|
27
|
-
try {
|
|
28
|
-
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
29
|
-
} catch (error) {
|
|
30
|
-
throw new Error(`Error loading profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
31
|
-
}
|
|
32
|
-
if (!response.ok) throw new Error(`Error loading profile from ${url}: ${response.status} ${response.statusText}`);
|
|
33
22
|
try {
|
|
23
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
24
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
25
|
+
if (!response.ok) throw new Error(`Failed to load profile: ${response.status} ${response.statusText}`);
|
|
34
26
|
const profile = await response.json();
|
|
35
27
|
if (this.useCache) this.cache.set(cacheKey, profile);
|
|
36
28
|
return profile;
|
|
37
29
|
} catch (error) {
|
|
38
|
-
throw new Error(`Error
|
|
30
|
+
throw new Error(`Error loading profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
39
31
|
}
|
|
40
32
|
}
|
|
41
33
|
/**
|
|
@@ -44,20 +36,15 @@ var MetadataResolver = class {
|
|
|
44
36
|
async loadMetadata(url) {
|
|
45
37
|
const cacheKey = `metadata:${url}`;
|
|
46
38
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
47
|
-
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
48
|
-
let response;
|
|
49
|
-
try {
|
|
50
|
-
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
51
|
-
} catch (error) {
|
|
52
|
-
throw new Error(`Error loading metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
53
|
-
}
|
|
54
|
-
if (!response.ok) throw new Error(`Error loading metadata from ${url}: ${response.status} ${response.statusText}`);
|
|
55
39
|
try {
|
|
40
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
41
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
42
|
+
if (!response.ok) throw new Error(`Failed to load metadata: ${response.status} ${response.statusText}`);
|
|
56
43
|
const metadata = await response.json();
|
|
57
44
|
if (this.useCache) this.cache.set(cacheKey, metadata);
|
|
58
45
|
return metadata;
|
|
59
46
|
} catch (error) {
|
|
60
|
-
throw new Error(`Error
|
|
47
|
+
throw new Error(`Error loading metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
61
48
|
}
|
|
62
49
|
}
|
|
63
50
|
/**
|
|
@@ -67,7 +54,7 @@ var MetadataResolver = class {
|
|
|
67
54
|
const urls = /* @__PURE__ */ new Set();
|
|
68
55
|
profile.groups?.forEach((group) => {
|
|
69
56
|
group.entries?.forEach((entry) => {
|
|
70
|
-
if (entry.type === "
|
|
57
|
+
if (entry.type.toUpperCase() === "VOCABULARY" && entry.parameters) {
|
|
71
58
|
const params = entry.parameters;
|
|
72
59
|
if (params.url) urls.add(params.url);
|
|
73
60
|
}
|
|
@@ -82,56 +69,15 @@ var MetadataResolver = class {
|
|
|
82
69
|
async loadVocabulary(url) {
|
|
83
70
|
if (this.useCache && this.vocabulariesStore.has(url)) return this.vocabulariesStore.get(url);
|
|
84
71
|
try {
|
|
72
|
+
console.log(`Loading vocabulary: ${url}`);
|
|
85
73
|
const jsonLdUrl = await this.resolveJsonLdUrl(url);
|
|
86
|
-
if (jsonLdUrl !== url) {}
|
|
74
|
+
if (jsonLdUrl !== url) console.log(` Resolved to: ${jsonLdUrl}`);
|
|
87
75
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonLdUrl)}` : jsonLdUrl;
|
|
88
|
-
if (this.corsProxy)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
94
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
95
|
-
return {
|
|
96
|
-
url,
|
|
97
|
-
data: { hasTopConcept: [] },
|
|
98
|
-
dictionary: {},
|
|
99
|
-
error: errorMsg
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
if (!response.ok) {
|
|
103
|
-
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
|
104
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
105
|
-
return {
|
|
106
|
-
url,
|
|
107
|
-
data: { hasTopConcept: [] },
|
|
108
|
-
dictionary: {},
|
|
109
|
-
error: errorMsg
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
let data;
|
|
113
|
-
try {
|
|
114
|
-
data = await response.json();
|
|
115
|
-
} catch (error) {
|
|
116
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
117
|
-
console.error(` Failed to parse vocabulary JSON: ${errorMsg}`);
|
|
118
|
-
return {
|
|
119
|
-
url,
|
|
120
|
-
data: { hasTopConcept: [] },
|
|
121
|
-
dictionary: {},
|
|
122
|
-
error: errorMsg
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
if (!data.hasTopConcept || !Array.isArray(data.hasTopConcept)) {
|
|
126
|
-
const errorMsg = "Invalid structure: missing hasTopConcept";
|
|
127
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
128
|
-
return {
|
|
129
|
-
url,
|
|
130
|
-
data: { hasTopConcept: [] },
|
|
131
|
-
dictionary: {},
|
|
132
|
-
error: errorMsg
|
|
133
|
-
};
|
|
134
|
-
}
|
|
76
|
+
if (this.corsProxy) console.log(` Using CORS proxy`);
|
|
77
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
78
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
79
|
+
const data = await response.json();
|
|
80
|
+
if (!data.hasTopConcept || !Array.isArray(data.hasTopConcept)) throw new Error("Invalid structure: missing hasTopConcept");
|
|
135
81
|
const dictionary = this.buildVocabularyDictionary(data);
|
|
136
82
|
const resolved = {
|
|
137
83
|
url,
|
|
@@ -139,6 +85,7 @@ var MetadataResolver = class {
|
|
|
139
85
|
dictionary
|
|
140
86
|
};
|
|
141
87
|
if (this.useCache) this.vocabulariesStore.set(url, resolved);
|
|
88
|
+
console.log(` Loaded: ${data.hasTopConcept.length} concepts, ${Object.keys(dictionary).length} entries`);
|
|
142
89
|
return resolved;
|
|
143
90
|
} catch (error) {
|
|
144
91
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
@@ -156,7 +103,9 @@ var MetadataResolver = class {
|
|
|
156
103
|
*/
|
|
157
104
|
async loadVocabularies(profile) {
|
|
158
105
|
const urls = this.extractVocabularyUrls(profile);
|
|
159
|
-
|
|
106
|
+
const vocabularies = (await Promise.allSettled(urls.map((url) => this.loadVocabulary(url)))).filter((result) => result.status === "fulfilled").map((result) => result.value);
|
|
107
|
+
console.log(`Loaded ${vocabularies.length} vocabularies (${this.vocabulariesStore.size} total in store)`);
|
|
108
|
+
return vocabularies;
|
|
160
109
|
}
|
|
161
110
|
/**
|
|
162
111
|
* Build a dictionary from vocabulary data
|
|
@@ -196,7 +145,10 @@ var MetadataResolver = class {
|
|
|
196
145
|
*/
|
|
197
146
|
async resolveJsonLdUrl(originalUrl) {
|
|
198
147
|
try {
|
|
148
|
+
console.log(`Resolving URL: ${originalUrl}`);
|
|
149
|
+
console.log(` CORS proxy: ${this.corsProxy || "disabled"}`);
|
|
199
150
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(originalUrl)}` : originalUrl;
|
|
151
|
+
console.log(` Fetching: ${fetchUrl}`);
|
|
200
152
|
const res = await fetch(fetchUrl, {
|
|
201
153
|
method: "GET",
|
|
202
154
|
redirect: "follow",
|
|
@@ -212,7 +164,10 @@ var MetadataResolver = class {
|
|
|
212
164
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonldUrl)}` : jsonldUrl, {
|
|
213
165
|
method: "HEAD",
|
|
214
166
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
215
|
-
})).ok)
|
|
167
|
+
})).ok) {
|
|
168
|
+
console.log(` Found JSON-LD: ${jsonldUrl}`);
|
|
169
|
+
return jsonldUrl;
|
|
170
|
+
}
|
|
216
171
|
} catch (err) {
|
|
217
172
|
console.log(` JSON-LD not available: ${err instanceof Error ? err.message : String(err)}`);
|
|
218
173
|
}
|
|
@@ -221,7 +176,10 @@ var MetadataResolver = class {
|
|
|
221
176
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonUrl)}` : jsonUrl, {
|
|
222
177
|
method: "HEAD",
|
|
223
178
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
224
|
-
})).ok)
|
|
179
|
+
})).ok) {
|
|
180
|
+
console.log(` Found JSON: ${jsonUrl}`);
|
|
181
|
+
return jsonUrl;
|
|
182
|
+
}
|
|
225
183
|
} catch (err) {
|
|
226
184
|
console.log(` JSON not available: ${err instanceof Error ? err.message : String(err)}`);
|
|
227
185
|
}
|
|
@@ -231,7 +189,10 @@ var MetadataResolver = class {
|
|
|
231
189
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(candidate)}` : candidate, {
|
|
232
190
|
method: "HEAD",
|
|
233
191
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
234
|
-
})).ok)
|
|
192
|
+
})).ok) {
|
|
193
|
+
console.log(` Found: ${candidate}`);
|
|
194
|
+
return candidate;
|
|
195
|
+
}
|
|
235
196
|
} catch (err) {
|
|
236
197
|
console.log(` Not available: ${candidate} - ${err instanceof Error ? err.message : String(err)}`);
|
|
237
198
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,34 +1,12 @@
|
|
|
1
|
-
import { MDProfile } from "@
|
|
1
|
+
import { LanguageCodedText, LanguageCodedText as LanguageCodedText$1, MDProfile, MDProfile as MDProfile$1 } from "@iqbspecs/metadata-profile";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Configuration options for the MetadataResolver
|
|
6
|
-
*/
|
|
7
4
|
interface LoaderOptions {
|
|
8
|
-
/**
|
|
9
|
-
* CORS proxy URL (e.g., 'https://corsproxy.io/?')
|
|
10
|
-
* If provided, all fetch requests will be proxied through this URL
|
|
11
|
-
*/
|
|
12
5
|
corsProxy?: string;
|
|
13
|
-
/**
|
|
14
|
-
* Enable caching of loaded resources
|
|
15
|
-
* @default true
|
|
16
|
-
*/
|
|
17
6
|
cache?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Preferred language for vocabulary labels
|
|
20
|
-
* @default 'de'
|
|
21
|
-
*/
|
|
22
7
|
preferredLanguage?: string;
|
|
23
|
-
/**
|
|
24
|
-
* Request timeout in milliseconds
|
|
25
|
-
* @default 10000
|
|
26
|
-
*/
|
|
27
8
|
requestTimeout?: number;
|
|
28
9
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Vocabulary concept node structure
|
|
31
|
-
*/
|
|
32
10
|
interface VocabConcept {
|
|
33
11
|
id: string;
|
|
34
12
|
notation?: string[];
|
|
@@ -40,9 +18,6 @@ interface VocabConcept {
|
|
|
40
18
|
description?: string;
|
|
41
19
|
narrower?: VocabConcept[];
|
|
42
20
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Raw vocabulary data structure from vocabulary JSON files
|
|
45
|
-
*/
|
|
46
21
|
interface VocabularyData {
|
|
47
22
|
id?: string;
|
|
48
23
|
type?: string;
|
|
@@ -54,45 +29,33 @@ interface VocabularyEntry {
|
|
|
54
29
|
name: string;
|
|
55
30
|
notation: string[];
|
|
56
31
|
description?: string;
|
|
57
|
-
text:
|
|
32
|
+
text: LanguageCodedText$1[];
|
|
58
33
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Resolved vocabulary with data and lookup dictionary
|
|
61
|
-
*/
|
|
62
34
|
interface ResolvedVocabulary {
|
|
63
35
|
url: string;
|
|
64
36
|
data: VocabularyData;
|
|
65
37
|
dictionary: Record<string, VocabularyEntry>;
|
|
66
38
|
error?: string;
|
|
67
39
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Profile loaded with all its vocabularies
|
|
70
|
-
*/
|
|
71
40
|
interface ProfileWithVocabularies {
|
|
72
|
-
profile: MDProfile;
|
|
41
|
+
profile: MDProfile$1;
|
|
73
42
|
vocabularies: ResolvedVocabulary[];
|
|
74
43
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Complete metadata package with profile, metadata, and vocabularies
|
|
77
|
-
*/
|
|
78
44
|
interface MetadataWithProfile {
|
|
79
|
-
profile: MDProfile;
|
|
80
|
-
metadata:
|
|
45
|
+
profile: MDProfile$1;
|
|
46
|
+
metadata: any;
|
|
81
47
|
vocabularies: ResolvedVocabulary[];
|
|
82
48
|
}
|
|
83
49
|
/**
|
|
84
|
-
*
|
|
50
|
+
* @deprecated Use LanguageCodedText instead.
|
|
85
51
|
*/
|
|
86
|
-
|
|
87
|
-
lang: string;
|
|
88
|
-
value: string;
|
|
89
|
-
}
|
|
52
|
+
type TextWithLanguage = LanguageCodedText$1;
|
|
90
53
|
/**
|
|
91
|
-
*
|
|
54
|
+
* @deprecated Will be removed when values spec is adopted.
|
|
92
55
|
*/
|
|
93
56
|
interface TextWithLanguageAndId {
|
|
94
57
|
id: string;
|
|
95
|
-
text:
|
|
58
|
+
text: LanguageCodedText$1[];
|
|
96
59
|
}
|
|
97
60
|
//#endregion
|
|
98
61
|
//#region src/metadata-resolver.d.ts
|
|
@@ -104,7 +67,6 @@ declare class MetadataResolver {
|
|
|
104
67
|
private preferredLanguage;
|
|
105
68
|
private requestTimeout;
|
|
106
69
|
constructor(options?: LoaderOptions);
|
|
107
|
-
setCorsProxy(proxy: string | undefined): void;
|
|
108
70
|
/**
|
|
109
71
|
* Load a profile from a URL
|
|
110
72
|
*/
|
|
@@ -112,7 +74,7 @@ declare class MetadataResolver {
|
|
|
112
74
|
/**
|
|
113
75
|
* Load metadata from a URL
|
|
114
76
|
*/
|
|
115
|
-
loadMetadata(url: string): Promise<
|
|
77
|
+
loadMetadata(url: string): Promise<any>;
|
|
116
78
|
/**
|
|
117
79
|
* Extract vocabulary URLs from a profile
|
|
118
80
|
*/
|
|
@@ -145,10 +107,7 @@ declare class MetadataResolver {
|
|
|
145
107
|
/**
|
|
146
108
|
* Extract text from a label (string or multilingual array)
|
|
147
109
|
*/
|
|
148
|
-
static extractLabelText(label: string |
|
|
149
|
-
lang: string;
|
|
150
|
-
value: string;
|
|
151
|
-
}>, preferredLanguage?: string): string;
|
|
110
|
+
static extractLabelText(label: string | LanguageCodedText[], preferredLanguage?: string): string;
|
|
152
111
|
/**
|
|
153
112
|
* Clear all caches (profiles, metadata, and vocabularies)
|
|
154
113
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -1,34 +1,12 @@
|
|
|
1
|
-
import { MDProfile } from "@
|
|
1
|
+
import { LanguageCodedText, LanguageCodedText as LanguageCodedText$1, MDProfile, MDProfile as MDProfile$1 } from "@iqbspecs/metadata-profile";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Configuration options for the MetadataResolver
|
|
6
|
-
*/
|
|
7
4
|
interface LoaderOptions {
|
|
8
|
-
/**
|
|
9
|
-
* CORS proxy URL (e.g., 'https://corsproxy.io/?')
|
|
10
|
-
* If provided, all fetch requests will be proxied through this URL
|
|
11
|
-
*/
|
|
12
5
|
corsProxy?: string;
|
|
13
|
-
/**
|
|
14
|
-
* Enable caching of loaded resources
|
|
15
|
-
* @default true
|
|
16
|
-
*/
|
|
17
6
|
cache?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Preferred language for vocabulary labels
|
|
20
|
-
* @default 'de'
|
|
21
|
-
*/
|
|
22
7
|
preferredLanguage?: string;
|
|
23
|
-
/**
|
|
24
|
-
* Request timeout in milliseconds
|
|
25
|
-
* @default 10000
|
|
26
|
-
*/
|
|
27
8
|
requestTimeout?: number;
|
|
28
9
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Vocabulary concept node structure
|
|
31
|
-
*/
|
|
32
10
|
interface VocabConcept {
|
|
33
11
|
id: string;
|
|
34
12
|
notation?: string[];
|
|
@@ -40,9 +18,6 @@ interface VocabConcept {
|
|
|
40
18
|
description?: string;
|
|
41
19
|
narrower?: VocabConcept[];
|
|
42
20
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Raw vocabulary data structure from vocabulary JSON files
|
|
45
|
-
*/
|
|
46
21
|
interface VocabularyData {
|
|
47
22
|
id?: string;
|
|
48
23
|
type?: string;
|
|
@@ -54,45 +29,33 @@ interface VocabularyEntry {
|
|
|
54
29
|
name: string;
|
|
55
30
|
notation: string[];
|
|
56
31
|
description?: string;
|
|
57
|
-
text:
|
|
32
|
+
text: LanguageCodedText$1[];
|
|
58
33
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Resolved vocabulary with data and lookup dictionary
|
|
61
|
-
*/
|
|
62
34
|
interface ResolvedVocabulary {
|
|
63
35
|
url: string;
|
|
64
36
|
data: VocabularyData;
|
|
65
37
|
dictionary: Record<string, VocabularyEntry>;
|
|
66
38
|
error?: string;
|
|
67
39
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Profile loaded with all its vocabularies
|
|
70
|
-
*/
|
|
71
40
|
interface ProfileWithVocabularies {
|
|
72
|
-
profile: MDProfile;
|
|
41
|
+
profile: MDProfile$1;
|
|
73
42
|
vocabularies: ResolvedVocabulary[];
|
|
74
43
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Complete metadata package with profile, metadata, and vocabularies
|
|
77
|
-
*/
|
|
78
44
|
interface MetadataWithProfile {
|
|
79
|
-
profile: MDProfile;
|
|
80
|
-
metadata:
|
|
45
|
+
profile: MDProfile$1;
|
|
46
|
+
metadata: any;
|
|
81
47
|
vocabularies: ResolvedVocabulary[];
|
|
82
48
|
}
|
|
83
49
|
/**
|
|
84
|
-
*
|
|
50
|
+
* @deprecated Use LanguageCodedText instead.
|
|
85
51
|
*/
|
|
86
|
-
|
|
87
|
-
lang: string;
|
|
88
|
-
value: string;
|
|
89
|
-
}
|
|
52
|
+
type TextWithLanguage = LanguageCodedText$1;
|
|
90
53
|
/**
|
|
91
|
-
*
|
|
54
|
+
* @deprecated Will be removed when values spec is adopted.
|
|
92
55
|
*/
|
|
93
56
|
interface TextWithLanguageAndId {
|
|
94
57
|
id: string;
|
|
95
|
-
text:
|
|
58
|
+
text: LanguageCodedText$1[];
|
|
96
59
|
}
|
|
97
60
|
//#endregion
|
|
98
61
|
//#region src/metadata-resolver.d.ts
|
|
@@ -104,7 +67,6 @@ declare class MetadataResolver {
|
|
|
104
67
|
private preferredLanguage;
|
|
105
68
|
private requestTimeout;
|
|
106
69
|
constructor(options?: LoaderOptions);
|
|
107
|
-
setCorsProxy(proxy: string | undefined): void;
|
|
108
70
|
/**
|
|
109
71
|
* Load a profile from a URL
|
|
110
72
|
*/
|
|
@@ -112,7 +74,7 @@ declare class MetadataResolver {
|
|
|
112
74
|
/**
|
|
113
75
|
* Load metadata from a URL
|
|
114
76
|
*/
|
|
115
|
-
loadMetadata(url: string): Promise<
|
|
77
|
+
loadMetadata(url: string): Promise<any>;
|
|
116
78
|
/**
|
|
117
79
|
* Extract vocabulary URLs from a profile
|
|
118
80
|
*/
|
|
@@ -145,10 +107,7 @@ declare class MetadataResolver {
|
|
|
145
107
|
/**
|
|
146
108
|
* Extract text from a label (string or multilingual array)
|
|
147
109
|
*/
|
|
148
|
-
static extractLabelText(label: string |
|
|
149
|
-
lang: string;
|
|
150
|
-
value: string;
|
|
151
|
-
}>, preferredLanguage?: string): string;
|
|
110
|
+
static extractLabelText(label: string | LanguageCodedText[], preferredLanguage?: string): string;
|
|
152
111
|
/**
|
|
153
112
|
* Clear all caches (profiles, metadata, and vocabularies)
|
|
154
113
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -12,29 +12,21 @@ var MetadataResolver = class {
|
|
|
12
12
|
this.preferredLanguage = options?.preferredLanguage || "de";
|
|
13
13
|
this.requestTimeout = options?.requestTimeout || 1e4;
|
|
14
14
|
}
|
|
15
|
-
setCorsProxy(proxy) {
|
|
16
|
-
this.corsProxy = proxy;
|
|
17
|
-
}
|
|
18
15
|
/**
|
|
19
16
|
* Load a profile from a URL
|
|
20
17
|
*/
|
|
21
18
|
async loadProfile(url) {
|
|
22
19
|
const cacheKey = `profile:${url}`;
|
|
23
20
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
24
|
-
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
25
|
-
let response;
|
|
26
|
-
try {
|
|
27
|
-
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
28
|
-
} catch (error) {
|
|
29
|
-
throw new Error(`Error loading profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
30
|
-
}
|
|
31
|
-
if (!response.ok) throw new Error(`Error loading profile from ${url}: ${response.status} ${response.statusText}`);
|
|
32
21
|
try {
|
|
22
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
23
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
24
|
+
if (!response.ok) throw new Error(`Failed to load profile: ${response.status} ${response.statusText}`);
|
|
33
25
|
const profile = await response.json();
|
|
34
26
|
if (this.useCache) this.cache.set(cacheKey, profile);
|
|
35
27
|
return profile;
|
|
36
28
|
} catch (error) {
|
|
37
|
-
throw new Error(`Error
|
|
29
|
+
throw new Error(`Error loading profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
38
30
|
}
|
|
39
31
|
}
|
|
40
32
|
/**
|
|
@@ -43,20 +35,15 @@ var MetadataResolver = class {
|
|
|
43
35
|
async loadMetadata(url) {
|
|
44
36
|
const cacheKey = `metadata:${url}`;
|
|
45
37
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
46
|
-
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
47
|
-
let response;
|
|
48
|
-
try {
|
|
49
|
-
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
50
|
-
} catch (error) {
|
|
51
|
-
throw new Error(`Error loading metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
52
|
-
}
|
|
53
|
-
if (!response.ok) throw new Error(`Error loading metadata from ${url}: ${response.status} ${response.statusText}`);
|
|
54
38
|
try {
|
|
39
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
40
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
41
|
+
if (!response.ok) throw new Error(`Failed to load metadata: ${response.status} ${response.statusText}`);
|
|
55
42
|
const metadata = await response.json();
|
|
56
43
|
if (this.useCache) this.cache.set(cacheKey, metadata);
|
|
57
44
|
return metadata;
|
|
58
45
|
} catch (error) {
|
|
59
|
-
throw new Error(`Error
|
|
46
|
+
throw new Error(`Error loading metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
60
47
|
}
|
|
61
48
|
}
|
|
62
49
|
/**
|
|
@@ -66,7 +53,7 @@ var MetadataResolver = class {
|
|
|
66
53
|
const urls = /* @__PURE__ */ new Set();
|
|
67
54
|
profile.groups?.forEach((group) => {
|
|
68
55
|
group.entries?.forEach((entry) => {
|
|
69
|
-
if (entry.type === "
|
|
56
|
+
if (entry.type.toUpperCase() === "VOCABULARY" && entry.parameters) {
|
|
70
57
|
const params = entry.parameters;
|
|
71
58
|
if (params.url) urls.add(params.url);
|
|
72
59
|
}
|
|
@@ -81,56 +68,15 @@ var MetadataResolver = class {
|
|
|
81
68
|
async loadVocabulary(url) {
|
|
82
69
|
if (this.useCache && this.vocabulariesStore.has(url)) return this.vocabulariesStore.get(url);
|
|
83
70
|
try {
|
|
71
|
+
console.log(`Loading vocabulary: ${url}`);
|
|
84
72
|
const jsonLdUrl = await this.resolveJsonLdUrl(url);
|
|
85
|
-
if (jsonLdUrl !== url) {}
|
|
73
|
+
if (jsonLdUrl !== url) console.log(` Resolved to: ${jsonLdUrl}`);
|
|
86
74
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonLdUrl)}` : jsonLdUrl;
|
|
87
|
-
if (this.corsProxy)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
93
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
94
|
-
return {
|
|
95
|
-
url,
|
|
96
|
-
data: { hasTopConcept: [] },
|
|
97
|
-
dictionary: {},
|
|
98
|
-
error: errorMsg
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
if (!response.ok) {
|
|
102
|
-
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
|
103
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
104
|
-
return {
|
|
105
|
-
url,
|
|
106
|
-
data: { hasTopConcept: [] },
|
|
107
|
-
dictionary: {},
|
|
108
|
-
error: errorMsg
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
let data;
|
|
112
|
-
try {
|
|
113
|
-
data = await response.json();
|
|
114
|
-
} catch (error) {
|
|
115
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
116
|
-
console.error(` Failed to parse vocabulary JSON: ${errorMsg}`);
|
|
117
|
-
return {
|
|
118
|
-
url,
|
|
119
|
-
data: { hasTopConcept: [] },
|
|
120
|
-
dictionary: {},
|
|
121
|
-
error: errorMsg
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
if (!data.hasTopConcept || !Array.isArray(data.hasTopConcept)) {
|
|
125
|
-
const errorMsg = "Invalid structure: missing hasTopConcept";
|
|
126
|
-
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
127
|
-
return {
|
|
128
|
-
url,
|
|
129
|
-
data: { hasTopConcept: [] },
|
|
130
|
-
dictionary: {},
|
|
131
|
-
error: errorMsg
|
|
132
|
-
};
|
|
133
|
-
}
|
|
75
|
+
if (this.corsProxy) console.log(` Using CORS proxy`);
|
|
76
|
+
const response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
77
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
78
|
+
const data = await response.json();
|
|
79
|
+
if (!data.hasTopConcept || !Array.isArray(data.hasTopConcept)) throw new Error("Invalid structure: missing hasTopConcept");
|
|
134
80
|
const dictionary = this.buildVocabularyDictionary(data);
|
|
135
81
|
const resolved = {
|
|
136
82
|
url,
|
|
@@ -138,6 +84,7 @@ var MetadataResolver = class {
|
|
|
138
84
|
dictionary
|
|
139
85
|
};
|
|
140
86
|
if (this.useCache) this.vocabulariesStore.set(url, resolved);
|
|
87
|
+
console.log(` Loaded: ${data.hasTopConcept.length} concepts, ${Object.keys(dictionary).length} entries`);
|
|
141
88
|
return resolved;
|
|
142
89
|
} catch (error) {
|
|
143
90
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
@@ -155,7 +102,9 @@ var MetadataResolver = class {
|
|
|
155
102
|
*/
|
|
156
103
|
async loadVocabularies(profile) {
|
|
157
104
|
const urls = this.extractVocabularyUrls(profile);
|
|
158
|
-
|
|
105
|
+
const vocabularies = (await Promise.allSettled(urls.map((url) => this.loadVocabulary(url)))).filter((result) => result.status === "fulfilled").map((result) => result.value);
|
|
106
|
+
console.log(`Loaded ${vocabularies.length} vocabularies (${this.vocabulariesStore.size} total in store)`);
|
|
107
|
+
return vocabularies;
|
|
159
108
|
}
|
|
160
109
|
/**
|
|
161
110
|
* Build a dictionary from vocabulary data
|
|
@@ -195,7 +144,10 @@ var MetadataResolver = class {
|
|
|
195
144
|
*/
|
|
196
145
|
async resolveJsonLdUrl(originalUrl) {
|
|
197
146
|
try {
|
|
147
|
+
console.log(`Resolving URL: ${originalUrl}`);
|
|
148
|
+
console.log(` CORS proxy: ${this.corsProxy || "disabled"}`);
|
|
198
149
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(originalUrl)}` : originalUrl;
|
|
150
|
+
console.log(` Fetching: ${fetchUrl}`);
|
|
199
151
|
const res = await fetch(fetchUrl, {
|
|
200
152
|
method: "GET",
|
|
201
153
|
redirect: "follow",
|
|
@@ -211,7 +163,10 @@ var MetadataResolver = class {
|
|
|
211
163
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonldUrl)}` : jsonldUrl, {
|
|
212
164
|
method: "HEAD",
|
|
213
165
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
214
|
-
})).ok)
|
|
166
|
+
})).ok) {
|
|
167
|
+
console.log(` Found JSON-LD: ${jsonldUrl}`);
|
|
168
|
+
return jsonldUrl;
|
|
169
|
+
}
|
|
215
170
|
} catch (err) {
|
|
216
171
|
console.log(` JSON-LD not available: ${err instanceof Error ? err.message : String(err)}`);
|
|
217
172
|
}
|
|
@@ -220,7 +175,10 @@ var MetadataResolver = class {
|
|
|
220
175
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonUrl)}` : jsonUrl, {
|
|
221
176
|
method: "HEAD",
|
|
222
177
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
223
|
-
})).ok)
|
|
178
|
+
})).ok) {
|
|
179
|
+
console.log(` Found JSON: ${jsonUrl}`);
|
|
180
|
+
return jsonUrl;
|
|
181
|
+
}
|
|
224
182
|
} catch (err) {
|
|
225
183
|
console.log(` JSON not available: ${err instanceof Error ? err.message : String(err)}`);
|
|
226
184
|
}
|
|
@@ -230,7 +188,10 @@ var MetadataResolver = class {
|
|
|
230
188
|
if ((await fetch(this.corsProxy ? `${this.corsProxy}${encodeURIComponent(candidate)}` : candidate, {
|
|
231
189
|
method: "HEAD",
|
|
232
190
|
signal: AbortSignal.timeout(this.requestTimeout)
|
|
233
|
-
})).ok)
|
|
191
|
+
})).ok) {
|
|
192
|
+
console.log(` Found: ${candidate}`);
|
|
193
|
+
return candidate;
|
|
194
|
+
}
|
|
234
195
|
} catch (err) {
|
|
235
196
|
console.log(` Not available: ${candidate} - ${err instanceof Error ? err.message : String(err)}`);
|
|
236
197
|
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iqb/metadata-resolver",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Framework-agnostic metadata and vocabulary resolver for IQB metadata profiles",
|
|
5
|
-
"main": "dist/index.
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
13
|
-
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch"
|
|
14
|
-
"test": "vitest run",
|
|
15
|
-
"lint": "eslint src",
|
|
16
|
-
"format": "prettier --write \"src/**/*.{ts,json}\"",
|
|
17
|
-
"typecheck": "tsc --noEmit",
|
|
18
|
-
"prepublishOnly": "npm run typecheck && npm run lint && npm run test && npm run build",
|
|
19
|
-
"release": "npm publish"
|
|
13
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch"
|
|
20
14
|
},
|
|
21
15
|
"keywords": [
|
|
22
16
|
"metadata",
|
|
@@ -27,32 +21,17 @@
|
|
|
27
21
|
"author": "IQB",
|
|
28
22
|
"license": "MIT",
|
|
29
23
|
"dependencies": {
|
|
30
|
-
"@
|
|
24
|
+
"@iqbspecs/metadata-profile": "^0.11.1"
|
|
31
25
|
},
|
|
32
26
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"@typescript-eslint/parser": "^7.18.0",
|
|
36
|
-
"eslint": "^8.57.0",
|
|
37
|
-
"eslint-config-prettier": "^9.1.0",
|
|
38
|
-
"eslint-plugin-prettier": "^5.1.3",
|
|
39
|
-
"@iqb/eslint-config": "^2.2.0",
|
|
40
|
-
"prettier": "^3.8.1",
|
|
41
|
-
"tsdown": "^0.20.1",
|
|
42
|
-
"typescript": "^5.3.0",
|
|
43
|
-
"typescript-eslint": "^7.18.0",
|
|
44
|
-
"vitest": "^4.0.18"
|
|
27
|
+
"tsdown": "^0.20.0-beta.2",
|
|
28
|
+
"typescript": "^5.3.0"
|
|
45
29
|
},
|
|
46
30
|
"exports": {
|
|
47
31
|
".": {
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
},
|
|
52
|
-
"require": {
|
|
53
|
-
"types": "./dist/index.d.cts",
|
|
54
|
-
"default": "./dist/index.cjs"
|
|
55
|
-
}
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.mjs",
|
|
34
|
+
"require": "./dist/index.js"
|
|
56
35
|
}
|
|
57
36
|
},
|
|
58
37
|
"publishConfig": {
|