{"size": 983, "ext": "js", "max_stars_repo_path": "cache.js", "max_stars_repo_name": "massless/zeit-client-api", "max_stars_repo_stars_event_min_datetime": "2016-07-28T09:21:46.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-19T17:46:16.000Z", "max_issues_repo_path": "cache.js", "max_issues_repo_name": "massless/zeit-client-api", "max_issues_repo_issues_event_min_datetime": "2016-07-29T13:07:36.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-18T19:14:15.000Z", "max_forks_repo_path": "cache.js", "max_forks_repo_name": "massless/zeit-client-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 10, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 21.8444444444, "max_line_length": 66, "alphanum_fraction": 0.5930824008}
const AsyncStorage = require('./storage');

var cache = {
  TOKEN: 'token',

  get: function(name, opt_parser) {
    // Then check local storage
    return AsyncStorage.getItem(name).then((data) => {
      // Then see if we can make an object, fallback to string
      try {
        data = JSON.parse(data);
      } catch(e) {
        // Debug here if you want, but this fallback is by design.
      }
      if (opt_parser) return opt_parser(data);
      return data;
    });
  },

  set: function(name, value) {
    // Then try to force objects to strings
    try {
      if (typeof value === "string") {
        value = JSON.stringify(value);
      }
    } catch(e) {
      // TODO: when debugging, show an error
    }

    // Lastly, we send it to local storage
    return AsyncStorage.setItem(name, value);
  },

  delete: function(name) {
    return AsyncStorage.removeItem(name);
  },

  clearAll: function() {
    return AsyncStorage.clear();
  }
};

module.exports = cache;

