database_downloader.js 826 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { type_manager } from "./functions.js";
  2. import { database } from "./database.js";
  3. class database_downloader {
  4. #url;
  5. #database;
  6. constructor(url) {
  7. if (!type_manager.is_string(url)) {
  8. throw "Url to the database must be an string.";
  9. }
  10. this.#url = url;
  11. }
  12. async download() {
  13. const response = await fetch(this.#url);
  14. if (!response.ok) {
  15. throw "Can not download database. Error: " + response.status;
  16. }
  17. if (response.headers.get("content-type") !== "application/json") {
  18. throw "Response content type of the database must be an json.";
  19. }
  20. this.#database = new database(await response.json());
  21. }
  22. get result() {
  23. return this.#database;
  24. }
  25. }
  26. export { database_downloader };