-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbase.js
50 lines (44 loc) · 1.6 KB
/
base.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @ts-check
/**
* Check the dataset base URL.
* Some hints are provided if the dataset base URL is not correctly formatted.
* If a value is empty, an error is thrown.
*
* @param {{warn: Function }} logger - The logger instance
* @param {string} datasetBaseUrl - The dataset base URL
* @returns {true} The dataset base URL as an array
*/
export const checkSingleDatasetBaseUrl = (logger, datasetBaseUrl) => {
if (typeof datasetBaseUrl !== 'string') {
throw new Error('The datasetBaseUrl must be a string')
}
if (!datasetBaseUrl) {
throw new Error("Value for 'datasetBaseUrl' is missing")
}
if (!datasetBaseUrl.endsWith('/')) {
logger.warn(`The value for 'datasetBaseUrl' should usually end with a '/' ; it is not the case for '${datasetBaseUrl}'`)
}
return true
}
/**
* Check the dataset base URL, and make sure it returns an array.
* Some hints are provided if the dataset base URL is not correctly formatted.
* If the dataset base URL is an array, each value is checked.
* If a value is empty, then an error is thrown.
*
* @param {{warn: Function }} logger - The logger instance
* @param {string | string[]} datasetBaseUrl - The dataset base URL
* @returns {string[]} The dataset base URL as an array
*/
export const checkDatasetBaseUrl = (logger, datasetBaseUrl) => {
if (!datasetBaseUrl) {
throw new Error('No datasetBaseUrl provided')
}
if (Array.isArray(datasetBaseUrl)) {
datasetBaseUrl.forEach((value) => checkSingleDatasetBaseUrl(logger, value))
return datasetBaseUrl
} else {
checkSingleDatasetBaseUrl(logger, datasetBaseUrl)
return [datasetBaseUrl]
}
}