Create api.js

This commit is contained in:
Aleksandr Statciuk 2022-02-05 02:28:21 +03:00
parent 039fe7d01c
commit a367e92bcf
1 changed files with 26 additions and 0 deletions

26
scripts/core/api.js Normal file
View File

@ -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