@cef-ebsi/ebsi-uri 0.1.0 → 0.1.1-alpha.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/dist/index.d.ts +16 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -44
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +102 -97
package/dist/index.d.ts
CHANGED
|
@@ -2,30 +2,32 @@ export declare const SCHEME = "ebsi";
|
|
|
2
2
|
export declare const PRODUCTION_NETWORK = "production";
|
|
3
3
|
export declare const NETWORKS: readonly ["production", "preprod", "conformance", "pilot", "test"];
|
|
4
4
|
export declare const HOSTS: {
|
|
5
|
-
readonly test: "api-test.ebsi.eu";
|
|
6
|
-
readonly pilot: "api-pilot.ebsi.";
|
|
7
5
|
readonly conformance: "api-conformance.ebsi.eu";
|
|
6
|
+
readonly pilot: "api-pilot.ebsi.";
|
|
8
7
|
readonly preprod: "api-preprod.ebsi.";
|
|
9
8
|
readonly production: "api.ebsi.";
|
|
9
|
+
readonly test: "api-test.ebsi.eu";
|
|
10
10
|
};
|
|
11
|
-
export
|
|
12
|
-
export type EBSIURIComponents = {
|
|
11
|
+
export interface EBSIURIComponents {
|
|
13
12
|
network?: Network;
|
|
14
|
-
service?: string;
|
|
15
13
|
resource?: string;
|
|
16
|
-
|
|
14
|
+
service?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface IFromUrlOptions {
|
|
17
|
+
services?: SupportedServices;
|
|
18
|
+
}
|
|
19
|
+
export interface IToUrlOptions {
|
|
20
|
+
readonly hosts: Partial<Record<Network, string>>;
|
|
21
|
+
readonly services: SupportedServices;
|
|
22
|
+
}
|
|
23
|
+
export type Network = (typeof NETWORKS)[number];
|
|
17
24
|
export type SupportedServices = Record<string, string>;
|
|
25
|
+
export declare function fromUrl(url: string, options?: IFromUrlOptions): string;
|
|
18
26
|
export declare function parse(uriString: string): {
|
|
19
27
|
network: "production" | "preprod" | "conformance" | "pilot" | "test";
|
|
20
|
-
service: string;
|
|
21
28
|
resource: string;
|
|
29
|
+
service: string;
|
|
22
30
|
};
|
|
23
31
|
export declare function serialize(components: EBSIURIComponents): string;
|
|
24
|
-
export declare function toUrl(uriString: string, options:
|
|
25
|
-
readonly services: SupportedServices;
|
|
26
|
-
readonly hosts: Partial<Record<Network, string>>;
|
|
27
|
-
}): string;
|
|
28
|
-
export declare function fromUrl(url: string, options?: {
|
|
29
|
-
services?: SupportedServices;
|
|
30
|
-
}): string;
|
|
32
|
+
export declare function toUrl(uriString: string, options: IToUrlOptions): string;
|
|
31
33
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,SAAS,CAAC;AAE7B,eAAO,MAAM,kBAAkB,eAAe,CAAC;AAE/C,eAAO,MAAM,QAAQ,oEAMX,CAAC;AAEX,eAAO,MAAM,KAAK;;;;;;CAM0B,CAAC;AAE7C,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,SAAS,CAAC;AAE7B,eAAO,MAAM,kBAAkB,eAAe,CAAC;AAE/C,eAAO,MAAM,QAAQ,oEAMX,CAAC;AAEX,eAAO,MAAM,KAAK;;;;;;CAM0B,CAAC;AAE7C,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAGD,MAAM,WAAW,eAAe;IAI9B,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAC9B;AAGD,MAAM,WAAW,aAAa;IAI5B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAKjD,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;CACtC;AAED,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhD,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAavD,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,UAsD7D;AAmBD,wBAAgB,KAAK,CAAC,SAAS,EAAE,MAAM;;;;EAgCtC;AAgBD,wBAAgB,SAAS,CAAC,UAAU,EAAE,iBAAiB,UAOtD;AAgBD,wBAAgB,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,CAmBvE"}
|
package/dist/index.js
CHANGED
|
@@ -8,14 +8,49 @@ export const NETWORKS = [
|
|
|
8
8
|
"test",
|
|
9
9
|
];
|
|
10
10
|
export const HOSTS = {
|
|
11
|
-
test: "api-test.ebsi.eu",
|
|
12
|
-
pilot: "api-pilot.ebsi.",
|
|
13
11
|
conformance: "api-conformance.ebsi.eu",
|
|
12
|
+
pilot: "api-pilot.ebsi.",
|
|
14
13
|
preprod: "api-preprod.ebsi.",
|
|
15
14
|
production: "api.ebsi.",
|
|
15
|
+
test: "api-test.ebsi.eu",
|
|
16
16
|
};
|
|
17
|
-
function
|
|
18
|
-
|
|
17
|
+
export function fromUrl(url, options) {
|
|
18
|
+
let urlObject;
|
|
19
|
+
try {
|
|
20
|
+
urlObject = new URL(url);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new Error("Invalid URL");
|
|
24
|
+
}
|
|
25
|
+
const { hash, host, pathname, search } = urlObject;
|
|
26
|
+
const network = Object.keys(HOSTS).find((n) => host.includes(HOSTS[n]));
|
|
27
|
+
if (!network) {
|
|
28
|
+
throw new Error(`No network configured for host ${host}`);
|
|
29
|
+
}
|
|
30
|
+
const [service, serviceVersion, ...rest] = pathname
|
|
31
|
+
.split("/")
|
|
32
|
+
.filter(Boolean);
|
|
33
|
+
const resource = rest.join("/");
|
|
34
|
+
if (!service) {
|
|
35
|
+
throw new Error(`No service found in EBSI URI ${url}`);
|
|
36
|
+
}
|
|
37
|
+
if (!serviceVersion) {
|
|
38
|
+
throw new Error(`No service version found in EBSI URI ${url}`);
|
|
39
|
+
}
|
|
40
|
+
if (options?.services) {
|
|
41
|
+
const supportedServiceVersion = options.services[service];
|
|
42
|
+
if (!supportedServiceVersion) {
|
|
43
|
+
throw new Error(`Unsupported service '${service}'`);
|
|
44
|
+
}
|
|
45
|
+
if (serviceVersion !== supportedServiceVersion) {
|
|
46
|
+
throw new Error(`Unsupported version '${serviceVersion}' of service '${service}'`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return serialize({
|
|
50
|
+
network,
|
|
51
|
+
resource: `/${resource}${search}${hash}`,
|
|
52
|
+
service,
|
|
53
|
+
});
|
|
19
54
|
}
|
|
20
55
|
export function parse(uriString) {
|
|
21
56
|
const [scheme, firstComponent, secondComponent, ...rest] = uriString.split(":");
|
|
@@ -35,17 +70,17 @@ export function parse(uriString) {
|
|
|
35
70
|
}
|
|
36
71
|
return {
|
|
37
72
|
network,
|
|
38
|
-
service,
|
|
39
73
|
resource,
|
|
74
|
+
service,
|
|
40
75
|
};
|
|
41
76
|
}
|
|
42
77
|
export function serialize(components) {
|
|
43
|
-
const { network,
|
|
78
|
+
const { network, resource, service } = components;
|
|
44
79
|
const networkName = network === PRODUCTION_NETWORK ? "" : `${network}:`;
|
|
45
80
|
return `${SCHEME}:${networkName}${service}:${resource}`;
|
|
46
81
|
}
|
|
47
82
|
export function toUrl(uriString, options) {
|
|
48
|
-
const { network,
|
|
83
|
+
const { network, resource, service } = parse(uriString);
|
|
49
84
|
const host = options.hosts[network];
|
|
50
85
|
if (!host) {
|
|
51
86
|
throw new Error(`No host for network ${network}`);
|
|
@@ -56,42 +91,7 @@ export function toUrl(uriString, options) {
|
|
|
56
91
|
}
|
|
57
92
|
return `https://${host}/${service}/${serviceVersion}${resource}`;
|
|
58
93
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
urlObject = new URL(url);
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
throw new Error("Invalid URL");
|
|
66
|
-
}
|
|
67
|
-
const { host, pathname, search, hash } = urlObject;
|
|
68
|
-
const network = Object.keys(HOSTS).find((n) => host.includes(HOSTS[n]));
|
|
69
|
-
if (!network) {
|
|
70
|
-
throw new Error(`No network configured for host ${host}`);
|
|
71
|
-
}
|
|
72
|
-
const [service, serviceVersion, ...rest] = pathname
|
|
73
|
-
.split("/")
|
|
74
|
-
.filter(Boolean);
|
|
75
|
-
const resource = rest.join("/");
|
|
76
|
-
if (!service) {
|
|
77
|
-
throw new Error(`No service found in EBSI URI ${url}`);
|
|
78
|
-
}
|
|
79
|
-
if (!serviceVersion) {
|
|
80
|
-
throw new Error(`No service version found in EBSI URI ${url}`);
|
|
81
|
-
}
|
|
82
|
-
if (options?.services) {
|
|
83
|
-
const supportedServiceVersion = options.services[service];
|
|
84
|
-
if (!supportedServiceVersion) {
|
|
85
|
-
throw new Error(`Unsupported service '${service}'`);
|
|
86
|
-
}
|
|
87
|
-
if (serviceVersion !== supportedServiceVersion) {
|
|
88
|
-
throw new Error(`Unsupported version '${serviceVersion}' of service '${service}'`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return serialize({
|
|
92
|
-
network,
|
|
93
|
-
service,
|
|
94
|
-
resource: `/${resource}${search}${hash}`,
|
|
95
|
-
});
|
|
94
|
+
function isNetwork(network) {
|
|
95
|
+
return network !== undefined && NETWORKS.includes(network);
|
|
96
96
|
}
|
|
97
97
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.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C,CAAC;IAGxD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEpC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,uBAAuB,OAAO,EAAE,CAAC,CAAC;IACpD,CAAC;IAGD,MAAM,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAEjD,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,wBAAwB,OAAO,EAAE,CAAC,CAAC;IACrD,CAAC;IAGD,OAAO,WAAW,IAAI,IAAI,OAAO,IAAI,cAAc,GAAG,QAAQ,EAAE,CAAC;AACnE,CAAC;AAOD,SAAS,SAAS,CAAC,OAA2B;IAC5C,OAAO,OAAO,KAAK,SAAS,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAkB,CAAC,CAAC;AACxE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cef-ebsi/ebsi-uri",
|
|
3
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.1.1-alpha.0",
|
|
4
4
|
"description": "EBSI URIs parser and serializer",
|
|
5
5
|
"license": "EUPL-1.2",
|
|
6
6
|
"publishConfig": {
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"test:e2e": "echo \"Info: there's no e2e tests for @cef-ebsi/ebsi-uri\" && exit 0",
|
|
31
31
|
"test:ci": "yarn test:unit --coverage --reporter=default"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "4900bd20f6d6ee78709b0f7f04a5f4e4f11e0b67"
|
|
34
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,25 +11,109 @@ export const NETWORKS = [
|
|
|
11
11
|
] as const;
|
|
12
12
|
|
|
13
13
|
export const HOSTS = {
|
|
14
|
-
test: "api-test.ebsi.eu",
|
|
15
|
-
pilot: "api-pilot.ebsi.",
|
|
16
14
|
conformance: "api-conformance.ebsi.eu",
|
|
15
|
+
pilot: "api-pilot.ebsi.",
|
|
17
16
|
preprod: "api-preprod.ebsi.",
|
|
18
17
|
production: "api.ebsi.",
|
|
18
|
+
test: "api-test.ebsi.eu",
|
|
19
19
|
} as const satisfies Record<Network, string>;
|
|
20
20
|
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
export type EBSIURIComponents = {
|
|
21
|
+
export interface EBSIURIComponents {
|
|
24
22
|
network?: Network;
|
|
25
|
-
service?: string;
|
|
26
23
|
resource?: string;
|
|
27
|
-
|
|
24
|
+
service?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Options for the `fromUrl()` function */
|
|
28
|
+
export interface IFromUrlOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Map of supported services and their versions
|
|
31
|
+
*/
|
|
32
|
+
services?: SupportedServices;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Options for the `toUrl()` function */
|
|
36
|
+
export interface IToUrlOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Map of supported networks and their hosts
|
|
39
|
+
*/
|
|
40
|
+
readonly hosts: Partial<Record<Network, string>>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Map of supported services and their versions
|
|
44
|
+
*/
|
|
45
|
+
readonly services: SupportedServices;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type Network = (typeof NETWORKS)[number];
|
|
28
49
|
|
|
29
50
|
export type SupportedServices = Record<string, string>;
|
|
30
51
|
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Transform a URL into an EBSI URI.
|
|
54
|
+
* @example
|
|
55
|
+
* ```js
|
|
56
|
+
* fromUrl("https://api-test.ebsi.eu/did-registry/v4/identifiers/did:ebsi:zx23VhpbGYeqF2MRJk3HzPw")
|
|
57
|
+
* ```
|
|
58
|
+
* returns `ebsi:test:did-registry:/identifiers/did:ebsi:zx23VhpbGYeqF2MRJk3HzPw`
|
|
59
|
+
* @param url - URL to transform
|
|
60
|
+
* @param options - Supported services
|
|
61
|
+
* @returns EBSI URI
|
|
62
|
+
*/
|
|
63
|
+
export function fromUrl(url: string, options?: IFromUrlOptions) {
|
|
64
|
+
// Parse URL
|
|
65
|
+
let urlObject: URL;
|
|
66
|
+
try {
|
|
67
|
+
urlObject = new URL(url);
|
|
68
|
+
} catch {
|
|
69
|
+
throw new Error("Invalid URL");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const { hash, host, pathname, search } = urlObject;
|
|
73
|
+
|
|
74
|
+
// Identify network
|
|
75
|
+
const network = (Object.keys(HOSTS) as (keyof typeof HOSTS)[]).find((n) =>
|
|
76
|
+
host.includes(HOSTS[n]),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (!network) {
|
|
80
|
+
throw new Error(`No network configured for host ${host}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Extract service and resource from pathname: /<service>/<serviceVersion>/<resource>
|
|
84
|
+
const [service, serviceVersion, ...rest] = pathname
|
|
85
|
+
.split("/")
|
|
86
|
+
.filter(Boolean); // Remove empty path segments
|
|
87
|
+
const resource = rest.join("/");
|
|
88
|
+
|
|
89
|
+
if (!service) {
|
|
90
|
+
throw new Error(`No service found in EBSI URI ${url}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!serviceVersion) {
|
|
94
|
+
throw new Error(`No service version found in EBSI URI ${url}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check if the service and its version are supported
|
|
98
|
+
if (options?.services) {
|
|
99
|
+
const supportedServiceVersion = options.services[service];
|
|
100
|
+
|
|
101
|
+
if (!supportedServiceVersion) {
|
|
102
|
+
throw new Error(`Unsupported service '${service}'`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (serviceVersion !== supportedServiceVersion) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Unsupported version '${serviceVersion}' of service '${service}'`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return serialize({
|
|
113
|
+
network,
|
|
114
|
+
resource: `/${resource}${search}${hash}`,
|
|
115
|
+
service,
|
|
116
|
+
});
|
|
33
117
|
}
|
|
34
118
|
|
|
35
119
|
/**
|
|
@@ -78,8 +162,8 @@ export function parse(uriString: string) {
|
|
|
78
162
|
// Return EBSI URI components
|
|
79
163
|
return {
|
|
80
164
|
network,
|
|
81
|
-
service,
|
|
82
165
|
resource,
|
|
166
|
+
service,
|
|
83
167
|
} satisfies EBSIURIComponents;
|
|
84
168
|
}
|
|
85
169
|
|
|
@@ -98,7 +182,7 @@ export function parse(uriString: string) {
|
|
|
98
182
|
* @returns EBSI URI
|
|
99
183
|
*/
|
|
100
184
|
export function serialize(components: EBSIURIComponents) {
|
|
101
|
-
const { network,
|
|
185
|
+
const { network, resource, service } = components;
|
|
102
186
|
|
|
103
187
|
const networkName = network === PRODUCTION_NETWORK ? "" : `${network}:`;
|
|
104
188
|
|
|
@@ -120,21 +204,8 @@ export function serialize(components: EBSIURIComponents) {
|
|
|
120
204
|
* @param options - Supported services and hosts
|
|
121
205
|
* @returns URL
|
|
122
206
|
*/
|
|
123
|
-
export function toUrl(
|
|
124
|
-
|
|
125
|
-
options: {
|
|
126
|
-
/**
|
|
127
|
-
* Map of supported services and their versions
|
|
128
|
-
*/
|
|
129
|
-
readonly services: SupportedServices;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Map of supported networks and their hosts
|
|
133
|
-
*/
|
|
134
|
-
readonly hosts: Partial<Record<Network, string>>;
|
|
135
|
-
},
|
|
136
|
-
): string {
|
|
137
|
-
const { network, service, resource } = parse(uriString);
|
|
207
|
+
export function toUrl(uriString: string, options: IToUrlOptions): string {
|
|
208
|
+
const { network, resource, service } = parse(uriString);
|
|
138
209
|
|
|
139
210
|
// Replace host
|
|
140
211
|
const host = options.hosts[network];
|
|
@@ -155,76 +226,10 @@ export function toUrl(
|
|
|
155
226
|
}
|
|
156
227
|
|
|
157
228
|
/**
|
|
158
|
-
*
|
|
159
|
-
* @
|
|
160
|
-
*
|
|
161
|
-
* fromUrl("https://api-test.ebsi.eu/did-registry/v4/identifiers/did:ebsi:zx23VhpbGYeqF2MRJk3HzPw")
|
|
162
|
-
* ```
|
|
163
|
-
* returns `ebsi:test:did-registry:/identifiers/did:ebsi:zx23VhpbGYeqF2MRJk3HzPw`
|
|
164
|
-
* @param url - URL to transform
|
|
165
|
-
* @param options - Supported services
|
|
166
|
-
* @returns EBSI URI
|
|
229
|
+
* Typeguard that checks if the given string is a supported network.
|
|
230
|
+
* @param network - String to check
|
|
231
|
+
* @returns - network is Network
|
|
167
232
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
options?: {
|
|
171
|
-
/**
|
|
172
|
-
* Map of supported services and their versions
|
|
173
|
-
*/
|
|
174
|
-
services?: SupportedServices;
|
|
175
|
-
},
|
|
176
|
-
) {
|
|
177
|
-
// Parse URL
|
|
178
|
-
let urlObject: URL;
|
|
179
|
-
try {
|
|
180
|
-
urlObject = new URL(url);
|
|
181
|
-
} catch {
|
|
182
|
-
throw new Error("Invalid URL");
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const { host, pathname, search, hash } = urlObject;
|
|
186
|
-
|
|
187
|
-
// Identify network
|
|
188
|
-
const network = (Object.keys(HOSTS) as (keyof typeof HOSTS)[]).find((n) =>
|
|
189
|
-
host.includes(HOSTS[n]),
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
if (!network) {
|
|
193
|
-
throw new Error(`No network configured for host ${host}`);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Extract service and resource from pathname: /<service>/<serviceVersion>/<resource>
|
|
197
|
-
const [service, serviceVersion, ...rest] = pathname
|
|
198
|
-
.split("/")
|
|
199
|
-
.filter(Boolean); // Remove empty path segments
|
|
200
|
-
const resource = rest.join("/");
|
|
201
|
-
|
|
202
|
-
if (!service) {
|
|
203
|
-
throw new Error(`No service found in EBSI URI ${url}`);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (!serviceVersion) {
|
|
207
|
-
throw new Error(`No service version found in EBSI URI ${url}`);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Check if the service and its version are supported
|
|
211
|
-
if (options?.services) {
|
|
212
|
-
const supportedServiceVersion = options.services[service];
|
|
213
|
-
|
|
214
|
-
if (!supportedServiceVersion) {
|
|
215
|
-
throw new Error(`Unsupported service '${service}'`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (serviceVersion !== supportedServiceVersion) {
|
|
219
|
-
throw new Error(
|
|
220
|
-
`Unsupported version '${serviceVersion}' of service '${service}'`,
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return serialize({
|
|
226
|
-
network,
|
|
227
|
-
service,
|
|
228
|
-
resource: `/${resource}${search}${hash}`,
|
|
229
|
-
});
|
|
233
|
+
function isNetwork(network: string | undefined): network is Network {
|
|
234
|
+
return network !== undefined && NETWORKS.includes(network as Network);
|
|
230
235
|
}
|