Repository URL to install this package:
|
Version:
0.0.0-a6ed18aaafe302 ▾
|
@doodle/geolocation
/
index.js
|
|---|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var effects = require('redux-saga/effects');
var fetch = require('fetch-everywhere');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
var ActionTypes = {
CHANGE_GEOLOCATION: '@doodle/geolocation/CHANGE_GEOLOCATION',
FETCH_GEOLOCATION: '@doodle/geolocation/FETCH_GEOLOCATION',
FETCH_GEOLOCATION_SUCCESS: '@doodle/geolocation/FETCH_GEOLOCATION_SUCCESS',
FETCH_GEOLOCATION_ERROR: '@doodle/geolocation/FETCH_GEOLOCATION_ERROR'
};
var changeGeolocation = function changeGeolocation(location) {
return {
type: ActionTypes.CHANGE_GEOLOCATION,
location: location
};
};
var fetchGeolocation = function fetchGeolocation() {
return {
type: ActionTypes.FETCH_GEOLOCATION
};
};
var fetchGeolocationSuccess = function fetchGeolocationSuccess() {
return {
type: ActionTypes.FETCH_GEOLOCATION_SUCCESS
};
};
var fetchGeolocationError = function fetchGeolocationError(error) {
return {
type: ActionTypes.FETCH_GEOLOCATION_ERROR,
error: error
};
};
var initialState = {
geolocation: '',
error: null,
loading: false
};
/**
* Creates an initial state object containing the user state
*
* @param {Object} options
* @param {string} options.domain For mounting this state under a different key in redux. Default: `geolocation`.
* @param {Object} options.initialState override the initial state
*
* @example
* import { createState as createGeolocationState } from '@doodle/geolocation';
*
* const initialState = {
* ...createGeolocationState(), // providing the state under the `domain` key
* };
* const store = createStore(rootReducer, initialState, enhancers);
*/
var createState = function createState() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _defineProperty__default['default']({}, options.domain || 'geolocation', options.initialState || initialState);
};
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default['default'](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var reducer = function reducer() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
var action = arguments.length > 1 ? arguments[1] : undefined;
switch (action.type) {
case ActionTypes.CHANGE_GEOLOCATION:
return _objectSpread(_objectSpread({}, state), {}, {
geolocation: action.location
});
case ActionTypes.FETCH_GEOLOCATION:
{
return _objectSpread(_objectSpread({}, state), {}, {
loading: true,
error: null
});
}
case ActionTypes.FETCH_GEOLOCATION_SUCCESS:
{
return _objectSpread(_objectSpread({}, state), {}, {
loading: false,
error: null
});
}
case ActionTypes.FETCH_GEOLOCATION_ERROR:
{
return _objectSpread(_objectSpread({}, state), {}, {
loading: false,
error: action.error
});
}
default:
return state;
}
};
var createReducer = function createReducer() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _defineProperty__default['default']({}, options.domain || 'geolocation', reducer);
};
var GEOLOCATION_TIMEOUT = 3000;
/**
* Implement timeout for fetch:
* https://github.com/github/fetch/issues/175
*/
function timeout(ms, promise) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('timeout'));
}, ms);
promise.then(resolve, reject);
});
}
var fetchGeolocation$1 = function fetchGeolocation(_ref) {
var geoipServiceUrl = _ref.geoipServiceUrl;
return timeout(GEOLOCATION_TIMEOUT, fetch__default['default'](geoipServiceUrl, {
headers: {
Accept: 'application/json'
}
}))["catch"](function (error) {
throw new Error('fetch error');
}).then(function (response) {
return response.json();
}).then(function (geolocation) {
return {
ip: geolocation.ip,
country_code: geolocation.iso_code,
country_name: geolocation.names.en,
region_code: geolocation.region_code,
region_name: geolocation.region_name,
city: geolocation.city,
zip_code: geolocation.zip_code,
time_zone: geolocation.time_zone,
latitude: geolocation.latitude,
longitude: geolocation.longitude,
metro_code: geolocation.metro_code
};
});
};
var geolocationApi = /*#__PURE__*/Object.freeze({
__proto__: null,
fetchGeolocation: fetchGeolocation$1
});
var _marked = /*#__PURE__*/_regeneratorRuntime__default['default'].mark(onFetchGeolocation),
_marked2 = /*#__PURE__*/_regeneratorRuntime__default['default'].mark(watchFetchGeolocation),
_marked3 = /*#__PURE__*/_regeneratorRuntime__default['default'].mark(geolocationSaga);
function onFetchGeolocation(options) {
var geolocation;
return _regeneratorRuntime__default['default'].wrap(function onFetchGeolocation$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return effects.call(fetchGeolocation$1, {
geoipServiceUrl: options.geoipServiceUrl
});
case 3:
geolocation = _context.sent;
if (!geolocation) {
_context.next = 7;
break;
}
_context.next = 7;
return effects.put(changeGeolocation(geolocation));
case 7:
_context.next = 9;
return effects.put(fetchGeolocationSuccess());
case 9:
_context.next = 15;
break;
case 11:
_context.prev = 11;
_context.t0 = _context["catch"](0);
_context.next = 15;
return effects.put(fetchGeolocationError(_context.t0));
case 15:
case "end":
return _context.stop();
}
}
}, _marked, null, [[0, 11]]);
}
function watchFetchGeolocation(options) {
return _regeneratorRuntime__default['default'].wrap(function watchFetchGeolocation$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return effects.takeLatest(ActionTypes.FETCH_GEOLOCATION, onFetchGeolocation, options);
case 2:
case "end":
return _context2.stop();
}
}
}, _marked2);
} // uses by default the preproduction service: (currently) without CORS restrictions
function geolocationSaga() {
var options,
_args3 = arguments;
return _regeneratorRuntime__default['default'].wrap(function geolocationSaga$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
options = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {
geoipServiceUrl: 'https://doodle-test.com/api/geoip/v1.0/'
};
_context3.next = 3;
return effects.all([effects.call(watchFetchGeolocation, options)]);
case 3:
case "end":
return _context3.stop();
}
}
}, _marked3);
}
var _marked$1 = /*#__PURE__*/_regeneratorRuntime__default['default'].mark(loadGeolocationSaga);
function loadGeolocationSaga() {
var options,
_args = arguments;
return _regeneratorRuntime__default['default'].wrap(function loadGeolocationSaga$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
_context.next = 3;
return effects.all([effects.call(geolocationSaga, options)]);
case 3:
case "end":
return _context.stop();
}
}
}, _marked$1);
}
exports.API = geolocationApi;
exports.ActionTypes = ActionTypes;
exports.changeGeolocation = changeGeolocation;
exports.createReducer = createReducer;
exports.createState = createState;
exports.fetchGeolocation = fetchGeolocation;
exports.fetchGeolocationError = fetchGeolocationError;
exports.fetchGeolocationSuccess = fetchGeolocationSuccess;
exports.loadGeolocationSaga = loadGeolocationSaga;
exports.reducer = reducer;