UNPKG

@eastsideco/escshopify

Version:

WIP JS library for Shopify, containing a variety of useful functionality.

138 lines (120 loc) 3.13 kB
import Evee from 'evee'; import API from './API'; /** * @typedef {Object} GSGeoResponse * @property {GSContinent} continent * @property {GSCountry} country * @property {GSLocation} location * @property {GSCountry} registered_country * @property {String} currency */ /** * @typedef {Object} GSTranslatedString * @property {String} de * @property {String} en * @property {String} es * @property {String} fr * @property {String} ja * @property {String} pt-BR * @property {String} ru * @property {String} zh-CN */ /** * @typedef {Object} GSContinent * @property {String} code * @property {Number} geoname_id * @property {GSTranslatedString} names */ /** * @typedef {Object} GSCountry * @property {Number} geoname_id * @property {String} iso_code * @property {GSTranslatedString} names */ /** * @typedef {Object} GSLocation * @property {Number} accuracy_radius * @property {Number} latitude * @property {Number} longitude */ /** * @typedef {Object} GSCurrencyResponse * @property {String} base * @property {String} date - Update date in Y-M-D format * @property {GSCurrencyRates} rates */ /** * @typedef {Object} GSCurrencyRates * @property {Number} AUD * @property {Number} BGN * @property {Number} BRL * @property {Number} CAD * @property {Number} CHF * @property {Number} CNY * @property {Number} CZK * @property {Number} DKK * @property {Number} GBP * @property {Number} HKD * @property {Number} HUF * @property {Number} IDR * @property {Number} ILD * @property {Number} INR * @property {Number} JPY * @property {Number} KRW * @property {Number} MXN * @property {Number} MYR * @property {Number} NOK * @property {Number} NZD * @property {Number} PHP * @property {Number} PLN * @property {Number} RON * @property {Number} RUB * @property {Number} SEK * @property {Number} SGD * @property {Number} THB * @property {Number} TRY * @property {Number} ZAR * @property {Number} EUR */ /** * Provides access to the ESC Geoservice. */ export default class GeoService extends Evee { /** * Create a new instance of GeoService. */ constructor() { super(); /** @type {src/plugins/geoservice/API.js~API} */ this.api = new API(); /** @type {Boolean} */ this.cacheLookups = true; /** @type {Object<String, Object>} */ this._cachedLookups = {}; } /** * Retrieves geolocation information. * @param {String} [ip] - IP address to get information on. If not supplied, uses local IP. * @return {Promise<GSGeoResponse|Error>} */ async lookupGeo(ip) { if (!ip) { ip = ''; } if (this.cacheLookups && this._cachedLookups[ip]) { return this._cachedLookups[ip]; } var result = await this.api.geoip(ip); if (this.cacheLookups) { this._cachedLookups[ip] = result; } return result; } /** * Retrieves currency info. * @return {Promise<GSCurrencyResponse, Error>} */ async getCurrencyInfo() { return await this.api.currency(); } }