UNPKG

greip-node

Version:

Official Node.js library for Greip API

81 lines 2.28 kB
import axios from "axios"; export const BASE_URL = "https://greipapi.com"; export const availableGeoIPParams = [ "location", "security", "timezone", "currency", "device" ]; export const availableLanguages = [ "EN", "AR", "DE", "FR", "ES", "JA", "ZH", "RU" ]; export const availableFormats = ["JSON", "XML", "CSV", "Newline"]; export const availableCountryParams = [ "language", "flag", "currency", "timezone" ]; export const serialize = (obj) => { const str = []; for (const p in obj) if (obj.hasOwnProperty(p)) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); }; export const makeHttpRquest = (endpoint, options, callback, token) => { options.source = "JS-Package"; axios .get(BASE_URL + "/" + endpoint + "?" + serialize(options), { headers: { Authorization: `Bearer ${token}` } }) .then((response) => { if (response.status === 200) { if (response.data.status !== "success") { callback(response.data); } else { callback(response.data.data); } } else { throw new Error("An unknown error occurred while sending the request to Greip API 2."); } }) .catch(() => { throw new Error("An unknown error occurred while sending the request to Greip API 1."); }); }; export const makePostRquest = (endpoint, options, callback, token) => { options.source = "JS-Package"; axios .post(BASE_URL + "/" + endpoint, options, { headers: { Authorization: `Bearer ${token}` } }) .then((response) => { if (response.status === 200) { if (response.data.status !== "success") { callback(response.data); } else { callback(response.data.data); } } else { throw new Error("An unknown error occurred while sending the request to Greip API."); } }) .catch(() => { throw new Error("An unknown error occurred while sending the request to Greip API."); }); }; //# sourceMappingURL=common.js.map