forked from openmrs/openmrs-esm-admin-tools
/
utils.ts
27 lines (24 loc) · 886 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const NUMBER_OF_SLASHES_AFTER_BASE_URL = 5;
/*
* This checks if collection version has been passed to subscription url by checking number of forward slashes after base url
* If the number is 5, such as with https://api.openconceptlab.org/users/username/collections/collectionname/v1.0
* that means collection version was passed and isVersionDefinedInUrl() will return true
* Also returns false if the string is not a valid URL
*/
export const isVersionDefinedInUrl = (subscriptionUrl: string) => {
if (subscriptionUrl.endsWith('/')) {
subscriptionUrl = subscriptionUrl.substring(0, subscriptionUrl.lastIndexOf('/'));
}
let url;
try {
url = new URL(subscriptionUrl);
} catch (e) {
return false;
}
let count = url.pathname.match(/\//g)?.length ?? 0;
if (count == NUMBER_OF_SLASHES_AFTER_BASE_URL) {
return true;
} else {
return false;
}
};