openbrewerydb-node
v1.0.2
Published
Node.js wrapper for Open Brewery DB API
Downloads
3
Maintainers
Readme
openbrewerydb-node
Node.js wrapper for Open Brewery DB API Credit for the API goes to Chris J. Mears
Installation
npm i openbrewerydb-node
Importing
import { getAll } from "openbrewerydb-node";
Or
const { getAll } = require("openbrewerydb-node");
Building
openbrewerydb-node uses newer Node.js syntax and requires to be build first if you're running the examples locally.
After checking out the repository run npm run build
to create the distribution folder.
Usage
The data fetching functions in this library return a promise. This means you can either use regular JavaScript promises or take advantage of async/await syntax. The promise returns a json string consisting of an array of json objects.
getAll().then((response) => console.log(response)).catch((err) => console.log(err));
Or
try {
const data = await getAll();
console.log(data);
} catch (err) {
console.log(err);
}
Functions
Note that the API paginates requests. Therefore each function takes a page number as a parameter with a default value of 1
getAll(): Return all breweries Ex: getAll(); getAll(2);
searchPhrase(): Searches database using particular phrase Ex: searchPhrase("new_york");
filter(): Filters database by creating complex queries Ex: filter(["california", "arizona"], [], ["patio"], ["micro"], 2);
Functions are documented inside of the lib folder as well
Fetching multiple pages
const getAllCABreweries = async () => {
const breweries = [];
let page = 1;
for (;;) {
const data = await filter(["california"], [], [], [], page);
const dataJson = JSON.parse(data);
if (!dataJson.length) {
break;
} else {
breweries.push(dataJson);
}
}
const flattenedBreweries = breweries.flat();
};