src: Trying to implement module factory

This commit is contained in:
Samuel Ortion 2022-08-26 10:48:56 +02:00
parent 44b71c5296
commit da431878dd
2 changed files with 103 additions and 103 deletions

View File

@ -1,13 +1,14 @@
const axios = require('axios'); const axios = require('axios');
const BASE_URL = "https://api.ebird.org/v2" module.exports = function (EBIRD_API_KEY) {
const API_KEY = process.env.EBIRD_API_KEY; const BASE_URL = "https://api.ebird.org/v2"
let key = EBIRD_API_KEY;
function query(endpoint, params) { function query(endpoint, params) {
let data = undefined; let data = undefined;
axios.get(`${BASE_URL}/${endpoint}`, { axios.get(`${BASE_URL}/${endpoint}`, {
headers: { headers: {
'X-eBirdApiToken': API_KEY 'X-eBirdApiToken': key
}, },
params: { params: {
...params ...params
@ -19,15 +20,15 @@ function query(endpoint, params) {
console.error(error); console.error(error);
}); });
return data; return data;
} }
function get(endpoint) { function get(endpoint) {
return params => { return params => {
query(endpoint, params); query(endpoint, params);
} }
} }
const ebird = { const api = {
data: { data: {
obs: { obs: {
recent: { recent: {
@ -119,9 +120,8 @@ const ebird = {
sppgroup: (speciesGrouping) => get(`ref/sppgroup/${speciesGrouping}`), sppgroup: (speciesGrouping) => get(`ref/sppgroup/${speciesGrouping}`),
region: { region: {
info: (regionCode) => get(`ref/region/${regionCode}`), info: (regionCode) => get(`ref/region/${regionCode}`),
list: (regionType, parentRegionCode) => get(`ref/region/list/${regionTypie}/${parentRegionCode}`), list: (regionType, parentRegionCode) => get(`ref/region/list/${regionType}/${parentRegionCode}`),
}, },
}, },
} }
};
module.exports = ebird;

View File

@ -2,8 +2,8 @@ const ebird = require('../src/index');
require('dotenv').config; require('dotenv').config;
test('/data/obs/${regionCode}/recent for location France', () => { test('/data/obs/${regionCode}/recent for location France', () => {
let data = ebird.obs.recent.in('FR'); let data = ebird.data.obs.recent.in('FR')();
expect(data.length).toBeGreaterThan(1); console.log(data);
}); });