@iqb/metadata-resolver 0.2.0 → 0.2.1
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 +64 -2
- package/dist/index.cjs +69 -13
- package/dist/index.d.cts +11 -7
- package/dist/index.d.mts +11 -7
- package/dist/index.d.ts +133 -0
- package/dist/index.mjs +68 -13
- package/package.json +31 -10
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @iqb/metadata-resolver
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript library for loading metadata profiles and vocabularies.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
```bash
|
|
@@ -28,6 +28,68 @@ 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
|
+
|
|
31
93
|
## License
|
|
32
94
|
|
|
33
|
-
MIT © IQB Berlin
|
|
95
|
+
MIT © IQB Berlin
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
1
2
|
|
|
2
3
|
//#region src/metadata-resolver.ts
|
|
3
4
|
var MetadataResolver = class {
|
|
@@ -13,21 +14,29 @@ var MetadataResolver = class {
|
|
|
13
14
|
this.preferredLanguage = options?.preferredLanguage || "de";
|
|
14
15
|
this.requestTimeout = options?.requestTimeout || 1e4;
|
|
15
16
|
}
|
|
17
|
+
setCorsProxy(proxy) {
|
|
18
|
+
this.corsProxy = proxy;
|
|
19
|
+
}
|
|
16
20
|
/**
|
|
17
21
|
* Load a profile from a URL
|
|
18
22
|
*/
|
|
19
23
|
async loadProfile(url) {
|
|
20
24
|
const cacheKey = `profile:${url}`;
|
|
21
25
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
26
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
27
|
+
let response;
|
|
28
|
+
try {
|
|
29
|
+
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error(`Error loading profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
32
|
+
}
|
|
33
|
+
if (!response.ok) throw new Error(`Error loading profile from ${url}: ${response.status} ${response.statusText}`);
|
|
22
34
|
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}`);
|
|
26
35
|
const profile = await response.json();
|
|
27
36
|
if (this.useCache) this.cache.set(cacheKey, profile);
|
|
28
37
|
return profile;
|
|
29
38
|
} catch (error) {
|
|
30
|
-
throw new Error(`Error
|
|
39
|
+
throw new Error(`Error parsing profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
31
40
|
}
|
|
32
41
|
}
|
|
33
42
|
/**
|
|
@@ -36,15 +45,20 @@ var MetadataResolver = class {
|
|
|
36
45
|
async loadMetadata(url) {
|
|
37
46
|
const cacheKey = `metadata:${url}`;
|
|
38
47
|
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
48
|
+
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(url)}` : url;
|
|
49
|
+
let response;
|
|
50
|
+
try {
|
|
51
|
+
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new Error(`Error loading metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
54
|
+
}
|
|
55
|
+
if (!response.ok) throw new Error(`Error loading metadata from ${url}: ${response.status} ${response.statusText}`);
|
|
39
56
|
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}`);
|
|
43
57
|
const metadata = await response.json();
|
|
44
58
|
if (this.useCache) this.cache.set(cacheKey, metadata);
|
|
45
59
|
return metadata;
|
|
46
60
|
} catch (error) {
|
|
47
|
-
throw new Error(`Error
|
|
61
|
+
throw new Error(`Error parsing metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
48
62
|
}
|
|
49
63
|
}
|
|
50
64
|
/**
|
|
@@ -73,11 +87,53 @@ var MetadataResolver = class {
|
|
|
73
87
|
const jsonLdUrl = await this.resolveJsonLdUrl(url);
|
|
74
88
|
if (jsonLdUrl !== url) console.log(` Resolved to: ${jsonLdUrl}`);
|
|
75
89
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonLdUrl)}` : jsonLdUrl;
|
|
76
|
-
if (this.corsProxy) console.log(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
90
|
+
if (this.corsProxy) console.log(" Using CORS proxy");
|
|
91
|
+
let response;
|
|
92
|
+
try {
|
|
93
|
+
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
96
|
+
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
97
|
+
return {
|
|
98
|
+
url,
|
|
99
|
+
data: { hasTopConcept: [] },
|
|
100
|
+
dictionary: {},
|
|
101
|
+
error: errorMsg
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
|
106
|
+
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
107
|
+
return {
|
|
108
|
+
url,
|
|
109
|
+
data: { hasTopConcept: [] },
|
|
110
|
+
dictionary: {},
|
|
111
|
+
error: errorMsg
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
let data;
|
|
115
|
+
try {
|
|
116
|
+
data = await response.json();
|
|
117
|
+
} catch (error) {
|
|
118
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
119
|
+
console.error(` Failed to parse vocabulary JSON: ${errorMsg}`);
|
|
120
|
+
return {
|
|
121
|
+
url,
|
|
122
|
+
data: { hasTopConcept: [] },
|
|
123
|
+
dictionary: {},
|
|
124
|
+
error: errorMsg
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (!data.hasTopConcept || !Array.isArray(data.hasTopConcept)) {
|
|
128
|
+
const errorMsg = "Invalid structure: missing hasTopConcept";
|
|
129
|
+
console.error(` Failed to load vocabulary: ${errorMsg}`);
|
|
130
|
+
return {
|
|
131
|
+
url,
|
|
132
|
+
data: { hasTopConcept: [] },
|
|
133
|
+
dictionary: {},
|
|
134
|
+
error: errorMsg
|
|
135
|
+
};
|
|
136
|
+
}
|
|
81
137
|
const dictionary = this.buildVocabularyDictionary(data);
|
|
82
138
|
const resolved = {
|
|
83
139
|
url,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LanguageCodedText,
|
|
1
|
+
import { LanguageCodedText, MDProfile, MDProfile as MDProfile$1 } from "@iqbspecs/metadata-profile";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
interface LoaderOptions {
|
|
@@ -29,7 +29,7 @@ interface VocabularyEntry {
|
|
|
29
29
|
name: string;
|
|
30
30
|
notation: string[];
|
|
31
31
|
description?: string;
|
|
32
|
-
text: LanguageCodedText
|
|
32
|
+
text: LanguageCodedText[];
|
|
33
33
|
}
|
|
34
34
|
interface ResolvedVocabulary {
|
|
35
35
|
url: string;
|
|
@@ -43,19 +43,19 @@ interface ProfileWithVocabularies {
|
|
|
43
43
|
}
|
|
44
44
|
interface MetadataWithProfile {
|
|
45
45
|
profile: MDProfile$1;
|
|
46
|
-
metadata:
|
|
46
|
+
metadata: unknown;
|
|
47
47
|
vocabularies: ResolvedVocabulary[];
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
50
|
* @deprecated Use LanguageCodedText instead.
|
|
51
51
|
*/
|
|
52
|
-
type TextWithLanguage = LanguageCodedText
|
|
52
|
+
type TextWithLanguage = LanguageCodedText;
|
|
53
53
|
/**
|
|
54
54
|
* @deprecated Will be removed when values spec is adopted.
|
|
55
55
|
*/
|
|
56
56
|
interface TextWithLanguageAndId {
|
|
57
57
|
id: string;
|
|
58
|
-
text: LanguageCodedText
|
|
58
|
+
text: LanguageCodedText[];
|
|
59
59
|
}
|
|
60
60
|
//#endregion
|
|
61
61
|
//#region src/metadata-resolver.d.ts
|
|
@@ -67,6 +67,7 @@ declare class MetadataResolver {
|
|
|
67
67
|
private preferredLanguage;
|
|
68
68
|
private requestTimeout;
|
|
69
69
|
constructor(options?: LoaderOptions);
|
|
70
|
+
setCorsProxy(proxy: string | undefined): void;
|
|
70
71
|
/**
|
|
71
72
|
* Load a profile from a URL
|
|
72
73
|
*/
|
|
@@ -74,7 +75,7 @@ declare class MetadataResolver {
|
|
|
74
75
|
/**
|
|
75
76
|
* Load metadata from a URL
|
|
76
77
|
*/
|
|
77
|
-
loadMetadata(url: string): Promise<
|
|
78
|
+
loadMetadata(url: string): Promise<unknown>;
|
|
78
79
|
/**
|
|
79
80
|
* Extract vocabulary URLs from a profile
|
|
80
81
|
*/
|
|
@@ -107,7 +108,10 @@ declare class MetadataResolver {
|
|
|
107
108
|
/**
|
|
108
109
|
* Extract text from a label (string or multilingual array)
|
|
109
110
|
*/
|
|
110
|
-
static extractLabelText(label: string |
|
|
111
|
+
static extractLabelText(label: string | Array<{
|
|
112
|
+
lang: string;
|
|
113
|
+
value: string;
|
|
114
|
+
}>, preferredLanguage?: string): string;
|
|
111
115
|
/**
|
|
112
116
|
* Clear all caches (profiles, metadata, and vocabularies)
|
|
113
117
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LanguageCodedText,
|
|
1
|
+
import { LanguageCodedText, MDProfile, MDProfile as MDProfile$1 } from "@iqbspecs/metadata-profile";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
interface LoaderOptions {
|
|
@@ -29,7 +29,7 @@ interface VocabularyEntry {
|
|
|
29
29
|
name: string;
|
|
30
30
|
notation: string[];
|
|
31
31
|
description?: string;
|
|
32
|
-
text: LanguageCodedText
|
|
32
|
+
text: LanguageCodedText[];
|
|
33
33
|
}
|
|
34
34
|
interface ResolvedVocabulary {
|
|
35
35
|
url: string;
|
|
@@ -43,19 +43,19 @@ interface ProfileWithVocabularies {
|
|
|
43
43
|
}
|
|
44
44
|
interface MetadataWithProfile {
|
|
45
45
|
profile: MDProfile$1;
|
|
46
|
-
metadata:
|
|
46
|
+
metadata: unknown;
|
|
47
47
|
vocabularies: ResolvedVocabulary[];
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
50
|
* @deprecated Use LanguageCodedText instead.
|
|
51
51
|
*/
|
|
52
|
-
type TextWithLanguage = LanguageCodedText
|
|
52
|
+
type TextWithLanguage = LanguageCodedText;
|
|
53
53
|
/**
|
|
54
54
|
* @deprecated Will be removed when values spec is adopted.
|
|
55
55
|
*/
|
|
56
56
|
interface TextWithLanguageAndId {
|
|
57
57
|
id: string;
|
|
58
|
-
text: LanguageCodedText
|
|
58
|
+
text: LanguageCodedText[];
|
|
59
59
|
}
|
|
60
60
|
//#endregion
|
|
61
61
|
//#region src/metadata-resolver.d.ts
|
|
@@ -67,6 +67,7 @@ declare class MetadataResolver {
|
|
|
67
67
|
private preferredLanguage;
|
|
68
68
|
private requestTimeout;
|
|
69
69
|
constructor(options?: LoaderOptions);
|
|
70
|
+
setCorsProxy(proxy: string | undefined): void;
|
|
70
71
|
/**
|
|
71
72
|
* Load a profile from a URL
|
|
72
73
|
*/
|
|
@@ -74,7 +75,7 @@ declare class MetadataResolver {
|
|
|
74
75
|
/**
|
|
75
76
|
* Load metadata from a URL
|
|
76
77
|
*/
|
|
77
|
-
loadMetadata(url: string): Promise<
|
|
78
|
+
loadMetadata(url: string): Promise<unknown>;
|
|
78
79
|
/**
|
|
79
80
|
* Extract vocabulary URLs from a profile
|
|
80
81
|
*/
|
|
@@ -107,7 +108,10 @@ declare class MetadataResolver {
|
|
|
107
108
|
/**
|
|
108
109
|
* Extract text from a label (string or multilingual array)
|
|
109
110
|
*/
|
|
110
|
-
static extractLabelText(label: string |
|
|
111
|
+
static extractLabelText(label: string | Array<{
|
|
112
|
+
lang: string;
|
|
113
|
+
value: string;
|
|
114
|
+
}>, preferredLanguage?: string): string;
|
|
111
115
|
/**
|
|
112
116
|
* Clear all caches (profiles, metadata, and vocabularies)
|
|
113
117
|
*/
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { LanguageCodedText, MDProfile, MDProfile as MDProfile$1 } from "@iqbspecs/metadata-profile";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
interface LoaderOptions {
|
|
5
|
+
corsProxy?: string;
|
|
6
|
+
cache?: boolean;
|
|
7
|
+
preferredLanguage?: string;
|
|
8
|
+
requestTimeout?: number;
|
|
9
|
+
}
|
|
10
|
+
interface VocabConcept {
|
|
11
|
+
id: string;
|
|
12
|
+
notation?: string[];
|
|
13
|
+
prefLabel?: {
|
|
14
|
+
de?: string;
|
|
15
|
+
en?: string;
|
|
16
|
+
[key: string]: string | undefined;
|
|
17
|
+
};
|
|
18
|
+
description?: string;
|
|
19
|
+
narrower?: VocabConcept[];
|
|
20
|
+
}
|
|
21
|
+
interface VocabularyData {
|
|
22
|
+
id?: string;
|
|
23
|
+
type?: string;
|
|
24
|
+
title?: Record<string, string>;
|
|
25
|
+
hasTopConcept?: VocabConcept[];
|
|
26
|
+
}
|
|
27
|
+
interface VocabularyEntry {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
notation: string[];
|
|
31
|
+
description?: string;
|
|
32
|
+
text: LanguageCodedText[];
|
|
33
|
+
}
|
|
34
|
+
interface ResolvedVocabulary {
|
|
35
|
+
url: string;
|
|
36
|
+
data: VocabularyData;
|
|
37
|
+
dictionary: Record<string, VocabularyEntry>;
|
|
38
|
+
error?: string;
|
|
39
|
+
}
|
|
40
|
+
interface ProfileWithVocabularies {
|
|
41
|
+
profile: MDProfile$1;
|
|
42
|
+
vocabularies: ResolvedVocabulary[];
|
|
43
|
+
}
|
|
44
|
+
interface MetadataWithProfile {
|
|
45
|
+
profile: MDProfile$1;
|
|
46
|
+
metadata: unknown;
|
|
47
|
+
vocabularies: ResolvedVocabulary[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated Use LanguageCodedText instead.
|
|
51
|
+
*/
|
|
52
|
+
type TextWithLanguage = LanguageCodedText;
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Will be removed when values spec is adopted.
|
|
55
|
+
*/
|
|
56
|
+
interface TextWithLanguageAndId {
|
|
57
|
+
id: string;
|
|
58
|
+
text: LanguageCodedText[];
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/metadata-resolver.d.ts
|
|
62
|
+
declare class MetadataResolver {
|
|
63
|
+
private corsProxy;
|
|
64
|
+
private cache;
|
|
65
|
+
private vocabulariesStore;
|
|
66
|
+
private useCache;
|
|
67
|
+
private preferredLanguage;
|
|
68
|
+
private requestTimeout;
|
|
69
|
+
constructor(options?: LoaderOptions);
|
|
70
|
+
setCorsProxy(proxy: string | undefined): void;
|
|
71
|
+
/**
|
|
72
|
+
* Load a profile from a URL
|
|
73
|
+
*/
|
|
74
|
+
loadProfile(url: string): Promise<MDProfile>;
|
|
75
|
+
/**
|
|
76
|
+
* Load metadata from a URL
|
|
77
|
+
*/
|
|
78
|
+
loadMetadata(url: string): Promise<unknown>;
|
|
79
|
+
/**
|
|
80
|
+
* Extract vocabulary URLs from a profile
|
|
81
|
+
*/
|
|
82
|
+
extractVocabularyUrls(profile: MDProfile): string[];
|
|
83
|
+
/**
|
|
84
|
+
* Load a single vocabulary from URL
|
|
85
|
+
* Uses vocabulariesStore as the cache
|
|
86
|
+
*/
|
|
87
|
+
loadVocabulary(url: string): Promise<ResolvedVocabulary>;
|
|
88
|
+
/**
|
|
89
|
+
* Load all vocabularies referenced in a profile
|
|
90
|
+
*/
|
|
91
|
+
loadVocabularies(profile: MDProfile): Promise<ResolvedVocabulary[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Build a dictionary from vocabulary data
|
|
94
|
+
*/
|
|
95
|
+
private buildVocabularyDictionary;
|
|
96
|
+
/**
|
|
97
|
+
* Load a profile and all its vocabularies
|
|
98
|
+
*/
|
|
99
|
+
loadProfileWithVocabularies(profileUrl: string): Promise<ProfileWithVocabularies>;
|
|
100
|
+
/**
|
|
101
|
+
* Resolve a URL to its JSON-LD representation
|
|
102
|
+
*/
|
|
103
|
+
private resolveJsonLdUrl;
|
|
104
|
+
/**
|
|
105
|
+
* Load both profile with vocabularies and metadata
|
|
106
|
+
*/
|
|
107
|
+
loadAll(profileUrl: string, metadataUrl: string): Promise<MetadataWithProfile>;
|
|
108
|
+
/**
|
|
109
|
+
* Extract text from a label (string or multilingual array)
|
|
110
|
+
*/
|
|
111
|
+
static extractLabelText(label: string | Array<{
|
|
112
|
+
lang: string;
|
|
113
|
+
value: string;
|
|
114
|
+
}>, preferredLanguage?: string): string;
|
|
115
|
+
/**
|
|
116
|
+
* Clear all caches (profiles, metadata, and vocabularies)
|
|
117
|
+
*/
|
|
118
|
+
clearCache(): void;
|
|
119
|
+
/**
|
|
120
|
+
* Get all loaded vocabularies
|
|
121
|
+
*/
|
|
122
|
+
getVocabularies(): ResolvedVocabulary[];
|
|
123
|
+
/**
|
|
124
|
+
* Get a specific vocabulary by URL
|
|
125
|
+
*/
|
|
126
|
+
getVocabulary(url: string): ResolvedVocabulary | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Get combined dictionary of all vocabulary entries
|
|
129
|
+
*/
|
|
130
|
+
getVocabularyDictionary(): Record<string, VocabularyEntry>;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { type LoaderOptions, MetadataResolver, type MetadataWithProfile, type ProfileWithVocabularies, type ResolvedVocabulary, type TextWithLanguage, type TextWithLanguageAndId, type VocabConcept, type VocabularyData, type VocabularyEntry };
|
package/dist/index.mjs
CHANGED
|
@@ -12,21 +12,29 @@ 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
|
+
}
|
|
15
18
|
/**
|
|
16
19
|
* Load a profile from a URL
|
|
17
20
|
*/
|
|
18
21
|
async loadProfile(url) {
|
|
19
22
|
const cacheKey = `profile:${url}`;
|
|
20
23
|
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}`);
|
|
21
32
|
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}`);
|
|
25
33
|
const profile = await response.json();
|
|
26
34
|
if (this.useCache) this.cache.set(cacheKey, profile);
|
|
27
35
|
return profile;
|
|
28
36
|
} catch (error) {
|
|
29
|
-
throw new Error(`Error
|
|
37
|
+
throw new Error(`Error parsing profile from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
30
38
|
}
|
|
31
39
|
}
|
|
32
40
|
/**
|
|
@@ -35,15 +43,20 @@ var MetadataResolver = class {
|
|
|
35
43
|
async loadMetadata(url) {
|
|
36
44
|
const cacheKey = `metadata:${url}`;
|
|
37
45
|
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}`);
|
|
38
54
|
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}`);
|
|
42
55
|
const metadata = await response.json();
|
|
43
56
|
if (this.useCache) this.cache.set(cacheKey, metadata);
|
|
44
57
|
return metadata;
|
|
45
58
|
} catch (error) {
|
|
46
|
-
throw new Error(`Error
|
|
59
|
+
throw new Error(`Error parsing metadata from ${url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
47
60
|
}
|
|
48
61
|
}
|
|
49
62
|
/**
|
|
@@ -72,11 +85,53 @@ var MetadataResolver = class {
|
|
|
72
85
|
const jsonLdUrl = await this.resolveJsonLdUrl(url);
|
|
73
86
|
if (jsonLdUrl !== url) console.log(` Resolved to: ${jsonLdUrl}`);
|
|
74
87
|
const fetchUrl = this.corsProxy ? `${this.corsProxy}${encodeURIComponent(jsonLdUrl)}` : jsonLdUrl;
|
|
75
|
-
if (this.corsProxy) console.log(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
if (this.corsProxy) console.log(" Using CORS proxy");
|
|
89
|
+
let response;
|
|
90
|
+
try {
|
|
91
|
+
response = await fetch(fetchUrl, { signal: AbortSignal.timeout(this.requestTimeout) });
|
|
92
|
+
} catch (error) {
|
|
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
|
+
}
|
|
80
135
|
const dictionary = this.buildVocabularyDictionary(data);
|
|
81
136
|
const resolved = {
|
|
82
137
|
url,
|
package/package.json
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iqb/metadata-resolver",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Framework-agnostic metadata and vocabulary resolver for IQB metadata profiles",
|
|
5
|
-
"main": "dist/index.
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.
|
|
7
|
+
"types": "dist/index.d.cts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
13
|
-
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch"
|
|
12
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean && cp dist/index.d.mts dist/index.d.ts",
|
|
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"
|
|
14
20
|
},
|
|
15
21
|
"keywords": [
|
|
16
22
|
"metadata",
|
|
@@ -24,14 +30,29 @@
|
|
|
24
30
|
"@iqbspecs/metadata-profile": "^0.11.1"
|
|
25
31
|
},
|
|
26
32
|
"devDependencies": {
|
|
27
|
-
"
|
|
28
|
-
"typescript": "^
|
|
33
|
+
"@types/node": "^25.0.10",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
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"
|
|
29
45
|
},
|
|
30
46
|
"exports": {
|
|
31
47
|
".": {
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
48
|
+
"import": {
|
|
49
|
+
"types": "./dist/index.d.mts",
|
|
50
|
+
"default": "./dist/index.mjs"
|
|
51
|
+
},
|
|
52
|
+
"require": {
|
|
53
|
+
"types": "./dist/index.d.cts",
|
|
54
|
+
"default": "./dist/index.cjs"
|
|
55
|
+
}
|
|
35
56
|
}
|
|
36
57
|
},
|
|
37
58
|
"publishConfig": {
|