src: Trying to implement module factory
This commit is contained in:
parent
44b71c5296
commit
da431878dd
24
src/index.js
24
src/index.js
@ -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;
|
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user