diff options
| author | Fridon <[email protected]> | 2022-06-20 17:04:00 +0400 |
|---|---|---|
| committer | Fridon <[email protected]> | 2022-06-20 17:04:00 +0400 |
| commit | 82a2f864b8f676b575d075151d7a2db0fe663b51 (patch) | |
| tree | 6198ec3cd7e2b96d08f631ed78c8a90d6d1ea362 | |
| parent | 5a801fa98fb052d9f4bbd90f497f076e9c5425c1 (diff) | |
| download | countryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.tar.xz countryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.zip | |
store and read data in ~/.cache/countries
| -rw-r--r-- | countries.ts | 26 | ||||
| -rw-r--r-- | environment/environment.ts | 4 | ||||
| -rw-r--r-- | main.ts | 3 | ||||
| -rw-r--r-- | util/cache.ts | 39 |
4 files changed, 63 insertions, 9 deletions
diff --git a/countries.ts b/countries.ts index 79fe2f7..895a93f 100644 --- a/countries.ts +++ b/countries.ts @@ -1,37 +1,47 @@ import * as nano from "https://deno.land/x/[email protected]/mod.ts"; import { environment } from "./environment/environment.ts"; import { Country } from "./models/country.model.ts"; +import { Cache } from "./util/cache.ts"; export class Countries { list: Country[] = []; names: string[] = []; query = environment.queries; + constructor(private cache: Cache) {} + async sync(config?: { force: boolean }): Promise<Country[]> { - const lastSynced = localStorage.getItem("lastSynced"); - const savedCountries = localStorage.getItem("countries"); + const lastSynced = this.cache.readTxt("last-synced"); + const savedCountries = this.cache.readJson("countries") as + | Country[] + | undefined; const week = environment.syncInterval * 23 * 60 * 60 * 1000; - if ( + + const shouldSync = !savedCountries || !lastSynced || config?.force || - Date.now() - Number(lastSynced) > week - ) { + Date.now() - Number(lastSynced) > week; + + if (shouldSync) { console.log( nano.cyan("Syncronizing countries database..."), config?.force ? "" : `\nThis will only happen every ${environment.syncInterval} days` ); + // Fetch and parse countries data from API const response = await fetch(environment.baseUrl + this.query); const countries = (await response.json()) as Country[]; + this.list = countries; - localStorage.setItem("countries", JSON.stringify(countries)); - localStorage.setItem("lastSynced", JSON.stringify(Date.now())); + this.cache.saveJson("countries", countries); + this.cache.saveTxt("last-synced", JSON.stringify(Date.now())); + console.log("Synced successfully"); } else { - this.list = JSON.parse(savedCountries); + this.list = savedCountries; } this.names = this.list.map((c) => c.name.common); return this.list; diff --git a/environment/environment.ts b/environment/environment.ts index 15d71f3..0e29225 100644 --- a/environment/environment.ts +++ b/environment/environment.ts @@ -1,6 +1,10 @@ +import home_dir from "https://deno.land/x/dir/home_dir/mod.ts"; +import { join } from "https://deno.land/[email protected]/path/mod.ts"; + export const environment = { baseUrl: "https://restcountries.com/v3.1/", syncInterval: 7, + cacheDir: join(home_dir() as string, ".cache", "countryfetch"), queries: "all?fields=" + "name," + @@ -1,6 +1,7 @@ import { Countries } from "./countries.ts"; +import { Cache } from "./util/cache.ts"; -const countries = new Countries(); +const countries = new Countries(new Cache()); await countries.sync(); diff --git a/util/cache.ts b/util/cache.ts new file mode 100644 index 0000000..3a0c5d4 --- /dev/null +++ b/util/cache.ts @@ -0,0 +1,39 @@ +import { join } from "https://deno.land/[email protected]/path/mod.ts"; +import { ensureDirSync } from "https://deno.land/[email protected]/fs/mod.ts"; +import { environment } from "../environment/environment.ts"; + +export class Cache { + path = environment.cacheDir; + + public saveJson(name: string, data: {}) { + ensureDirSync(this.path); + + Deno.writeTextFileSync( + join(this.path, `${name}.json`), + JSON.stringify(data) + ); + } + + public saveTxt(name: string, value: string) { + ensureDirSync(this.path); + Deno.writeTextFileSync(join(this.path, `${name}.txt`), value); + } + + public readJson(name: string): {} | [] | undefined { + let data; + try { + data = Deno.readTextFileSync(join(this.path, `${name}.json`)); + } catch (err) { + return undefined; + } + return JSON.parse(data); + } + + public readTxt(name: string) { + try { + return Deno.readTextFileSync(join(this.path, `${name}.txt`)); + } catch (err) { + return undefined; + } + } +} |
