|
Lines 1-106
Link Here
|
| 1 |
import { setError } from "../messages"; |
|
|
| 2 |
|
| 3 |
//TODO: all of these functions should be deleted and reimplemented in the components using ERMAPIClient |
| 4 |
|
| 5 |
const _fetchPackage = function (apiUrl, package_id) { |
| 6 |
if (!package_id) return; |
| 7 |
return myFetch(apiUrl, { |
| 8 |
headers: { |
| 9 |
"x-koha-embed": |
| 10 |
"package_agreements,package_agreements.agreement,resources+count,vendor", |
| 11 |
}, |
| 12 |
}); |
| 13 |
}; |
| 14 |
export const fetchEBSCOPackage = function (package_id) { |
| 15 |
const apiUrl = "/api/v1/erm/eholdings/ebsco/packages/" + package_id; |
| 16 |
return _fetchPackage(apiUrl, package_id); |
| 17 |
}; |
| 18 |
|
| 19 |
export const _fetchPackages = function (apiUrl) { |
| 20 |
return myFetch(apiUrl, { |
| 21 |
headers: { |
| 22 |
"x-koha-embed": "resources+count,vendor.name", |
| 23 |
}, |
| 24 |
}); |
| 25 |
}; |
| 26 |
export const fetchEBSCOPackages = function () { |
| 27 |
const apiUrl = "/api/v1/erm/eholdings/ebsco/packages"; |
| 28 |
return _fetchPackages(apiUrl); |
| 29 |
}; |
| 30 |
|
| 31 |
export const _fetchTitle = function (apiUrl, title_id) { |
| 32 |
if (!title_id) return; |
| 33 |
return myFetch(apiUrl, { |
| 34 |
headers: { |
| 35 |
"x-koha-embed": "resources,resources.package", |
| 36 |
}, |
| 37 |
}); |
| 38 |
}; |
| 39 |
export const fetchEBSCOTitle = function (title_id) { |
| 40 |
const apiUrl = "/api/v1/erm/eholdings/ebsco/titles/" + title_id; |
| 41 |
return _fetchTitle(apiUrl, title_id); |
| 42 |
}; |
| 43 |
|
| 44 |
export const _fetchResource = function (apiUrl, resource_id) { |
| 45 |
if (!resource_id) return; |
| 46 |
return myFetch(apiUrl, { |
| 47 |
headers: { |
| 48 |
"x-koha-embed": "title,package,vendor", |
| 49 |
}, |
| 50 |
}); |
| 51 |
}; |
| 52 |
export const fetchEBSCOResource = function (resource_id) { |
| 53 |
const apiUrl = "/api/v1/erm/eholdings/ebsco/resources/" + resource_id; |
| 54 |
return _fetchResource(apiUrl, resource_id); |
| 55 |
}; |
| 56 |
|
| 57 |
export const _fetchResources = async function (apiUrl) { |
| 58 |
return await myFetch(apiUrl); |
| 59 |
}; |
| 60 |
|
| 61 |
export const fetchEBSCOResources = function () { |
| 62 |
const apiUrl = "/api/v1/erm/eholdings/ebsco/resources"; |
| 63 |
return _fetchResources(apiUrl); |
| 64 |
}; |
| 65 |
|
| 66 |
export const myFetch = async function (url, options, return_response) { |
| 67 |
let r; |
| 68 |
await fetch(url, options || {}) |
| 69 |
.then((response) => checkError(response, return_response)) |
| 70 |
.then( |
| 71 |
(result) => { |
| 72 |
r = result; |
| 73 |
}, |
| 74 |
(error) => { |
| 75 |
setError(error.toString()); |
| 76 |
} |
| 77 |
) |
| 78 |
.catch((error) => { |
| 79 |
setError(error); |
| 80 |
}); |
| 81 |
return r; |
| 82 |
}; |
| 83 |
export const myFetchTotal = async function (url, options) { |
| 84 |
let r; |
| 85 |
await myFetch(url, options, 1).then( |
| 86 |
(response) => { |
| 87 |
if (response) { |
| 88 |
r = response.headers.get("X-Total-Count"); |
| 89 |
} |
| 90 |
}, |
| 91 |
(error) => { |
| 92 |
setError(error.toString()); |
| 93 |
} |
| 94 |
); |
| 95 |
return r; |
| 96 |
}; |
| 97 |
|
| 98 |
export const checkError = function (response, return_response) { |
| 99 |
if (response.status >= 200 && response.status <= 299) { |
| 100 |
return return_response ? response : response.json(); |
| 101 |
} else { |
| 102 |
console.log("Server returned an error:"); |
| 103 |
console.log(response); |
| 104 |
throw Error("%s (%s)".format(response.statusText, response.status)); |
| 105 |
} |
| 106 |
}; |
| 107 |
- |