From a367e92bcf1d883db6c31c67d7fb47668f4da6b7 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sat, 5 Feb 2022 02:28:21 +0300 Subject: [PATCH] Create api.js --- scripts/core/api.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 scripts/core/api.js diff --git a/scripts/core/api.js b/scripts/core/api.js new file mode 100644 index 000000000..4f82ae518 --- /dev/null +++ b/scripts/core/api.js @@ -0,0 +1,26 @@ +const _ = require('lodash') +const file = require('./file') + +const DATA_DIR = process.env.DATA_DIR || './scripts/data' + +class API { + constructor(filepath) { + this.filepath = file.resolve(filepath) + } + + async load() { + const data = await file.read(this.filepath) + this.collection = JSON.parse(data) + } + + find(query) { + return _.find(this.collection, query) + } +} + +const api = {} + +api.channels = new API(`${DATA_DIR}/channels.json`) +api.countries = new API(`${DATA_DIR}/countries.json`) + +module.exports = api