Skip to content

Commit

Permalink
chore: add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Apr 27, 2022
1 parent b1eeacd commit 2f6c520
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/geo-location.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
const fetch = require('node-fetch')

const API_URL = 'https://netlifind.netlify.app'
Expand All @@ -9,13 +10,36 @@ const CACHE_TTL = 8.64e7
// 10 seconds
const REQUEST_TIMEOUT = 1e4

/**
* @typedef GeoLocation
* @type {object}
* @property {string} city
* @property {object} country
* @property {string} country.code
* @property {string} country.name
* @property {object} country
* @property {string} country.code
* @property {string} country.name
*/

// The default location to be used if we're unable to talk to the API.
const mockLocation = {
city: 'San Francisco',
country: { code: 'US', name: 'United States' },
subdivision: { code: 'CA', name: 'California' },
}

/**
* Returns geolocation data from a remote API, the local cache, or a mock
* location, depending on the mode selected.
*
* @param {object} params
* @param {string} params.geolocationMode
* @param {"cache"|"update"|"mock"} params.mode
* @param {boolean} params.offline
* @param {import('../utils/state-config').StateConfig} params.state
* @returns {Promise<GeoLocation>}
*/
const getGeoLocation = async ({ mode, offline, state }) => {
const cacheObject = state.get(STATE_GEO_PROPERTY)

Expand Down Expand Up @@ -57,6 +81,11 @@ const getGeoLocation = async ({ mode, offline, state }) => {
}
}

/**
* Returns geolocation data from a remote API
*
* @returns {Promise<GeoLocation>}
*/
const getGeoLocationFromAPI = async () => {
const res = await fetch(API_URL, {
method: 'GET',
Expand Down

0 comments on commit 2f6c520

Please sign in to comment.